From 361fbf89bfecc57ab7e4f14815109102fc35d054 Mon Sep 17 00:00:00 2001 From: Akiranya Date: Sat, 25 Sep 2021 21:56:13 +0800 Subject: [PATCH] Fixed potential memory leak --- .../ecoenchants/normal/Necrotic.java | 3 ++- .../enchantments/util/WeakMetadata.java | 9 +++++++++ .../summoning/SummoningEnchantment.java | 17 +++++++---------- 3 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WeakMetadata.java 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 53e3c73a..db33aea1 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 @@ -5,6 +5,7 @@ import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.meta.EnchantmentType; import com.willfp.ecoenchants.enchantments.util.EnchantChecks; import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils; +import com.willfp.ecoenchants.enchantments.util.WeakMetadata; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.entity.WitherSkeleton; @@ -55,7 +56,7 @@ public class Necrotic extends EcoEnchant { return; } - if (!event.getEntity().getMetadata("eco-target").isEmpty()) { + if (WeakMetadata.ECO_TARGET.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 new file mode 100644 index 00000000..a786b8ca --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/WeakMetadata.java @@ -0,0 +1,9 @@ +package com.willfp.ecoenchants.enchantments.util; + +import org.bukkit.entity.Entity; + +import java.util.WeakHashMap; + +public class WeakMetadata { + public final static WeakHashMap ECO_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 04c3d768..fc179ad4 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 @@ -6,14 +6,11 @@ import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.meta.EnchantmentType; import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils; +import com.willfp.ecoenchants.enchantments.util.WeakMetadata; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.attribute.Attribute; -import org.bukkit.entity.Arrow; -import org.bukkit.entity.EntityType; -import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Mob; -import org.bukkit.entity.Trident; +import org.bukkit.entity.*; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.entity.EntityDamageByEntityEvent; @@ -86,7 +83,7 @@ public abstract class SummoningEnchantment extends EcoEnchant { return; } - if (!victim.getMetadata("eco-target").isEmpty()) { + if (WeakMetadata.ECO_TARGET.containsKey(victim)) { return; } @@ -106,24 +103,24 @@ public abstract class SummoningEnchantment extends EcoEnchant { health = entity.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); } entity.setHealth(health); - entity.setMetadata("eco-target", this.getPlugin().getMetadataValueFactory().create(victim)); + WeakMetadata.ECO_TARGET.put(entity, victim); this.getPlugin().getScheduler().runLater(entity::remove, ticksToLive); } } @EventHandler public void onSwitchTarget(@NotNull final EntityTargetEvent event) { - if (event.getEntity().getMetadata("eco-target").isEmpty()) { + if (!WeakMetadata.ECO_TARGET.containsKey(event.getEntity())) { return; } - LivingEntity target = (LivingEntity) event.getEntity().getMetadata("eco-target").get(0).value(); + LivingEntity target = (LivingEntity) WeakMetadata.ECO_TARGET.get(event.getEntity()); event.setTarget(target); } @EventHandler(priority = EventPriority.LOW) public void onDropItem(@NotNull final EntityDeathEvent event) { - if (event.getEntity().getMetadata("eco-target").isEmpty()) { + if (!WeakMetadata.ECO_TARGET.containsKey(event.getEntity())) { return; }