StoredUserList.java

net.minecraft.server.players.StoredUserList

信息

  • 全限定名:net.minecraft.server.players.StoredUserList
  • 类型:public abstract class
  • 包:net.minecraft.server.players
  • 源码路径:src/main/java/net/minecraft/server/players/StoredUserList.java
  • 起始行号:L27
  • 职责:

    TODO

字段/常量

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L28
    • 说明:

      TODO

  • GSON

    • 类型: Gson
    • 修饰符: private static final
    • 源码定位: L29
    • 说明:

      TODO

  • file

    • 类型: File
    • 修饰符: private final
    • 源码定位: L30
    • 说明:

      TODO

  • map

    • 类型: Map<String,V>
    • 修饰符: private final
    • 源码定位: L31
    • 说明:

      TODO

  • notificationService

    • 类型: NotificationService
    • 修饰符: protected final
    • 源码定位: L32
    • 说明:

      TODO

内部类/嵌套类型

构造器

public StoredUserList(File file, NotificationService notificationService) @ L34

  • 构造器名:StoredUserList
  • 源码定位:L34
  • 修饰符:public

参数:

  • file: File
  • notificationService: NotificationService

说明:

TODO

方法

下面的方法块按源码顺序生成。

public File getFile() @ L39

  • 方法名:getFile
  • 源码定位:L39
  • 返回类型:File
  • 修饰符:public

参数:

说明:

TODO

public boolean add(V infos) @ L43

  • 方法名:add
  • 源码定位:L43
  • 返回类型:boolean
  • 修饰符:public

参数:

  • infos: V

说明:

TODO

public V get(K user) @ L61

  • 方法名:get
  • 源码定位:L61
  • 返回类型:V
  • 修饰符:public

参数:

  • user: K

说明:

TODO

public boolean remove(K user) @ L66

  • 方法名:remove
  • 源码定位:L66
  • 返回类型:boolean
  • 修饰符:public

参数:

  • user: K

说明:

TODO

public boolean remove(StoredUserEntry<K> infos) @ L81

  • 方法名:remove
  • 源码定位:L81
  • 返回类型:boolean
  • 修饰符:public

参数:

  • infos: StoredUserEntry

说明:

TODO

public void clear() @ L85

  • 方法名:clear
  • 源码定位:L85
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

public String[] getUserList() @ L95

  • 方法名:getUserList
  • 源码定位:L95
  • 返回类型:String[]
  • 修饰符:public

参数:

说明:

TODO

public boolean isEmpty() @ L99

  • 方法名:isEmpty
  • 源码定位:L99
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

protected String getKeyForUser(K user) @ L103

  • 方法名:getKeyForUser
  • 源码定位:L103
  • 返回类型:String
  • 修饰符:protected

参数:

  • user: K

说明:

TODO

protected boolean contains(K user) @ L107

  • 方法名:contains
  • 源码定位:L107
  • 返回类型:boolean
  • 修饰符:protected

参数:

  • user: K

说明:

TODO

private void removeExpired() @ L111

  • 方法名:removeExpired
  • 源码定位:L111
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

protected abstract StoredUserEntry<K> createEntry(JsonObject object) @ L125

  • 方法名:createEntry
  • 源码定位:L125
  • 返回类型:StoredUserEntry
  • 修饰符:protected abstract

参数:

  • object: JsonObject

说明:

TODO

public Collection<V> getEntries() @ L127

  • 方法名:getEntries
  • 源码定位:L127
  • 返回类型:Collection
  • 修饰符:public

参数:

说明:

TODO

public void save() @ L131

  • 方法名:save
  • 源码定位:L131
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

public void load() @ L140

  • 方法名:load
  • 源码定位:L140
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

代码

public abstract class StoredUserList<K, V extends StoredUserEntry<K>> {
    private static final Logger LOGGER = LogUtils.getLogger();
    private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
    private final File file;
    private final Map<String, V> map = Maps.newHashMap();
    protected final NotificationService notificationService;
 
    public StoredUserList(File file, NotificationService notificationService) {
        this.file = file;
        this.notificationService = notificationService;
    }
 
    public File getFile() {
        return this.file;
    }
 
    public boolean add(V infos) {
        String keyForUser = this.getKeyForUser(infos.getUser());
        V previous = this.map.get(keyForUser);
        if (infos.equals(previous)) {
            return false;
        } else {
            this.map.put(keyForUser, infos);
 
            try {
                this.save();
            } catch (IOException var5) {
                LOGGER.warn("Could not save the list after adding a user.", (Throwable)var5);
            }
 
            return true;
        }
    }
 
    public @Nullable V get(K user) {
        this.removeExpired();
        return this.map.get(this.getKeyForUser(user));
    }
 
    public boolean remove(K user) {
        V removed = this.map.remove(this.getKeyForUser(user));
        if (removed == null) {
            return false;
        } else {
            try {
                this.save();
            } catch (IOException var4) {
                LOGGER.warn("Could not save the list after removing a user.", (Throwable)var4);
            }
 
            return true;
        }
    }
 
    public boolean remove(StoredUserEntry<K> infos) {
        return this.remove(Objects.requireNonNull(infos.getUser()));
    }
 
    public void clear() {
        this.map.clear();
 
        try {
            this.save();
        } catch (IOException var2) {
            LOGGER.warn("Could not save the list after removing a user.", (Throwable)var2);
        }
    }
 
    public String[] getUserList() {
        return this.map.keySet().toArray(new String[0]);
    }
 
    public boolean isEmpty() {
        return this.map.isEmpty();
    }
 
    protected String getKeyForUser(K user) {
        return user.toString();
    }
 
    protected boolean contains(K user) {
        return this.map.containsKey(this.getKeyForUser(user));
    }
 
    private void removeExpired() {
        List<K> toRemove = Lists.newArrayList();
 
        for (V entry : this.map.values()) {
            if (entry.hasExpired()) {
                toRemove.add(entry.getUser());
            }
        }
 
        for (K user : toRemove) {
            this.map.remove(this.getKeyForUser(user));
        }
    }
 
    protected abstract StoredUserEntry<K> createEntry(final JsonObject object);
 
    public Collection<V> getEntries() {
        return this.map.values();
    }
 
    public void save() throws IOException {
        JsonArray result = new JsonArray();
        this.map.values().stream().map(entry -> Util.make(new JsonObject(), entry::serialize)).forEach(result::add);
 
        try (BufferedWriter writer = Files.newWriter(this.file, StandardCharsets.UTF_8)) {
            GSON.toJson(result, GSON.newJsonWriter(writer));
        }
    }
 
    public void load() throws IOException {
        if (this.file.exists()) {
            try (BufferedReader reader = Files.newReader(this.file, StandardCharsets.UTF_8)) {
                this.map.clear();
                JsonArray contents = GSON.fromJson(reader, JsonArray.class);
                if (contents == null) {
                    return;
                }
 
                for (JsonElement element : contents) {
                    JsonObject object = GsonHelper.convertToJsonObject(element, "entry");
                    StoredUserEntry<K> entry = this.createEntry(object);
                    if (entry.getUser() != null) {
                        this.map.put(this.getKeyForUser(entry.getUser()), (V)entry);
                    }
                }
            }
        }
    }
}

引用的其他类