From 6cd7f448c380f8a0af40cdc37d16a962a7829c55 Mon Sep 17 00:00:00 2001 From: Jamie Mansfield Date: Sun, 14 Dec 2014 17:52:31 +0000 Subject: [PATCH] Use Player objects where possible. Needs to be tested before ready for release. --- .../playtimelimiter/PlayTimeCommands.java | 16 ++-- .../playtimelimiter/PlayTimeLimiter.java | 86 ++++++++++--------- .../playtimelimiter/PlayTimeListener.java | 8 +- .../exceptions/UnknownPlayerException.java | 4 +- .../threads/PlayTimeCheckerTask.java | 14 +-- 5 files changed, 64 insertions(+), 64 deletions(-) diff --git a/src/main/java/unomodding/canary/playtimelimiter/PlayTimeCommands.java b/src/main/java/unomodding/canary/playtimelimiter/PlayTimeCommands.java index 6c3c6ae..d7b11fe 100644 --- a/src/main/java/unomodding/canary/playtimelimiter/PlayTimeCommands.java +++ b/src/main/java/unomodding/canary/playtimelimiter/PlayTimeCommands.java @@ -80,11 +80,9 @@ public void checkCommand(MessageReceiver caller, String[] args) { Player player = Canary.getServer().getPlayer(caller.getName()); caller.message(Colors.GREEN + "You have played for " - + plugin.secondsToDaysHoursSecondsString(plugin.getPlayerPlayTime(player - .getUUIDString())) + + plugin.secondsToDaysHoursSecondsString(plugin.getPlayerPlayTime(player)) + " and have " - + plugin.secondsToDaysHoursSecondsString(plugin.getTimeAllowedInSeconds(player - .getUUIDString())) + " remaining!"); + + plugin.secondsToDaysHoursSecondsString(plugin.getTimeAllowedInSeconds(player)) + " remaining!"); } } else if (args.length == 1) { if (!caller.hasPermission("playtimelimiter.playtime.check.others")) { @@ -94,11 +92,9 @@ public void checkCommand(MessageReceiver caller, String[] args) { caller.message(Colors.GREEN + player.getName() + " has played for " - + plugin.secondsToDaysHoursSecondsString(plugin.getPlayerPlayTime(player - .getUUIDString())) + + plugin.secondsToDaysHoursSecondsString(plugin.getPlayerPlayTime(player)) + " and has " - + plugin.secondsToDaysHoursSecondsString(plugin.getTimeAllowedInSeconds(player - .getUUIDString())) + " remaining!"); + + plugin.secondsToDaysHoursSecondsString(plugin.getTimeAllowedInSeconds(player)) + " remaining!"); } } } @@ -115,7 +111,7 @@ public void addCommand(MessageReceiver caller, String[] args) { caller.message(Colors.RED + "Playtime hasn't started yet!"); } else { try { - plugin.addPlayTime(Canary.getServer().getPlayer(args[0]).getUUIDString(), + plugin.addPlayTime(Canary.getServer().getPlayer(args[0]), Integer.parseInt(args[1])); caller.message(Colors.GREEN + "Added " + Integer.parseInt(args[1]) + " seconds of playtime from " + args[0]); @@ -140,7 +136,7 @@ public void removeCommand(MessageReceiver caller, String[] args) { caller.message(Colors.RED + "Playtime hasn't started yet!"); } else { try { - plugin.removePlayTime(Canary.getServer().getPlayer(args[0]).getUUIDString(), + plugin.removePlayTime(Canary.getServer().getPlayer(args[0]), Integer.parseInt(args[1])); caller.message(Colors.GREEN + "Removed " + Integer.parseInt(args[1]) + " seconds of playtime from " + args[0]); diff --git a/src/main/java/unomodding/canary/playtimelimiter/PlayTimeLimiter.java b/src/main/java/unomodding/canary/playtimelimiter/PlayTimeLimiter.java index 16e67c0..a4e44a0 100644 --- a/src/main/java/unomodding/canary/playtimelimiter/PlayTimeLimiter.java +++ b/src/main/java/unomodding/canary/playtimelimiter/PlayTimeLimiter.java @@ -15,10 +15,12 @@ import java.util.HashMap; import java.util.Map; import java.util.Timer; +import java.util.UUID; import org.mcstats.Metrics; import net.canarymod.Canary; +import net.canarymod.api.entity.living.humanoid.Player; import net.canarymod.chat.Colors; import net.canarymod.commandsys.CommandDependencyException; import net.canarymod.plugin.Plugin; @@ -30,8 +32,8 @@ import com.google.gson.reflect.TypeToken; public final class PlayTimeLimiter extends Plugin { - private Map timePlayed = new HashMap(); - private Map timeLoggedIn = new HashMap(); + private Map timePlayed = new HashMap(); + private Map timeLoggedIn = new HashMap(); private Map seenWarningMessages = new HashMap(); private Timer savePlayTimeTimer = null; @@ -159,88 +161,88 @@ public int getTimeAllowedInSeconds() { return secondsAllowed; } - public int getTimeAllowedInSeconds(String uuid) { + public int getTimeAllowedInSeconds(Player player) { int secondsAllowed = this.getTimeAllowedInSeconds(); // Remove the amount of time the player has played to get their time // allowed - secondsAllowed -= getPlayerPlayTime(uuid); + secondsAllowed -= getPlayerPlayTime(player); return secondsAllowed; } - public void addPlayTime(String uuid, int seconds) throws UnknownPlayerException { - if (this.timePlayed.containsKey(uuid)) { - this.timePlayed.put(uuid, this.timePlayed.get(uuid) - seconds); + public void addPlayTime(Player player, int seconds) throws UnknownPlayerException { + if (this.timePlayed.containsKey(player.getUUID())) { + this.timePlayed.put(player.getUUID(), this.timePlayed.get(player.getUUID()) - seconds); } else { - throw new UnknownPlayerException(uuid); + throw new UnknownPlayerException(player.getUUID()); } } - public void removePlayTime(String uuid, int seconds) { - if (this.timePlayed.containsKey(uuid)) { - this.timePlayed.put(uuid, this.timePlayed.get(uuid) + seconds); + public void removePlayTime(Player player, int seconds) { + if (this.timePlayed.containsKey(player.getUUID())) { + this.timePlayed.put(player.getUUID(), this.timePlayed.get(player.getUUID()) + seconds); } else { - this.timePlayed.put(uuid, seconds); + this.timePlayed.put(player.getUUID(), seconds); } } - public int getPlayerPlayTime(String uuid) { + public int getPlayerPlayTime(Player player) { int timePlayed = 0; - if (this.timePlayed.containsKey(uuid)) { - timePlayed += this.timePlayed.get(uuid); + if (this.timePlayed.containsKey(player.getUUID())) { + timePlayed += this.timePlayed.get(player.getUUID()); } - if (this.timeLoggedIn.containsKey(uuid)) { - timePlayed += (int) ((System.currentTimeMillis() / 1000) - this.timeLoggedIn.get(uuid)); + if (this.timeLoggedIn.containsKey(player.getUUID())) { + timePlayed += (int) ((System.currentTimeMillis() / 1000) - this.timeLoggedIn.get(player.getUUID())); } return timePlayed; } - public void setPlayerLoggedIn(String uuid) { - if (!this.timePlayed.containsKey(uuid)) { - this.timePlayed.put(uuid, 0); + public void setPlayerLoggedIn(Player player) { + if (!this.timePlayed.containsKey(player.getUUID())) { + this.timePlayed.put(player.getUUID(), 0); this.savePlayTime(); } - this.timeLoggedIn.put(uuid, (int) (System.currentTimeMillis() / 1000)); + this.timeLoggedIn.put(player.getUUID(), (int) (System.currentTimeMillis() / 1000)); } - public void setPlayerLoggedOut(String uuid) { - if (this.timeLoggedIn.containsKey(uuid)) { - int timePlayed = (int) ((System.currentTimeMillis() / 1000) - this.timeLoggedIn.get(uuid)); - if (this.timePlayed.containsKey(uuid)) { - timePlayed += this.timePlayed.get(uuid); + public void setPlayerLoggedOut(Player player) { + if (this.timeLoggedIn.containsKey(player.getUUID())) { + int timePlayed = (int) ((System.currentTimeMillis() / 1000) - this.timeLoggedIn.get(player.getUUID())); + if (this.timePlayed.containsKey(player.getUUID())) { + timePlayed += this.timePlayed.get(player.getUUID()); } if (timePlayed > this.getTimeAllowedInSeconds()) { timePlayed = this.getTimeAllowedInSeconds(); } - this.timePlayed.put(uuid, timePlayed); - this.timeLoggedIn.remove(uuid); + this.timePlayed.put(player.getUUID(), timePlayed); + this.timeLoggedIn.remove(player.getUUID()); getLogman().info( - "Player " + Canary.getServer().getPlayerFromUUID(uuid).getName() + "Player " + Canary.getServer().getPlayerFromUUID(player.getUUID()).getName() + " played for a total of " + timePlayed + " seconds!"); this.savePlayTime(); } - if (this.seenWarningMessages.containsKey(uuid + ":10")) { - this.seenWarningMessages.remove(uuid + ":10"); + if (this.seenWarningMessages.containsKey(player.getUUID() + ":10")) { + this.seenWarningMessages.remove(player.getUUID() + ":10"); } - if (this.seenWarningMessages.containsKey(uuid + ":60")) { - this.seenWarningMessages.remove(uuid + ":60"); + if (this.seenWarningMessages.containsKey(player.getUUID() + ":60")) { + this.seenWarningMessages.remove(player.getUUID() + ":60"); } - if (this.seenWarningMessages.containsKey(uuid + ":300")) { - this.seenWarningMessages.remove(uuid + ":300"); + if (this.seenWarningMessages.containsKey(player.getUUID() + ":300")) { + this.seenWarningMessages.remove(player.getUUID() + ":300"); } } - public boolean hasPlayerSeenMessage(String uuid, int time) { - if (this.seenWarningMessages.containsKey(uuid + ":" + time)) { - return this.seenWarningMessages.get(uuid + ":" + time); + public boolean hasPlayerSeenMessage(Player player, int time) { + if (this.seenWarningMessages.containsKey(player.getUUID() + ":" + time)) { + return this.seenWarningMessages.get(player.getUUID() + ":" + time); } else { return false; } } - public void sentPlayerWarningMessage(String uuid, int time) { - this.seenWarningMessages.put(uuid + ":" + time, true); + public void sentPlayerWarningMessage(Player player, int time) { + this.seenWarningMessages.put(player.getUUID() + ":" + time, true); } public boolean start() { @@ -310,8 +312,8 @@ public void savePlayTime(boolean force) { } if (force) { - for (String key : this.timeLoggedIn.keySet()) { - this.setPlayerLoggedOut(key); + for (UUID key : this.timeLoggedIn.keySet()) { + this.setPlayerLoggedOut(Canary.getServer().getPlayerFromUUID(key)); } } File file = new File(getDataFolder(), "playtime.json"); diff --git a/src/main/java/unomodding/canary/playtimelimiter/PlayTimeListener.java b/src/main/java/unomodding/canary/playtimelimiter/PlayTimeListener.java index f1c623c..3bfc8ac 100644 --- a/src/main/java/unomodding/canary/playtimelimiter/PlayTimeListener.java +++ b/src/main/java/unomodding/canary/playtimelimiter/PlayTimeListener.java @@ -35,7 +35,7 @@ public void onSeverShutdown(ServerShutdownHook hook) { public void onPlayerJoin(ConnectionHook hook) { FileUtils.appendStringToFile(new File(this.plugin.getDataFolder(), "playtime.log"), String.format("[%s] %s logged in", Timestamper.now(), hook.getPlayer().getName())); - if (this.plugin.getTimeAllowedInSeconds(hook.getPlayer().getUUIDString()) <= 0) { + if (this.plugin.getTimeAllowedInSeconds(hook.getPlayer()) <= 0) { FileUtils.appendStringToFile(new File(this.plugin.getDataFolder(), "playtime.log"), String .format("[%s] %s was kicked for exceeding play time", Timestamper.now(), hook.getPlayer() .getName())); @@ -44,19 +44,19 @@ public void onPlayerJoin(ConnectionHook hook) { + this.plugin.secondsToDaysHoursSecondsString(this.plugin.secondsUntilNextDay()) + "!"); } else { - this.plugin.setPlayerLoggedIn(hook.getPlayer().getUUIDString()); + this.plugin.setPlayerLoggedIn(hook.getPlayer()); } hook.getPlayer().message( "You have " + Colors.GREEN + plugin.secondsToDaysHoursSecondsString(plugin.getTimeAllowedInSeconds(hook - .getPlayer().getUUIDString())) + TextFormat.RESET + " of playtime left!"); + .getPlayer())) + TextFormat.RESET + " of playtime left!"); } @HookHandler public void onPlayerQuit(DisconnectionHook hook) { FileUtils.appendStringToFile(new File(this.plugin.getDataFolder(), "playtime.log"), String.format("[%s] %s logged out", Timestamper.now(), hook.getPlayer().getName())); - this.plugin.setPlayerLoggedOut(hook.getPlayer().getUUIDString()); + this.plugin.setPlayerLoggedOut(hook.getPlayer()); } } diff --git a/src/main/java/unomodding/canary/playtimelimiter/exceptions/UnknownPlayerException.java b/src/main/java/unomodding/canary/playtimelimiter/exceptions/UnknownPlayerException.java index 4e7e84f..1cd0c91 100644 --- a/src/main/java/unomodding/canary/playtimelimiter/exceptions/UnknownPlayerException.java +++ b/src/main/java/unomodding/canary/playtimelimiter/exceptions/UnknownPlayerException.java @@ -6,10 +6,12 @@ */ package unomodding.canary.playtimelimiter.exceptions; +import java.util.UUID; + public class UnknownPlayerException extends Exception { private static final long serialVersionUID = -5987543214085051018L; - public UnknownPlayerException(String uuid) { + public UnknownPlayerException(UUID uuid) { super("Unknown player with UUID of " + uuid); } } diff --git a/src/main/java/unomodding/canary/playtimelimiter/threads/PlayTimeCheckerTask.java b/src/main/java/unomodding/canary/playtimelimiter/threads/PlayTimeCheckerTask.java index 3a4c0a9..9244e1f 100644 --- a/src/main/java/unomodding/canary/playtimelimiter/threads/PlayTimeCheckerTask.java +++ b/src/main/java/unomodding/canary/playtimelimiter/threads/PlayTimeCheckerTask.java @@ -27,7 +27,7 @@ public PlayTimeCheckerTask(PlayTimeLimiter instance) { @Override public void run() { for (Player player : Canary.getServer().getPlayerList()) { - int timeLeft = this.plugin.getTimeAllowedInSeconds(player.getUUIDString()); + int timeLeft = this.plugin.getTimeAllowedInSeconds(player); if (timeLeft <= 0) { FileUtils.appendStringToFile( new File(this.plugin.getDataFolder(), "playtime.log"), @@ -36,26 +36,26 @@ public void run() { player.kick("You have exceeded the time allowed to play! Come back in " + this.plugin.secondsToDaysHoursSecondsString(this.plugin.secondsUntilNextDay()) + "!"); - } else if (timeLeft <= 10 && !this.plugin.hasPlayerSeenMessage(player.getUUIDString(), 10)) { + } else if (timeLeft <= 10 && !this.plugin.hasPlayerSeenMessage(player, 10)) { player.message(Colors.RED + "WARNING!" + TextFormat.RESET + " You have less than 10 seconds of playtime left! Stop what your doing and prepare to be disconnected!"); - this.plugin.sentPlayerWarningMessage(player.getUUIDString(), 10); + this.plugin.sentPlayerWarningMessage(player, 10); } else if (timeLeft <= 60 && timeLeft > 10 - && !this.plugin.hasPlayerSeenMessage(player.getUUIDString(), 60)) { + && !this.plugin.hasPlayerSeenMessage(player, 60)) { player.message(Colors.RED + "WARNING!" + TextFormat.RESET + " You have less than 60 seconds of playtime left! Stop what your doing and prepare to be disconnected!"); - this.plugin.sentPlayerWarningMessage(player.getUUIDString(), 60); + this.plugin.sentPlayerWarningMessage(player, 60); } else if (timeLeft <= 300 && timeLeft > 60 - && !this.plugin.hasPlayerSeenMessage(player.getUUIDString(), 300)) { + && !this.plugin.hasPlayerSeenMessage(player, 300)) { player.message(Colors.RED + "WARNING!" + TextFormat.RESET + " You have less than 5 minutes of playtime left! Stop what your doing and prepare to be disconnected!"); - this.plugin.sentPlayerWarningMessage(player.getUUIDString(), 300); + this.plugin.sentPlayerWarningMessage(player, 300); } } }