Simplify world blacklist loading

This commit is contained in:
Warrior 2021-09-23 21:46:30 +02:00
parent 7fc6577196
commit 3b343b9a7f

View File

@ -3,28 +3,28 @@ package com.gmail.nossr50.config;
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<String> blacklist;
private static final Set<String> 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 {
@ -35,59 +35,21 @@ public class WorldBlacklist {
}
//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)
{
for(String s : blacklist)
{
if(world.getName().equalsIgnoreCase(s))
return true;
public static boolean isWorldBlacklisted(World world) {
return blacklist.contains(world.getName().toLowerCase());
}
return false;
public static boolean isWorldBlacklisted(String worldName) {
return blacklist.contains(worldName.toLowerCase());
}
}