From 268319b862b110dc6cb119250afccfe20e7f03fd Mon Sep 17 00:00:00 2001 From: Jack Lin Date: Thu, 16 Jul 2015 21:38:47 +1200 Subject: [PATCH] Add InstanceType to Ability, Update BaseAbility * MULTIPLE = A move that has several instances under one player * SINGLE = A move that can only have one instance under one player * By default all moves are SINGLE * To make a move MULTIPLE have the ability override the getInstanceType() and make it return InstanceType.MULITPLE --- .../ProjectKorra/Ability/Ability.java | 15 +++ .../ProjectKorra/Ability/BaseAbility.java | 96 +++++++++++++++---- 2 files changed, 93 insertions(+), 18 deletions(-) diff --git a/src/com/projectkorra/ProjectKorra/Ability/Ability.java b/src/com/projectkorra/ProjectKorra/Ability/Ability.java index b008d716..a102491a 100644 --- a/src/com/projectkorra/ProjectKorra/Ability/Ability.java +++ b/src/com/projectkorra/ProjectKorra/Ability/Ability.java @@ -13,5 +13,20 @@ public interface Ability extends ConfigLoadable { * A method to remove an instance of an Ability. */ public void remove(); + + /** + * Gets the {@link InstanceType} of the ability. + * + * @return the instance type + */ + public InstanceType getInstanceType(); + + /** + * Used to signify whether an ability can have multiple instances + * per player or only a single instance. + */ + public enum InstanceType { + SINGLE, MULTIPLE; + } } diff --git a/src/com/projectkorra/ProjectKorra/Ability/BaseAbility.java b/src/com/projectkorra/ProjectKorra/Ability/BaseAbility.java index daf7ed00..4567a010 100644 --- a/src/com/projectkorra/ProjectKorra/Ability/BaseAbility.java +++ b/src/com/projectkorra/ProjectKorra/Ability/BaseAbility.java @@ -18,23 +18,44 @@ public abstract class BaseAbility implements Ability { * ability instance or {@link #getInstance(StockAbilities)} from the * outside. */ - private static ConcurrentHashMap> instances = new ConcurrentHashMap<>(); + private static ConcurrentHashMap> instances = new ConcurrentHashMap<>(); //protected static AbilityMap instances = new AbilityMap<>(); + private static int ID = Integer.MIN_VALUE; private final StockAbilities stockAbility = getStockAbility(); + private final InstanceType type = getInstanceType(); private Player player; private UUID uniqueId; + private int id; protected final void putInstance(Player player, BaseAbility ability) { + if (type == InstanceType.MULTIPLE) { + this.id = ID; + } else { + this.id = -1; + } this.uniqueId = player.getUniqueId(); this.player = player; if (instances.containsKey(stockAbility)) { - instances.get(stockAbility).put(uniqueId, ability); + if (type == InstanceType.MULTIPLE) { + instances.get(stockAbility).put(id, ability); + } else { + instances.get(stockAbility).put(uniqueId, ability); + } } else { - ConcurrentHashMap map = new ConcurrentHashMap<>(); - map.put(uniqueId, ability); + ConcurrentHashMap map = new ConcurrentHashMap<>(); + if (type == InstanceType.MULTIPLE) { + map.put(id, ability); + } else { + map.put(uniqueId, ability); + } instances.put(stockAbility, map); } + if (id != -1) { + if (ID == Integer.MAX_VALUE) + ID = Integer.MIN_VALUE; + ID++; + } } /** @@ -43,7 +64,11 @@ public abstract class BaseAbility implements Ability { private final void removeInstance() { if (instances.containsKey(stockAbility)) { if (instances.get(stockAbility) != null) { - instances.get(getStockAbility()).remove(uniqueId); + if (type == InstanceType.MULTIPLE) { + instances.get(getStockAbility()).remove(id); + } else { + instances.get(getStockAbility()).remove(uniqueId); + } } } } @@ -54,28 +79,56 @@ public abstract class BaseAbility implements Ability { * @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); + public final static ConcurrentHashMap getInstance(StockAbilities ability) { + if (instances.containsKey(ability)) { + return instances.get(ability); + } + return new ConcurrentHashMap(); + } + + /** + * Convenience method that calls {@link #progress()} for all instances + * of a specified ability. + * + * @see #progressAll() + */ + public static void progressAll(StockAbilities ability) { + for (Object object : getInstance(ability).keySet()) { + instances.get(ability).get(object).progress(); } - return new ConcurrentHashMap(); } /** * Convenience method that calls {@link #progress()} for all instances. + * + * @see #progressAll(StockAbilities) */ - public static void progressAll(StockAbilities ability) { - for (UUID uuid : getInstance(ability).keySet()) { - instances.get(ability).get(uuid).progress(); + public static void progressAll() { + for (StockAbilities ability : instances.keySet()) { + progressAll(ability); + } + } + + /** + * Convenience method that calls {@link #remove()} for all instances + * of a specified ability. + * + * @see #removeAll() + */ + public static void removeAll(StockAbilities ability) { + for (Object object : getInstance(ability).keySet()) { + instances.get(ability).get(object).remove(); } } /** * Convenience method that calls {@link #remove()} for all instances. + * + * @see #removeAll(StockAbilities) */ - public static void removeAll(StockAbilities ability) { - for (UUID uuid : getInstance(ability).keySet()) { - instances.get(ability).get(uuid).remove(); + public static void removeAll() { + for (StockAbilities ability : instances.keySet()) { + removeAll(ability); } } @@ -102,10 +155,10 @@ public abstract class BaseAbility implements Ability { * * @return {@link #getInstance(StockAbilities)} for the current ability */ - public ConcurrentHashMap getInstance() { + public ConcurrentHashMap getInstance() { return getInstance(stockAbility); } - + public Player getPlayer() { return player; } @@ -113,5 +166,12 @@ public abstract class BaseAbility implements Ability { public UUID getUniqueId() { return uniqueId; } - + + public InstanceType getInstanceType() { + return InstanceType.SINGLE; + } + + public int getID() { + return id; + } }