Resolved conflicts and refactored code

This commit is contained in:
Akiranya 2021-09-25 23:16:25 +08:00
parent 28ba125daa
commit d506276bd5
3 changed files with 20 additions and 9 deletions

View File

@ -56,7 +56,7 @@ public class Necrotic extends EcoEnchant {
return; return;
} }
if (WeakMetadata.WEAK_META.containsKey(event.getEntity())) { if (WeakMetadata.SUMMONED_ENTITY_MEMORY.containsKey(event.getEntity())) {
return; return;
} }

View File

@ -6,10 +6,20 @@ import java.util.Map;
import java.util.WeakHashMap; import java.util.WeakHashMap;
public class WeakMetadata { public class WeakMetadata {
/** /**
* Weak metadata to prevent memory leaks. * Summoned entities by the summoning enchantments.
* <p> * <p>
* Thanks Akiranya for the change! * K: summoned entity
* V: the target of the summoned entity
*/ */
public static final Map<Entity, Object> WEAK_META = new WeakHashMap<>(); public final static Map<Entity, Object> SUMMONED_ENTITY_MEMORY = new WeakHashMap<>();
/**
* Victim entities which the summoned entities target at.
* <p>
* K: entity which the summoned entities target at
* V: nothing (passing null is fine)
*/
public final static Map<Entity, Object> SUMMONED_ENTITY_TARGET = new WeakHashMap<>();
} }

View File

@ -83,7 +83,7 @@ public abstract class SummoningEnchantment extends EcoEnchant {
return; return;
} }
if (WeakMetadata.WEAK_META.containsKey(victim)) { if (WeakMetadata.SUMMONED_ENTITY_MEMORY.containsKey(victim) || WeakMetadata.SUMMONED_ENTITY_TARGET.containsKey(victim)) {
return; return;
} }
@ -103,24 +103,25 @@ public abstract class SummoningEnchantment extends EcoEnchant {
health = entity.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); health = entity.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue();
} }
entity.setHealth(health); entity.setHealth(health);
WeakMetadata.WEAK_META.put(entity, victim); WeakMetadata.SUMMONED_ENTITY_MEMORY.put(entity, victim);
WeakMetadata.SUMMONED_ENTITY_TARGET.put(victim, null);
this.getPlugin().getScheduler().runLater(entity::remove, ticksToLive); this.getPlugin().getScheduler().runLater(entity::remove, ticksToLive);
} }
} }
@EventHandler @EventHandler
public void onSwitchTarget(@NotNull final EntityTargetEvent event) { public void onSwitchTarget(@NotNull final EntityTargetEvent event) {
if (!WeakMetadata.WEAK_META.containsKey(event.getEntity())) { if (!WeakMetadata.SUMMONED_ENTITY_MEMORY.containsKey(event.getEntity())) {
return; return;
} }
LivingEntity target = (LivingEntity) WeakMetadata.WEAK_META.get(event.getEntity()); LivingEntity target = (LivingEntity) WeakMetadata.SUMMONED_ENTITY_MEMORY.get(event.getEntity());
event.setTarget(target); event.setTarget(target);
} }
@EventHandler(priority = EventPriority.LOW) @EventHandler(priority = EventPriority.LOW)
public void onDropItem(@NotNull final EntityDeathEvent event) { public void onDropItem(@NotNull final EntityDeathEvent event) {
if (!WeakMetadata.WEAK_META.containsKey(event.getEntity())) { if (!WeakMetadata.SUMMONED_ENTITY_MEMORY.containsKey(event.getEntity())) {
return; return;
} }