From 8f19f8c05b2b08e7645a46eda8cdd3c6231a9186 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 5 Feb 2018 16:23:32 +0200 Subject: [PATCH] Reduced complexity of some methods. --- .../com/djrapitops/plan/ShutdownHook.java | 17 ++++-- .../plan/data/container/StickyData.java | 54 ++++++++++++------- .../bukkit/CommandPreprocessListener.java | 30 +++++------ .../system/settings/PlanErrorManager.java | 3 +- .../webserver/pages/parsing/InspectPage.java | 7 +-- .../html/tables/PlayersTableCreator.java | 1 + .../html/tables/SessionsTableCreator.java | 1 + 7 files changed, 69 insertions(+), 44 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/ShutdownHook.java b/Plan/src/main/java/com/djrapitops/plan/ShutdownHook.java index f642e9664..e7f11935d 100644 --- a/Plan/src/main/java/com/djrapitops/plan/ShutdownHook.java +++ b/Plan/src/main/java/com/djrapitops/plan/ShutdownHook.java @@ -29,13 +29,22 @@ import java.util.UUID; */ public class ShutdownHook extends Thread { - private static boolean active = false; + private static boolean activated = false; + + private static boolean isActivated() { + return activated; + } + + private static void activate(ShutdownHook hook) { + activated = true; + Runtime.getRuntime().addShutdownHook(hook); + } public void register() { - if (!active) { - Runtime.getRuntime().addShutdownHook(this); + if (isActivated()) { + return; } - active = true; + activate(this); } @Override diff --git a/Plan/src/main/java/com/djrapitops/plan/data/container/StickyData.java b/Plan/src/main/java/com/djrapitops/plan/data/container/StickyData.java index da59fe797..58d4176c4 100644 --- a/Plan/src/main/java/com/djrapitops/plan/data/container/StickyData.java +++ b/Plan/src/main/java/com/djrapitops/plan/data/container/StickyData.java @@ -9,6 +9,8 @@ import com.djrapitops.plan.data.PlayerProfile; import com.djrapitops.plugin.api.TimeAmount; import com.google.common.base.Objects; +import java.util.List; + public class StickyData { private final double activityIndex; private Integer messagesSent; @@ -16,28 +18,26 @@ public class StickyData { public StickyData(PlayerProfile player) { activityIndex = player.getActivityIndex(player.getRegistered() + TimeAmount.DAY.ms()).getValue(); - for (Action action : player.getActions()) { - if (messagesSent == null && action.getDoneAction() == Actions.FIRST_LOGOUT) { - String additionalInfo = action.getAdditionalInfo(); - String[] split = additionalInfo.split(": "); - if (split.length == 2) { - try { - messagesSent = Integer.parseInt(split[1]); - } catch (NumberFormatException ignored) { - } + loadActionVariables(player.getActions()); + } + + private void loadActionVariables(List actions) { + for (Action action : actions) { + try { + if (messagesSent == null && action.getDoneAction() == Actions.FIRST_LOGOUT) { + messagesSent = loadSentMessages(action); } - } - if (onlineOnJoin == null && action.getDoneAction() == Actions.FIRST_SESSION) { - String additionalInfo = action.getAdditionalInfo(); - String[] split = additionalInfo.split(" "); - if (split.length == 3) { - try { - onlineOnJoin = Integer.parseInt(split[1]); - } catch (NumberFormatException ignored) { - } + if (onlineOnJoin == null && action.getDoneAction() == Actions.FIRST_SESSION) { + onlineOnJoin = loadOnlineOnJoin(action); } + } catch (IllegalArgumentException ignore) { + /* continue */ } } + setDefaultValuesIfNull(); + } + + private void setDefaultValuesIfNull() { if (messagesSent == null) { messagesSent = 0; } @@ -46,6 +46,24 @@ public class StickyData { } } + private int loadOnlineOnJoin(Action action) { + String additionalInfo = action.getAdditionalInfo(); + String[] split = additionalInfo.split(" "); + if (split.length == 3) { + return Integer.parseInt(split[1]); + } + throw new IllegalArgumentException("Improper Action"); + } + + private int loadSentMessages(Action action) { + String additionalInfo = action.getAdditionalInfo(); + String[] split = additionalInfo.split(": "); + if (split.length == 2) { + return Integer.parseInt(split[1]); + } + throw new IllegalArgumentException("Improper Action"); + } + public double distance(StickyData data) { double num = 0; num += Math.abs(data.activityIndex - activityIndex) * 2.0; diff --git a/Plan/src/main/java/com/djrapitops/plan/system/listeners/bukkit/CommandPreprocessListener.java b/Plan/src/main/java/com/djrapitops/plan/system/listeners/bukkit/CommandPreprocessListener.java index f97de50b0..0db8038e3 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/listeners/bukkit/CommandPreprocessListener.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/listeners/bukkit/CommandPreprocessListener.java @@ -6,7 +6,6 @@ import com.djrapitops.plan.system.settings.Permissions; import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plugin.api.utility.log.Log; import org.bukkit.command.Command; -import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -37,30 +36,19 @@ public class CommandPreprocessListener implements Listener { */ @EventHandler(priority = EventPriority.MONITOR) public void onPlayerCommand(PlayerCommandPreprocessEvent event) { - if (event.isCancelled()) { + boolean hasIgnorePermission = event.getPlayer().hasPermission(Permissions.IGNORE_COMMANDUSE.getPermission()); + if (event.isCancelled() || hasIgnorePermission) { return; } - Player player = event.getPlayer(); try { - if (player.hasPermission(Permissions.IGNORE_COMMANDUSE.getPermission())) { - return; - } - String commandName = event.getMessage().substring(1).split(" ")[0].toLowerCase(); boolean logUnknownCommands = Settings.LOG_UNKNOWN_COMMANDS.isTrue(); boolean combineCommandAliases = Settings.COMBINE_COMMAND_ALIASES.isTrue(); if (!logUnknownCommands || combineCommandAliases) { - Command command = plugin.getServer().getPluginCommand(commandName); - if (command == null) { - try { - command = plugin.getServer().getCommandMap().getCommand(commandName); - } catch (NoSuchMethodError ignored) { - /* Ignored, Bukkit 1.8 has no such method */ - } - } + Command command = getBukkitCommand(commandName); if (command == null) { if (!logUnknownCommands) { return; @@ -74,4 +62,16 @@ public class CommandPreprocessListener implements Listener { Log.toLog(this.getClass(), e); } } + + private Command getBukkitCommand(String commandName) { + Command command = plugin.getServer().getPluginCommand(commandName); + if (command == null) { + try { + command = plugin.getServer().getCommandMap().getCommand(commandName); + } catch (NoSuchMethodError ignored) { + /* Ignored, Bukkit 1.8 has no such method */ + } + } + return command; + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/settings/PlanErrorManager.java b/Plan/src/main/java/com/djrapitops/plan/system/settings/PlanErrorManager.java index a7db9b8c8..8d16f3495 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/settings/PlanErrorManager.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/settings/PlanErrorManager.java @@ -44,9 +44,8 @@ public class PlanErrorManager extends ErrorManager { System.out.println("Failed to log error to file because of " + exception); System.out.println("Error:"); // Fallback - Logger.getGlobal().log(Level.WARNING, source, e); System.out.println("Fail Reason:"); - exception.printStackTrace(); + Logger.getGlobal().log(Level.WARNING, source, e); } } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/parsing/InspectPage.java b/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/parsing/InspectPage.java index fd6693190..61fdc2244 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/parsing/InspectPage.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/parsing/InspectPage.java @@ -101,11 +101,8 @@ public class InspectPage extends Page { addValue("registered", FormatUtils.formatTimeStampYear(registered)); addValue("playerName", playerName); addValue("kickCount", timesKicked); - if (lastSeen != 0) { - addValue("lastSeen", FormatUtils.formatTimeStampYear(lastSeen)); - } else { - addValue("lastSeen", "-"); - } + + addValue("lastSeen", lastSeen != 0 ? FormatUtils.formatTimeStampYear(lastSeen) : "-"); Map worldTimesPerServer = profile.getWorldTimesPerServer(); addValue("serverPieSeries", new ServerPreferencePie(serverNames, worldTimesPerServer).toHighChartsSeries()); diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/html/tables/PlayersTableCreator.java b/Plan/src/main/java/com/djrapitops/plan/utilities/html/tables/PlayersTableCreator.java index 4db1bd978..3afbb48cc 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/html/tables/PlayersTableCreator.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/html/tables/PlayersTableCreator.java @@ -20,6 +20,7 @@ import java.util.*; /** * @author Rsl1122 */ +// TODO Start using TableContainer for both tables public class PlayersTableCreator { /** diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/html/tables/SessionsTableCreator.java b/Plan/src/main/java/com/djrapitops/plan/utilities/html/tables/SessionsTableCreator.java index 56656b45e..db7153c48 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/html/tables/SessionsTableCreator.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/html/tables/SessionsTableCreator.java @@ -24,6 +24,7 @@ import java.util.stream.Collectors; * * @author Rsl1122 */ +// TODO start using TableContainer public class SessionsTableCreator { private static Map getUUIDsByID(Map> sessionsByUser) {