Reduced complexity of some methods.

This commit is contained in:
Rsl1122 2018-02-05 16:23:32 +02:00
parent 54535f2da1
commit 8f19f8c05b
7 changed files with 69 additions and 44 deletions

View File

@ -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

View File

@ -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<Action> 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;

View File

@ -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;
}
}

View File

@ -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);
}
}
}

View File

@ -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<UUID, WorldTimes> worldTimesPerServer = profile.getWorldTimesPerServer();
addValue("serverPieSeries", new ServerPreferencePie(serverNames, worldTimesPerServer).toHighChartsSeries());

View File

@ -20,6 +20,7 @@ import java.util.*;
/**
* @author Rsl1122
*/
// TODO Start using TableContainer for both tables
public class PlayersTableCreator {
/**

View File

@ -24,6 +24,7 @@ import java.util.stream.Collectors;
*
* @author Rsl1122
*/
// TODO start using TableContainer
public class SessionsTableCreator {
private static Map<Integer, UUID> getUUIDsByID(Map<UUID, List<Session>> sessionsByUser) {