Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Fido2603 committed Jul 16, 2019
2 parents c2dd112 + 9f67c25 commit bdaa908
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 63 deletions.
118 changes: 96 additions & 22 deletions src/main/java/dk/fido2603/semihardcore/Commands.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dk.fido2603.semihardcore;

import java.util.UUID;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
Expand All @@ -20,21 +22,21 @@ public void commandReload(Player player)
if (player == null)
{
this.plugin.reloadSettings();
this.plugin.log(this.plugin.getDescription().getFullName() + ": Reloaded configuration.");
this.plugin.log("Reloaded configuration.");
}
else if (player.isOp() || (SemiHardcore.getPermissionsManager().hasPermission(player, "semihardcore.reload")))
{
this.plugin.reloadSettings();
player.sendMessage(ChatColor.YELLOW + this.plugin.getDescription().getFullName() + ": " + ChatColor.WHITE + "Reloaded configuration.");
}
}

public void commandSave(Player player)
{
if (player == null)
{
SemiHardcore.getPlayerManager().save();
this.plugin.log(this.plugin.getDescription().getFullName() + ": Saved configuration(s).");
this.plugin.log("Saved configuration(s).");
}
else if (player.isOp() || (SemiHardcore.getPermissionsManager().hasPermission(player, "semihardcore.save")))
{
Expand All @@ -50,39 +52,107 @@ public void commandUnban(Player player, String[] args) {
}
}

private boolean commandHelp(CommandSender sender)
public void commandInfo(Player player, String[] args) {
if (player == null || player.isOp() || SemiHardcore.getPermissionsManager().hasPermission(player, "semihardcore.info")) {
String playerName = args[1];
UUID playerUUID = SemiHardcore.getPlayerManager().getUUID(playerName);

if (player == null) {
this.plugin.log("---------------- " + plugin.getDescription().getFullName() + " ----------------");
this.plugin.log("Playerinfo for " + playerName);
this.plugin.log("");

if (playerUUID == null) {
this.plugin.log("The given player could not be found in the list of dead players!");
return;
}

this.plugin.log("Banned: " + SemiHardcore.getPlayerManager().isBanned(playerUUID));
this.plugin.log("Deaths: " + SemiHardcore.getPlayerManager().getDeaths(playerUUID));
this.plugin.log("Time of last death: " + SemiHardcore.getPlayerManager().getDeathTime(playerUUID));
this.plugin.log("Time since last death: " + SemiHardcore.getPlayerManager().getTimeSinceDeathTime(playerUUID));

return;
}

player.sendMessage(ChatColor.YELLOW + "---------------- " + plugin.getDescription().getFullName() + " ----------------");
player.sendMessage(ChatColor.AQUA + "Playerinfo for " + ChatColor.WHITE + playerName);
player.sendMessage(ChatColor.AQUA + "");

if (playerUUID == null) {
player.sendMessage(ChatColor.RED + "The given player could not be found in the list of dead players!");
return;
}

player.sendMessage(ChatColor.AQUA + "Banned: " + ChatColor.WHITE + SemiHardcore.getPlayerManager().isBanned(playerUUID));
player.sendMessage(ChatColor.AQUA + "Deaths: " + ChatColor.WHITE + SemiHardcore.getPlayerManager().getDeaths(playerUUID));
player.sendMessage(ChatColor.AQUA + "Time of last death: " + ChatColor.WHITE + SemiHardcore.getPlayerManager().getDeathTime(playerUUID));
player.sendMessage(ChatColor.AQUA + "Time since last death: " + ChatColor.WHITE + SemiHardcore.getPlayerManager().getTimeSinceDeathTime(playerUUID));

return;
}
}

private boolean commandHelp(Player player)
{
sender.sendMessage(ChatColor.YELLOW + "---------------- " + plugin.getDescription().getFullName() + " ----------------");
sender.sendMessage(ChatColor.AQUA + "Made by Fido2603");
sender.sendMessage(ChatColor.AQUA + "");
if (player == null) {
this.plugin.log("---------------- " + plugin.getDescription().getFullName() + " ----------------");
this.plugin.log("Made by Fido2603");
this.plugin.log("");
if (plugin.isUHCDay) {
this.plugin.log("It is currently UHC Day! No natural regeneration!");
this.plugin.log("");
}
this.plugin.log("Use /semihardcore help, to get a list of available commands.");

return true;
}
player.sendMessage(ChatColor.YELLOW + "---------------- " + plugin.getDescription().getFullName() + " ----------------");
player.sendMessage(ChatColor.AQUA + "Made by Fido2603");
player.sendMessage(ChatColor.AQUA + "");
if (plugin.isUHCDay) {
sender.sendMessage(ChatColor.AQUA + "It is currently UHC Day! No natural regeneration!");
sender.sendMessage(ChatColor.AQUA + "");
player.sendMessage(ChatColor.AQUA + "It is currently UHC Day! No natural regeneration!");
player.sendMessage(ChatColor.AQUA + "");
}
sender.sendMessage(ChatColor.AQUA + "Use " + ChatColor.WHITE + "/semihardcore help" + ChatColor.AQUA + ", to get a list of available commands.");
player.sendMessage(ChatColor.AQUA + "Use " + ChatColor.WHITE + "/semihardcore help" + ChatColor.AQUA + ", to get a list of available commands.");

return true;
}

private boolean commandList(CommandSender sender)
private boolean commandList(Player player)
{
sender.sendMessage(ChatColor.YELLOW + "---------------- " + this.plugin.getDescription().getFullName() + " ----------------");
sender.sendMessage(ChatColor.AQUA + "/semihardcore" + ChatColor.WHITE + " - Info about the plugin");
if ((sender.isOp()) || (sender.hasPermission("semihardcore.help")))
if (player == null) {
this.plugin.log("---------------- " + this.plugin.getDescription().getFullName() + " ----------------");
this.plugin.log("/semihardcore - Info about the plugin");
this.plugin.log("/semihardcore help - Shows this message");
this.plugin.log("/semihardcore unban <player> - Unbans a player from the server, if the cause of the ban is death");
this.plugin.log("/semihardcore info <player> - Gets info about a player");
this.plugin.log("/semihardcore reload - Reloads configs from disk");
this.plugin.log("/semihardcore save - Saves modified configs to disk");

return true;
}

player.sendMessage(ChatColor.YELLOW + "---------------- " + this.plugin.getDescription().getFullName() + " ----------------");
player.sendMessage(ChatColor.AQUA + "/semihardcore" + ChatColor.WHITE + " - Info about the plugin");
if ((player.isOp()) || (player.hasPermission("semihardcore.help")))
{
sender.sendMessage(ChatColor.AQUA + "/semihardcore help" + ChatColor.WHITE + " - Shows this message");
player.sendMessage(ChatColor.AQUA + "/semihardcore help" + ChatColor.WHITE + " - Shows this message");
}
if ((sender.isOp()) || (sender.hasPermission("semihardcore.unban")))
if ((player.isOp()) || (player.hasPermission("semihardcore.unban")))
{
sender.sendMessage(ChatColor.AQUA + "/semihardcore unban <player>" + ChatColor.WHITE + " - Unbans a player from the server, if the cause of the ban is death");
player.sendMessage(ChatColor.AQUA + "/semihardcore unban <player>" + ChatColor.WHITE + " - Unbans a player from the server, if the cause of the ban is death");
}
if ((player.isOp()) || (player.hasPermission("semihardcore.info"))) {
player.sendMessage(ChatColor.AQUA + "/semihardcore info <player>" + ChatColor.WHITE + " - Gets info about a player");
}
if ((sender.isOp()) || (sender.hasPermission("semihardcore.reload")))
if ((player.isOp()) || (player.hasPermission("semihardcore.reload")))
{
sender.sendMessage(ChatColor.AQUA + "/semihardcore reload" + ChatColor.WHITE + " - Reloads configs from disk");
player.sendMessage(ChatColor.AQUA + "/semihardcore reload" + ChatColor.WHITE + " - Reloads configs from disk");
}
if ((sender.isOp()) || (sender.hasPermission("semihardcore.save")))
if ((player.isOp()) || (player.hasPermission("semihardcore.save")))
{
sender.sendMessage(ChatColor.AQUA + "/semihardcore save" + ChatColor.WHITE + " - Saves modified configs to disk");
player.sendMessage(ChatColor.AQUA + "/semihardcore save" + ChatColor.WHITE + " - Saves modified configs to disk");
}

return true;
Expand All @@ -96,7 +166,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
{
player = (Player) sender;
}

if ((label.equalsIgnoreCase("semihardcore")) || (label.equalsIgnoreCase("sh")))
{
if (args.length == 0) {
Expand Down Expand Up @@ -124,6 +194,10 @@ else if (args.length == 2)
{
commandUnban(player, args);
}
if (args[0].equalsIgnoreCase("info"))
{
commandInfo(player, args);
}
return true;
}
return false;
Expand Down
105 changes: 64 additions & 41 deletions src/main/java/dk/fido2603/semihardcore/PlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ public void saveTimed()
{
return;
}

save();
}

public void newPlayerCheck(PlayerJoinEvent event)
{
Player player = event.getPlayer();
UUID playerId = player.getUniqueId();

if (!isDead(playerId)) {
return;
}
Expand All @@ -92,27 +92,16 @@ public void run() {
}
unbanPlayer(playerId);
}

public void unbanPlayer(UUID playerId) {
this.semihardcoreConfig.set(playerId.toString() + ".IsDead", false);

saveTimed();
}

public void unbanPlayer(Player player, String bannedPlayer) {
Map<String, Object> deadPlayers = this.semihardcoreConfig.getValues(true);
UUID unbanPlayerId = null;
for (Map.Entry<String, Object> entry : deadPlayers.entrySet()) {
if (entry.getValue() instanceof String) {
this.plugin.logDebug("Entry key: " + entry.getKey());
this.plugin.logDebug("Entry value: " + entry.getValue());
if (entry.getKey().contains(".Name") && entry.getValue().toString().equalsIgnoreCase(bannedPlayer)) {
unbanPlayerId = UUID.fromString(entry.getKey().replace(".Name", ""));
break;
}
}
}

UUID unbanPlayerId = getUUID(bannedPlayer);

if (unbanPlayerId == null) {
if (player == null) {
this.plugin.log(this.plugin.getDescription().getFullName() + ": Error unbanning player: Player doesn't exist in dead-players.yml!");
Expand All @@ -122,8 +111,8 @@ public void unbanPlayer(Player player, String bannedPlayer) {
}
return;
}
if (!SemiHardcore.getPlayerManager().isBanned(unbanPlayerId)) {

if (!isBanned(unbanPlayerId)) {
if (player == null) {
this.plugin.log(this.plugin.getDescription().getFullName() + ": Error unbanning player: Player is not banned!");
}
Expand All @@ -132,41 +121,54 @@ public void unbanPlayer(Player player, String bannedPlayer) {
}
return;
}

SemiHardcore.getPlayerManager().unbanPlayer(unbanPlayerId);

unbanPlayer(unbanPlayerId);

if (player == null) {
this.plugin.log(this.plugin.getDescription().getFullName() + ": Unbanned " + bannedPlayer + "!");
}
else {
player.sendMessage(ChatColor.YELLOW + this.plugin.getDescription().getFullName() + ": " + ChatColor.WHITE + "Unbanned " + bannedPlayer + "!");
}
}

public boolean isDead(UUID playerId) {
return this.semihardcoreConfig.getBoolean(playerId.toString() + ".IsDead");
}


public String getDeathTime(UUID playerId) {
return this.semihardcoreConfig.getString(playerId.toString() + ".LastDeath");
}

public String getTimeSinceDeathTime(UUID playerId) {
return TimeConverter.parseMillisToUFString(timeDiff(playerId));
}

public String getDeaths(UUID playerId) {
return this.semihardcoreConfig.getString(playerId.toString() + ".Deaths");
}

public boolean isBanned(UUID playerId) {
return this.semihardcoreConfig.getBoolean(playerId.toString() + ".IsDead");
}

public boolean shouldPlayerBeUnbanned(UUID playerId) {
long diff = timeDiff(playerId);

if (diff >= plugin.timeToBan) {
plugin.logDebug("Player can now be unbanned... :)");
return true;
}

return false;
}

private long timeDiff(UUID playerId) {
Date deathTime = new Date();
Date currentTime = new Date();
String deathTimeStr = this.semihardcoreConfig.getString(playerId.toString() + ".LastDeath");

String deathTimeStr = getDeathTime(playerId);

try
{
deathTime = new SimpleDateFormat(datePattern).parse(deathTimeStr);
Expand All @@ -176,48 +178,69 @@ private long timeDiff(UUID playerId) {
plugin.log("Error while parsing datetime for player...: " + e.getMessage());
return 0;
}

long diff = Math.abs(currentTime.getTime() - deathTime.getTime());
//long diff = TimeUnit.HOURS.convert(diffInMillis, TimeUnit.MILLISECONDS);

plugin.logDebug("Diff in milliseconds: " + diff);

return diff;
}

public boolean banPlayer(Player player, UUID playerId)
{
if (isBanned(playerId)) {
plugin.log("Tried to ban a killed player that is already banned... UUID: " + playerId.toString());
return false;
}

int deaths = this.semihardcoreConfig.getInt(playerId.toString() + ".Deaths");
plugin.logDebug("Current deaths: " + deaths);
deaths++;
this.semihardcoreConfig.set(playerId.toString() + ".Deaths", deaths);

Date currentTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(datePattern);
this.semihardcoreConfig.set(playerId.toString() + ".LastDeath", sdf.format(currentTime));

this.semihardcoreConfig.set(playerId.toString() + ".IsDead", true);

// Delay the kick, to not have the console make a "Removing entity while ticking!" Exception
SemiHardcore.server.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
public void run() {
String kickMessage = plugin.messageKickPlayerOnBan.replace("{banTime}",plugin.timeToBanStringUF);
player.kickPlayer(ChatColor.translateAlternateColorCodes('&', kickMessage));
}
}, 1L);

this.semihardcoreConfig.set(playerId.toString() + ".Name", player.getName());

String banMessage = plugin.messageBanPlayer.replace("{playerName}", player.getName()).replace("{timeBanned}", plugin.timeToBanStringUF);
plugin.sendInfoAll(banMessage);

saveTimed();

return true;
}
}

public UUID getUUID(String playerName) {
Map<String, Object> deadPlayers = this.semihardcoreConfig.getValues(true);
UUID playerId = null;
for (Map.Entry<String, Object> entry : deadPlayers.entrySet()) {
if (entry.getValue() instanceof String) {
this.plugin.logDebug("Entry key: " + entry.getKey());
this.plugin.logDebug("Entry value: " + entry.getValue());
if (entry.getKey().contains(".Name") && entry.getValue().toString().equalsIgnoreCase(playerName)) {
playerId = UUID.fromString(entry.getKey().replace(".Name", ""));
break;
}
}
}

if (playerId == null) {
this.plugin.logDebug("Error finding player: Player doesn't exist in dead-players.yml!");
}

return playerId;
}
}
Loading

0 comments on commit bdaa908

Please sign in to comment.