Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify world blacklist loading #4638

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 23 additions & 55 deletions src/main/java/com/gmail/nossr50/config/WorldBlacklist.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,60 @@

import com.gmail.nossr50.mcMMO;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;

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.Locale;
import java.util.Set;

/**
* Blacklist certain features in certain worlds
*/
public class WorldBlacklist {
private static ArrayList<String> blacklist;
private final Set<String> 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;
blacklist = new ArrayList<>();
init();
}

public void init()
{
public void init() {
//Make the blacklist file if it doesn't exist
File blackListFile = new File(plugin.getDataFolder() + File.separator + blackListFileName);

try {
if(!blackListFile.exists())
if (!blackListFile.exists())
blackListFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return;
}

//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)
if (!currentLine.isEmpty())
blacklist.add(currentLine.toLowerCase());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
blacklist.add(currentLine.toLowerCase());
blacklist.add(currentLine.toLowerCase(Locale.ROOT));


} 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(@NotNull World world) {
return isWorldBlacklisted(world.getName());
}

public static boolean isWorldBlacklisted(World world)
{

for(String s : blacklist)
{
if(world.getName().equalsIgnoreCase(s))
return true;
}

return false;
public static boolean isWorldBlacklisted(@NotNull String worldName) {
return instance.blacklist.contains(worldName.toLowerCase(Locale.ROOT));
}
}