| 1 | package net.minecraft.server.management;
|
| 2 |
|
| 3 | import com.google.common.base.Charsets;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.google.common.collect.Maps;
|
| 6 | import com.google.common.io.Files;
|
| 7 | import com.google.gson.Gson;
|
| 8 | import com.google.gson.GsonBuilder;
|
| 9 | import com.google.gson.JsonDeserializationContext;
|
| 10 | import com.google.gson.JsonDeserializer;
|
| 11 | import com.google.gson.JsonElement;
|
| 12 | import com.google.gson.JsonObject;
|
| 13 | import com.google.gson.JsonParseException;
|
| 14 | import com.google.gson.JsonSerializationContext;
|
| 15 | import com.google.gson.JsonSerializer;
|
| 16 | import java.io.BufferedWriter;
|
| 17 | import java.io.File;
|
| 18 | import java.io.IOException;
|
| 19 | import java.io.Writer;
|
| 20 | import java.lang.reflect.ParameterizedType;
|
| 21 | import java.lang.reflect.Type;
|
| 22 | import java.util.Collection;
|
| 23 | import java.util.List;
|
| 24 | import java.util.Map;
|
| 25 | import org.apache.commons.io.IOUtils;
|
| 26 | import org.apache.logging.log4j.LogManager;
|
| 27 | import org.apache.logging.log4j.Logger;
|
| 28 |
|
| 29 | public class UserList<K, V extends UserListEntry<K>>
|
| 30 | {
|
| 31 | protected static final Logger logger = LogManager.getLogger();
|
| 32 | protected final Gson gson;
|
| 33 | private final File saveFile;
|
| 34 | private final Map<String, V> values = Maps.<String, V>newHashMap();
|
| 35 | private boolean lanServer = true;
|
| 36 | private static final ParameterizedType saveFileFormat = new ParameterizedType()
|
| 37 | {
|
| 38 | public Type[] getActualTypeArguments()
|
| 39 | {
|
| 40 | return new Type[] {UserListEntry.class};
|
| 41 | }
|
| 42 | public Type getRawType()
|
| 43 | {
|
| 44 | return List.class;
|
| 45 | }
|
| 46 | public Type getOwnerType()
|
| 47 | {
|
| 48 | return null;
|
| 49 | }
|
| 50 | };
|
| 51 |
|
| 52 | public UserList(File saveFile)
|
| 53 | {
|
| 54 | this.saveFile = saveFile;
|
| 55 | GsonBuilder gsonbuilder = (new GsonBuilder()).setPrettyPrinting();
|
| 56 | gsonbuilder.registerTypeHierarchyAdapter(UserListEntry.class, new UserList.Serializer());
|
| 57 | this.gson = gsonbuilder.create();
|
| 58 | }
|
| 59 |
|
| 60 | public boolean isLanServer()
|
| 61 | {
|
| 62 | return this.lanServer;
|
| 63 | }
|
| 64 |
|
| 65 | public void setLanServer(boolean state)
|
| 66 | {
|
| 67 | this.lanServer = state;
|
| 68 | }
|
| 69 |
|
| 70 | /**
|
| 71 | * Adds an entry to the list
|
| 72 | */
|
| 73 | public void addEntry(V entry)
|
| 74 | {
|
| 75 | this.values.put(this.getObjectKey(entry.getValue()), entry);
|
| 76 |
|
| 77 | try
|
| 78 | {
|
| 79 | this.writeChanges();
|
| 80 | }
|
| 81 | catch (IOException ioexception)
|
| 82 | {
|
| 83 | logger.warn((String)"Could not save the list after adding a user.", (Throwable)ioexception);
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | public V getEntry(K obj)
|
| 88 | {
|
| 89 | this.removeExpired();
|
| 90 | return (V)((UserListEntry)this.values.get(this.getObjectKey(obj)));
|
| 91 | }
|
| 92 |
|
| 93 | public void removeEntry(K entry)
|
| 94 | {
|
| 95 | this.values.remove(this.getObjectKey(entry));
|
| 96 |
|
| 97 | try
|
| 98 | {
|
| 99 | this.writeChanges();
|
| 100 | }
|
| 101 | catch (IOException ioexception)
|
| 102 | {
|
| 103 | logger.warn((String)"Could not save the list after removing a user.", (Throwable)ioexception);
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | public String[] getKeys()
|
| 108 | {
|
| 109 | return (String[])this.values.keySet().toArray(new String[this.values.size()]);
|
| 110 | }
|
| 111 |
|
| 112 | /**
|
| 113 | * Gets the key value for the given object
|
| 114 | */
|
| 115 | protected String getObjectKey(K obj)
|
| 116 | {
|
| 117 | return obj.toString();
|
| 118 | }
|
| 119 |
|
| 120 | protected boolean hasEntry(K entry)
|
| 121 | {
|
| 122 | return this.values.containsKey(this.getObjectKey(entry));
|
| 123 | }
|
| 124 |
|
| 125 | /**
|
| 126 | * Removes expired bans from the list. See {@link BanEntry#hasBanExpired}
|
| 127 | */
|
| 128 | private void removeExpired()
|
| 129 | {
|
| 130 | List<K> list = Lists.<K>newArrayList();
|
| 131 |
|
| 132 | for (V v : this.values.values())
|
| 133 | {
|
| 134 | if (v.hasBanExpired())
|
| 135 | {
|
| 136 | list.add(v.getValue());
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | for (K k : list)
|
| 141 | {
|
| 142 | this.values.remove(k);
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | protected UserListEntry<K> createEntry(JsonObject entryData)
|
| 147 | {
|
| 148 | return new UserListEntry((Object)null, entryData);
|
| 149 | }
|
| 150 |
|
| 151 | protected Map<String, V> getValues()
|
| 152 | {
|
| 153 | return this.values;
|
| 154 | }
|
| 155 |
|
| 156 | public void writeChanges() throws IOException
|
| 157 | {
|
| 158 | Collection<V> collection = this.values.values();
|
| 159 | String s = this.gson.toJson((Object)collection);
|
| 160 | BufferedWriter bufferedwriter = null;
|
| 161 |
|
| 162 | try
|
| 163 | {
|
| 164 | bufferedwriter = Files.newWriter(this.saveFile, Charsets.UTF_8);
|
| 165 | bufferedwriter.write(s);
|
| 166 | }
|
| 167 | finally
|
| 168 | {
|
| 169 | IOUtils.closeQuietly((Writer)bufferedwriter);
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | class Serializer implements JsonDeserializer<UserListEntry<K>>, JsonSerializer<UserListEntry<K>>
|
| 174 | {
|
| 175 | private Serializer()
|
| 176 | {
|
| 177 | }
|
| 178 |
|
| 179 | public JsonElement serialize(UserListEntry<K> p_serialize_1_, Type p_serialize_2_, JsonSerializationContext p_serialize_3_)
|
| 180 | {
|
| 181 | JsonObject jsonobject = new JsonObject();
|
| 182 | p_serialize_1_.onSerialization(jsonobject);
|
| 183 | return jsonobject;
|
| 184 | }
|
| 185 |
|
| 186 | public UserListEntry<K> deserialize(JsonElement p_deserialize_1_, Type p_deserialize_2_, JsonDeserializationContext p_deserialize_3_) throws JsonParseException
|
| 187 | {
|
| 188 | if (p_deserialize_1_.isJsonObject())
|
| 189 | {
|
| 190 | JsonObject jsonobject = p_deserialize_1_.getAsJsonObject();
|
| 191 | UserListEntry<K> userlistentry = UserList.this.createEntry(jsonobject);
|
| 192 | return userlistentry;
|
| 193 | }
|
| 194 | else
|
| 195 | {
|
| 196 | return null;
|
| 197 | }
|
| 198 | }
|
| 199 | }
|
| 200 | }
|
| 201 |
|