From 3b343b9a7f5252915ed68eb08c5b07b6f03542b0 Mon Sep 17 00:00:00 2001 From: Warrior <50800980+Warriorrrr@users.noreply.github.com> Date: Thu, 23 Sep 2021 21:46:30 +0200 Subject: [PATCH 1/3] Simplify world blacklist loading --- .../gmail/nossr50/config/WorldBlacklist.java | 74 +++++-------------- 1 file changed, 18 insertions(+), 56 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/config/WorldBlacklist.java b/src/main/java/com/gmail/nossr50/config/WorldBlacklist.java index f0fd6bd2bb..35abec3d6c 100644 --- a/src/main/java/com/gmail/nossr50/config/WorldBlacklist.java +++ b/src/main/java/com/gmail/nossr50/config/WorldBlacklist.java @@ -3,91 +3,53 @@ import com.gmail.nossr50.mcMMO; import org.bukkit.World; -import java.io.*; -import java.util.ArrayList; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashSet; +import java.util.Set; /** * Blacklist certain features in certain worlds */ public class WorldBlacklist { - private static ArrayList blacklist; + private static final Set blacklist = new HashSet<>(); private final mcMMO plugin; - private final String blackListFileName = "world_blacklist.txt"; - - public WorldBlacklist(mcMMO plugin) - { + public WorldBlacklist(mcMMO plugin) { this.plugin = plugin; - blacklist = new ArrayList<>(); init(); } - public void init() - { + public void init() { //Make the blacklist file if it doesn't exist + String blackListFileName = "world_blacklist.txt"; File blackListFile = new File(plugin.getDataFolder() + File.separator + blackListFileName); try { - if(!blackListFile.exists()) + if (!blackListFile.exists()) blackListFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } //Load up the blacklist - loadBlacklist(blackListFile); - //registerFlags(); - } - - private void loadBlacklist(File blackListFile) { - FileReader fileReader = null; - BufferedReader bufferedReader = null; - try { - fileReader = new FileReader(blackListFile); - bufferedReader = new BufferedReader(fileReader); - + try (BufferedReader reader = new BufferedReader(new FileReader(blackListFile))) { String currentLine; - - while((currentLine = bufferedReader.readLine()) != null) - { - if(currentLine.length() == 0) - continue; - - if(!blacklist.contains(currentLine)) - blacklist.add(currentLine); - } - + while((currentLine = reader.readLine()) != null) + blacklist.add(currentLine.toLowerCase()); } catch (IOException e) { e.printStackTrace(); - } finally { - //Close readers - closeRead(bufferedReader); - closeRead(fileReader); } - - plugin.getLogger().info(blacklist.size()+" entries in mcMMO World Blacklist"); } - private void closeRead(Reader reader) { - if(reader != null) { - try { - reader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } + public static boolean isWorldBlacklisted(World world) { + return blacklist.contains(world.getName().toLowerCase()); } - public static boolean isWorldBlacklisted(World world) - { - - for(String s : blacklist) - { - if(world.getName().equalsIgnoreCase(s)) - return true; - } - - return false; + public static boolean isWorldBlacklisted(String worldName) { + return blacklist.contains(worldName.toLowerCase()); } } From 577ec718f15527e0b438ed012fe016826560b469 Mon Sep 17 00:00:00 2001 From: Warrior <50800980+Warriorrrr@users.noreply.github.com> Date: Thu, 23 Sep 2021 21:54:37 +0200 Subject: [PATCH 2/3] Return if createNewFile throws an exception --- src/main/java/com/gmail/nossr50/config/WorldBlacklist.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/gmail/nossr50/config/WorldBlacklist.java b/src/main/java/com/gmail/nossr50/config/WorldBlacklist.java index 35abec3d6c..cf930d1e65 100644 --- a/src/main/java/com/gmail/nossr50/config/WorldBlacklist.java +++ b/src/main/java/com/gmail/nossr50/config/WorldBlacklist.java @@ -32,13 +32,15 @@ public void init() { blackListFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); + return; } //Load up the blacklist try (BufferedReader reader = new BufferedReader(new FileReader(blackListFile))) { String currentLine; while((currentLine = reader.readLine()) != null) - blacklist.add(currentLine.toLowerCase()); + if (!currentLine.isEmpty()) + blacklist.add(currentLine.toLowerCase()); } catch (IOException e) { e.printStackTrace(); From 942bc23edb546c3686bc6c3cd01acb117a18ebd3 Mon Sep 17 00:00:00 2001 From: Warrior <50800980+Warriorrrr@users.noreply.github.com> Date: Sat, 6 Nov 2021 00:22:55 +0100 Subject: [PATCH 3/3] CR --- .../gmail/nossr50/config/WorldBlacklist.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/config/WorldBlacklist.java b/src/main/java/com/gmail/nossr50/config/WorldBlacklist.java index cf930d1e65..0ff81c30ae 100644 --- a/src/main/java/com/gmail/nossr50/config/WorldBlacklist.java +++ b/src/main/java/com/gmail/nossr50/config/WorldBlacklist.java @@ -2,29 +2,33 @@ import com.gmail.nossr50.mcMMO; import org.bukkit.World; +import org.jetbrains.annotations.NotNull; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.HashSet; +import java.util.Locale; import java.util.Set; /** * Blacklist certain features in certain worlds */ public class WorldBlacklist { - private static final Set blacklist = new HashSet<>(); + private final Set blacklist = new HashSet<>(); + private static WorldBlacklist instance; private final mcMMO plugin; + private final String blackListFileName = "world_blacklist.txt"; - public WorldBlacklist(mcMMO plugin) { + public WorldBlacklist(@NotNull mcMMO plugin) { + WorldBlacklist.instance = this; this.plugin = plugin; init(); } public void init() { //Make the blacklist file if it doesn't exist - String blackListFileName = "world_blacklist.txt"; File blackListFile = new File(plugin.getDataFolder() + File.separator + blackListFileName); try { @@ -47,11 +51,11 @@ public void init() { } } - public static boolean isWorldBlacklisted(World world) { - return blacklist.contains(world.getName().toLowerCase()); + public static boolean isWorldBlacklisted(@NotNull World world) { + return isWorldBlacklisted(world.getName()); } - public static boolean isWorldBlacklisted(String worldName) { - return blacklist.contains(worldName.toLowerCase()); + public static boolean isWorldBlacklisted(@NotNull String worldName) { + return instance.blacklist.contains(worldName.toLowerCase(Locale.ROOT)); } }