WorldProperties: Use PropertyUpdateHandler functional interface with Player and World

Dieser Commit ist enthalten in:
Moulberry 2023-11-16 17:48:04 +08:00
Ursprung 8c6fd853ab
Commit 1ce77ea5e0
5 geänderte Dateien mit 30 neuen und 15 gelöschten Zeilen

Datei anzeigen

@ -5,7 +5,6 @@ import com.moulberry.axiom.world_properties.WorldPropertyCategory;
import com.moulberry.axiom.world_properties.WorldPropertyWidgetType; import com.moulberry.axiom.world_properties.WorldPropertyWidgetType;
import com.moulberry.axiom.world_properties.server.ServerWorldProperty; import com.moulberry.axiom.world_properties.server.ServerWorldProperty;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Unit; import net.minecraft.util.Unit;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.World; import org.bukkit.World;
@ -20,32 +19,30 @@ public class WorldPropertiesExample implements Listener {
public void onCreateWorldProperties(AxiomCreateWorldPropertiesEvent event) { public void onCreateWorldProperties(AxiomCreateWorldPropertiesEvent event) {
WorldPropertyCategory category = new WorldPropertyCategory("Examples", false); WorldPropertyCategory category = new WorldPropertyCategory("Examples", false);
World world = event.getWorld();
ServerWorldProperty<Boolean> checkbox = new ServerWorldProperty<>(new NamespacedKey("axiom", "checkbox"), ServerWorldProperty<Boolean> checkbox = new ServerWorldProperty<>(new NamespacedKey("axiom", "checkbox"),
"Checkbox", "Checkbox",
false, WorldPropertyWidgetType.CHECKBOX, false, bool -> { false, WorldPropertyWidgetType.CHECKBOX, false, (player, world, bool) -> {
world.sendMessage(Component.text("Checkbox: " + bool)); // Do something with input world.sendMessage(Component.text("Checkbox: " + bool)); // Do something with input
return true; // true to sync with client return true; // true to sync with client
}); });
ServerWorldProperty<Integer> slider = new ServerWorldProperty<>(new NamespacedKey("axiom", "slider"), ServerWorldProperty<Integer> slider = new ServerWorldProperty<>(new NamespacedKey("axiom", "slider"),
"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 world.sendMessage(Component.text("Slider: " + integer)); // Do something with input
return true; // true to sync with client return true; // true to sync with client
}); });
ServerWorldProperty<String> textbox = new ServerWorldProperty<>(new NamespacedKey("axiom", "textbox"), ServerWorldProperty<String> textbox = new ServerWorldProperty<>(new NamespacedKey("axiom", "textbox"),
"Textbox", "Textbox",
false, WorldPropertyWidgetType.TEXTBOX, "Hello", string -> { false, WorldPropertyWidgetType.TEXTBOX, "Hello", (player, world, string) -> {
world.sendMessage(Component.text("Textbox: " + string)); // Do something with input world.sendMessage(Component.text("Textbox: " + string)); // Do something with input
return true; // true to sync with client return true; // true to sync with client
}); });
ServerWorldProperty<Unit> button = new ServerWorldProperty<>(new NamespacedKey("axiom", "button"), ServerWorldProperty<Unit> button = new ServerWorldProperty<>(new NamespacedKey("axiom", "button"),
"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 world.sendMessage(Component.text("Button pressed")); // Do something with input
return true; // true to sync with client return true; // true to sync with client
}); });

Datei anzeigen

@ -34,7 +34,7 @@ public class SetWorldPropertyListener implements PluginMessageListener {
ServerWorldProperty<?> property = registry.getById(id); ServerWorldProperty<?> property = registry.getById(id);
if (property != null && property.getType().getTypeId() == type) { if (property != null && property.getType().getTypeId() == type) {
property.update(player.getWorld(), data); property.update(player, player.getWorld(), data);
} }
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());

Datei anzeigen

@ -0,0 +1,17 @@
package com.moulberry.axiom.world_properties;
import org.bukkit.World;
import org.bukkit.entity.Player;
@FunctionalInterface
public interface PropertyUpdateHandler<T> {
/**
* @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);
}

Datei anzeigen

@ -67,7 +67,7 @@ public class ServerWorldPropertiesRegistry {
ServerWorldProperty<Integer> time = new ServerWorldProperty<>(new NamespacedKey("axiom", "time"), ServerWorldProperty<Integer> time = new ServerWorldProperty<>(new NamespacedKey("axiom", "time"),
"axiom.editorui.window.world_properties.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)); this.addCategory(timeCategory, List.of(time));
@ -78,7 +78,7 @@ public class ServerWorldPropertiesRegistry {
ServerWorldProperty<Boolean> pauseWeather = new ServerWorldProperty<>(new NamespacedKey("axiom", "pause_weather"), ServerWorldProperty<Boolean> pauseWeather = new ServerWorldProperty<>(new NamespacedKey("axiom", "pause_weather"),
"axiom.editorui.window.world_properties.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); world.setGameRule(GameRule.DO_WEATHER_CYCLE, !bool);
return false; return false;
}); });
@ -87,7 +87,7 @@ public class ServerWorldPropertiesRegistry {
"axiom.editorui.window.world_properties.clear_weather", "axiom.editorui.window.world_properties.clear_weather",
true, new WorldPropertyWidgetType.ButtonArray( true, new WorldPropertyWidgetType.ButtonArray(
List.of("axiom.editorui.window.world_properties.rain_weather", "axiom.editorui.window.world_properties.thunder_weather") 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) { if (index == 0) {
serverLevel.setWeatherParameters(ServerLevel.RAIN_DELAY.sample(serverLevel.random), 0, false, false); serverLevel.setWeatherParameters(ServerLevel.RAIN_DELAY.sample(serverLevel.random), 0, false, false);
} else if (index == 1) { } else if (index == 1) {

Datei anzeigen

@ -1,6 +1,7 @@
package com.moulberry.axiom.world_properties.server; package com.moulberry.axiom.world_properties.server;
import com.moulberry.axiom.AxiomPaper; 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.WorldPropertyDataType;
import com.moulberry.axiom.world_properties.WorldPropertyWidgetType; import com.moulberry.axiom.world_properties.WorldPropertyWidgetType;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
@ -20,10 +21,10 @@ public class ServerWorldProperty<T> {
private final boolean localizeName; private final boolean localizeName;
private WorldPropertyWidgetType<T> widget; private WorldPropertyWidgetType<T> widget;
private T value; private T value;
private Predicate<T> handler; private PropertyUpdateHandler<T> handler;
public ServerWorldProperty(NamespacedKey id, String name, boolean localizeName, WorldPropertyWidgetType<T> widget, public ServerWorldProperty(NamespacedKey id, String name, boolean localizeName, WorldPropertyWidgetType<T> widget,
T value, Predicate<T> handler) { T value, PropertyUpdateHandler<T> handler) {
this.id = CraftNamespacedKey.toMinecraft(id); this.id = CraftNamespacedKey.toMinecraft(id);
this.name = name; this.name = name;
this.localizeName = localizeName; this.localizeName = localizeName;
@ -40,9 +41,9 @@ public class ServerWorldProperty<T> {
return this.widget.dataType(); 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); this.value = this.widget.dataType().deserialize(data);
if (this.handler.test(this.value)) { if (this.handler.update(player, world, this.value)) {
this.sync(world); this.sync(world);
} }
} }