diff --git a/src/main/java/net/minecraft/server/JsonList.java b/src/main/java/net/minecraft/server/JsonList.java new file mode 100644 index 0000000000..36df988832 --- /dev/null +++ b/src/main/java/net/minecraft/server/JsonList.java @@ -0,0 +1,160 @@ +package net.minecraft.server; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.ParameterizedType; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; + +import net.minecraft.util.com.google.common.base.Charsets; +import net.minecraft.util.com.google.common.collect.Lists; +import net.minecraft.util.com.google.common.collect.Maps; +import net.minecraft.util.com.google.common.io.Files; +import net.minecraft.util.com.google.gson.Gson; +import net.minecraft.util.com.google.gson.GsonBuilder; +import net.minecraft.util.com.google.gson.JsonObject; +import net.minecraft.util.org.apache.commons.io.IOUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +public class JsonList { + + protected static final Logger a = LogManager.getLogger(); + protected final Gson b; + private final File c; + private final Map d = Maps.newHashMap(); + private boolean e = true; + private static final ParameterizedType f = new JsonListType(); + + public JsonList(File file1) { + this.c = file1; + GsonBuilder gsonbuilder = (new GsonBuilder()).setPrettyPrinting(); + + gsonbuilder.registerTypeHierarchyAdapter(JsonListEntry.class, new JsonListEntrySerializer(this, (JsonListType) null)); + this.b = gsonbuilder.create(); + } + + public boolean isEnabled() { + return this.e; + } + + public void a(boolean flag) { + this.e = flag; + } + + public File c() { + return this.c; + } + + public void add(JsonListEntry jsonlistentry) { + this.d.put(this.a(jsonlistentry.f()), jsonlistentry); + + try { + this.save(); + } catch (IOException ioexception) { + a.warn("Could not save the list after adding a user.", ioexception); + } + } + + public JsonListEntry get(Object object) { + this.h(); + return (JsonListEntry) this.d.get(this.a(object)); + } + + public void remove(Object object) { + this.d.remove(this.a(object)); + + try { + this.save(); + } catch (IOException ioexception) { + a.warn("Could not save the list after removing a user.", ioexception); + } + } + + public String[] getEntries() { + return (String[]) this.d.keySet().toArray(new String[this.d.size()]); + } + + public boolean d() { + return this.d.size() < 1; + } + + protected String a(Object object) { + return object.toString(); + } + + protected boolean d(Object object) { + return this.d.containsKey(this.a(object)); + } + + private void h() { + ArrayList arraylist = Lists.newArrayList(); + Iterator iterator = this.d.values().iterator(); + + while (iterator.hasNext()) { + JsonListEntry jsonlistentry = (JsonListEntry) iterator.next(); + + if (jsonlistentry.e()) { + arraylist.add(jsonlistentry.f()); + } + } + + iterator = arraylist.iterator(); + + while (iterator.hasNext()) { + Object object = iterator.next(); + + this.d.remove(object); + } + } + + protected JsonListEntry a(JsonObject jsonobject) { + return new JsonListEntry(null, jsonobject); + } + + protected Map e() { + return this.d; + } + + public void save() { + Collection collection = this.d.values(); + String s = this.b.toJson(collection); + BufferedWriter bufferedwriter = null; + + try { + bufferedwriter = Files.newWriter(this.c, Charsets.UTF_8); + bufferedwriter.write(s); + } finally { + IOUtils.closeQuietly(bufferedwriter); + } + } + + public void load() { + Collection collection = null; + BufferedReader bufferedreader = null; + + try { + bufferedreader = Files.newReader(this.c, Charsets.UTF_8); + collection = (Collection) this.b.fromJson(bufferedreader, f); + } finally { + IOUtils.closeQuietly(bufferedreader); + } + + if (collection != null) { + this.d.clear(); + Iterator iterator = collection.iterator(); + + while (iterator.hasNext()) { + JsonListEntry jsonlistentry = (JsonListEntry) iterator.next(); + + if (jsonlistentry.f() != null) { + this.d.put(this.a(jsonlistentry.f()), jsonlistentry); + } + } + } + } +} diff --git a/src/main/java/net/minecraft/server/JsonListEntry.java b/src/main/java/net/minecraft/server/JsonListEntry.java new file mode 100644 index 0000000000..3a7e2c3825 --- /dev/null +++ b/src/main/java/net/minecraft/server/JsonListEntry.java @@ -0,0 +1,26 @@ +package net.minecraft.server; + +import net.minecraft.util.com.google.gson.JsonObject; + +public class JsonListEntry { + + private final Object a; + + public JsonListEntry(Object object) { + this.a = object; + } + + protected JsonListEntry(Object object, JsonObject jsonobject) { + this.a = object; + } + + Object f() { + return this.a; + } + + boolean e() { + return false; + } + + protected void a(JsonObject jsonobject) {} +} diff --git a/src/main/java/net/minecraft/server/JsonListEntrySerializer.java b/src/main/java/net/minecraft/server/JsonListEntrySerializer.java new file mode 100644 index 0000000000..b6aba6a60c --- /dev/null +++ b/src/main/java/net/minecraft/server/JsonListEntrySerializer.java @@ -0,0 +1,49 @@ +package net.minecraft.server; + +import java.lang.reflect.Type; + +import net.minecraft.util.com.google.gson.JsonDeserializationContext; +import net.minecraft.util.com.google.gson.JsonDeserializer; +import net.minecraft.util.com.google.gson.JsonElement; +import net.minecraft.util.com.google.gson.JsonObject; +import net.minecraft.util.com.google.gson.JsonSerializationContext; +import net.minecraft.util.com.google.gson.JsonSerializer; + +class JsonListEntrySerializer implements JsonDeserializer, JsonSerializer { + + final JsonList a; + + private JsonListEntrySerializer(JsonList jsonlist) { + this.a = jsonlist; + } + + public JsonElement a(JsonListEntry jsonlistentry, Type type, JsonSerializationContext jsonserializationcontext) { + JsonObject jsonobject = new JsonObject(); + + jsonlistentry.a(jsonobject); + return jsonobject; + } + + public JsonListEntry a(JsonElement jsonelement, Type type, JsonDeserializationContext jsondeserializationcontext) { + if (jsonelement.isJsonObject()) { + JsonObject jsonobject = jsonelement.getAsJsonObject(); + JsonListEntry jsonlistentry = this.a.a(jsonobject); + + return jsonlistentry; + } else { + return null; + } + } + + public JsonElement serialize(Object object, Type type, JsonSerializationContext jsonserializationcontext) { + return this.a((JsonListEntry) object, type, jsonserializationcontext); + } + + public Object deserialize(JsonElement jsonelement, Type type, JsonDeserializationContext jsondeserializationcontext) { + return this.a(jsonelement, type, jsondeserializationcontext); + } + + JsonListEntrySerializer(JsonList jsonlist, JsonListType jsonlisttype) { + this(jsonlist); + } +}