From 8e855bc9bb4ee50c2e2975266ec590d48196f721 Mon Sep 17 00:00:00 2001 From: Driftay Date: Sat, 28 Dec 2019 04:58:33 -0500 Subject: [PATCH] More Audit Things + Code Cleanup + SaberException Added for Debugging --- .../java/com/massivecraft/factions/Conf.java | 3 + .../massivecraft/factions/cmd/FCmdRoot.java | 9 ++ .../factions/cmd/audit/FAuditMenu.java | 80 ++++-------- .../factions/cmd/audit/FChestListener.java | 6 +- .../factions/cmd/audit/FLogManager.java | 10 +- .../factions/cmd/audit/FLogType.java | 21 +++ .../factions/cmd/audit/FactionLogs.java | 19 ++- .../factions/cmd/audit/LogTimer.java | 7 +- .../factions/discord/CmdSetGuild.java | 3 +- .../factions/discord/Discord.java | 4 +- .../factions/discord/DiscordListener.java | 121 +++++------------- .../factions/discord/DiscordSetupAttempt.java | 2 +- .../factions/discord/FactionChatHandler.java | 22 +--- .../massivecraft/factions/util/JSONUtils.java | 2 + .../util/exceptions/SaberException.java | 11 ++ .../exceptions/impl/DiscordException.java | 12 ++ .../zcore/fperms/PermissableAction.java | 1 - src/main/resources/config.yml | 35 +++++ 18 files changed, 178 insertions(+), 190 deletions(-) create mode 100644 src/main/java/com/massivecraft/factions/util/exceptions/SaberException.java create mode 100644 src/main/java/com/massivecraft/factions/util/exceptions/impl/DiscordException.java diff --git a/src/main/java/com/massivecraft/factions/Conf.java b/src/main/java/com/massivecraft/factions/Conf.java index 69cec4fc..8e68573e 100644 --- a/src/main/java/com/massivecraft/factions/Conf.java +++ b/src/main/java/com/massivecraft/factions/Conf.java @@ -101,6 +101,9 @@ public class Conf { public static boolean worldGuardBuildPriority = false; public static boolean factionsDrainEnabled = false; + //AUDIT + public static boolean useAuditSystem = true; + //DISCORD public static boolean useDiscordSystem = false; public static String discordBotToken = ""; diff --git a/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java b/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java index adc38d93..9af731db 100644 --- a/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java +++ b/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java @@ -3,6 +3,7 @@ package com.massivecraft.factions.cmd; import com.massivecraft.factions.Conf; import com.massivecraft.factions.FactionsPlugin; import com.massivecraft.factions.cmd.alts.CmdAlts; +import com.massivecraft.factions.cmd.audit.CmdAudit; import com.massivecraft.factions.cmd.check.CmdCheck; import com.massivecraft.factions.cmd.check.CmdWeeWoo; import com.massivecraft.factions.cmd.chest.CmdChest; @@ -164,6 +165,7 @@ public class FCmdRoot extends FCommand implements CommandExecutor { public CmdDebug cmdDebug = new CmdDebug(); public CmdDrain cmdDrain = new CmdDrain(); public CmdLookup cmdLookup = new CmdLookup(); + public CmdAudit cmdAudit = new CmdAudit(); //Variables to know if we already setup certain sub commands public Boolean discordEnabled = false; public Boolean checkEnabled = false; @@ -179,6 +181,7 @@ public class FCmdRoot extends FCommand implements CommandExecutor { public Boolean coreProtectEnabled = false; public Boolean internalFTOPEnabled = false; public Boolean fWildEnabled = false; + public Boolean fAuditEnabled = false; public FCmdRoot() { super(); @@ -340,6 +343,12 @@ public class FCmdRoot extends FCommand implements CommandExecutor { this.addSubCommand(this.cmdTop); internalFTOPEnabled = true; } + + if(Conf.useAuditSystem){ + this.addSubCommand(cmdAudit); + fAuditEnabled = true; + } + //Other if (FactionsPlugin.getInstance().getConfig().getBoolean("Wild.Enabled", false) && !fWildEnabled) { this.addSubCommand(this.cmdWild); diff --git a/src/main/java/com/massivecraft/factions/cmd/audit/FAuditMenu.java b/src/main/java/com/massivecraft/factions/cmd/audit/FAuditMenu.java index c12d7089..5573798e 100644 --- a/src/main/java/com/massivecraft/factions/cmd/audit/FAuditMenu.java +++ b/src/main/java/com/massivecraft/factions/cmd/audit/FAuditMenu.java @@ -1,7 +1,7 @@ package com.massivecraft.factions.cmd.audit; -/** - * @author Saser +/* + @author Saser */ import com.google.common.collect.Lists; import com.massivecraft.factions.Faction; @@ -20,7 +20,6 @@ import java.util.LinkedList; import java.util.List; public class FAuditMenu extends GUIMenu { - private static int logsPerPage = 20; private Player player; private boolean showTimestamps = true; private Faction faction; @@ -33,62 +32,42 @@ public class FAuditMenu extends GUIMenu { public void drawItems() { int index = 0; - FLogType[] logTypes = FLogType.values(); - int length1 = logTypes.length; - - for (FLogType type : logTypes) { + for (FLogType type : FLogType.values()) { + if (type.getSlot() == -1) continue; if (type != FLogType.F_TNT || FactionsPlugin.getInstance().getConfig().getBoolean("f-points.Enabled")) { - if (index == 9) { - index = FactionsPlugin.getInstance().getConfig().getBoolean("f-points.Enabled") ? 11 : 12; - } - - FactionLogs logs = FactionsPlugin.instance.getFlogManager().getFactionLogMap().get(this.faction.getId()); - if (logs == null) { - logs = new FactionLogs(); - } - + if (index == 9) index = FactionsPlugin.getInstance().getConfig().getBoolean("f-points.Enabled") ? 11 : 12; + FactionLogs logs = FactionsPlugin.instance.getFlogManager().getFactionLogMap().get(faction.getId()); + if (logs == null) logs = new FactionLogs(); LinkedList recentLogs = logs.getMostRecentLogs().get(type); - if (recentLogs == null) { - recentLogs = Lists.newLinkedList(); - } - + if (recentLogs == null) recentLogs = Lists.newLinkedList(); List lore = Lists.newArrayList("", CC.GreenB + "Recent Logs " + CC.Green + "(" + CC.GreenB + recentLogs.size() + CC.Green + ")"); int added = 0; Iterator backwars = recentLogs.descendingIterator(); + int logsPerPage = 20; while (backwars.hasNext()) { FactionLogs.FactionLog log = (FactionLogs.FactionLog) backwars.next(); - if (added >= logsPerPage) { - break; - } - - String length = log.getLogLine(type, this.showTimestamps); + if (added >= logsPerPage) break; + String length = log.getLogLine(type, showTimestamps); lore.add(" " + CC.Yellow + length); ++added; } - int logSize = recentLogs.size(); int logsLeft = logSize - logsPerPage; - if (logsLeft > 0) { - lore.add(CC.YellowB + logsLeft + CC.Yellow + " more logs..."); - } - + if (logsLeft > 0) lore.add(CC.YellowB + logsLeft + CC.Yellow + " more logs..."); lore.add(""); - if (logsLeft > 0) { - lore.add(CC.Yellow + "Left-Click " + CC.Gray + "to view more logs"); - } - + if (logsLeft > 0) lore.add(CC.Yellow + "Left-Click " + CC.Gray + "to view more logs"); lore.add(CC.Yellow + "Right-Click " + CC.Gray + "to toggle timestamps"); - this.setItem(index++, (new ClickableItemStack((new ItemBuilder(type.getDisplayMaterial())).name(CC.GreenB + type.getDisplayName()).lore(lore).build())).setClickCallback((click) -> { + setItem(index++, (new ClickableItemStack((new ItemBuilder(type.getDisplayMaterial())).name(CC.GreenB + type.getDisplayName()).lore(lore).build())).setClickCallback((click) -> { click.setCancelled(true); if (click.getClick() == ClickType.RIGHT) { - this.showTimestamps = !this.showTimestamps; - this.drawItems(); + showTimestamps = !showTimestamps; + drawItems(); } else { if (logsLeft <= 0) { - this.player.sendMessage(CC.Red + "No extra logs to load."); + player.sendMessage(CC.Red + "No extra logs to load."); return; } - Bukkit.getScheduler().scheduleSyncDelayedTask(FactionsPlugin.instance, () -> (new FAuditLogMenu(this.player, this.faction, type)).open(this.player)); + Bukkit.getScheduler().scheduleSyncDelayedTask(FactionsPlugin.instance, () -> (new FAuditLogMenu(player, faction, type)).open(player)); } })); } @@ -96,7 +75,7 @@ public class FAuditMenu extends GUIMenu { } - class FAuditLogMenu extends GUIMenu { + static class FAuditLogMenu extends GUIMenu { private Player player; private Faction faction; private FLogType logType; @@ -111,11 +90,11 @@ public class FAuditMenu extends GUIMenu { public void drawItems() { FactionLogs logs = FactionsPlugin.instance.getFlogManager().getFactionLogMap().get(faction.getId()); - int perPage = this.logType == FLogType.F_TNT ? 25 : 20; + int perPage = logType == FLogType.F_TNT ? 25 : 20; if (logs != null) { - LinkedList log = logs.getMostRecentLogs().get(this.logType); + LinkedList log = logs.getMostRecentLogs().get(logType); if (log != null) { - int slot = this.logType == FLogType.F_TNT ? 0 : 3; + int slot = logType == FLogType.F_TNT ? 0 : 3; int pagesToShow = (int)Math.max(1.0D, Math.ceil((double)log.size() / (double)perPage)); for(int page = 1; page <= pagesToShow; ++page) { @@ -131,26 +110,23 @@ public class FAuditMenu extends GUIMenu { if (i < 0) { break; } - FactionLogs.FactionLog l = log.get(i); - lore.add(" " + CC.Yellow + l.getLogLine(this.logType, this.timeStamp)); + lore.add(" " + CC.Yellow + l.getLogLine(logType, timeStamp)); } } - lore.add(""); lore.add(CC.Gray + "Click to toggle timestamp"); - this.setItem(slot++, (new ClickableItemStack((new ItemBuilder(Material.PAPER)).name(CC.GreenB + "Log #" + page).lore(lore).build())).setClickCallback((e) -> { + setItem(slot++, (new ClickableItemStack((new ItemBuilder(Material.PAPER)).name(CC.GreenB + "Log #" + page).lore(lore).build())).setClickCallback((e) -> { e.setCancelled(true); - this.timeStamp = !this.timeStamp; - this.drawItems(); + timeStamp = !timeStamp; + drawItems(); })); } } } - - this.setItem(this.getSize() - 1, (new ClickableItemStack((new ItemBuilder(Material.ARROW)).name(CC.Green + "Previous Page").lore("", CC.Gray + "Click to view previous page!").build())).setClickCallback((event) -> { + setItem(getSize() - 1, (new ClickableItemStack((new ItemBuilder(Material.ARROW)).name(CC.Green + "Previous Page").lore("", CC.Gray + "Click to view previous page!").build())).setClickCallback((event) -> { event.setCancelled(true); - Bukkit.getScheduler().scheduleSyncDelayedTask(FactionsPlugin.instance, () -> (new FAuditMenu(this.player, this.faction)).open(this.player)); + Bukkit.getScheduler().scheduleSyncDelayedTask(FactionsPlugin.instance, () -> (new FAuditMenu(player, faction)).open(player)); })); } } diff --git a/src/main/java/com/massivecraft/factions/cmd/audit/FChestListener.java b/src/main/java/com/massivecraft/factions/cmd/audit/FChestListener.java index ee5c6f51..5807e04c 100644 --- a/src/main/java/com/massivecraft/factions/cmd/audit/FChestListener.java +++ b/src/main/java/com/massivecraft/factions/cmd/audit/FChestListener.java @@ -88,12 +88,12 @@ public class FChestListener implements Listener { return; } - this.logRemoveItem(currentItem, fplayer, player); + logRemoveItem(currentItem, fplayer, player); } else if (cursorItemType != Material.AIR && !event.isShiftClick()) { - this.logAddItem(cursorItem, fplayer, player); + logAddItem(cursorItem, fplayer, player); } } else if (event.isShiftClick() && currentItemType != Material.AIR) { - this.logAddItem(currentItem, fplayer, player); + logAddItem(currentItem, fplayer, player); } } diff --git a/src/main/java/com/massivecraft/factions/cmd/audit/FLogManager.java b/src/main/java/com/massivecraft/factions/cmd/audit/FLogManager.java index 1aa90ce7..a6b99a7d 100644 --- a/src/main/java/com/massivecraft/factions/cmd/audit/FLogManager.java +++ b/src/main/java/com/massivecraft/factions/cmd/audit/FLogManager.java @@ -13,10 +13,8 @@ import org.bukkit.Bukkit; import java.io.File; import java.lang.reflect.Type; import java.util.Map; -import java.util.Timer; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.TimeUnit; public class FLogManager { private Map factionLogMap = new ConcurrentHashMap<>(); @@ -41,7 +39,7 @@ public class FLogManager { logFile.createNewFile(); } - factionLogMap = (Map) JSONUtils.fromJson(logFile, logToken); + factionLogMap = (Map) JSONUtils.fromJson(logFile, logToken); if (factionLogMap == null) { factionLogMap = new ConcurrentHashMap<>(); } @@ -64,8 +62,6 @@ public class FLogManager { e.printStackTrace(); } - long delay = TimeUnit.SECONDS.toMillis(15L); - long sellDelay = TimeUnit.SECONDS.toMillis(30L); Bukkit.getScheduler().scheduleSyncRepeatingTask(FactionsPlugin.instance, () -> { if (saving) { Bukkit.getLogger().info("Ignoring saveLogs scheduler due to saving == true!"); @@ -100,7 +96,7 @@ public class FLogManager { } if (type != null) { - Map timers = (Map) logTimer.get(type); + Map timers = logTimer.get(type); if (timers != null && faction != null) { logTimer.pushLogs(faction, type); } @@ -133,7 +129,7 @@ public class FLogManager { try { JSONUtils.saveJSONToFile(logFile, factionLogMap,logToken); } catch (Exception e1) { - Bukkit.getLogger().info("ERRRO SAVING JSON LOGS: " + e1.getMessage()); + Bukkit.getLogger().info("ERROR SAVING JSON LOGS: " + e1.getMessage()); e1.printStackTrace(); } diff --git a/src/main/java/com/massivecraft/factions/cmd/audit/FLogType.java b/src/main/java/com/massivecraft/factions/cmd/audit/FLogType.java index 512e63f3..1a3ca41f 100644 --- a/src/main/java/com/massivecraft/factions/cmd/audit/FLogType.java +++ b/src/main/java/com/massivecraft/factions/cmd/audit/FLogType.java @@ -1,6 +1,8 @@ package com.massivecraft.factions.cmd.audit; +import com.massivecraft.factions.FactionsPlugin; import com.massivecraft.factions.util.XMaterial; +import com.massivecraft.factions.zcore.fperms.PermissableAction; import org.bukkit.Material; /** @@ -31,6 +33,25 @@ public enum FLogType { return this.displayName; } + /** + * Case insensitive check for action. + * + * @param check + * @return - action + */ + public static FLogType fromString(String check) { + for (FLogType fLogType : values()) { + if (fLogType.displayName.equalsIgnoreCase(check)) { + return fLogType; + } + } + return null; + } + public int getSlot() { + return FactionsPlugin.getInstance().getConfig().getInt("faudit-gui.slots." + this.displayName.toLowerCase()); + } + + public Material getDisplayMaterial() { return this.displayMaterial; } diff --git a/src/main/java/com/massivecraft/factions/cmd/audit/FactionLogs.java b/src/main/java/com/massivecraft/factions/cmd/audit/FactionLogs.java index 6746b488..78a12f11 100644 --- a/src/main/java/com/massivecraft/factions/cmd/audit/FactionLogs.java +++ b/src/main/java/com/massivecraft/factions/cmd/audit/FactionLogs.java @@ -18,7 +18,6 @@ import java.util.concurrent.TimeUnit; public class FactionLogs { private Map> mostRecentLogs = new ConcurrentHashMap<>(); public static transient SimpleDateFormat format = new SimpleDateFormat("MM/dd hh:mmaa"); - private static transient int MAX_LOG_SIZE = 60; public FactionLogs() { } @@ -28,7 +27,7 @@ public class FactionLogs { Bukkit.getLogger().info("INVALID ARGUMENT COUNT MET: " + type.getRequiredArgs() + " REQUIRED: "); Thread.dumpStack(); } else { - LinkedList logs = this.mostRecentLogs.computeIfAbsent(type, (lists) -> new LinkedList<>()); + LinkedList logs = mostRecentLogs.computeIfAbsent(type, (lists) -> new LinkedList<>()); logs.add(new FactionLog(System.currentTimeMillis(), Lists.newArrayList(arguments))); int maxLog = type == FLogType.F_TNT ? 200 : 60; if (logs.size() > maxLog) { @@ -69,16 +68,14 @@ public class FactionLogs { toRemove.add(logType); } }); - toRemove.forEach((rem) -> { - LinkedList linkedList = this.mostRecentLogs.remove(rem); - }); + toRemove.forEach((rem) -> mostRecentLogs.remove(rem)); } public Map> getMostRecentLogs() { - return this.mostRecentLogs; + return mostRecentLogs; } - public class FactionLog { + public static class FactionLog { private long t; private List a; @@ -88,19 +85,19 @@ public class FactionLogs { } public boolean isExpired(long duration) { - return System.currentTimeMillis() - this.t >= duration; + return System.currentTimeMillis() - t >= duration; } public String getLogLine(FLogType type, boolean timestamp) { - String[] args = this.a.toArray(new String[0]); + String[] args = a.toArray(new String[0]); String timeFormat = ""; if (timestamp) { - timeFormat = FactionLogs.format.format(this.t); + timeFormat = FactionLogs.format.format(t); if (timeFormat.startsWith("0")) { timeFormat = timeFormat.substring(1); } } - return String.format(ChatColor.translateAlternateColorCodes('&', type.getMsg()), (String[])args) + (timestamp ? ChatColor.GRAY + " - " + timeFormat : ""); + return String.format(ChatColor.translateAlternateColorCodes('&', type.getMsg()), args) + (timestamp ? ChatColor.GRAY + " - " + timeFormat : ""); } } } diff --git a/src/main/java/com/massivecraft/factions/cmd/audit/LogTimer.java b/src/main/java/com/massivecraft/factions/cmd/audit/LogTimer.java index 0a824ea2..23afd63e 100644 --- a/src/main/java/com/massivecraft/factions/cmd/audit/LogTimer.java +++ b/src/main/java/com/massivecraft/factions/cmd/audit/LogTimer.java @@ -33,7 +33,6 @@ public class LogTimer extends ConcurrentHashMap { if (timerType == type) { if (timerType == LogTimer.TimerType.SPAWNER_EDIT) { @@ -44,18 +43,18 @@ public class LogTimer extends ConcurrentHashMap { EntityType types = EntityType.fromId(data.getData()); if (types == null) { - Bukkit.getLogger().info("Unable to find EntityType for " + data.getData() + " for " + subTimer + " for fac " + this.factionId + "!"); + Bukkit.getLogger().info("Unable to find EntityType for " + data.getData() + " for " + subTimer + " for fac " + factionId + "!"); } else { entityCounts.computeIfAbsent(types, (e) -> new AtomicInteger(0)).addAndGet(ints.get()); } }); - entityCounts.forEach((entityType, count) -> FactionsPlugin.instance.getFlogManager().log(faction, FLogType.SPAWNER_EDIT, this.username, subTimer == TimerSubType.SPAWNER_BREAK ? "broke" : "placed", count.get() + "x", StringUtils.capitaliseAllWords(entityType.name().toLowerCase().replace("_", " ")))); + entityCounts.forEach((entityType, count) -> FactionsPlugin.instance.getFlogManager().log(faction, FLogType.SPAWNER_EDIT, username, subTimer == TimerSubType.SPAWNER_BREAK ? "broke" : "placed", count.get() + "x", StringUtils.capitaliseAllWords(entityType.name().toLowerCase().replace("_", " ")))); } }); } } }); - this.remove(type); + remove(type); } public String getFactionId() { diff --git a/src/main/java/com/massivecraft/factions/discord/CmdSetGuild.java b/src/main/java/com/massivecraft/factions/discord/CmdSetGuild.java index cc1f7f9e..1477b6de 100644 --- a/src/main/java/com/massivecraft/factions/discord/CmdSetGuild.java +++ b/src/main/java/com/massivecraft/factions/discord/CmdSetGuild.java @@ -53,10 +53,9 @@ public class CmdSetGuild extends FCommand { if (guildId != null && !guildId.equalsIgnoreCase("null")) { Guild guild = null; - try { guild = jda.getGuildById(guildId); - } catch (NumberFormatException var7) { + } catch (NumberFormatException e) { } if (guild == null) { diff --git a/src/main/java/com/massivecraft/factions/discord/Discord.java b/src/main/java/com/massivecraft/factions/discord/Discord.java index 1a816557..4ca08445 100644 --- a/src/main/java/com/massivecraft/factions/discord/Discord.java +++ b/src/main/java/com/massivecraft/factions/discord/Discord.java @@ -189,9 +189,7 @@ public class Discord { * @return Boolean */ public static Boolean isInMainGuild(User u) { - if (mainGuild == null) { - return false; - } + if (mainGuild == null) return false; return mainGuild.getMember(u) == null ? Boolean.FALSE : Boolean.TRUE; } diff --git a/src/main/java/com/massivecraft/factions/discord/DiscordListener.java b/src/main/java/com/massivecraft/factions/discord/DiscordListener.java index 7b0c3988..d4e9e52a 100644 --- a/src/main/java/com/massivecraft/factions/discord/DiscordListener.java +++ b/src/main/java/com/massivecraft/factions/discord/DiscordListener.java @@ -66,9 +66,7 @@ public class DiscordListener extends ListenerAdapter { public void onPrivateMessageReceived(PrivateMessageReceivedEvent e) { Integer i; - if (e.getAuthor().isBot()) { - return; - } + if (e.getAuthor().isBot()) return; try { i = Integer.valueOf(e.getMessage().getContentDisplay()); } catch (NumberFormatException ex) { @@ -89,17 +87,13 @@ public class DiscordListener extends ListenerAdapter { public void onGuildMessageReceived(GuildMessageReceivedEvent event) { try { - if (event.getMessage().isWebhookMessage() || event.getAuthor().isBot()) { - return; - } + if (event.getMessage().isWebhookMessage() || event.getAuthor().isBot()) return; String prefix = DiscordListener.guilds.getGuildById(event.getGuild().getId()).getPrefix(); if (prefix == null || prefix.isEmpty()) { prefix = "."; } String content = event.getMessage().getContentRaw(); - if (!content.startsWith(prefix) && !content.startsWith(event.getGuild().getSelfMember().getAsMention())) { - return; - } + if (!content.startsWith(prefix) && !content.startsWith(event.getGuild().getSelfMember().getAsMention())) return; if (content.startsWith(prefix + "help") || content.startsWith(event.getGuild().getSelfMember().getAsMention() + " help")) { this.help(event, content, prefix); } else if (content.startsWith(prefix + "stats")) { @@ -130,9 +124,7 @@ public class DiscordListener extends ListenerAdapter { this.settings(event); } } catch (PermissionException exception) { - if (!event.getGuild().getSelfMember().hasPermission(event.getChannel(), Permission.MESSAGE_READ, Permission.MESSAGE_WRITE)) { - return; - } + if (!event.getGuild().getSelfMember().hasPermission(event.getChannel(), Permission.MESSAGE_READ, Permission.MESSAGE_WRITE)) return; event.getChannel().sendMessage((":x: Missing permission, `" + exception.getPermission().toString() + "`")).queue(); } } @@ -143,33 +135,25 @@ public class DiscordListener extends ListenerAdapter { private Faction getFactionWithWarning(TextChannel textChannel) { Faction faction = this.getFaction(textChannel.getGuild()); - if (faction == null) { - textChannel.sendMessage((":x: This guild isn't linked to a faction, use `/f setguild " + textChannel.getGuild().getId() + "` in game")).queue(); - } + if (faction == null) textChannel.sendMessage((":x: This guild isn't linked to a faction, use `/f setguild " + textChannel.getGuild().getId() + "` in game")).queue(); return faction; } private boolean cantAccessPermissionWithWarning(TextChannel textChannel, Member member) { boolean can = member.hasPermission(Permission.MANAGE_SERVER); - if (!can) { - textChannel.sendMessage(":x: You need to have the Manage Server permission to do that").queue(); - } + if (!can) textChannel.sendMessage(":x: You need to have the Manage Server permission to do that").queue(); return !can; } private boolean canAccessRole(Faction faction, Member member) { - if (member.hasPermission(Permission.MANAGE_SERVER)) { - return true; - } + if (member.hasPermission(Permission.MANAGE_SERVER)) return true; Role role = member.getGuild().getRoleById(faction.getMemberRoleId()); return role != null && member.getRoles().stream().anyMatch(r -> r.getPosition() >= role.getPosition()); } private boolean cantAccessRoleWithWarning(TextChannel textChannel, Faction faction, Member member) { boolean can = this.canAccessRole(faction, member); - if (!can) { - textChannel.sendMessage(":x: You don't have a faction member role").queue(); - } + if (!can) textChannel.sendMessage(":x: You don't have a faction member role").queue(); return !can; } @@ -261,9 +245,7 @@ public class DiscordListener extends ListenerAdapter { } private void setPrefix(GuildMessageReceivedEvent event, String content) { - if (this.cantAccessPermissionWithWarning(event.getChannel(), event.getMember())) { - return; - } + if (cantAccessPermissionWithWarning(event.getChannel(), event.getMember())) return; String[] split = content.split(" "); if (split.length != 3) { event.getChannel().sendMessage((":x: Usage, `@" + event.getGuild().getSelfMember().getEffectiveName() + " setprefix `")).queue(); @@ -281,12 +263,8 @@ public class DiscordListener extends ListenerAdapter { private void setFChatChannel(GuildMessageReceivedEvent event) { Faction faction = this.getFactionWithWarning(event.getChannel()); - if (faction == null) { - return; - } - if (this.cantAccessPermissionWithWarning(event.getChannel(), event.getMember())) { - return; - } + if (faction == null) return; + if (cantAccessPermissionWithWarning(event.getChannel(), event.getMember())) return; List mentionedChannels = event.getMessage().getMentionedChannels(); if (mentionedChannels.isEmpty()) { faction.setFactionChatChannelId(null); @@ -304,12 +282,8 @@ public class DiscordListener extends ListenerAdapter { private void setWallNotifyChannel(GuildMessageReceivedEvent event) { Faction faction = this.getFactionWithWarning(event.getChannel()); - if (faction == null) { - return; - } - if (this.cantAccessPermissionWithWarning(event.getChannel(), event.getMember())) { - return; - } + if (faction == null) return; + if (cantAccessPermissionWithWarning(event.getChannel(), event.getMember())) return; List mentionedChannels = event.getMessage().getMentionedChannels(); if (mentionedChannels.isEmpty()) { faction.setWallNotifyChannelId(null); @@ -327,12 +301,8 @@ public class DiscordListener extends ListenerAdapter { private void setBufferNotifyChannel(GuildMessageReceivedEvent event) { Faction faction = this.getFactionWithWarning(event.getChannel()); - if (faction == null) { - return; - } - if (this.cantAccessPermissionWithWarning(event.getChannel(), event.getMember())) { - return; - } + if (faction == null) return; + if (cantAccessPermissionWithWarning(event.getChannel(), event.getMember())) return; List mentionedChannels = event.getMessage().getMentionedChannels(); if (mentionedChannels.isEmpty()) { faction.setBufferNotifyChannelId(null); @@ -350,9 +320,7 @@ public class DiscordListener extends ListenerAdapter { private void setWeewooChannel(GuildMessageReceivedEvent event) { Faction faction = this.getFactionWithWarning(event.getChannel()); - if (faction == null) { - return; - } + if (faction == null) return; if (!event.getMember().hasPermission(Permission.MANAGE_SERVER)) { event.getChannel().sendMessage(":x: You need to have the Manage Server permission to do that").queue(); return; @@ -374,12 +342,8 @@ public class DiscordListener extends ListenerAdapter { private void setNotifyFormat(GuildMessageReceivedEvent event, String content, String prefix) { Faction faction = this.getFactionWithWarning(event.getChannel()); - if (faction == null) { - return; - } - if (this.cantAccessPermissionWithWarning(event.getChannel(), event.getMember())) { - return; - } + if (faction == null)return; + if (cantAccessPermissionWithWarning(event.getChannel(), event.getMember())) return; if (!content.contains(" ")) { event.getChannel().sendMessage((":x: Usage, `" + prefix + "setnotifyformat ` (%type%)")).queue(); return; @@ -397,12 +361,8 @@ public class DiscordListener extends ListenerAdapter { private void setWeewooFormat(GuildMessageReceivedEvent event, String content, String prefix) { Faction faction = this.getFactionWithWarning(event.getChannel()); - if (faction == null) { - return; - } - if (this.cantAccessPermissionWithWarning(event.getChannel(), event.getMember())) { - return; - } + if (faction == null) return; + if (cantAccessPermissionWithWarning(event.getChannel(), event.getMember())) return; if (!content.contains(" ")) { event.getChannel().sendMessage((":x: Usage, `" + prefix + "setweewooformat `")).queue(); return; @@ -420,12 +380,8 @@ public class DiscordListener extends ListenerAdapter { private void setMemberRole(GuildMessageReceivedEvent event, String content, String prefix) { Faction faction = this.getFactionWithWarning(event.getChannel()); - if (faction == null) { - return; - } - if (this.cantAccessPermissionWithWarning(event.getChannel(), event.getMember())) { - return; - } + if (faction == null) return; + if (cantAccessPermissionWithWarning(event.getChannel(), event.getMember())) return; List split = new ArrayList<>(Arrays.asList(content.split(" "))); if (split.size() < 2) { event.getChannel().sendMessage((":x: Usage, `" + prefix + "setmemberrole <@role/role name/role id>`")).queue(); @@ -452,12 +408,9 @@ public class DiscordListener extends ListenerAdapter { private void checkLeaderboard(GuildMessageReceivedEvent event) { Faction faction = this.getFactionWithWarning(event.getChannel()); - if (faction == null) { - return; - } - if (this.cantAccessRoleWithWarning(event.getChannel(), faction, event.getMember())) { - return; - } + if (faction == null) return; + if (cantAccessRoleWithWarning(event.getChannel(), faction, event.getMember())) return; + Map players = new HashMap<>(); for (Map.Entry entry : faction.getPlayerWallCheckCount().entrySet()) { players.put(entry.getKey(), entry.getValue()); @@ -485,12 +438,8 @@ public class DiscordListener extends ListenerAdapter { private void weewoo(GuildMessageReceivedEvent event, String content, String prefix) { Faction faction = this.getFactionWithWarning(event.getChannel()); - if (faction == null) { - return; - } - if (this.cantAccessRoleWithWarning(event.getChannel(), faction, event.getMember())) { - return; - } + if (faction == null) return; + if (cantAccessRoleWithWarning(event.getChannel(), faction, event.getMember())) return; if (!content.contains(" ")) { event.getChannel().sendMessage((":x: Usage, `" + prefix + "weewoo `")).queue(); return; @@ -508,9 +457,7 @@ public class DiscordListener extends ListenerAdapter { String discordChannelId = faction.getWeeWooChannelId(); if (discordChannelId != null && !discordChannelId.isEmpty()) { TextChannel textChannel = event.getJDA().getTextChannelById(discordChannelId); - if (textChannel == null) { - return; - } + if (textChannel == null) return; textChannel.sendMessage(TL.WEEWOO_STARTED_DISCORD.format(event.getAuthor().getAsTag())).queue(); } } else if (arguments.get(1).equalsIgnoreCase("stop")) { @@ -524,9 +471,7 @@ public class DiscordListener extends ListenerAdapter { String discordChannelId = faction.getWeeWooChannelId(); if (discordChannelId != null && !discordChannelId.isEmpty()) { TextChannel textChannel = event.getJDA().getTextChannelById(discordChannelId); - if (textChannel == null) { - return; - } + if (textChannel == null) return; textChannel.sendMessage(TL.WEEWOO_STOPPED_DISCORD.format(event.getAuthor().getAsTag())).queue(); } } else { @@ -536,12 +481,8 @@ public class DiscordListener extends ListenerAdapter { private void settings(GuildMessageReceivedEvent event) { Faction faction = this.getFactionWithWarning(event.getChannel()); - if (faction == null) { - return; - } - if (this.cantAccessRoleWithWarning(event.getChannel(), faction, event.getMember())) { - return; - } + if (faction == null) return; + if (cantAccessRoleWithWarning(event.getChannel(), faction, event.getMember())) return; int wallCheck = faction.getWallCheckMinutes(); int bufferCheck = faction.getBufferCheckMinutes(); String wallChannel = faction.getWallNotifyChannelId(); diff --git a/src/main/java/com/massivecraft/factions/discord/DiscordSetupAttempt.java b/src/main/java/com/massivecraft/factions/discord/DiscordSetupAttempt.java index 717c986d..9426f6e5 100644 --- a/src/main/java/com/massivecraft/factions/discord/DiscordSetupAttempt.java +++ b/src/main/java/com/massivecraft/factions/discord/DiscordSetupAttempt.java @@ -55,7 +55,7 @@ public class DiscordSetupAttempt { * @return String with formatted time difference */ public String getDifferentialFormatted() { - Integer timeIndex = 0; + int timeIndex = 0; //Milliseconds Long inProcessTime = getDifferentialTime(); if (inProcessTime >= 1000) { diff --git a/src/main/java/com/massivecraft/factions/discord/FactionChatHandler.java b/src/main/java/com/massivecraft/factions/discord/FactionChatHandler.java index 2e73a6d2..2dca8849 100644 --- a/src/main/java/com/massivecraft/factions/discord/FactionChatHandler.java +++ b/src/main/java/com/massivecraft/factions/discord/FactionChatHandler.java @@ -37,16 +37,10 @@ public class FactionChatHandler extends ListenerAdapter { public static void sendMessage(FactionsPlugin plugin, Faction faction, UUID uuid, String username, String message) { String factionsChatChannelId = faction.getFactionChatChannelId(); String messageWithMentions = null; - if (factionsChatChannelId == null || factionsChatChannelId.isEmpty()) { - return; - } - if (Discord.jda == null) { - return; - } + if (factionsChatChannelId == null || factionsChatChannelId.isEmpty()) return; + if (Discord.jda == null) return; TextChannel textChannel = Discord.jda.getTextChannelById(factionsChatChannelId); - if (textChannel == null) { - return; - } + if (textChannel == null) return; if (!textChannel.getGuild().getSelfMember().hasPermission(textChannel, Permission.MANAGE_WEBHOOKS)) { textChannel.sendMessage("Missing `Manage Webhooks` permission in this channel").queue(); return; @@ -80,7 +74,6 @@ public class FactionChatHandler extends ListenerAdapter { } if (mention.toString().contains("#")) { String[] mentionA = mention.toString().replace(" @", "").split("#"); - for (User u : Discord.jda.getUsersByName(mentionA[0], false)) { if (u.getDiscriminator().equals(mentionA[1])) { for (Integer l : ii) { @@ -112,13 +105,10 @@ public class FactionChatHandler extends ListenerAdapter { @Override public void onGuildMessageReceived(GuildMessageReceivedEvent event) { - if (event.isWebhookMessage() || event.getAuthor().isBot()) { - return; - } + if (event.isWebhookMessage() || event.getAuthor().isBot()) return; Faction faction = Factions.getInstance().getAllFactions().stream().filter(f -> event.getChannel().getId().equals(f.getFactionChatChannelId())).findAny().orElse(null); - if (faction == null) { - return; - } + if (faction == null) return; + String content = event.getMessage().getContentDisplay(); String message = (content.length() > 500) ? content.substring(0, 500) : content; FancyMessage fancyMessage = new FancyMessage(); diff --git a/src/main/java/com/massivecraft/factions/util/JSONUtils.java b/src/main/java/com/massivecraft/factions/util/JSONUtils.java index d5c6e51f..fbb7a9d1 100644 --- a/src/main/java/com/massivecraft/factions/util/JSONUtils.java +++ b/src/main/java/com/massivecraft/factions/util/JSONUtils.java @@ -6,10 +6,12 @@ package com.massivecraft.factions.util; import com.google.common.reflect.TypeToken; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.massivecraft.factions.cmd.audit.FactionLogs; import org.bukkit.Bukkit; import java.io.*; import java.lang.reflect.Type; +import java.util.Map; public class JSONUtils { public static Gson gson = (new GsonBuilder()).enableComplexMapKeySerialization().create(); diff --git a/src/main/java/com/massivecraft/factions/util/exceptions/SaberException.java b/src/main/java/com/massivecraft/factions/util/exceptions/SaberException.java new file mode 100644 index 00000000..c71ee835 --- /dev/null +++ b/src/main/java/com/massivecraft/factions/util/exceptions/SaberException.java @@ -0,0 +1,11 @@ +package com.massivecraft.factions.util.exceptions; + +/** + * @author Saser + */ + +public abstract class SaberException extends Exception { + public SaberException(String message) { + super(message); + } +} diff --git a/src/main/java/com/massivecraft/factions/util/exceptions/impl/DiscordException.java b/src/main/java/com/massivecraft/factions/util/exceptions/impl/DiscordException.java new file mode 100644 index 00000000..9956fb0e --- /dev/null +++ b/src/main/java/com/massivecraft/factions/util/exceptions/impl/DiscordException.java @@ -0,0 +1,12 @@ +package com.massivecraft.factions.util.exceptions.impl; + +import com.massivecraft.factions.util.exceptions.SaberException; + +/** + * @author Saser + */ +public class DiscordException extends SaberException { + public DiscordException(String message) { + super(message); + } +} diff --git a/src/main/java/com/massivecraft/factions/zcore/fperms/PermissableAction.java b/src/main/java/com/massivecraft/factions/zcore/fperms/PermissableAction.java index f068811a..e0928999 100644 --- a/src/main/java/com/massivecraft/factions/zcore/fperms/PermissableAction.java +++ b/src/main/java/com/massivecraft/factions/zcore/fperms/PermissableAction.java @@ -66,7 +66,6 @@ public enum PermissableAction { return permissableAction; } } - return null; } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 1ad61717..970e97d5 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1269,6 +1269,41 @@ fupgrades: slots: - 4 +############################################################ +# +------------------------------------------------------+ # +# | Faction Audit | # +# +------------------------------------------------------+ # +############################################################ +faudit-gui: + slots: + invites: 0 + bans: 1 + chunk_claims: 2 + perm_edit_defaults: 3 + bank_edit: 4 + fchest_edit: 5 + relation_change: 6 + ftag_edit: 7 + fdesc_edit: 8 + role_perm_edit: 9 + spawner_edit: 10 + rank_edit: 11 + f_tnt: 12 + materials: + invites: 0 + bans: 1 + chunk_claims: 2 + perm_edit_defaults: 3 + bank_edit: 4 + fchest_edit: 5 + relation_change: 6 + ftag_edit: 7 + fdesc_edit: 8 + role_perm_edit: 9 + spawner_edit: 10 + rank_edit: 11 + f_tnt: 12 + ############################################################ # +------------------------------------------------------+ # # | Faction Banners | #