finalised in-game whitelist logging

This commit is contained in:
Joe 2020-07-21 20:26:47 +01:00
parent 04cefa74ff
commit a2266f4b07
5 changed files with 204 additions and 196 deletions

View File

@ -18,6 +18,7 @@ import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import uk.co.angrybee.joe.Configs.CustomPrefixConfig;
import uk.co.angrybee.joe.Stores.InGameRemovedList;
import uk.co.angrybee.joe.Stores.WhitelistedPlayers;
import javax.annotation.Nonnull;
import javax.security.auth.login.LoginException;
@ -411,44 +412,22 @@ public class DiscordClient extends ListenerAdapter
}
}
// EasyWhitelist username store
FileConfiguration tempFileConfiguration = new YamlConfiguration();
// Default Minecraft username store
File whitelistJSON = (new File(".", "whitelist.json"));
if (onlyHasLimitedAdd) {
DiscordWhitelister.getPlugin().getLogger().info(author.getName() + "(" + author.getId() + ") attempted to whitelist: " + finalNameToAdd + ", " + (maxWhitelistAmount - timesWhitelisted) + " whitelists remaining");
} else {
DiscordWhitelister.getPlugin().getLogger().info(author.getName() + "(" + author.getId() + ") attempted to whitelist: " + finalNameToAdd);
}
if (DiscordWhitelister.useEasyWhitelist)
{
try
{
tempFileConfiguration.load(new File(DiscordWhitelister.easyWhitelist.getDataFolder(), "config.yml"));
}
catch (IOException | InvalidConfigurationException e)
{
EmbedBuilder failure = new EmbedBuilder();
failure.setColor(new Color(231, 76, 60));
failure.addField("Internal Error", (author.getAsMention() + ", something went wrong while accessing EasyWhitelist file. Please contact a staff member."), false);
channel.sendMessage(failure.build()).queue();
e.printStackTrace();
return;
}
}
boolean alreadyOnWhitelist = false;
if(DiscordWhitelister.useEasyWhitelist)
if(WhitelistedPlayers.usingEasyWhitelist)
{
if (tempFileConfiguration.getStringList("whitelisted").contains(finalNameToAdd))
if (WhitelistedPlayers.CheckForPlayerEasyWhitelist(finalNameToAdd))
{
alreadyOnWhitelist = true;
}
}
else if (checkWhitelistJSON(whitelistJSON, finalNameToAdd))
else if (WhitelistedPlayers.CheckForPlayer(finalNameToAdd))
{
alreadyOnWhitelist = true;
}
@ -666,30 +645,25 @@ public class DiscordClient extends ListenerAdapter
final int successfulTimesWhitelisted = maxWhitelistAmount - finalTimesWhitelisted;
final int failedTimesWhitelisted = maxWhitelistAmount - timesWhitelisted;
if (!DiscordWhitelister.useEasyWhitelist) {
if (authorPermissions.isUserCanUseCommand()) {
if (!WhitelistedPlayers.usingEasyWhitelist)
{
if (authorPermissions.isUserCanUseCommand())
executeServerCommand("whitelist add " + finalNameToAdd);
}
}
if (DiscordWhitelister.useEasyWhitelist) {
if (WhitelistedPlayers.usingEasyWhitelist)
{
if (!invalidMinecraftName) // have to do this else the easy whitelist plugin will add the name regardless of whether it is valid on not
{
if (authorPermissions.isUserCanUseCommand()) {
if (authorPermissions.isUserCanUseCommand())
executeServerCommand("easywl add " + finalNameToAdd);
}
}
// run through the server so that the check doesn't execute before the server has had a chance to run the whitelist command -- unsure if this is the best way of doing this, but it works
DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () ->
{
try {
tempFileConfiguration.load(new File(DiscordWhitelister.easyWhitelist.getDataFolder(), "config.yml"));
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
if (!invalidMinecraftName && tempFileConfiguration.getStringList("whitelisted").contains(finalNameToAdd)) {
if (!invalidMinecraftName && WhitelistedPlayers.CheckForPlayerEasyWhitelist(finalNameToAdd))
{
channel.sendMessage(embedBuilderWhitelistSuccess.build()).queue();
// Add role to user when they have been added to the whitelist if need be
@ -744,10 +718,13 @@ public class DiscordClient extends ListenerAdapter
}
return null;
});
} else {
}
else
{
DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () ->
{
if (checkWhitelistJSON(whitelistJSON, finalNameToAdd)) {
if (WhitelistedPlayers.CheckForPlayer(finalNameToAdd))
{
channel.sendMessage(embedBuilderWhitelistSuccess.build()).queue();
// Add role to user when they have been added to the whitelist if need be
@ -827,30 +804,20 @@ public class DiscordClient extends ListenerAdapter
final String finalNameToRemove = messageContentsAfterCommand.replaceAll(" .*", ""); // The name is everything up to the first space
if (finalNameToRemove.isEmpty()) {
if (finalNameToRemove.isEmpty())
{
channel.sendMessage(removeCommandInfo).queue();
return;
} else {
// easy whitelist
FileConfiguration tempFileConfiguration = new YamlConfiguration();
// default whitelist
File whitelistJSON = (new File(".", "whitelist.json"));
}
else
{
DiscordWhitelister.getPlugin().getLogger().info(author.getName() + "(" + author.getId() + ") attempted to remove " + finalNameToRemove + " from the whitelist");
if (DiscordWhitelister.useEasyWhitelist) {
try {
tempFileConfiguration.load(new File(DiscordWhitelister.easyWhitelist.getDataFolder(), "config.yml"));
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
}
boolean notOnWhitelist = false;
if (DiscordWhitelister.useEasyWhitelist)
if (WhitelistedPlayers.usingEasyWhitelist)
{
if (!tempFileConfiguration.getStringList("whitelisted").contains(finalNameToRemove))
if (!WhitelistedPlayers.CheckForPlayerEasyWhitelist(finalNameToRemove))
{
notOnWhitelist = true;
@ -875,7 +842,7 @@ public class DiscordClient extends ListenerAdapter
}
}
if (!DiscordWhitelister.useEasyWhitelist && !checkWhitelistJSON(whitelistJSON, finalNameToRemove))
if (!WhitelistedPlayers.usingEasyWhitelist && !WhitelistedPlayers.CheckForPlayer(finalNameToRemove))
{
notOnWhitelist = true;
@ -901,23 +868,10 @@ public class DiscordClient extends ListenerAdapter
if (!notOnWhitelist)
{
if (DiscordWhitelister.useEasyWhitelist)
{
try
{
tempFileConfiguration.load(new File(DiscordWhitelister.easyWhitelist.getDataFolder(), "config.yml"));
}
catch (IOException | InvalidConfigurationException e)
{
e.printStackTrace();
}
if (WhitelistedPlayers.usingEasyWhitelist)
executeServerCommand("easywl remove " + finalNameToRemove);
}
else
{
executeServerCommand("whitelist remove " + finalNameToRemove);
}
// Configure message here instead of on the main thread - this means this will run even if the message is never sent, but is a good trade off (I think)
EmbedBuilder embedBuilderSuccess = new EmbedBuilder();
@ -947,16 +901,12 @@ public class DiscordClient extends ListenerAdapter
"This should never happen, you may have to remove the player manually and report the issue."), false);
if (DiscordWhitelister.useEasyWhitelist) {
if (WhitelistedPlayers.usingEasyWhitelist)
{
DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () ->
{
try {
tempFileConfiguration.load(new File(DiscordWhitelister.easyWhitelist.getDataFolder(), "config.yml"));
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
if (!tempFileConfiguration.getStringList("whitelisted").contains(finalNameToRemove)) {
if (!WhitelistedPlayers.CheckForPlayerEasyWhitelist(finalNameToRemove))
{
channel.sendMessage(embedBuilderSuccess.build()).queue();
// Remove role from user when they have been removed from the whitelist if need be
@ -1014,7 +964,7 @@ public class DiscordClient extends ListenerAdapter
} else {
DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () ->
{
if (!checkWhitelistJSON(whitelistJSON, finalNameToRemove)) {
if (!WhitelistedPlayers.CheckForPlayer(finalNameToRemove)) {
channel.sendMessage(embedBuilderSuccess.build()).queue();
// Remove role from user when they have been removed from the whitelist if need be
@ -1129,11 +1079,12 @@ public class DiscordClient extends ListenerAdapter
DiscordWhitelister.getPlugin().getLogger().info(discordUserToRemove + " left. Removing their whitelisted entries...");
List<?> ls = DiscordWhitelister.getRegisteredUsers(discordUserToRemove);
if(ls != null) {
if(ls != null)
{
for (Object minecraftNameToRemove : ls) {
DiscordWhitelister.getPlugin().getLogger().info(minecraftNameToRemove.toString() + " left. Removing their whitelisted entries.");
if (DiscordWhitelister.useEasyWhitelist) {
if (WhitelistedPlayers.usingEasyWhitelist)
{
executeServerCommand("easywl remove " + minecraftNameToRemove.toString());
} else {
executeServerCommand("whitelist remove " + minecraftNameToRemove.toString());
@ -1146,42 +1097,13 @@ public class DiscordClient extends ListenerAdapter
return;
}
DiscordWhitelister.getPlugin().getLogger().info(discordUserToRemove + " left. Successfully removed their whitelisted entries.");
}
else {
else
{
DiscordWhitelister.getPlugin().getLogger().warning(discordUserToRemove + " left. Could not removed their whitelisted entries as they did not whitelist through this plugin.");
}
}
// Input name should always be set to lower case
public static boolean checkWhitelistJSON(File whitelistFile, String minecraftUsername)
{
boolean correctUsername = false;
try
{
JSONParser jsonParser = new JSONParser();
JSONArray jsonArray = (JSONArray) jsonParser.parse(new FileReader(whitelistFile));
for (Object object : jsonArray)
{
JSONObject player = (JSONObject) object;
String userName = (String) player.get("name");
userName = userName.toLowerCase();
if (userName.equals(minecraftUsername))
correctUsername = true;
}
}
catch (IOException | ParseException e)
{
e.printStackTrace();
}
return correctUsername;
}
private String minecraftUsernameToUUID(String minecraftUsername)
{
URL playerURL;

View File

@ -15,6 +15,7 @@ import uk.co.angrybee.joe.Configs.MainConfig;
import uk.co.angrybee.joe.Events.JoinLeaveEvents;
import uk.co.angrybee.joe.Events.OnWhitelistEvents;
import uk.co.angrybee.joe.Stores.InGameRemovedList;
import uk.co.angrybee.joe.Stores.WhitelistedPlayers;
import java.io.File;
import java.io.IOException;
@ -29,21 +30,16 @@ public class DiscordWhitelister extends JavaPlugin
private static FileConfiguration userList;
private static FileConfiguration removedList;
// easy whitelist
public static Plugin easyWhitelist;
public static String botToken;
private static boolean configCreated = false;
private static boolean userListCreated = false;
private static boolean removedListCreated = false;
public static boolean useEasyWhitelist = false;
public static boolean useCustomMessages = false;
public static boolean useIdForRoles = false;
public static boolean useCustomPrefixes = false;
public static boolean showPlayerSkin = true;
public static boolean addInGameRemovesToList = true;
public static boolean showVanishedPlayersInCount = false;
public static boolean useInGameAddRemoves = true;
@ -205,22 +201,22 @@ public class DiscordWhitelister extends JavaPlugin
DiscordClient.allowedToAddLimitedRoles[roles] = getWhitelisterBotConfig().getList("limited-add-roles").get(roles).toString();
}
// easy whitelist check
if(getWhitelisterBotConfig().getBoolean("use-easy-whitelist"))
{
pluginLogger.info("Checking for Easy Whitelist...");
if(thisServer.getPluginManager().getPlugin("EasyWhitelist") != null)
{
pluginLogger.info("Easy Whitelist found! Will use over default whitelist command.");
easyWhitelist = thisServer.getPluginManager().getPlugin("EasyWhitelist");
useEasyWhitelist = true;
}
else
{
pluginLogger.warning("Easy Whitelist was not found but is enabled in the config. " +
"Falling back to default whitelist command.");
}
}
// // easy whitelist check
// if(getWhitelisterBotConfig().getBoolean("use-easy-whitelist"))
// {
// pluginLogger.info("Checking for Easy Whitelist...");
// if(thisServer.getPluginManager().getPlugin("EasyWhitelist") != null)
// {
// pluginLogger.info("Easy Whitelist found! Will use over default whitelist command.");
// easyWhitelist = thisServer.getPluginManager().getPlugin("EasyWhitelist");
// useEasyWhitelist = true;
// }
// else
// {
// pluginLogger.warning("Easy Whitelist was not found but is enabled in the config. " +
// "Falling back to default whitelist command");
// }
// }
// Custom messages check
useCustomMessages = getWhitelisterBotConfig().getBoolean("use-custom-messages");
@ -266,6 +262,8 @@ public class DiscordWhitelister extends JavaPlugin
// Init Stores
InGameRemovedList.StoreSetup();
WhitelistedPlayers.Setup();
userListFile = new File(dataFolder, "user-list.yml");
removedListFile = new File(dataFolder, "removed-list.yml");

View File

@ -8,6 +8,7 @@ import org.jetbrains.annotations.NotNull;
import uk.co.angrybee.joe.DiscordClient;
import uk.co.angrybee.joe.DiscordWhitelister;
import uk.co.angrybee.joe.Stores.InGameRemovedList;
import uk.co.angrybee.joe.Stores.WhitelistedPlayers;
import java.io.File;
import java.io.IOException;
@ -17,20 +18,17 @@ public class OnWhitelistEvents implements Listener
{
private enum CommandContext { VANILLA_ADD, EASYWL_ADD, VANILLA_REMOVE, EASYWL_REMOVE }
//private static final File whitelistFile = (new File(".", "whitelist.json"));
// TODO: incorporate this into the checkWhitelistJSON function
private static final File whitelistFile = (new File(".", "whitelist.json"));
@EventHandler
public void onCommandPreprocess(PlayerCommandPreprocessEvent event) // ? no need to log uuid as the minecraft name is just for reference
public void onCommandPreprocess(PlayerCommandPreprocessEvent event)
{
// @NotNull Player commandCaller, @NotNull String message
// Initial check
CommandContext commandContext;
Player commandCaller = event.getPlayer();
String message = event.getMessage();
event.getPlayer().chat("test");
if(message.startsWith("/whitelist add"))
commandContext = CommandContext.VANILLA_ADD;
else if(message.startsWith("/easywl add"))
@ -42,8 +40,6 @@ public class OnWhitelistEvents implements Listener
else
return;
commandCaller.chat(commandContext.toString());
// Don't proceed to run if the player does not have permission
if(commandContext.equals(CommandContext.VANILLA_ADD) && !commandCaller.hasPermission("bukkit.command.whitelist.add"))
return;
@ -55,72 +51,68 @@ public class OnWhitelistEvents implements Listener
return;
// Determine what command to check
// TODO: sort out case sensitive stuff when adding & removing in-game
// TODO: PM staff member of removal from list
event.getPlayer().chat("passed check");
String targetName = "";
// Check for adds to remove player's off the removed list (if they are on it)
if(commandContext.equals(CommandContext.VANILLA_ADD) && !DiscordWhitelister.useEasyWhitelist)
if(commandContext.equals(CommandContext.VANILLA_ADD) && !WhitelistedPlayers.usingEasyWhitelist)
{
// TODO
// DONE Check removed-list.yml, remove username from there if it exists
// DONE Check in-game removed list when created and remove from there if it exists
// Log removal of name from list if it existed
targetName = message.substring("/whitelist add".length() + 1).toLowerCase();
// TODO: change when removed list is moved to stores folder
if(DiscordWhitelister.getRemovedList().get(targetName) != null)
{
DiscordWhitelister.getPluginLogger().info(commandCaller.getName() + " is attempting to add " + targetName + ". Removing " + targetName +
" from removed-list.yml");
DiscordWhitelister.getRemovedList().set(targetName, null);
// Save changes
// TODO: call save function when created instead of doing it again here
try
{
DiscordWhitelister.getRemovedList().save(DiscordWhitelister.getRemovedListFile().getPath());
}
catch (IOException e)
{
e.printStackTrace();
}
}
if(InGameRemovedList.CheckStoreForPlayer(targetName))
{
InGameRemovedList.RemoveUserFromStore(targetName);
DiscordWhitelister.getPluginLogger().info(commandCaller.getName() + " is attempting to add " + targetName + ". Removing " + targetName +
" from in-game-removed-list.yml");
}
ClearPlayerFromRemovedLists(targetName, commandCaller);
}
else if(commandContext.equals(CommandContext.EASYWL_ADD) && DiscordWhitelister.useEasyWhitelist)
else if(commandContext.equals(CommandContext.EASYWL_ADD) && WhitelistedPlayers.usingEasyWhitelist)
{
targetName = message.substring("/easywl add".length() + 1).toLowerCase();
ClearPlayerFromRemovedLists(targetName, commandCaller);
}
else if(commandContext.equals(CommandContext.VANILLA_REMOVE) && !DiscordWhitelister.useEasyWhitelist)
else if(commandContext.equals(CommandContext.VANILLA_REMOVE) && !WhitelistedPlayers.usingEasyWhitelist)
{
// TODO
// DONE Check if the player is in the whitelist as this runs before the command is executed
// DONE Add player to in-game removed list and the players' name that removed it
targetName = message.substring("/whitelist remove".length() + 1).toLowerCase();
// Should this be checked?
// TODO: incorporate this into the function
File tempWhitelistJSON = (new File(".", "whitelist.json"));
if(DiscordClient.checkWhitelistJSON(tempWhitelistJSON, targetName))
if(WhitelistedPlayers.CheckForPlayer(targetName))
{
InGameRemovedList.AddUserToStore(targetName, commandCaller.getName());
DiscordWhitelister.getPluginLogger().info(commandCaller.getName() + " has added " + targetName + " to the in-game removed list");
}
}
else if(commandContext.equals(CommandContext.EASYWL_REMOVE) && DiscordWhitelister.useEasyWhitelist)
else if(commandContext.equals(CommandContext.EASYWL_REMOVE) && WhitelistedPlayers.usingEasyWhitelist)
{
targetName = message.substring("/easywl remove".length() + 1).toLowerCase();
if(WhitelistedPlayers.CheckForPlayerEasyWhitelist(targetName))
{
InGameRemovedList.AddUserToStore(targetName, commandCaller.getName());
DiscordWhitelister.getPluginLogger().info(commandCaller.getName() + " has added " + targetName + " to the in-game removed list");
}
}
}
private static void ClearPlayerFromRemovedLists(String playerName, Player commandCaller)
{
// TODO: change when removed list is moved to stores folder
if(DiscordWhitelister.getRemovedList().get(playerName) != null)
{
DiscordWhitelister.getPluginLogger().info(commandCaller.getName() + " is attempting to add " + playerName + ". Removing " + playerName +
" from removed-list.yml");
DiscordWhitelister.getRemovedList().set(playerName, null);
// Save changes
// TODO: call save function when created instead of doing it again here
try
{
DiscordWhitelister.getRemovedList().save(DiscordWhitelister.getRemovedListFile().getPath());
}
catch (IOException e)
{
e.printStackTrace();
}
}
if(InGameRemovedList.CheckStoreForPlayer(playerName))
{
InGameRemovedList.RemoveUserFromStore(playerName);
DiscordWhitelister.getPluginLogger().info(commandCaller.getName() + " is attempting to add " + playerName + ". Removing " + playerName +
" from in-game-removed-list.yml");
}
}
}

View File

@ -0,0 +1,101 @@
package uk.co.angrybee.joe.Stores;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import uk.co.angrybee.joe.Configs.MainConfig;
import uk.co.angrybee.joe.DiscordWhitelister;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;
// TODO: 1 File to access Vanilla & EWhitelist whitelisted players
public class WhitelistedPlayers
{
private static Plugin easyWhitelist;
private static FileConfiguration easyWhitelistPlayers;
private static final Server server = DiscordWhitelister.getPlugin().getServer();
private static final PluginManager pluginManager = server.getPluginManager();
private static final Logger pluginLogger = DiscordWhitelister.getPluginLogger();
public static boolean usingEasyWhitelist;
public static void Setup()
{
// Check if we are using EasyWhitelist
if(MainConfig.getMainConfig().getBoolean("use-easy-whitelist"))
GetEasyWhitelist();
if(usingEasyWhitelist)
{
try
{
easyWhitelistPlayers = new YamlConfiguration();
easyWhitelistPlayers.load(new File(easyWhitelist.getDataFolder(), "config.yml"));
}
catch (IOException | InvalidConfigurationException e)
{
pluginLogger.severe("Failed to load the EasyWhitelist file, reverting back to vanilla whitelist command");
usingEasyWhitelist = false;
e.printStackTrace();
}
}
}
private static void GetEasyWhitelist()
{
if(pluginManager.getPlugin("EasyWhitelist") != null)
{
pluginLogger.info("Easy Whitelist found; will use over default whitelist command");
easyWhitelist = pluginManager.getPlugin("EasyWhitelist");
usingEasyWhitelist = true;
}
else
{
usingEasyWhitelist = false; // Define this for config hot reloads
pluginLogger.warning("Easy Whitelist was not found but is enabled in the config. " +
"Falling back to default whitelist command");
}
}
public static boolean CheckForPlayer(String playerName)
{
for(OfflinePlayer offlinePlayer : server.getWhitelistedPlayers())
{
if(offlinePlayer.getName().equalsIgnoreCase(playerName))
return true;
}
return false;
}
public static boolean CheckForPlayerEasyWhitelist(String playerName)
{
// Check just in case
if(!usingEasyWhitelist)
return false;
// Load changes
try
{
easyWhitelistPlayers.load(new File(easyWhitelist.getDataFolder(), "config.yml"));
}
catch (IOException | InvalidConfigurationException e)
{
e.printStackTrace();
}
for(String name : easyWhitelistPlayers.getStringList("whitelisted"))
{
if(name.equalsIgnoreCase(playerName))
return true;
}
return false;
}
}

View File

@ -1,5 +0,0 @@
package uk.co.angrybee.joe.Stores;
public class WhitelistedPlayersList
{
}