diff --git a/src/main/java/com/moulberry/axiom/WorldPropertiesExample.java b/src/main/java/com/moulberry/axiom/WorldPropertiesExample.java index e1a8b72..080fa9e 100644 --- a/src/main/java/com/moulberry/axiom/WorldPropertiesExample.java +++ b/src/main/java/com/moulberry/axiom/WorldPropertiesExample.java @@ -5,7 +5,6 @@ import com.moulberry.axiom.world_properties.WorldPropertyCategory; import com.moulberry.axiom.world_properties.WorldPropertyWidgetType; import com.moulberry.axiom.world_properties.server.ServerWorldProperty; import net.kyori.adventure.text.Component; -import net.minecraft.resources.ResourceLocation; import net.minecraft.util.Unit; import org.bukkit.NamespacedKey; import org.bukkit.World; @@ -20,32 +19,30 @@ public class WorldPropertiesExample implements Listener { public void onCreateWorldProperties(AxiomCreateWorldPropertiesEvent event) { WorldPropertyCategory category = new WorldPropertyCategory("Examples", false); - World world = event.getWorld(); - ServerWorldProperty checkbox = new ServerWorldProperty<>(new NamespacedKey("axiom", "checkbox"), "Checkbox", - false, WorldPropertyWidgetType.CHECKBOX, false, bool -> { + false, WorldPropertyWidgetType.CHECKBOX, false, (player, world, bool) -> { world.sendMessage(Component.text("Checkbox: " + bool)); // Do something with input return true; // true to sync with client }); ServerWorldProperty slider = new ServerWorldProperty<>(new NamespacedKey("axiom", "slider"), "Slider", - false, new WorldPropertyWidgetType.Slider(0, 8), 4, integer -> { + false, new WorldPropertyWidgetType.Slider(0, 8), 4, (player, world, integer) -> { world.sendMessage(Component.text("Slider: " + integer)); // Do something with input return true; // true to sync with client }); ServerWorldProperty textbox = new ServerWorldProperty<>(new NamespacedKey("axiom", "textbox"), "Textbox", - false, WorldPropertyWidgetType.TEXTBOX, "Hello", string -> { + false, WorldPropertyWidgetType.TEXTBOX, "Hello", (player, world, string) -> { world.sendMessage(Component.text("Textbox: " + string)); // Do something with input return true; // true to sync with client }); ServerWorldProperty button = new ServerWorldProperty<>(new NamespacedKey("axiom", "button"), "Button", - false, WorldPropertyWidgetType.BUTTON, Unit.INSTANCE, unit -> { + false, WorldPropertyWidgetType.BUTTON, Unit.INSTANCE, (player, world, unit) -> { world.sendMessage(Component.text("Button pressed")); // Do something with input return true; // true to 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 d9a4ce8..f1166ec 100644 --- a/src/main/java/com/moulberry/axiom/packet/SetWorldPropertyListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SetWorldPropertyListener.java @@ -34,7 +34,7 @@ public class SetWorldPropertyListener implements PluginMessageListener { ServerWorldProperty property = registry.getById(id); if (property != null && property.getType().getTypeId() == type) { - property.update(player.getWorld(), data); + property.update(player, player.getWorld(), data); } FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); diff --git a/src/main/java/com/moulberry/axiom/world_properties/PropertyUpdateHandler.java b/src/main/java/com/moulberry/axiom/world_properties/PropertyUpdateHandler.java new file mode 100644 index 0000000..de754f7 --- /dev/null +++ b/src/main/java/com/moulberry/axiom/world_properties/PropertyUpdateHandler.java @@ -0,0 +1,17 @@ +package com.moulberry.axiom.world_properties; + +import org.bukkit.World; +import org.bukkit.entity.Player; + +@FunctionalInterface +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 + */ + boolean update(Player player, World world, T value); + +} 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 cb15a52..b19b703 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,7 +67,7 @@ public class ServerWorldPropertiesRegistry { ServerWorldProperty time = new ServerWorldProperty<>(new NamespacedKey("axiom", "time"), "axiom.editorui.window.world_properties.time", - true, WorldPropertyWidgetType.TIME, 0, integer -> false + true, WorldPropertyWidgetType.TIME, 0, (player, w, integer) -> false ); this.addCategory(timeCategory, List.of(time)); @@ -78,7 +78,7 @@ public class ServerWorldPropertiesRegistry { ServerWorldProperty pauseWeather = new ServerWorldProperty<>(new NamespacedKey("axiom", "pause_weather"), "axiom.editorui.window.world_properties.pause_weather", - true, WorldPropertyWidgetType.CHECKBOX, !world.getGameRuleValue(GameRule.DO_WEATHER_CYCLE), bool -> { + true, WorldPropertyWidgetType.CHECKBOX, !world.getGameRuleValue(GameRule.DO_WEATHER_CYCLE), (player, w, bool) -> { world.setGameRule(GameRule.DO_WEATHER_CYCLE, !bool); return false; }); @@ -87,7 +87,7 @@ public class ServerWorldPropertiesRegistry { "axiom.editorui.window.world_properties.clear_weather", true, new WorldPropertyWidgetType.ButtonArray( List.of("axiom.editorui.window.world_properties.rain_weather", "axiom.editorui.window.world_properties.thunder_weather") - ), 0, index -> { + ), 0, (player, w, index) -> { if (index == 0) { serverLevel.setWeatherParameters(ServerLevel.RAIN_DELAY.sample(serverLevel.random), 0, false, false); } else if (index == 1) { 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 0d44f90..0a2cbda 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,6 +1,7 @@ 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 io.netty.buffer.Unpooled; @@ -20,10 +21,10 @@ public class ServerWorldProperty { private final boolean localizeName; private WorldPropertyWidgetType widget; private T value; - private Predicate handler; + private PropertyUpdateHandler handler; public ServerWorldProperty(NamespacedKey id, String name, boolean localizeName, WorldPropertyWidgetType widget, - T value, Predicate handler) { + T value, PropertyUpdateHandler handler) { this.id = CraftNamespacedKey.toMinecraft(id); this.name = name; this.localizeName = localizeName; @@ -40,9 +41,9 @@ public class ServerWorldProperty { return this.widget.dataType(); } - public void update(World world, byte[] data) { + public void update(Player player, World world, byte[] data) { this.value = this.widget.dataType().deserialize(data); - if (this.handler.test(this.value)) { + if (this.handler.update(player, world, this.value)) { this.sync(world); } }