Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
at develop 3.3 kB view raw
1package com.legacyminecraft.poseidon.util; 2 3import java.io.File; 4import java.io.FileReader; 5import java.lang.reflect.Type; 6import java.nio.file.Files; 7import java.util.List; 8 9import com.google.gson.Gson; 10import com.google.gson.GsonBuilder; 11import com.google.gson.JsonArray; 12import com.google.gson.JsonPrimitive; 13import com.google.gson.JsonSyntaxException; 14import com.google.gson.reflect.TypeToken; 15import com.google.gson.stream.JsonReader; 16 17public class CrackedAllowlist { 18 19 private static CrackedAllowlist singleton; 20 private final Gson parser = new GsonBuilder().setPrettyPrinting().create(); 21 private JsonArray namesJsonArray; 22 private File crackedListFile = new File("cracked-allowlist.json"); 23 24 public CrackedAllowlist() { 25 26 if (!crackedListFile.exists()) { 27 namesJsonArray = new JsonArray(); 28 saveAllowlist(); 29 return; 30 } 31 32 try { 33 System.out.println("[Poseidon] Reading cracked-allowlist.json for Project Poseidon"); 34 35 JsonReader reader = new JsonReader(new FileReader(crackedListFile)); 36 namesJsonArray = parser.fromJson(reader, JsonArray.class); 37 38 } catch (JsonSyntaxException e) { 39 System.out.println("[Poseidon] Cracked names allowlist is corrupt or unreadable, resetting"); 40 namesJsonArray = new JsonArray(); 41 saveAllowlist(); 42 43 } catch (Throwable t) { 44 System.out.println("[Poseidon] Error reading cracked-allowlist.json: " + t + ": " + t.getMessage()); 45 namesJsonArray = new JsonArray(); 46 } 47 } 48 49 public void saveAllowlist() { 50 try { 51 System.out.println("[Poseidon] Saving cracked names allowlist"); 52 53 Files.write(crackedListFile.toPath(), parser.toJson(namesJsonArray).getBytes("UTF-8")); 54 } catch (Throwable t) { 55 t.printStackTrace(); 56 } 57 } 58 59 /** 60 * Adds username to cracked names allowlist. 61 * 62 * @param name username to allowlist 63 * @return true if added, false if already on the list 64 */ 65 public boolean addName(String name) { 66 JsonPrimitive jsonString = new JsonPrimitive(name.toLowerCase()); 67 if (namesJsonArray.contains(jsonString)) { 68 return false; 69 } 70 71 namesJsonArray.add(jsonString); 72 saveAllowlist(); 73 return true; 74 } 75 76 /** 77 * Removes username from cracked names allowlist. 78 * 79 * @param name username to remove 80 * @return true if removed, false if the name wasn't on the list 81 */ 82 public boolean removeName(String name) { 83 JsonPrimitive jsonString = new JsonPrimitive(name.toLowerCase()); 84 85 boolean removed = namesJsonArray.remove(jsonString); 86 saveAllowlist(); 87 88 return removed; 89 } 90 91 public boolean contains(String name) { 92 JsonPrimitive jsonString = new JsonPrimitive(name.toLowerCase()); 93 94 return namesJsonArray.contains(jsonString); 95 } 96 97 public List<String> getAsList() { 98 Type listType = new TypeToken<List<String>>() {}.getType(); 99 return parser.fromJson(namesJsonArray, listType); 100 } 101 102 public static CrackedAllowlist get() { 103 if (singleton == null) { 104 singleton = new CrackedAllowlist(); 105 } 106 return singleton; 107 } 108}