From 50cbaade7148d459825f526a22de3490226532f9 Mon Sep 17 00:00:00 2001 From: Jack Lin Date: Wed, 15 Jul 2015 17:42:34 +1200 Subject: [PATCH] Update BaseAbility * Add 3 new fields: * UniqueId * Player * StockAbility * Add Final modifier to putInstance(), removeInstance() getInstance(StockAbilities) * Add private modifier to removeInstance() * Implement remove() calling removeInstance() * Created Convenience method getInstance() to be called from abilities themselves * Add abstract method getStockAbility() * Add getters for Player. UniqueId --- .../ProjectKorra/Ability/BaseAbility.java | 81 ++++++++++++++++--- 1 file changed, 70 insertions(+), 11 deletions(-) diff --git a/src/com/projectkorra/ProjectKorra/Ability/BaseAbility.java b/src/com/projectkorra/ProjectKorra/Ability/BaseAbility.java index 6b88cb30..daf7ed00 100644 --- a/src/com/projectkorra/ProjectKorra/Ability/BaseAbility.java +++ b/src/com/projectkorra/ProjectKorra/Ability/BaseAbility.java @@ -3,6 +3,8 @@ package com.projectkorra.ProjectKorra.Ability; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import org.bukkit.entity.Player; + import com.projectkorra.ProjectKorra.configuration.ConfigLoadable; /** @@ -10,28 +12,49 @@ import com.projectkorra.ProjectKorra.configuration.ConfigLoadable; */ public abstract class BaseAbility implements Ability { + /** + * ConcurrentHashMap that stores all Ability instances under UUID key. + * To access this hashmap use either {@link #getInstance()} from the + * ability instance or {@link #getInstance(StockAbilities)} from the + * outside. + */ private static ConcurrentHashMap> instances = new ConcurrentHashMap<>(); //protected static AbilityMap instances = new AbilityMap<>(); - protected void putInstance(StockAbilities sb, UUID uuid, BaseAbility ability) { - if (instances.containsKey(sb)) { - instances.get(sb).put(uuid, ability); + private final StockAbilities stockAbility = getStockAbility(); + private Player player; + private UUID uniqueId; + + protected final void putInstance(Player player, BaseAbility ability) { + this.uniqueId = player.getUniqueId(); + this.player = player; + if (instances.containsKey(stockAbility)) { + instances.get(stockAbility).put(uniqueId, ability); } else { ConcurrentHashMap map = new ConcurrentHashMap<>(); - map.put(uuid, ability); - instances.put(sb, map); + map.put(uniqueId, ability); + instances.put(stockAbility, map); } } - protected void removeInstance(StockAbilities sb, UUID uuid) { - if (instances.containsKey(sb)) { - if (instances.get(sb) != null) { - instances.get(sb).remove(uuid); + /** + * Removes the UUID from the instances map + */ + private final void removeInstance() { + if (instances.containsKey(stockAbility)) { + if (instances.get(stockAbility) != null) { + instances.get(getStockAbility()).remove(uniqueId); } } } - public static ConcurrentHashMap getInstance(StockAbilities abilty) { + /** + * An access method to get an the instances of a {@link StockAbilities StockAbility}. + * + * @param abilty The instances map to get + * @return a map of instances from the specified {@link StockAbilities StockAbility} + */ + public final static ConcurrentHashMap getInstance(StockAbilities abilty) { if (instances.containsKey(abilty)) { return instances.get(abilty); } @@ -42,8 +65,9 @@ public abstract class BaseAbility implements Ability { * Convenience method that calls {@link #progress()} for all instances. */ public static void progressAll(StockAbilities ability) { - for (UUID uuid : getInstance(ability).keySet()) + for (UUID uuid : getInstance(ability).keySet()) { instances.get(ability).get(uuid).progress(); + } } /** @@ -54,5 +78,40 @@ public abstract class BaseAbility implements Ability { instances.get(ability).get(uuid).remove(); } } + + /** + * Calls {@link #removeInstance()}, Developers can override this + * method to do other things when remove is called but they + * MUST remember to call {@code super.remove()} + * for the UUID to be properly removed from the {@link #instances}. + */ + @Override + public void remove() { + removeInstance(); + } + + /** + * Gets the {@link StockAbilities StockAbility} that created this instance. + * + * @return stockabilities enum or null + */ + public abstract StockAbilities getStockAbility(); + + /** + * Convenience method to get instance map for current ability class. + * + * @return {@link #getInstance(StockAbilities)} for the current ability + */ + public ConcurrentHashMap getInstance() { + return getInstance(stockAbility); + } + + public Player getPlayer() { + return player; + } + + public UUID getUniqueId() { + return uniqueId; + } }