3
0
Mirror von https://github.com/Moulberry/AxiomPaperPlugin.git synchronisiert 2024-09-29 16:00:04 +02:00

Use WeakReference in ServerWorldPropertiesRegistry

Dieser Commit ist enthalten in:
Moulberry 2024-05-26 22:37:06 +08:00
Ursprung afe4649531
Commit 1c01f0550c
2 geänderte Dateien mit 11 neuen und 4 gelöschten Zeilen

Datei anzeigen

@ -44,6 +44,7 @@ import org.jetbrains.annotations.Nullable;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.lang.ref.WeakReference;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.*;
@ -399,7 +400,7 @@ public class AxiomPaper extends JavaPlugin implements Listener {
} }
private ServerWorldPropertiesRegistry createWorldProperties(World world) { private ServerWorldPropertiesRegistry createWorldProperties(World world) {
ServerWorldPropertiesRegistry registry = new ServerWorldPropertiesRegistry(world); ServerWorldPropertiesRegistry registry = new ServerWorldPropertiesRegistry(new WeakReference<>(world));
AxiomCreateWorldPropertiesEvent createEvent = new AxiomCreateWorldPropertiesEvent(world, registry); AxiomCreateWorldPropertiesEvent createEvent = new AxiomCreateWorldPropertiesEvent(world, registry);
Bukkit.getPluginManager().callEvent(createEvent); Bukkit.getPluginManager().callEvent(createEvent);

Datei anzeigen

@ -13,15 +13,16 @@ import org.bukkit.craftbukkit.v1_20_R1.CraftWorld;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import java.lang.ref.Reference;
import java.util.*; import java.util.*;
public class ServerWorldPropertiesRegistry { public class ServerWorldPropertiesRegistry {
private final LinkedHashMap<WorldPropertyCategory, List<ServerWorldPropertyHolder<?>>> propertyList = new LinkedHashMap<>(); private final LinkedHashMap<WorldPropertyCategory, List<ServerWorldPropertyHolder<?>>> propertyList = new LinkedHashMap<>();
private final Map<ResourceLocation, ServerWorldPropertyHolder<?>> propertyMap = new HashMap<>(); private final Map<ResourceLocation, ServerWorldPropertyHolder<?>> propertyMap = new HashMap<>();
private final World world; private final Reference<World> world;
public ServerWorldPropertiesRegistry(World world) { public ServerWorldPropertiesRegistry(Reference<World> world) {
this.world = world; this.world = world;
this.registerDefault(); this.registerDefault();
} }
@ -32,9 +33,14 @@ public class ServerWorldPropertiesRegistry {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void addCategory(WorldPropertyCategory category, List<ServerWorldPropertyBase<?>> properties) { public void addCategory(WorldPropertyCategory category, List<ServerWorldPropertyBase<?>> properties) {
World world = this.world.get();
if (world == null) {
return;
}
List<ServerWorldPropertyHolder<?>> holders = new ArrayList<>(); List<ServerWorldPropertyHolder<?>> holders = new ArrayList<>();
for (ServerWorldPropertyBase<?> property : properties) { for (ServerWorldPropertyBase<?> property : properties) {
Object defaultValue = property.getDefaultValue(this.world); Object defaultValue = property.getDefaultValue(world);
holders.add(new ServerWorldPropertyHolder<>(defaultValue, (ServerWorldPropertyBase<Object>) property)); holders.add(new ServerWorldPropertyHolder<>(defaultValue, (ServerWorldPropertyBase<Object>) property));
} }