WorldProperties: Pass correct value to handleUpdateProperty, prevent excessive syncing with repeated call to setValue

Dieser Commit ist enthalten in:
Moulberry 2023-11-16 22:30:37 +08:00
Ursprung d4e950f996
Commit a8b4ee2a1d

Datei anzeigen

@ -16,6 +16,7 @@ public class ServerWorldPropertyHolder<T> {
private T value; private T value;
private ServerWorldPropertyBase<T> property; private ServerWorldPropertyBase<T> property;
private boolean unsyncedValue = false;
public ServerWorldPropertyHolder(T value, ServerWorldPropertyBase<T> property) { public ServerWorldPropertyHolder(T value, ServerWorldPropertyBase<T> property) {
this.value = value; this.value = value;
@ -35,10 +36,15 @@ public class ServerWorldPropertyHolder<T> {
} }
public void update(Player player, World world, byte[] data) { public void update(Player player, World world, byte[] data) {
PropertyUpdateResult result = this.property.handleUpdateProperty(player, world, this.value); T newValue = this.property.widget.dataType().deserialize(data);
PropertyUpdateResult result = this.property.handleUpdateProperty(player, world, newValue);
if (result.isUpdate()) { if (result.isUpdate()) {
this.value = this.property.widget.dataType().deserialize(data); this.value = newValue;
if (!result.isSync()) {
this.unsyncedValue = true;
}
} }
if (result.isSync()) { if (result.isSync()) {
this.sync(world); this.sync(world);
@ -50,9 +56,12 @@ public class ServerWorldPropertyHolder<T> {
} }
public void setValue(World world, T value) { public void setValue(World world, T value) {
boolean sync = this.unsyncedValue || !value.equals(this.value);
this.value = value; this.value = value;
if (sync) {
this.sync(world); this.sync(world);
} }
}
public void sync(World world) { public void sync(World world) {
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());