From bcdc42f65ba930c69508a148fadd0a80c7fb8014 Mon Sep 17 00:00:00 2001 From: Moulberry Date: Sun, 26 May 2024 22:37:06 +0800 Subject: [PATCH] Use WeakReference in ServerWorldPropertiesRegistry --- src/main/java/com/moulberry/axiom/AxiomPaper.java | 3 ++- .../server/ServerWorldPropertiesRegistry.java | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/moulberry/axiom/AxiomPaper.java b/src/main/java/com/moulberry/axiom/AxiomPaper.java index 300d79d..147f0d5 100644 --- a/src/main/java/com/moulberry/axiom/AxiomPaper.java +++ b/src/main/java/com/moulberry/axiom/AxiomPaper.java @@ -44,6 +44,7 @@ import org.jetbrains.annotations.Nullable; import java.io.FileReader; import java.io.IOException; +import java.lang.ref.WeakReference; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; @@ -399,7 +400,7 @@ public class AxiomPaper extends JavaPlugin implements Listener { } private ServerWorldPropertiesRegistry createWorldProperties(World world) { - ServerWorldPropertiesRegistry registry = new ServerWorldPropertiesRegistry(world); + ServerWorldPropertiesRegistry registry = new ServerWorldPropertiesRegistry(new WeakReference<>(world)); AxiomCreateWorldPropertiesEvent createEvent = new AxiomCreateWorldPropertiesEvent(world, registry); Bukkit.getPluginManager().callEvent(createEvent); diff --git a/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldPropertiesRegistry.java b/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldPropertiesRegistry.java index 09e0b9d..9bb5668 100644 --- a/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldPropertiesRegistry.java +++ b/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldPropertiesRegistry.java @@ -13,15 +13,16 @@ import org.bukkit.craftbukkit.v1_20_R3.CraftWorld; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; +import java.lang.ref.Reference; import java.util.*; public class ServerWorldPropertiesRegistry { private final LinkedHashMap>> propertyList = new LinkedHashMap<>(); private final Map> propertyMap = new HashMap<>(); - private final World world; + private final Reference world; - public ServerWorldPropertiesRegistry(World world) { + public ServerWorldPropertiesRegistry(Reference world) { this.world = world; this.registerDefault(); } @@ -32,9 +33,14 @@ public class ServerWorldPropertiesRegistry { @SuppressWarnings("unchecked") public void addCategory(WorldPropertyCategory category, List> properties) { + World world = this.world.get(); + if (world == null) { + return; + } + List> holders = new ArrayList<>(); for (ServerWorldPropertyBase property : properties) { - Object defaultValue = property.getDefaultValue(this.world); + Object defaultValue = property.getDefaultValue(world); holders.add(new ServerWorldPropertyHolder<>(defaultValue, (ServerWorldPropertyBase) property)); }