Skip to content

Commit

Permalink
Use Player objects where possible.
Browse files Browse the repository at this point in the history
Needs to be tested before ready for release.
  • Loading branch information
jamierocks committed Dec 14, 2014
1 parent f3b9215 commit 6cd7f44
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand All @@ -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!");
}
}
}
Expand All @@ -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]);
Expand All @@ -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]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,8 +32,8 @@
import com.google.gson.reflect.TypeToken;

public final class PlayTimeLimiter extends Plugin {
private Map<String, Integer> timePlayed = new HashMap<String, Integer>();
private Map<String, Integer> timeLoggedIn = new HashMap<String, Integer>();
private Map<UUID, Integer> timePlayed = new HashMap<UUID, Integer>();
private Map<UUID, Integer> timeLoggedIn = new HashMap<UUID, Integer>();
private Map<String, Boolean> seenWarningMessages = new HashMap<String, Boolean>();

private Timer savePlayTimeTimer = null;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit 6cd7f44

Please sign in to comment.