3
0
Mirror von https://github.com/Moulberry/AxiomPaperPlugin.git synchronisiert 2024-11-09 01:50:05 +01:00

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.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<Boolean> 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<Integer> 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<String> 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<Unit> 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
});

Datei anzeigen

@ -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());

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"),
"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<Boolean> 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) {

Datei anzeigen

@ -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<T> {
private final boolean localizeName;
private WorldPropertyWidgetType<T> widget;
private T value;
private Predicate<T> handler;
private PropertyUpdateHandler<T> handler;
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.name = name;
this.localizeName = localizeName;
@ -40,9 +41,9 @@ public class ServerWorldProperty<T> {
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);
}
}