Skip to content

Commit

Permalink
[0.4] Use OfflinePlayer's as opposed to Player's. Improve commands.
Browse files Browse the repository at this point in the history
You will have to start over with new data starting from this release, sorry.
  • Loading branch information
jamierocks committed Dec 14, 2014
1 parent d730d19 commit 5c06fac
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import java.util.List;

import net.canarymod.Canary;
import net.canarymod.api.entity.living.humanoid.Player;
import net.canarymod.api.OfflinePlayer;
import net.canarymod.chat.Colors;
import net.canarymod.chat.MessageReceiver;
import net.canarymod.chat.ReceiverType;
import net.canarymod.chat.TextFormat;
import net.canarymod.commandsys.Command;
import net.canarymod.commandsys.CommandListener;
Expand Down Expand Up @@ -77,17 +78,22 @@ public void checkCommand(MessageReceiver caller, String[] args) {
if (!caller.hasPermission("playtimelimiter.playtime.check.self")) {
caller.message(Colors.RED + "You don't have permission to check your playtime!");
} else {
Player player = Canary.getServer().getPlayer(caller.getName());
caller.message(Colors.GREEN + "You have played for "
+ plugin.secondsToDaysHoursSecondsString(plugin.getPlayerPlayTime(player)) + " and have "
+ plugin.secondsToDaysHoursSecondsString(plugin.getTimeAllowedInSeconds(player))
+ " remaining!");
if(caller.getReceiverType() == ReceiverType.PLAYER) {
OfflinePlayer player = Canary.getServer().getOfflinePlayer(caller.getName());
plugin.loadPlayTime(player);
caller.message(Colors.GREEN + "You have played for "
+ plugin.secondsToDaysHoursSecondsString(plugin.getPlayerPlayTime(player)) + " and have "
+ plugin.secondsToDaysHoursSecondsString(plugin.getTimeAllowedInSeconds(player))
+ " remaining!");
} else {
caller.message(Colors.RED + "Only Players have playtime!");
}
}
} else if (args.length == 1) {
if (!caller.hasPermission("playtimelimiter.playtime.check.others")) {
caller.message(Colors.RED + "You don't have permission to check other players playtime!");
} else {
Player player = Canary.getServer().getPlayer(args[0]);
OfflinePlayer player = Canary.getServer().getOfflinePlayer(args[0]);
caller.message(Colors.GREEN + player.getName() + " has played for "
+ plugin.secondsToDaysHoursSecondsString(plugin.getPlayerPlayTime(player)) + " and has "
+ plugin.secondsToDaysHoursSecondsString(plugin.getTimeAllowedInSeconds(player))
Expand All @@ -108,7 +114,9 @@ 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]), Integer.parseInt(args[1]));
OfflinePlayer player = Canary.getServer().getOfflinePlayer(args[0]);
plugin.loadPlayTime(player);
plugin.addPlayTime(player, Integer.parseInt(args[1]));
caller.message(Colors.GREEN + "Added " + Integer.parseInt(args[1]) + " seconds of playtime from "
+ args[0]);
} catch (NumberFormatException e) {
Expand All @@ -132,7 +140,9 @@ 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]), Integer.parseInt(args[1]));
OfflinePlayer player = Canary.getServer().getOfflinePlayer(args[0]);
plugin.loadPlayTime(player);
plugin.removePlayTime(player, Integer.parseInt(args[1]));
caller.message(Colors.GREEN + "Removed " + Integer.parseInt(args[1]) + " seconds of playtime from "
+ args[0]);
} catch (NumberFormatException e) {
Expand Down
118 changes: 85 additions & 33 deletions src/main/java/unomodding/canary/playtimelimiter/PlayTimeLimiter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Timer;

import net.canarymod.Canary;
import net.canarymod.api.OfflinePlayer;
import net.canarymod.api.entity.living.humanoid.Player;
import net.canarymod.chat.Colors;
import net.canarymod.commandsys.CommandDependencyException;
Expand Down Expand Up @@ -152,88 +153,131 @@ public int getTimeAllowedInSeconds() {
return secondsAllowed;
}

public int getTimeAllowedInSeconds(OfflinePlayer player) {
return getTimeAllowedInSeconds(player.getUUIDString());
}

public int getTimeAllowedInSeconds(Player player) {
return getTimeAllowedInSeconds(player.getUUIDString());
}

private int getTimeAllowedInSeconds(String uuid) {
int secondsAllowed = this.getTimeAllowedInSeconds();

// Remove the amount of time the player has played to get their time
// allowed
secondsAllowed -= getPlayerPlayTime(player);
// Remove the amount of time the player has played to get their time allowed
secondsAllowed -= getPlayerPlayTime(uuid);

return secondsAllowed;
}

public void addPlayTime(Player player, int seconds) throws UnknownPlayerException {
public void addPlayTime(OfflinePlayer player, int seconds) throws UnknownPlayerException {
if (this.timePlayed.containsKey(player.getUUIDString())) {
this.timePlayed.put(player.getUUIDString(), this.timePlayed.get(player.getUUIDString()) - seconds);
} else {
throw new UnknownPlayerException(player.getUUID());
}
}

public void removePlayTime(Player player, int seconds) {
public void removePlayTime(OfflinePlayer player, int seconds) {
if (this.timePlayed.containsKey(player.getUUIDString())) {
this.timePlayed.put(player.getUUIDString(), this.timePlayed.get(player.getUUIDString()) + seconds);
} else {
this.timePlayed.put(player.getUUIDString(), seconds);
}
}

public int getPlayerPlayTime(Player player) {
public int getPlayerPlayTime(OfflinePlayer player) {
return getPlayerPlayTime(player.getUUIDString());
}

private int getPlayerPlayTime(String uuid) {
int timePlayed = 0;
if (this.timePlayed.containsKey(player.getUUIDString())) {
timePlayed += this.timePlayed.get(player.getUUIDString());
if (this.timePlayed.containsKey(uuid)) {
timePlayed += this.timePlayed.get(uuid);
}
if (this.timeLoggedIn.containsKey(player.getUUIDString())) {
timePlayed += (int) ((System.currentTimeMillis() / 1000) - this.timeLoggedIn.get(player.getUUIDString()));
if (this.timeLoggedIn.containsKey(uuid)) {
timePlayed += (int) ((System.currentTimeMillis() / 1000) - this.timeLoggedIn.get(uuid));
}
return timePlayed;
}

public void setPlayerLoggedIn(OfflinePlayer player) {
setPlayerLoggedIn(player.getUUIDString());
}

public void setPlayerLoggedIn(Player player) {
if (!this.timePlayed.containsKey(player.getUUIDString())) {
this.timePlayed.put(player.getUUIDString(), 0);
setPlayerLoggedIn(player.getUUIDString());
}

private void setPlayerLoggedIn(String uuid) {
if (!this.timePlayed.containsKey(uuid)) {
this.timePlayed.put(uuid, 0);
this.savePlayTime();
}
this.timeLoggedIn.put(player.getUUIDString(), (int) (System.currentTimeMillis() / 1000));
this.timeLoggedIn.put(uuid, (int) (System.currentTimeMillis() / 1000));
}

public void setPlayerLoggedOut(OfflinePlayer player) {
setPlayerLoggedOut(player.getUUIDString());
}

public void setPlayerLoggedOut(Player player) {
if (this.timeLoggedIn.containsKey(player.getUUIDString())) {
int timePlayed = (int) ((System.currentTimeMillis() / 1000) - this.timeLoggedIn.get(player.getUUIDString()));
if (this.timePlayed.containsKey(player.getUUIDString())) {
timePlayed += this.timePlayed.get(player.getUUIDString());
setPlayerLoggedOut(player.getUUIDString());
}

private 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);
}
if (timePlayed > this.getTimeAllowedInSeconds()) {
timePlayed = this.getTimeAllowedInSeconds();
}
this.timePlayed.put(player.getUUIDString(), timePlayed);
this.timeLoggedIn.remove(player.getUUIDString());
this.timePlayed.put(uuid, timePlayed);
this.timeLoggedIn.remove(uuid);
getLogman().info(
"Player " + Canary.getServer().getPlayerFromUUID(player.getUUIDString()).getName()
"Player " + Canary.getServer().getPlayerFromUUID(uuid).getName()
+ " played for a total of " + timePlayed + " seconds!");
this.savePlayTime();
}
if (this.seenWarningMessages.containsKey(player.getUUIDString() + ":10")) {
this.seenWarningMessages.remove(player.getUUIDString() + ":10");
}
if (this.seenWarningMessages.containsKey(player.getUUIDString() + ":60")) {
this.seenWarningMessages.remove(player.getUUIDString() + ":60");
if (this.seenWarningMessages.containsKey(uuid + ":10")) {
this.seenWarningMessages.remove(uuid + ":10");
}
if (this.seenWarningMessages.containsKey(player.getUUIDString() + ":300")) {
this.seenWarningMessages.remove(player.getUUIDString() + ":300");
if (this.seenWarningMessages.containsKey(uuid + ":60")) {
this.seenWarningMessages.remove(uuid + ":60");
}
if (this.seenWarningMessages.containsKey(uuid + ":300")) {
this.seenWarningMessages.remove(uuid + ":300");
}
}

public boolean hasPlayerSeenMessage(OfflinePlayer player, int time) {
return hasPlayerSeenMessage(player.getUUIDString(), time);
}

public boolean hasPlayerSeenMessage(Player player, int time) {
if (this.seenWarningMessages.containsKey(player.getUUIDString() + ":" + time)) {
return this.seenWarningMessages.get(player.getUUIDString() + ":" + time);
return hasPlayerSeenMessage(player.getUUIDString(), time);
}

private boolean hasPlayerSeenMessage(String uuid, int time) {
if (this.seenWarningMessages.containsKey(uuid + ":" + time)) {
return this.seenWarningMessages.get(uuid + ":" + time);
} else {
return false;
}
}

public void sentPlayerWarningMessage(OfflinePlayer player, int time) {
sentPlayerWarningMessage(player.getUUIDString(), time);
}

public void sentPlayerWarningMessage(Player player, int time) {
this.seenWarningMessages.put(player.getUUIDString() + ":" + time, true);
sentPlayerWarningMessage(player.getUUIDString(), time);
}

private void sentPlayerWarningMessage(String uuid, int time) {
this.seenWarningMessages.put(uuid + ":" + time, true);
}

public boolean start() {
Expand Down Expand Up @@ -265,14 +309,22 @@ public boolean hasStarted() {
return this.started;
}

public void loadPlayTime(OfflinePlayer player) {
loadPlayTime(player.getUUIDString());
}

public void loadPlayTime(Player player) {
loadPlayTime(player.getUUIDString());
}

private void loadPlayTime(String uuid) {
if (!hasStarted()) {
return;
}
PlayTimeDataAccess dataAccess = new PlayTimeDataAccess();
try {
HashMap<String, Object> filter = new HashMap<String, Object>();
filter.put("player_uuid", player.getUUIDString());
filter.put("player_uuid", uuid);

Database.get().load(dataAccess, filter);
} catch (DatabaseReadException e) {
Expand All @@ -281,7 +333,7 @@ public void loadPlayTime(Player player) {
if (dataAccess.hasData()) {
timePlayed.put(dataAccess.uuid, dataAccess.playtime);
} else {
timePlayed.put(player.getUUIDString(), 0);
timePlayed.put(uuid, 0);
}
}

Expand All @@ -295,7 +347,7 @@ public void savePlayTime(boolean force) {
}
if (force) {
for (String key : this.timeLoggedIn.keySet()) {
this.setPlayerLoggedOut(Canary.getServer().getPlayerFromUUID(key));
this.setPlayerLoggedOut(Canary.getServer().getOfflinePlayer(key));
}
}

Expand Down

0 comments on commit 5c06fac

Please sign in to comment.