From 8a17605969481673715893a27469b7a1a9d0d044 Mon Sep 17 00:00:00 2001 From: Moulberry Date: Thu, 16 Nov 2023 19:26:13 +0800 Subject: [PATCH] WorldProperties: Create more easily extendable ServerWorldPropertyBase class --- .../AxiomCreateWorldPropertiesEvent.java | 3 +- .../server/ServerWorldPropertiesRegistry.java | 8 +-- .../server/ServerWorldProperty.java | 56 ++++------------ .../server/ServerWorldPropertyBase.java | 64 +++++++++++++++++++ .../server/ServerWorldPropertyHolder.java | 8 +-- 5 files changed, 85 insertions(+), 54 deletions(-) create mode 100644 src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldPropertyBase.java diff --git a/src/main/java/com/moulberry/axiom/event/AxiomCreateWorldPropertiesEvent.java b/src/main/java/com/moulberry/axiom/event/AxiomCreateWorldPropertiesEvent.java index 087355c..7ae6581 100644 --- a/src/main/java/com/moulberry/axiom/event/AxiomCreateWorldPropertiesEvent.java +++ b/src/main/java/com/moulberry/axiom/event/AxiomCreateWorldPropertiesEvent.java @@ -3,6 +3,7 @@ package com.moulberry.axiom.event; import com.moulberry.axiom.world_properties.WorldPropertyCategory; import com.moulberry.axiom.world_properties.server.ServerWorldPropertiesRegistry; import com.moulberry.axiom.world_properties.server.ServerWorldProperty; +import com.moulberry.axiom.world_properties.server.ServerWorldPropertyBase; import com.moulberry.axiom.world_properties.server.ServerWorldPropertyHolder; import org.bukkit.World; import org.bukkit.event.Cancellable; @@ -29,7 +30,7 @@ public class AxiomCreateWorldPropertiesEvent extends Event implements Cancellabl return world; } - public void addCategory(WorldPropertyCategory category, List> properties) { + public void addCategory(WorldPropertyCategory category, List> properties) { this.registry.addCategory(category, properties); } 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 63a8bd9..3f3b5b5 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 @@ -31,11 +31,11 @@ public class ServerWorldPropertiesRegistry { } @SuppressWarnings("unchecked") - public void addCategory(WorldPropertyCategory category, List> properties) { + public void addCategory(WorldPropertyCategory category, List> properties) { List> holders = new ArrayList<>(); - for (ServerWorldProperty property : properties) { - Object defaultValue = property.defaultValueFunction.apply(this.world); - holders.add(new ServerWorldPropertyHolder<>(defaultValue, (ServerWorldProperty) property)); + for (ServerWorldPropertyBase property : properties) { + Object defaultValue = property.getDefaultValue(this.world); + holders.add(new ServerWorldPropertyHolder<>(defaultValue, (ServerWorldPropertyBase) property)); } this.propertyList.put(category, holders); diff --git a/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldProperty.java b/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldProperty.java index 8749368..ea69352 100644 --- a/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldProperty.java +++ b/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldProperty.java @@ -1,67 +1,33 @@ package com.moulberry.axiom.world_properties.server; -import com.moulberry.axiom.AxiomPaper; import com.moulberry.axiom.world_properties.PropertyUpdateHandler; -import com.moulberry.axiom.world_properties.WorldPropertyDataType; import com.moulberry.axiom.world_properties.WorldPropertyWidgetType; -import net.minecraft.resources.ResourceLocation; import org.bukkit.NamespacedKey; import org.bukkit.World; -import org.bukkit.craftbukkit.v1_20_R2.util.CraftNamespacedKey; +import org.bukkit.entity.Player; import java.util.function.Function; -public class ServerWorldProperty { +public class ServerWorldProperty extends ServerWorldPropertyBase { - private final ResourceLocation id; - /*package-private*/ final String name; - /*package-private*/ final boolean localizeName; - /*package-private*/ WorldPropertyWidgetType widget; - /*package-private*/ Function defaultValueFunction; - /*package-private*/ PropertyUpdateHandler handler; + private final Function defaultValueFunction; + private final PropertyUpdateHandler handler; public ServerWorldProperty(NamespacedKey id, String name, boolean localizeName, WorldPropertyWidgetType widget, Function defaultValueFunction, PropertyUpdateHandler handler) { - this.id = CraftNamespacedKey.toMinecraft(id); - this.name = name; - this.localizeName = localizeName; - this.widget = widget; + super(id, name, localizeName, widget); this.defaultValueFunction = defaultValueFunction; this.handler = handler; } - public ResourceLocation getId() { - return this.id; + @Override + public T getDefaultValue(World world) { + return this.defaultValueFunction.apply(world); } - public WorldPropertyDataType getType() { - return this.widget.dataType(); - } - - @SuppressWarnings("unchecked") - public boolean setValueWithoutSyncing(World world, T value) { - ServerWorldPropertiesRegistry properties = AxiomPaper.PLUGIN.getWorldPropertiesIfPresent(world); - if (properties != null) { - ServerWorldPropertyHolder property = properties.getById(this.id); - if (property != null && property.getProperty() == this) { - ((ServerWorldPropertyHolder)property).setValueWithoutSyncing(value); - return true; - } - } - return false; - } - - @SuppressWarnings("unchecked") - public boolean setValue(World world, T value) { - ServerWorldPropertiesRegistry properties = AxiomPaper.PLUGIN.getWorldPropertiesIfPresent(world); - if (properties != null) { - ServerWorldPropertyHolder property = properties.getById(this.id); - if (property != null && property.getProperty() == this) { - ((ServerWorldPropertyHolder)property).setValue(world, value); - return true; - } - } - return false; + @Override + public boolean handleUpdateProperty(Player player, World world, T value) { + return this.handler.update(player, world, value); } } diff --git a/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldPropertyBase.java b/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldPropertyBase.java new file mode 100644 index 0000000..ee3690a --- /dev/null +++ b/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldPropertyBase.java @@ -0,0 +1,64 @@ +package com.moulberry.axiom.world_properties.server; + +import com.moulberry.axiom.AxiomPaper; +import com.moulberry.axiom.world_properties.WorldPropertyDataType; +import com.moulberry.axiom.world_properties.WorldPropertyWidgetType; +import net.minecraft.resources.ResourceLocation; +import org.bukkit.NamespacedKey; +import org.bukkit.World; +import org.bukkit.craftbukkit.v1_20_R2.util.CraftNamespacedKey; +import org.bukkit.entity.Player; + +public abstract class ServerWorldPropertyBase { + + private final ResourceLocation id; + /*package-private*/ final String name; + /*package-private*/ final boolean localizeName; + /*package-private*/ WorldPropertyWidgetType widget; + + public ServerWorldPropertyBase(NamespacedKey id, String name, boolean localizeName, WorldPropertyWidgetType widget) { + this.id = CraftNamespacedKey.toMinecraft(id); + this.name = name; + this.localizeName = localizeName; + this.widget = widget; + } + + public abstract T getDefaultValue(World world); + + public abstract boolean handleUpdateProperty(Player player, World world, T value); + + public ResourceLocation getId() { + return this.id; + } + + public WorldPropertyDataType getType() { + return this.widget.dataType(); + } + + @SuppressWarnings("unchecked") + public boolean setValueWithoutSyncing(World world, T value) { + ServerWorldPropertiesRegistry properties = AxiomPaper.PLUGIN.getWorldPropertiesIfPresent(world); + if (properties != null) { + ServerWorldPropertyHolder property = properties.getById(this.id); + if (property != null && property.getProperty() == this) { + ((ServerWorldPropertyHolder)property).setValueWithoutSyncing(value); + return true; + } + } + return false; + } + + @SuppressWarnings("unchecked") + public boolean setValue(World world, T value) { + ServerWorldPropertiesRegistry properties = AxiomPaper.PLUGIN.getWorldPropertiesIfPresent(world); + if (properties != null) { + ServerWorldPropertyHolder property = properties.getById(this.id); + if (property != null && property.getProperty() == this) { + ((ServerWorldPropertyHolder)property).setValue(world, value); + return true; + } + } + return false; + } + +} diff --git a/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldPropertyHolder.java b/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldPropertyHolder.java index 72e1cb9..d092512 100644 --- a/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldPropertyHolder.java +++ b/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldPropertyHolder.java @@ -15,9 +15,9 @@ import org.bukkit.entity.Player; public class ServerWorldPropertyHolder { private T value; - private ServerWorldProperty property; + private ServerWorldPropertyBase property; - public ServerWorldPropertyHolder(T value, ServerWorldProperty property) { + public ServerWorldPropertyHolder(T value, ServerWorldPropertyBase property) { this.value = value; this.property = property; } @@ -30,13 +30,13 @@ public class ServerWorldPropertyHolder { return this.property.widget.dataType(); } - public ServerWorldProperty getProperty() { + public ServerWorldPropertyBase getProperty() { return property; } public void update(Player player, World world, byte[] data) { this.value = this.property.widget.dataType().deserialize(data); - if (this.property.handler.update(player, world, this.value)) { + if (this.property.handleUpdateProperty(player, world, this.value)) { this.sync(world); } }