Skip to content

Commit

Permalink
Use WeakReference in ServerWorldPropertiesRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
Moulberry committed May 26, 2024
1 parent 1d2a4e8 commit bcdc42f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/moulberry/axiom/AxiomPaper.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

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.*;
Expand Down Expand Up @@ -399,7 +400,7 @@ public void onGameRuleChanged(WorldGameRuleChangeEvent event) {
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.lang.ref.Reference;
import java.util.*;

public class ServerWorldPropertiesRegistry {

private final LinkedHashMap<WorldPropertyCategory, List<ServerWorldPropertyHolder<?>>> propertyList = new LinkedHashMap<>();
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.registerDefault();
}
Expand All @@ -32,9 +33,14 @@ public ServerWorldPropertyHolder<?> getById(ResourceLocation resourceLocation) {

@SuppressWarnings("unchecked")
public void addCategory(WorldPropertyCategory category, List<ServerWorldPropertyBase<?>> properties) {
World world = this.world.get();
if (world == null) {
return;
}

List<ServerWorldPropertyHolder<?>> holders = new ArrayList<>();
for (ServerWorldPropertyBase<?> property : properties) {
Object defaultValue = property.getDefaultValue(this.world);
Object defaultValue = property.getDefaultValue(world);
holders.add(new ServerWorldPropertyHolder<>(defaultValue, (ServerWorldPropertyBase<Object>) property));
}

Expand Down

0 comments on commit bcdc42f

Please sign in to comment.