3
0
Mirror von https://github.com/Moulberry/AxiomPaperPlugin.git synchronisiert 2024-09-29 16:00:04 +02:00

Add PropertyUpdateResult enum, adding possible 'cancel' action to prevent value write

Dieser Commit ist enthalten in:
Moulberry 2023-11-16 20:10:55 +08:00
Ursprung 970678f8b1
Commit d4e950f996
8 geänderte Dateien mit 72 neuen und 29 gelöschten Zeilen

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

@ -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<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
* @return {@link PropertyUpdateResult}
*/
boolean update(Player player, World world, T value);
PropertyUpdateResult update(Player player, World world, T value);
}

Datei anzeigen

@ -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;
}
}

Datei anzeigen

@ -67,16 +67,19 @@ public class ServerWorldPropertiesRegistry {
private static final ServerWorldProperty<Integer> 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<Boolean> 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) -> {
true, WorldPropertyWidgetType.CHECKBOX, world -> !world.getGameRuleValue(GameRule.DO_WEATHER_CYCLE),
(player, world, bool) -> {
world.setGameRule(GameRule.DO_WEATHER_CYCLE, !bool);
return false;
});
return PropertyUpdateResult.UPDATE_WITHOUT_SYNC;
}
);
private static final ServerWorldProperty<Integer> 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() {

Datei anzeigen

@ -26,7 +26,7 @@ public class ServerWorldProperty<T> extends ServerWorldPropertyBase<T> {
}
@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);
}

Datei anzeigen

@ -25,7 +25,7 @@ public abstract class ServerWorldPropertyBase<T> {
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;

Datei anzeigen

@ -35,8 +35,12 @@ public class ServerWorldPropertyHolder<T> {
}
public void update(Player player, World world, byte[] data) {
PropertyUpdateResult result = this.property.handleUpdateProperty(player, world, this.value);
if (result.isUpdate()) {
this.value = this.property.widget.dataType().deserialize(data);
if (this.property.handleUpdateProperty(player, world, this.value)) {
}
if (result.isSync()) {
this.sync(world);
}
}