Merge pull request #77

Fixed potential memory leak
This commit is contained in:
Will FP 2021-09-25 15:20:40 +01:00 committed by GitHub
commit b61ef51d20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 11 deletions

View File

@ -5,6 +5,7 @@ import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentType; import com.willfp.ecoenchants.enchantments.meta.EnchantmentType;
import com.willfp.ecoenchants.enchantments.util.EnchantChecks; import com.willfp.ecoenchants.enchantments.util.EnchantChecks;
import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils; import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils;
import com.willfp.ecoenchants.enchantments.util.WeakMetadata;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.WitherSkeleton; import org.bukkit.entity.WitherSkeleton;
@ -55,7 +56,7 @@ public class Necrotic extends EcoEnchant {
return; return;
} }
if (!event.getEntity().getMetadata("eco-target").isEmpty()) { if (WeakMetadata.ECO_TARGET.containsKey(event.getEntity())) {
return; return;
} }

View File

@ -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<Entity, Object> ECO_TARGET = new WeakHashMap<>();
}

View File

@ -6,14 +6,11 @@ import com.willfp.ecoenchants.enchantments.EcoEnchant;
import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.EcoEnchants;
import com.willfp.ecoenchants.enchantments.meta.EnchantmentType; import com.willfp.ecoenchants.enchantments.meta.EnchantmentType;
import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils; import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils;
import com.willfp.ecoenchants.enchantments.util.WeakMetadata;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.attribute.Attribute; import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Arrow; import org.bukkit.entity.*;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Mob;
import org.bukkit.entity.Trident;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageByEntityEvent;
@ -86,7 +83,7 @@ public abstract class SummoningEnchantment extends EcoEnchant {
return; return;
} }
if (!victim.getMetadata("eco-target").isEmpty()) { if (WeakMetadata.ECO_TARGET.containsKey(victim)) {
return; return;
} }
@ -106,24 +103,24 @@ 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);
entity.setMetadata("eco-target", this.getPlugin().getMetadataValueFactory().create(victim)); WeakMetadata.ECO_TARGET.put(entity, victim);
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 (event.getEntity().getMetadata("eco-target").isEmpty()) { if (!WeakMetadata.ECO_TARGET.containsKey(event.getEntity())) {
return; return;
} }
LivingEntity target = (LivingEntity) event.getEntity().getMetadata("eco-target").get(0).value(); LivingEntity target = (LivingEntity) WeakMetadata.ECO_TARGET.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 (event.getEntity().getMetadata("eco-target").isEmpty()) { if (!WeakMetadata.ECO_TARGET.containsKey(event.getEntity())) {
return; return;
} }