diff --git a/src/main/java/com/moulberry/axiom/WorldPropertiesExample.java b/src/main/java/com/moulberry/axiom/WorldPropertiesExample.java index 2decb48..314d253 100644 --- a/src/main/java/com/moulberry/axiom/WorldPropertiesExample.java +++ b/src/main/java/com/moulberry/axiom/WorldPropertiesExample.java @@ -3,6 +3,7 @@ package com.moulberry.axiom; import com.moulberry.axiom.event.AxiomCreateWorldPropertiesEvent; import com.moulberry.axiom.world_properties.WorldPropertyCategory; import com.moulberry.axiom.world_properties.WorldPropertyWidgetType; +import com.moulberry.axiom.world_properties.server.PropertyUpdateResult; import com.moulberry.axiom.world_properties.server.ServerWorldProperty; import net.kyori.adventure.text.Component; import org.bukkit.NamespacedKey; @@ -19,7 +20,7 @@ public class WorldPropertiesExample implements Listener { false, WorldPropertyWidgetType.CHECKBOX, world -> false, (player, world, bool) -> { world.sendMessage(Component.text("Checkbox: " + bool)); // Do something with input - return true; // true to sync with client + return PropertyUpdateResult.UPDATE_AND_SYNC; // sync with client } ); @@ -30,7 +31,7 @@ public class WorldPropertiesExample implements Listener { world -> 4, (player, world, integer) -> { world.sendMessage(Component.text("Slider: " + integer)); // Do something with input - return true; // true to sync with client + return PropertyUpdateResult.UPDATE_AND_SYNC; // sync with client } ); @@ -41,7 +42,7 @@ public class WorldPropertiesExample implements Listener { world -> "Hello", (player, world, string) -> { world.sendMessage(Component.text("Textbox: " + string)); // Do something with input - return true; // true to sync with client + return PropertyUpdateResult.UPDATE_AND_SYNC; // sync with client } ); @@ -52,7 +53,7 @@ public class WorldPropertiesExample implements Listener { world -> null, (player, world, unit) -> { world.sendMessage(Component.text("Button pressed")); // Do something with input - return true; // true to sync with client + return PropertyUpdateResult.UPDATE_AND_SYNC; // sync with client } ); diff --git a/src/main/java/com/moulberry/axiom/packet/SetWorldPropertyListener.java b/src/main/java/com/moulberry/axiom/packet/SetWorldPropertyListener.java index 35c70c1..a6b4691 100644 --- a/src/main/java/com/moulberry/axiom/packet/SetWorldPropertyListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SetWorldPropertyListener.java @@ -24,30 +24,39 @@ public class SetWorldPropertyListener implements PluginMessageListener { return; } - // Call modify world - if (!this.plugin.canModifyWorld(player, player.getWorld())) { - return; - } - - // Don't allow on plot worlds - if (PlotSquaredIntegration.isPlotWorld(player.getWorld())) { - return; - } - FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message)); ResourceLocation id = friendlyByteBuf.readResourceLocation(); int type = friendlyByteBuf.readVarInt(); byte[] data = friendlyByteBuf.readByteArray(); int updateId = friendlyByteBuf.readVarInt(); + // Call modify world + if (!this.plugin.canModifyWorld(player, player.getWorld())) { + sendAck(player, updateId); + return; + } + + // Don't allow on plot worlds + if (PlotSquaredIntegration.isPlotWorld(player.getWorld())) { + sendAck(player, updateId); + return; + } + ServerWorldPropertiesRegistry registry = AxiomPaper.PLUGIN.getOrCreateWorldProperties(player.getWorld()); - if (registry == null) return; + if (registry == null) { + sendAck(player, updateId); + return; + } ServerWorldPropertyHolder property = registry.getById(id); if (property != null && property.getType().getTypeId() == type) { property.update(player, player.getWorld(), data); } + sendAck(player, updateId); + } + + private void sendAck(Player player, int updateId) { FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); buf.writeVarInt(updateId); diff --git a/src/main/java/com/moulberry/axiom/world_properties/PropertyUpdateHandler.java b/src/main/java/com/moulberry/axiom/world_properties/PropertyUpdateHandler.java index de754f7..de081f3 100644 --- a/src/main/java/com/moulberry/axiom/world_properties/PropertyUpdateHandler.java +++ b/src/main/java/com/moulberry/axiom/world_properties/PropertyUpdateHandler.java @@ -1,5 +1,6 @@ package com.moulberry.axiom.world_properties; +import com.moulberry.axiom.world_properties.server.PropertyUpdateResult; import org.bukkit.World; import org.bukkit.entity.Player; @@ -10,8 +11,8 @@ public interface PropertyUpdateHandler { * @param player the player that updated the property * @param world the world for which the property has been updated * @param value the new value of the property - * @return whether to sync the value back to the client + * @return {@link PropertyUpdateResult} */ - boolean update(Player player, World world, T value); + PropertyUpdateResult update(Player player, World world, T value); } diff --git a/src/main/java/com/moulberry/axiom/world_properties/server/PropertyUpdateResult.java b/src/main/java/com/moulberry/axiom/world_properties/server/PropertyUpdateResult.java new file mode 100644 index 0000000..01072ec --- /dev/null +++ b/src/main/java/com/moulberry/axiom/world_properties/server/PropertyUpdateResult.java @@ -0,0 +1,25 @@ +package com.moulberry.axiom.world_properties.server; + +public enum PropertyUpdateResult { + + UPDATE_AND_SYNC(true, true), + UPDATE_WITHOUT_SYNC(true, false), + CANCEL(false, false); + + private final boolean update; + private final boolean sync; + + PropertyUpdateResult(boolean update, boolean sync) { + this.update = update; + this.sync = sync; + } + + public boolean isUpdate() { + return this.update; + } + + public boolean isSync() { + return this.sync; + } + +} 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 3f3b5b5..78fef4f 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 @@ -67,16 +67,19 @@ public class ServerWorldPropertiesRegistry { private static final ServerWorldProperty TIME = new ServerWorldProperty<>( new NamespacedKey("axiom", "time"), "axiom.editorui.window.world_properties.time", - true, WorldPropertyWidgetType.TIME, world -> 0, (player, w, integer) -> false + true, WorldPropertyWidgetType.TIME, world -> 0, + (player, w, integer) -> PropertyUpdateResult.UPDATE_WITHOUT_SYNC ); public static final ServerWorldProperty PAUSE_WEATHER = new ServerWorldProperty<>( - new NamespacedKey("axiom", "pause_weather"), - "axiom.editorui.window.world_properties.pause_weather", - true, WorldPropertyWidgetType.CHECKBOX, world -> !world.getGameRuleValue(GameRule.DO_WEATHER_CYCLE), (player, world, bool) -> { - world.setGameRule(GameRule.DO_WEATHER_CYCLE, !bool); - return false; - }); + new NamespacedKey("axiom", "pause_weather"), + "axiom.editorui.window.world_properties.pause_weather", + true, WorldPropertyWidgetType.CHECKBOX, world -> !world.getGameRuleValue(GameRule.DO_WEATHER_CYCLE), + (player, world, bool) -> { + world.setGameRule(GameRule.DO_WEATHER_CYCLE, !bool); + return PropertyUpdateResult.UPDATE_WITHOUT_SYNC; + } + ); private static final ServerWorldProperty WEATHER_TYPE = new ServerWorldProperty<>( new NamespacedKey("axiom", "weather_type"), @@ -92,7 +95,7 @@ public class ServerWorldPropertiesRegistry { } else if (index == 2) { serverLevel.setWeatherParameters(0, ServerLevel.THUNDER_DURATION.sample(serverLevel.random), true, true); } - return false; + return PropertyUpdateResult.UPDATE_WITHOUT_SYNC; }); public void registerDefault() { 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 ea69352..b38b2e6 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 @@ -26,7 +26,7 @@ public class ServerWorldProperty extends ServerWorldPropertyBase { } @Override - public boolean handleUpdateProperty(Player player, World world, T value) { + public PropertyUpdateResult 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 index ee3690a..802d9c8 100644 --- a/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldPropertyBase.java +++ b/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldPropertyBase.java @@ -25,7 +25,7 @@ public abstract class ServerWorldPropertyBase { public abstract T getDefaultValue(World world); - public abstract boolean handleUpdateProperty(Player player, World world, T value); + public abstract PropertyUpdateResult handleUpdateProperty(Player player, World world, T value); public ResourceLocation getId() { return this.id; 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 d092512..69cf77d 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 @@ -35,8 +35,12 @@ public class ServerWorldPropertyHolder { } public void update(Player player, World world, byte[] data) { - this.value = this.property.widget.dataType().deserialize(data); - if (this.property.handleUpdateProperty(player, world, this.value)) { + PropertyUpdateResult result = this.property.handleUpdateProperty(player, world, this.value); + + if (result.isUpdate()) { + this.value = this.property.widget.dataType().deserialize(data); + } + if (result.isSync()) { this.sync(world); } }