Merge pull request #79

Fixed a bug & resolved merge conflicts & refactored code
This commit is contained in:
Will FP 2021-09-26 13:47:41 +01:00 committed by GitHub
commit de9619b447
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 9 deletions

View File

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

View File

@ -5,11 +5,26 @@ import org.bukkit.entity.Entity;
import java.util.Map;
import java.util.WeakHashMap;
/**
* Weak metadata to prevent memory leaks.
* <p>
* Author: Akiranya (Nailm)
*/
public class WeakMetadata {
/**
* Weak metadata to prevent memory leaks.
* Summoned entities by the summoning enchantments.
* <p>
* Thanks Akiranya for the change!
* <p>K: summoned entity
* <p>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>
* <p>K: entity which the summoned entities target at
* <p>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;
}
if (WeakMetadata.WEAK_META.containsKey(victim)) {
if (WeakMetadata.SUMMONED_ENTITY_MEMORY.containsKey(victim) || WeakMetadata.SUMMONED_ENTITY_TARGET.containsKey(victim)) {
return;
}
@ -103,24 +103,25 @@ public abstract class SummoningEnchantment extends EcoEnchant {
health = entity.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue();
}
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);
}
}
@EventHandler
public void onSwitchTarget(@NotNull final EntityTargetEvent event) {
if (!WeakMetadata.WEAK_META.containsKey(event.getEntity())) {
if (!WeakMetadata.SUMMONED_ENTITY_MEMORY.containsKey(event.getEntity())) {
return;
}
LivingEntity target = (LivingEntity) WeakMetadata.WEAK_META.get(event.getEntity());
LivingEntity target = (LivingEntity) WeakMetadata.SUMMONED_ENTITY_MEMORY.get(event.getEntity());
event.setTarget(target);
}
@EventHandler(priority = EventPriority.LOW)
public void onDropItem(@NotNull final EntityDeathEvent event) {
if (!WeakMetadata.WEAK_META.containsKey(event.getEntity())) {
if (!WeakMetadata.SUMMONED_ENTITY_MEMORY.containsKey(event.getEntity())) {
return;
}