mirror of
https://github.com/TotalFreedomMC/TF-ProjectKorra.git
synced 2025-02-11 03:30:10 +00:00
Improve getInstances efficiency
This commit is contained in:
parent
486df1b504
commit
d14d9aec50
1 changed files with 27 additions and 21 deletions
|
@ -29,6 +29,7 @@ public abstract class CoreAbility implements Ability {
|
|||
private static ConcurrentHashMap<Integer, CoreAbility> instances = new ConcurrentHashMap<>();
|
||||
//protected static AbilityMap<Ability> instances = new AbilityMap<>();
|
||||
private static ConcurrentHashMap<StockAbility, ArrayList<Integer>> abilityMap = new ConcurrentHashMap<>();
|
||||
private static ConcurrentHashMap<Class<? extends CoreAbility>, ConcurrentHashMap<Integer, CoreAbility>> classAbilityMap = new ConcurrentHashMap<>();
|
||||
|
||||
private static int ID = Integer.MIN_VALUE;
|
||||
private final StockAbility stockAbility = getStockAbility();
|
||||
|
@ -49,15 +50,12 @@ public abstract class CoreAbility implements Ability {
|
|||
return true;
|
||||
}
|
||||
/*
|
||||
List<CoreAbility> abilities = getAbilitiesFromPlayer(player);
|
||||
for (CoreAbility coreAbility : abilities) {
|
||||
if (ability.isInstance(coreAbility)) {
|
||||
if (coreAbility.getPlayer().getUniqueId().equals(player.getUniqueId())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
* List<CoreAbility> abilities = getAbilitiesFromPlayer(player); for
|
||||
* (CoreAbility coreAbility : abilities) { if
|
||||
* (ability.isInstance(coreAbility)) { if
|
||||
* (coreAbility.getPlayer().getUniqueId().equals(player.getUniqueId()))
|
||||
* { return true; } } }
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -94,7 +92,7 @@ public abstract class CoreAbility implements Ability {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the ability instance by its id.
|
||||
*
|
||||
|
@ -108,8 +106,8 @@ public abstract class CoreAbility implements Ability {
|
|||
/**
|
||||
* An access method to get an the instances of a {@link StockAbility}.
|
||||
* <b>IMPORTANT: </b> If this is used in a for each loop use
|
||||
* {@link #getAbility(int)} to get the ability. Incorrect usage may
|
||||
* cause over looping and is capable of hanging the thead.
|
||||
* {@link #getAbility(int)} to get the ability. Incorrect usage may cause
|
||||
* over looping and is capable of hanging the thead.
|
||||
*
|
||||
* @param ability The instances map to get
|
||||
* @return a map of instances from the specified {@link StockAbility}
|
||||
|
@ -128,21 +126,16 @@ public abstract class CoreAbility implements Ability {
|
|||
/**
|
||||
* An access method to get an the instances of a {@link CoreAbility} by its
|
||||
* class. <b>IMPORTANT: </b> If this is used in a for each loop use
|
||||
* {@link #getAbility(int)} to get the ability. Incorrect usage may
|
||||
* cause over looping and is capable of hanging the thead.
|
||||
* {@link #getAbility(int)} to get the ability. Incorrect usage may cause
|
||||
* over looping and is capable of hanging the thead.
|
||||
*
|
||||
* @param ability The instances map to get
|
||||
* @return a map of instances from the specified class
|
||||
* @see #getInstances(StockAbility)
|
||||
*/
|
||||
public final static ConcurrentHashMap<Integer, CoreAbility> getInstances(Class<? extends CoreAbility> ability) {
|
||||
ConcurrentHashMap<Integer, CoreAbility> instanceMap = new ConcurrentHashMap<>();
|
||||
for (Integer id : instances.keySet()) {
|
||||
if (ability.isInstance(instances.get(id))) {
|
||||
instanceMap.put(id, instances.get(id));
|
||||
}
|
||||
}
|
||||
return instanceMap;
|
||||
ConcurrentHashMap<Integer, CoreAbility> instanceMap = classAbilityMap.get(ability.getClass());
|
||||
return instanceMap != null ? instanceMap : new ConcurrentHashMap<Integer, CoreAbility>();
|
||||
}
|
||||
|
||||
//TODO: Update bending managers to use bellow method
|
||||
|
@ -298,7 +291,14 @@ public abstract class CoreAbility implements Ability {
|
|||
this.id = ID;
|
||||
this.uniqueId = player.getUniqueId();
|
||||
this.player = player;
|
||||
Class<? extends CoreAbility> classKey = ability.getClass();
|
||||
|
||||
if (!classAbilityMap.containsKey(classKey)) {
|
||||
classAbilityMap.put(classKey, new ConcurrentHashMap<Integer, CoreAbility>());
|
||||
}
|
||||
classAbilityMap.get(classKey).put(id, ability);
|
||||
instances.put(id, ability);
|
||||
|
||||
if (stockAbility != null) {
|
||||
if (abilityMap.containsKey(stockAbility)) {
|
||||
abilityMap.get(stockAbility).add(id);
|
||||
|
@ -306,6 +306,7 @@ public abstract class CoreAbility implements Ability {
|
|||
abilityMap.put(stockAbility, new ArrayList<Integer>(Arrays.asList(id)));
|
||||
}
|
||||
}
|
||||
|
||||
if (ID == Integer.MAX_VALUE)
|
||||
ID = Integer.MIN_VALUE;
|
||||
ID++;
|
||||
|
@ -329,6 +330,11 @@ public abstract class CoreAbility implements Ability {
|
|||
if (instances.containsKey(id)) {
|
||||
instances.remove(id);
|
||||
}
|
||||
|
||||
if (classAbilityMap.containsKey(this.getClass())) {
|
||||
classAbilityMap.get(this.getClass()).remove(id);
|
||||
}
|
||||
|
||||
if (stockAbility != null) {
|
||||
if (abilityMap.containsKey(stockAbility)) {
|
||||
abilityMap.get(stockAbility).remove(id);
|
||||
|
|
Loading…
Reference in a new issue