mirror of
https://github.com/Auxilor/EcoEnchants.git
synced 2025-01-05 22:07:34 +01:00
Merge pull request #79
Fixed a bug & resolved merge conflicts & refactored code
This commit is contained in:
commit
de9619b447
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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<>();
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user