From 2bd1d312b8e3f47e381b295a10f85f5928203b95 Mon Sep 17 00:00:00 2001 From: Jack Lin Date: Wed, 15 Jul 2015 20:10:51 +1200 Subject: [PATCH] Update BendingPlayer and related references * Update modifiers to BendingPlayer variables * Add getters and setters for variables * Update references to these variables * Fully documented the class --- .../ProjectKorra/BendingManager.java | 9 +- .../ProjectKorra/BendingPlayer.java | 243 +++++++++++++++--- .../projectkorra/ProjectKorra/Commands.java | 36 +-- .../ProjectKorra/GeneralMethods.java | 20 +- .../projectkorra/ProjectKorra/PKListener.java | 10 +- .../earthbending/Tremorsense.java | 2 +- 6 files changed, 247 insertions(+), 73 deletions(-) diff --git a/src/com/projectkorra/ProjectKorra/BendingManager.java b/src/com/projectkorra/ProjectKorra/BendingManager.java index c2dec3c6..eb55d92f 100644 --- a/src/com/projectkorra/ProjectKorra/BendingManager.java +++ b/src/com/projectkorra/ProjectKorra/BendingManager.java @@ -1,6 +1,7 @@ package com.projectkorra.ProjectKorra; import java.util.HashMap; +import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.World; @@ -47,10 +48,10 @@ public class BendingManager implements Runnable, ConfigLoadable { } public void handleCooldowns() { - for (String bP: BendingPlayer.players.keySet()) { - BendingPlayer bPlayer = BendingPlayer.players.get(bP); - for (String abil: bPlayer.cooldowns.keySet()) { - if (System.currentTimeMillis() >= bPlayer.cooldowns.get(abil)) { + for (UUID uuid: BendingPlayer.getPlayers().keySet()) { + BendingPlayer bPlayer = BendingPlayer.getPlayers().get(uuid); + for (String abil: bPlayer.getCooldowns().keySet()) { + if (System.currentTimeMillis() >= bPlayer.getCooldown(abil)) { bPlayer.removeCooldown(abil); } } diff --git a/src/com/projectkorra/ProjectKorra/BendingPlayer.java b/src/com/projectkorra/ProjectKorra/BendingPlayer.java index b6cd723f..e25985d6 100644 --- a/src/com/projectkorra/ProjectKorra/BendingPlayer.java +++ b/src/com/projectkorra/ProjectKorra/BendingPlayer.java @@ -11,96 +11,230 @@ import org.bukkit.Bukkit; import com.projectkorra.ProjectKorra.CustomEvents.PlayerCooldownChangeEvent; import com.projectkorra.ProjectKorra.CustomEvents.PlayerCooldownChangeEvent.Result; +/** + * Class that presents a player and stores all bending information + * about the player. + */ public class BendingPlayer { - public static ConcurrentHashMap players = new ConcurrentHashMap(); + /** + * ConcurrentHashMap that contains all instances of BendingPlayer, with UUID key. + */ + private static ConcurrentHashMap players = new ConcurrentHashMap<>(); // public static ConcurrentHashMap blockedChi = new ConcurrentHashMap(); - UUID uuid; - String player; - ArrayList elements; + private UUID uuid; + private String name; + private ArrayList elements; private HashMap abilities; - ConcurrentHashMap cooldowns; - boolean permaRemoved; - boolean isToggled; + private ConcurrentHashMap cooldowns; + private boolean permaRemoved; + private boolean toggled = true; private long slowTime = 0; - private boolean tremorsense = true; - boolean blockedChi; + private boolean tremorSense = true; + private boolean chiBlocked = false; - public BendingPlayer(UUID uuid, String player, ArrayList elements, HashMap abilities, boolean permaRemoved) { + /** + * Creates a new {@link BendingPlayer}. + * + * @param uuid The unique identifier + * @param playerName The playername + * @param elements The known elements + * @param abilities The known abilities + * @param permaRemoved The permanent removed status + */ + public BendingPlayer(UUID uuid, String playerName, ArrayList elements, HashMap abilities, boolean permaRemoved) { this.uuid = uuid; - this.player = player; + this.name = playerName; this.elements = elements; this.setAbilities(abilities); this.permaRemoved = permaRemoved; cooldowns = new ConcurrentHashMap(); - isToggled = true; - blockedChi = false; - players.put(player, this); + players.put(uuid, this); PKListener.login(this); } + /** + * Gets the map of {@link BendingPlayers}. + * + * @return {@link #players} + */ + public static ConcurrentHashMap getPlayers() { + return players; + } + + /** + * Adds an ability to the cooldowns map while firing a + * {@link PlayerCooldownChangeEvent}. + * + * @param ability Name of the ability + * @param cooldown The cooldown time + */ public void addCooldown(String ability, long cooldown) { PlayerCooldownChangeEvent event = new PlayerCooldownChangeEvent(Bukkit.getPlayer(uuid), ability, Result.ADDED); Bukkit.getServer().getPluginManager().callEvent(event); - if(!event.isCancelled()) { + if (!event.isCancelled()) { this.cooldowns.put(ability, cooldown + System.currentTimeMillis()); } } + /** + * Adds an element to the {@link BendingPlayer}'s known list. + * + * @param e The element to add + */ public void addElement(Element e) { this.elements.add(e); } + /** + * Sets chiBlocked to true. + */ public void blockChi() { - blockedChi = true; + chiBlocked = true; } + /** + * Checks to see if {@link BendingPlayer} can be slowed. + * + * @return true If player can be slowed + */ public boolean canBeSlowed() { return (System.currentTimeMillis() > slowTime); } + /** + * Gets the map of abilities that the {@link BendingPlayer} knows. + * + * @return map of abilities + */ public HashMap getAbilities() { return this.abilities; } + + /** + * Gets the cooldown time of the ability. + * + * @param ability The ability to check + * @return the cooldown time + *

+ * or -1 if cooldown doesn't exist + *

+ */ + public long getCooldown(String ability) { + if (cooldowns.containsKey(ability)) { + return cooldowns.get(ability); + } + return -1; + } + /** + * Gets the map of cooldowns of the {@link BendingPlayer}. + * + * @return map of cooldowns + */ + public ConcurrentHashMap getCooldowns() { + return cooldowns; + } + + /** + * Gets the list of elements the {@link BendingPlayer} knows. + * + * @return a list of elements + */ public List getElements() { return this.elements; } - public String getPlayerName() { - return this.player; + /** + * Gets the name of the {@link BendingPlayer}. + * + * @return the player name + */ + public String getName() { + return this.name; } - + + /** + * Gets the unique identifier of the {@link BendingPlayer}. + * + * @return the uuid + */ public UUID getUUID() { return this.uuid; } + /** + * Convenience method to {@link #getUUID()} as a string. + * + * @return string version of uuid + */ + public String getUUIDString() { + return this.uuid.toString(); + } + + /** + * Checks to see if the {@link BendingPlayer} knows a specific element. + * + * @param e The element to check + * @return true If the player knows the element + */ public boolean hasElement(Element e) { return this.elements.contains(e); - } - - public boolean isChiBlocked() { - return this.blockedChi; - } - - public boolean isOnCooldown(String ability) { - return this.cooldowns.containsKey(ability); } + /** + * Checks to see if the {@link BendingPlayer} is chi blocked. + * + * @return true If the player is chi blocked + */ + public boolean isChiBlocked() { + return this.chiBlocked; + } + + /** + * Checks to see if a specific ability is on cooldown. + * + * @param ability The ability name to check + * @return true if the cooldown map contains the ability + */ + public boolean isOnCooldown(String ability) { + return this.cooldowns.containsKey(ability); + } + + /** + * Checks if the {@link BendingPlayer} is permaremoved. + * + * @return true If the player is permaremoved + */ public boolean isPermaRemoved() { return this.permaRemoved; } + /** + * Checks if the {@link BendingPlayer} has bending toggled on. + * + * @return true If bending is toggled on + */ public boolean isToggled() { - return this.isToggled; + return this.toggled; } - public boolean isTremorsensing() { - return this.tremorsense; + /** + * Checks if the {@link BendingPlayer} is tremor sensing. + * + * @return true if player is tremor sensing + */ + public boolean isTremorSensing() { + return this.tremorSense; } + /** + * Removes the cooldown of an ability. + * + * @param ability The ability's cooldown to remove + */ public void removeCooldown(String ability) { PlayerCooldownChangeEvent event = new PlayerCooldownChangeEvent(Bukkit.getPlayer(uuid), ability, Result.REMOVED); Bukkit.getServer().getPluginManager().callEvent(event); @@ -109,27 +243,66 @@ public class BendingPlayer { } } + /** + * Sets the {@link BendingPlayer}'s abilities. This method also + * saves the abilities to the database. + * + * @param abilities The abilities to set/save + */ public void setAbilities(HashMap abilities) { this.abilities = abilities; for (int i = 1; i <= 9; i++) { DBConnection.sql.modifyQuery("UPDATE pk_players SET slot" + i + " = '" + (abilities.get(i) == null ? null: abilities.get(i)) + "' WHERE uuid = '" + uuid + "'"); } } - + + /** + * Sets the {@link BendingPlayer}'s element. + * If the player had elements before they will be overwritten. + * + * @param e The element to set + */ public void setElement(Element e) { this.elements.clear(); this.elements.add(e); } + /** + * Sets the permanent removed state of the {@link BendingPlayer}. + * @param permaRemoved + */ + public void setPermaRemoved(boolean permaRemoved) { + this.permaRemoved = permaRemoved; + } + + /** + * Slow the {@link BendingPlayer} for a certain amount of time. + * + * @param cooldown The amount of time to slow. + */ public void slow(long cooldown) { slowTime = System.currentTimeMillis() + cooldown; } - public void toggleTremorsense() { - tremorsense = !tremorsense; + /** + * Toggles the {@link BendingPlayer}'s bending. + */ + public void toggleBending() { + toggled = !toggled; } - public void unblockChi() { - blockedChi = false; + /** + * Toggles the {@link BendingPlayer}'s tremor sensing. + */ + public void toggleTremorSense() { + tremorSense = !tremorSense; } + + /** + * Sets the {@link BendingPlayer}'s chi blocked to false. + */ + public void unblockChi() { + chiBlocked = false; + } + } diff --git a/src/com/projectkorra/ProjectKorra/Commands.java b/src/com/projectkorra/ProjectKorra/Commands.java index 4fdb1913..1dd6f157 100644 --- a/src/com/projectkorra/ProjectKorra/Commands.java +++ b/src/com/projectkorra/ProjectKorra/Commands.java @@ -1086,20 +1086,20 @@ public class Commands { HashMap abilities = bPlayer.getAbilities(); - ResultSet rs2 = DBConnection.sql.readQuery("SELECT * FROM pk_players WHERE uuid = '" + bPlayer.uuid.toString() + "'"); + ResultSet rs2 = DBConnection.sql.readQuery("SELECT * FROM pk_players WHERE uuid = '" + bPlayer.getUUIDString() + "'"); try { if (rs2.next()) { // SQL Data already exists for player. - DBConnection.sql.modifyQuery("UPDATE pk_players SET player = '" + bPlayer.player + "' WHERE uuid = '" + bPlayer.uuid.toString()); - DBConnection.sql.modifyQuery("UPDATE pk_players SET element = '" + elements + "' WHERE uuid = '" + bPlayer.uuid.toString()); - DBConnection.sql.modifyQuery("UPDATE pk_players SET permaremoved = '" + bPlayer.isPermaRemoved() + "' WHERE uuid = '" + bPlayer.uuid.toString()); + DBConnection.sql.modifyQuery("UPDATE pk_players SET player = '" + bPlayer.getName() + "' WHERE uuid = '" + bPlayer.getUUIDString()); + DBConnection.sql.modifyQuery("UPDATE pk_players SET element = '" + elements + "' WHERE uuid = '" + bPlayer.getUUIDString()); + DBConnection.sql.modifyQuery("UPDATE pk_players SET permaremoved = '" + bPlayer.isPermaRemoved() + "' WHERE uuid = '" + bPlayer.getUUIDString()); for (int slot = 1; slot < 10; slot++) { - DBConnection.sql.modifyQuery("UPDATE pk_players SET slot" + slot + " = '" + abilities.get(slot) + "' WHERE player = '" + bPlayer.getPlayerName() + "'"); + DBConnection.sql.modifyQuery("UPDATE pk_players SET slot" + slot + " = '" + abilities.get(slot) + "' WHERE player = '" + bPlayer.getName() + "'"); } } else { - DBConnection.sql.modifyQuery("INSERT INTO pk_players (uuid, player, element, permaremoved) VALUES ('" + bPlayer.uuid.toString() + "', '" + bPlayer.player + "', '" + elements + "', '" + bPlayer.isPermaRemoved() +"')"); + DBConnection.sql.modifyQuery("INSERT INTO pk_players (uuid, player, element, permaremoved) VALUES ('" + bPlayer.getUUIDString() + "', '" + bPlayer.getName() + "', '" + elements + "', '" + bPlayer.isPermaRemoved() +"')"); for (int slot = 1; slot < 10; slot++) { - DBConnection.sql.modifyQuery("UPDATE pk_players SET slot" + slot + " = '" + abilities.get(slot) + "' WHERE player = '" + bPlayer.getPlayerName() + "'"); + DBConnection.sql.modifyQuery("UPDATE pk_players SET slot" + slot + " = '" + abilities.get(slot) + "' WHERE player = '" + bPlayer.getName() + "'"); } } } catch (SQLException ex) { @@ -1107,7 +1107,7 @@ public class Commands { } i++; if (debug) { - System.out.println("[ProjectKorra] Successfully imported " + bPlayer.player + ". " + bPlayers.size() + " players left to import."); + System.out.println("[ProjectKorra] Successfully imported " + bPlayer.getName() + ". " + bPlayers.size() + " players left to import."); } } } @@ -1166,16 +1166,16 @@ public class Commands { BendingPlayer bPlayer = GeneralMethods.getBendingPlayer(player.getName()); if (bPlayer.isPermaRemoved()) { - bPlayer.permaRemoved = false; + bPlayer.setPermaRemoved(false); GeneralMethods.savePermaRemoved(bPlayer); s.sendMessage(ChatColor.RED + "You have restored the bending of: " + ChatColor.DARK_AQUA + player.getName()); return true; } - bPlayer.elements.clear(); + bPlayer.getElements().clear(); GeneralMethods.removeUnusableAbilities(player.getName()); GeneralMethods.saveElements(bPlayer); - bPlayer.permaRemoved = true; + bPlayer.setPermaRemoved(true); GeneralMethods.savePermaRemoved(bPlayer); player.sendMessage(ChatColor.RED + "Your bending has been permanently removed."); s.sendMessage(ChatColor.RED + "You have permanently removed the bending of: " + ChatColor.DARK_AQUA + player.getName()); @@ -1323,7 +1323,7 @@ public class Commands { BendingPlayer bPlayer = GeneralMethods.getBendingPlayer(player.getName()); GeneralMethods.removeUnusableAbilities(player.getName()); - bPlayer.elements.clear(); + bPlayer.getElements().clear(); GeneralMethods.saveElements(bPlayer); s.sendMessage(ChatColor.GREEN + "You have removed the bending of " + ChatColor.DARK_AQUA + player.getName()); player.sendMessage(ChatColor.GREEN + "Your bending has been removed by " + ChatColor.DARK_AQUA + s.getName()); @@ -1372,13 +1372,13 @@ public class Commands { BendingPlayer bPlayer = GeneralMethods.getBendingPlayer(s.getName()); - if (bPlayer.isToggled) { + if (bPlayer.isToggled()) { s.sendMessage(ChatColor.RED + "Your bending has been toggled off. You will not be able to use most abilities until you toggle it back."); - bPlayer.isToggled = false; + bPlayer.toggleBending(); return true; } else { s.sendMessage(ChatColor.GREEN + "You have turned your Bending back on."); - bPlayer.isToggled = true; + bPlayer.toggleBending(); return true; } } else if (args.length == 2 && args[1].equalsIgnoreCase("all")) { @@ -1525,7 +1525,7 @@ public class Commands { s.sendMessage(ChiMethods.getChiColor() + "- ChiBlocker"); } BendingPlayer bPlayer = GeneralMethods.getBendingPlayer(un); - UUID uuid2 = bPlayer.uuid; + UUID uuid2 = bPlayer.getUUID(); if (bPlayer != null) { s.sendMessage("Abilities: "); for (int i = 1; i <= 9; i++) { @@ -1591,11 +1591,11 @@ public class Commands { String un = player.getName(); BendingPlayer bp = GeneralMethods.getBendingPlayer(un); - if (bp.elements.size() > 1) { + if (bp.getElements().size() > 1) { players.add(GeneralMethods.getAvatarColor() + un); continue; } - if (bp.elements.size() == 0) { + if (bp.getElements().size() == 0) { players.add(un); continue; } diff --git a/src/com/projectkorra/ProjectKorra/GeneralMethods.java b/src/com/projectkorra/ProjectKorra/GeneralMethods.java index 5df2e130..749a4bd0 100644 --- a/src/com/projectkorra/ProjectKorra/GeneralMethods.java +++ b/src/com/projectkorra/ProjectKorra/GeneralMethods.java @@ -264,7 +264,7 @@ public class GeneralMethods { if (bPlayer == null) return false; if (plugin.getConfig().getStringList("Properties.DisabledWorlds") != null && plugin.getConfig().getStringList("Properties.DisabledWorlds").contains(p.getWorld().getName())) return false; if (Commands.isToggledForAll) return false; - if (!bPlayer.isToggled) return false; + if (!bPlayer.isToggled()) return false; if (p == null) return false; if (cooldowns.containsKey(p.getName())) { if (cooldowns.get(p.getName()) + ProjectKorra.plugin.getConfig().getLong("Properties.GlobalCooldown") >= System.currentTimeMillis()) { @@ -272,7 +272,7 @@ public class GeneralMethods { } cooldowns.remove(p.getName()); } - if (bPlayer.blockedChi) return false; + if (bPlayer.isChiBlocked()) return false; if (!p.hasPermission("bending.ability." + ability)) return false; if (AirMethods.isAirAbility(ability) && !isBender(player, Element.Air)) return false; if (WaterMethods.isWaterAbility(ability) && !isBender(player, Element.Water)) return false; @@ -294,10 +294,10 @@ public class GeneralMethods { if (bPlayer == null) return false; if (p == null) return false; if (!p.hasPermission("bending." + element.toString().toLowerCase() + ".passive")) return false; - if (!bPlayer.isToggled) return false; + if (!bPlayer.isToggled()) return false; if (!bPlayer.hasElement(element)) return false; if (isRegionProtectedFromBuild(p, null, p.getLocation())) return false; - if (bPlayer.blockedChi) return false; + if (bPlayer.isChiBlocked()) return false; return true; } @@ -659,7 +659,7 @@ public class GeneralMethods { * @return The BendingPlayer object if {@link BendingPlayer#players} contains the player name */ public static BendingPlayer getBendingPlayer(String player) { - return BendingPlayer.players.get(player); + return BendingPlayer.getPlayers().get(player); } public static List getBlocksAlongLine(Location ploc, Location tloc, World w) { @@ -1608,10 +1608,10 @@ public class GeneralMethods { public static void saveAbility(BendingPlayer bPlayer, int slot, String ability) { if (bPlayer == null) return; - String uuid = bPlayer.uuid.toString(); + String uuid = bPlayer.getUUIDString(); //Temp code to block modifications of binds, Should be replaced when bind event is added. - if (MultiAbilityManager.playerAbilities.containsKey(Bukkit.getPlayer(bPlayer.uuid))) + if (MultiAbilityManager.playerAbilities.containsKey(Bukkit.getPlayer(bPlayer.getUUID()))) return; HashMap abilities = bPlayer.getAbilities(); @@ -1620,7 +1620,7 @@ public class GeneralMethods { public static void saveElements(BendingPlayer bPlayer) { if (bPlayer == null) return; - String uuid = bPlayer.uuid.toString(); + String uuid = bPlayer.getUUIDString(); StringBuilder elements = new StringBuilder(); if (bPlayer.hasElement(Element.Air)) elements.append("a"); @@ -1634,8 +1634,8 @@ public class GeneralMethods { public static void savePermaRemoved(BendingPlayer bPlayer) { if (bPlayer == null) return; - String uuid = bPlayer.uuid.toString(); - boolean permaRemoved = bPlayer.permaRemoved; + String uuid = bPlayer.getUUIDString(); + boolean permaRemoved = bPlayer.isPermaRemoved(); DBConnection.sql.modifyQuery("UPDATE pk_players SET permaremoved = '" + (permaRemoved ? "true" : "false") + "' WHERE uuid = '" + uuid + "'"); } diff --git a/src/com/projectkorra/ProjectKorra/PKListener.java b/src/com/projectkorra/ProjectKorra/PKListener.java index 6558de2e..e040805a 100644 --- a/src/com/projectkorra/ProjectKorra/PKListener.java +++ b/src/com/projectkorra/ProjectKorra/PKListener.java @@ -166,14 +166,14 @@ public class PKListener implements Listener { } if (GeneralMethods.toggedOut.contains(player.getUniqueId())) { - GeneralMethods.getBendingPlayer(player.getName()).isToggled = false; + GeneralMethods.getBendingPlayer(player.getName()).toggleBending(); player.sendMessage(ChatColor.YELLOW + "Reminder, you toggled your bending before signing off. Enable it again with /bending toggle."); } Preset.loadPresets(player); String append = ""; boolean chatEnabled = ProjectKorra.plugin.getConfig().getBoolean("Properties.Chat.Enable"); - if ((player.hasPermission("bending.avatar") || GeneralMethods.getBendingPlayer(player.getName()).elements.size() > 1) && chatEnabled) { + if ((player.hasPermission("bending.avatar") || GeneralMethods.getBendingPlayer(player.getName()).getElements().size() > 1) && chatEnabled) { append = plugin.getConfig().getString("Properties.Chat.Prefixes.Avatar"); } else if (GeneralMethods.isBender(player.getName(), Element.Air) && chatEnabled) { append = plugin.getConfig().getString("Properties.Chat.Prefixes.Air"); @@ -586,7 +586,7 @@ public class PKListener implements Listener { Player player = event.getPlayer(); ChatColor color = ChatColor.WHITE; - if (player.hasPermission("bending.avatar") || GeneralMethods.getBendingPlayer(player.getName()).elements.size() > 1) { + if (player.hasPermission("bending.avatar") || GeneralMethods.getBendingPlayer(player.getName()).getElements().size() > 1) { color = ChatColor.valueOf(plugin.getConfig().getString("Properties.Chat.Colors.Avatar")); } else if (GeneralMethods.isBender(player.getName(), Element.Air)) { color = ChatColor.valueOf(plugin.getConfig().getString("Properties.Chat.Colors.Air")); @@ -1031,7 +1031,7 @@ public class PKListener implements Listener { Commands.invincible.remove(event.getPlayer().getName()); } Preset.unloadPreset(player); - BendingPlayer.players.remove(event.getPlayer().getName()); + BendingPlayer.getPlayers().remove(event.getPlayer().getUniqueId()); if (EarthArmor.instances.containsKey(event.getPlayer())) { EarthArmor.removeEffect(event.getPlayer()); event.getPlayer().removePotionEffect(PotionEffectType.DAMAGE_RESISTANCE); @@ -1184,7 +1184,7 @@ public class PKListener implements Listener { new EarthTunnel(player); } if (abil.equalsIgnoreCase("Tremorsense")) { - GeneralMethods.getBendingPlayer(player.getName()).toggleTremorsense(); + GeneralMethods.getBendingPlayer(player.getName()).toggleTremorSense(); } if (abil.equalsIgnoreCase("Extraction")) { new Extraction(player); diff --git a/src/com/projectkorra/ProjectKorra/earthbending/Tremorsense.java b/src/com/projectkorra/ProjectKorra/earthbending/Tremorsense.java index 1343cbfa..e9b52961 100644 --- a/src/com/projectkorra/ProjectKorra/earthbending/Tremorsense.java +++ b/src/com/projectkorra/ProjectKorra/earthbending/Tremorsense.java @@ -91,7 +91,7 @@ public class Tremorsense { .getRelative(BlockFace.DOWN); BendingPlayer bp = GeneralMethods.getBendingPlayer(player.getName()); - if (!bp.isTremorsensing()) { + if (!bp.isTremorSensing()) { if (block != null) revert(); return;