From d506276bd514f0cedb10eaa296de37afe9624529 Mon Sep 17 00:00:00 2001 From: Akiranya Date: Sat, 25 Sep 2021 23:16:25 +0800 Subject: [PATCH 1/4] Resolved conflicts and refactored code --- .../ecoenchants/normal/Necrotic.java | 2 +- .../enchantments/util/WeakMetadata.java | 16 +++++++++++++--- .../summoning/SummoningEnchantment.java | 11 ++++++----- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/ecoenchants/normal/Necrotic.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/ecoenchants/normal/Necrotic.java index 1512c0fd..79040fee 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/ecoenchants/normal/Necrotic.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/ecoenchants/normal/Necrotic.java @@ -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; } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WeakMetadata.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WeakMetadata.java index 6e08eedb..036517ff 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WeakMetadata.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WeakMetadata.java @@ -6,10 +6,20 @@ import java.util.Map; import java.util.WeakHashMap; public class WeakMetadata { + /** - * Weak metadata to prevent memory leaks. + * Summoned entities by the summoning enchantments. *

- * Thanks Akiranya for the change! + * K: summoned entity + * V: the target of the summoned entity */ - public static final Map WEAK_META = new WeakHashMap<>(); + public final static Map SUMMONED_ENTITY_MEMORY = new WeakHashMap<>(); + + /** + * Victim entities which the summoned entities target at. + *

+ * K: entity which the summoned entities target at + * V: nothing (passing null is fine) + */ + public final static Map SUMMONED_ENTITY_TARGET = new WeakHashMap<>(); } diff --git a/eco-extensions/summoning/src/main/java/com/willfp/ecoenchants/summoning/SummoningEnchantment.java b/eco-extensions/summoning/src/main/java/com/willfp/ecoenchants/summoning/SummoningEnchantment.java index f7eb7d6d..901e9b08 100644 --- a/eco-extensions/summoning/src/main/java/com/willfp/ecoenchants/summoning/SummoningEnchantment.java +++ b/eco-extensions/summoning/src/main/java/com/willfp/ecoenchants/summoning/SummoningEnchantment.java @@ -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; } From 022300f9d77da69134d4b41925560dbc800d5f89 Mon Sep 17 00:00:00 2001 From: Akiranya Date: Sun, 26 Sep 2021 11:30:52 +0800 Subject: [PATCH 2/4] Minor typo --- .../com/willfp/ecoenchants/summoning/SummoningEnchantment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eco-extensions/summoning/src/main/java/com/willfp/ecoenchants/summoning/SummoningEnchantment.java b/eco-extensions/summoning/src/main/java/com/willfp/ecoenchants/summoning/SummoningEnchantment.java index 901e9b08..f3076a15 100644 --- a/eco-extensions/summoning/src/main/java/com/willfp/ecoenchants/summoning/SummoningEnchantment.java +++ b/eco-extensions/summoning/src/main/java/com/willfp/ecoenchants/summoning/SummoningEnchantment.java @@ -83,7 +83,7 @@ public abstract class SummoningEnchantment extends EcoEnchant { return; } - if (WeakMetadata.SUMMONED_ENTITY_MEMORY.containsKey(victim) || WeakMetadata.SUMMONED_ENTITY_TARGET.containsKey(victim)) { + if (WeakMetadata.SUMMONED_ENTITY_MEMORY.containsKey(victim) || WeakMetadata.SUMMONED_ENTITY_TARGET.containsKey(victim)) { return; } From 9792ee23b5e28b7c6cd16742290f9c822d9651a0 Mon Sep 17 00:00:00 2001 From: Akiranya Date: Sun, 26 Sep 2021 11:40:59 +0800 Subject: [PATCH 3/4] Added author information /w\ --- .../com/willfp/ecoenchants/enchantments/util/WeakMetadata.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WeakMetadata.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WeakMetadata.java index 036517ff..365ffa4a 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WeakMetadata.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WeakMetadata.java @@ -5,6 +5,9 @@ import org.bukkit.entity.Entity; import java.util.Map; import java.util.WeakHashMap; +/** + * Author: Akiranya (Nailm) + */ public class WeakMetadata { /** From 9e07aedcc31dcc4c3933b1a200a8e024c7d0a6f5 Mon Sep 17 00:00:00 2001 From: Akiranya Date: Sun, 26 Sep 2021 11:44:49 +0800 Subject: [PATCH 4/4] Added more javadoc --- .../ecoenchants/enchantments/util/WeakMetadata.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WeakMetadata.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WeakMetadata.java index 365ffa4a..0e4b89dd 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WeakMetadata.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WeakMetadata.java @@ -6,6 +6,8 @@ import java.util.Map; import java.util.WeakHashMap; /** + * Weak metadata to prevent memory leaks. + *

* Author: Akiranya (Nailm) */ public class WeakMetadata { @@ -13,16 +15,16 @@ public class WeakMetadata { /** * Summoned entities by the summoning enchantments. *

- * K: summoned entity - * V: the target of the summoned entity + *

K: summoned entity + *

V: the target of the summoned entity */ public final static Map SUMMONED_ENTITY_MEMORY = new WeakHashMap<>(); /** * Victim entities which the summoned entities target at. *

- * K: entity which the summoned entities target at - * V: nothing (passing null is fine) + *

K: entity which the summoned entities target at + *

V: nothing (passing null is fine) */ public final static Map SUMMONED_ENTITY_TARGET = new WeakHashMap<>(); }