Skip to content

Commit 6384adc

Browse files
committed
fix: add some additional sync handlers
1 parent 935c477 commit 6384adc

9 files changed

+401
-14
lines changed

src/main/java/dev/galacticraft/machinelib/api/block/entity/RecipeMachineBlockEntity.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,13 @@ protected void setActiveRecipe(@Nullable RecipeHolder<R> recipe) {
302302
}
303303

304304
@Override
305-
protected void saveAdditional(@NotNull CompoundTag tag, HolderLookup.Provider lookup) {
305+
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider lookup) {
306306
super.saveAdditional(tag, lookup);
307307
tag.putInt(Constant.Nbt.PROGRESS, this.getProgress());
308308
}
309309

310310
@Override
311-
public void loadAdditional(@NotNull CompoundTag tag, HolderLookup.Provider lookup) {
311+
public void loadAdditional(CompoundTag tag, HolderLookup.Provider lookup) {
312312
super.loadAdditional(tag, lookup);
313313
this.progress = tag.getInt(Constant.Nbt.PROGRESS);
314314
}

src/main/java/dev/galacticraft/machinelib/api/menu/MachineMenu.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class MachineMenu<Machine extends MachineBlockEntity> extends ConfiguredM
6161
* Constructs a new menu for a machine.
6262
* Called on the logical server
6363
*
64+
* @param type The type of menu this is.
6465
* @param syncId The sync id for this menu.
6566
* @param player The player who is interacting with this menu.
6667
* @param machine The machine this menu is for.
@@ -82,10 +83,10 @@ public MachineMenu(MenuType<? extends MachineMenu<Machine>> type, int syncId, @N
8283
* Called on the logical client.
8384
* You must add the player inventory slots manually.
8485
*
86+
* @param type The type of menu this is.
8587
* @param syncId The sync id for this menu.
8688
* @param buf The synchronization buffer from the server.
8789
* @param inventory The inventory of the player interacting with this menu.
88-
* @param type The type of menu this is.
8990
*/
9091
protected MachineMenu(MenuType<? extends MachineMenu<Machine>> type, int syncId, @NotNull Inventory inventory, @NotNull RegistryFriendlyByteBuf buf) {
9192
super(type, syncId, inventory, buf);

src/main/java/dev/galacticraft/machinelib/api/menu/MenuData.java

+69-9
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222

2323
package dev.galacticraft.machinelib.api.menu;
2424

25+
import dev.architectury.utils.value.FloatSupplier;
2526
import dev.galacticraft.machinelib.api.misc.DeltaPacketSerializable;
26-
import dev.galacticraft.machinelib.impl.menu.sync.EnumPacketSerializable;
27-
import dev.galacticraft.machinelib.impl.menu.sync.IntPacketSerializable;
28-
import dev.galacticraft.machinelib.impl.menu.sync.LongPacketSerializable;
29-
import dev.galacticraft.machinelib.impl.menu.sync.StreamCodecPacketSerializable;
27+
import dev.galacticraft.machinelib.impl.menu.sync.*;
28+
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
29+
import it.unimi.dsi.fastutil.floats.FloatConsumer;
3030
import net.minecraft.network.RegistryFriendlyByteBuf;
3131
import net.minecraft.network.codec.StreamCodec;
3232
import org.jetbrains.annotations.ApiStatus;
@@ -78,6 +78,36 @@ public <T> void register(StreamCodec<? super RegistryFriendlyByteBuf, T> codec,
7878
this.register(new StreamCodecPacketSerializable<>(codec, getter, setter));
7979
}
8080

81+
/**
82+
* Registers a byte field to be synchronized.
83+
*
84+
* @param getter provides the current value of the data
85+
* @param setter sets the value of the data
86+
*/
87+
public void registerBoolean(BooleanSupplier getter, BooleanConsumer setter) {
88+
this.register(new BytePacketSerializable(() -> getter.getAsBoolean() ? 1 : 0, b -> setter.accept(b != 0)));
89+
}
90+
91+
/**
92+
* Registers a byte field to be synchronized.
93+
*
94+
* @param getter provides the current value of the data
95+
* @param setter sets the value of the data
96+
*/
97+
public void registerByte(IntSupplier getter, IntConsumer setter) {
98+
this.register(new BytePacketSerializable(getter, setter));
99+
}
100+
101+
/**
102+
* Registers a short field to be synchronized.
103+
*
104+
* @param getter provides the current value of the data
105+
* @param setter sets the value of the data
106+
*/
107+
public void registerShort(IntSupplier getter, IntConsumer setter) {
108+
this.register(new ShortPacketSerializable(getter, setter));
109+
}
110+
81111
/**
82112
* Registers an integer field to be synchronized.
83113
*
@@ -88,6 +118,36 @@ public void registerInt(IntSupplier getter, IntConsumer setter) {
88118
this.register(new IntPacketSerializable(getter, setter));
89119
}
90120

121+
/**
122+
* Registers a long field to be synchronized.
123+
*
124+
* @param getter provides the current value of the data
125+
* @param setter sets the value of the data
126+
*/
127+
public void registerLong(LongSupplier getter, LongConsumer setter) {
128+
this.register(new LongPacketSerializable(getter, setter));
129+
}
130+
131+
/**
132+
* Registers a float field to be synchronized.
133+
*
134+
* @param getter provides the current value of the data
135+
* @param setter sets the value of the data
136+
*/
137+
public void registerFloat(FloatSupplier getter, FloatConsumer setter) {
138+
this.register(new FloatPacketSerializable(getter, setter));
139+
}
140+
141+
/**
142+
* Registers a double field to be synchronized.
143+
*
144+
* @param getter provides the current value of the data
145+
* @param setter sets the value of the data
146+
*/
147+
public void registerDouble(DoubleSupplier getter, DoubleConsumer setter) {
148+
this.register(new DoublePacketSerializable(getter, setter));
149+
}
150+
91151
/**
92152
* Registers an enum field to be synchronized.
93153
*
@@ -101,13 +161,13 @@ public <E extends Enum<E>> void registerEnum(E[] world, Supplier<E> getter, Cons
101161
}
102162

103163
/**
104-
* Registers a long field to be synchronized.
164+
* Registers an array of booleans to be synchronized.
105165
*
106-
* @param getter provides the current value of the data
107-
* @param setter sets the value of the data
166+
* @param source the source array
167+
* @param dest the destination array
108168
*/
109-
public void registerLong(LongSupplier getter, LongConsumer setter) {
110-
this.register(new LongPacketSerializable(getter, setter));
169+
public void registerBits(int len, boolean[] source, boolean[] dest) {
170+
this.register(new BitsPacketSerializable(len, source, dest));
111171
}
112172

113173
@ApiStatus.Internal

src/main/java/dev/galacticraft/machinelib/api/menu/RecipeMachineMenu.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* @param <I> The type of storage the recipe uses
4141
* @see MachineMenu
4242
*/
43-
public abstract class RecipeMachineMenu<I extends RecipeInput, R extends Recipe<I>, Machine extends RecipeMachineBlockEntity<I, R>> extends MachineMenu<Machine> {
43+
public class RecipeMachineMenu<I extends RecipeInput, R extends Recipe<I>, Machine extends RecipeMachineBlockEntity<I, R>> extends MachineMenu<Machine> {
4444
/**
4545
* The amount of progress the machine has made in crafting a recipe.
4646
* Counts from zero to {@link #maxProgress}, if {@link #maxProgress} > 0.
@@ -55,6 +55,7 @@ public abstract class RecipeMachineMenu<I extends RecipeInput, R extends Recipe<
5555
/**
5656
* Constructs a new recipe menu.
5757
*
58+
* @param type The type of machine associated with this menu.
5859
* @param syncId The sync id for this menu.
5960
* @param player The player who is interacting with this menu.
6061
* @param machine The machine this menu is for.
@@ -66,15 +67,29 @@ public RecipeMachineMenu(MenuType<? extends RecipeMachineMenu<I, R, Machine>> ty
6667
/**
6768
* Constructs a new recipe menu for a machine.
6869
*
70+
* @param type The type of machine associated with this menu.
6971
* @param syncId The sync id for this menu.
7072
* @param inventory The inventory of the player interacting with this menu.
7173
* @param buf The data buffer containing the information needed to initialize the menu.
72-
* @param type The type of machine associated with this menu.
7374
*/
7475
protected RecipeMachineMenu(MenuType<? extends RecipeMachineMenu<I, R, Machine>> type, int syncId, @NotNull Inventory inventory, @NotNull RegistryFriendlyByteBuf buf) {
7576
super(type, syncId, inventory, buf);
7677
}
7778

79+
/**
80+
* Constructs a new recipe menu for a machine.
81+
*
82+
* @param type The type of machine associated with this menu.
83+
* @param syncId The sync id for this menu.
84+
* @param inventory The inventory of the player interacting with this menu.
85+
* @param buf The data buffer containing the information needed to initialize the menu.
86+
* @param invX The x position of the inventory in the menu.
87+
* @param invY The y position of the inventory in the menu.
88+
*/
89+
public RecipeMachineMenu(MenuType<? extends RecipeMachineMenu<I, R, Machine>> type, int syncId, @NotNull Inventory inventory, @NotNull RegistryFriendlyByteBuf buf, int invX, int invY) {
90+
super(type, syncId, inventory, buf, invX, invY);
91+
}
92+
7893
@Override
7994
public void registerData(@NotNull MenuData data) {
8095
super.registerData(data);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2021-2024 Team Galacticraft
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package dev.galacticraft.machinelib.impl.menu.sync;
24+
25+
import dev.galacticraft.machinelib.api.misc.DeltaPacketSerializable;
26+
import io.netty.buffer.ByteBuf;
27+
import org.jetbrains.annotations.NotNull;
28+
29+
public record BitsPacketSerializable(int len, boolean[] source, boolean[] dest) implements DeltaPacketSerializable<ByteBuf, boolean[]> {
30+
public BitsPacketSerializable {
31+
assert len == source.length;
32+
assert len == dest.length;
33+
}
34+
35+
@Override
36+
public boolean hasChanged(boolean[] previous) {
37+
for (int i = 0; i < this.len; i++) {
38+
if (previous[i] != this.source[i]) {
39+
return true;
40+
}
41+
}
42+
return false;
43+
}
44+
45+
@Override
46+
public void copyInto(boolean[] other) {
47+
System.arraycopy(this.source, 0, other, 0, this.len);
48+
}
49+
50+
@Override
51+
public void readPacket(@NotNull ByteBuf buf) {
52+
int bytes = (this.len / 8) + 1;
53+
for (int i = 0; i < bytes; i++) {
54+
byte b = buf.readByte();
55+
for (int j = 0; j < 8 && i * 8 + j < this.len; j++) {
56+
this.dest[i * 8 + j] = (b & (0b1 << j)) != 0;
57+
}
58+
}
59+
}
60+
61+
@Override
62+
public void writePacket(@NotNull ByteBuf buf) {
63+
int bytes = (this.len / 8) + 1;
64+
for (int i = 0; i < bytes; i++) {
65+
byte b = 0;
66+
for (int j = 0; j < 8 && i * 8 + j < this.len; j++) {
67+
if (this.source[i * 8 + j]) {
68+
b |= (byte) (0b1 << j);
69+
}
70+
}
71+
buf.writeByte(b);
72+
}
73+
}
74+
75+
@Override
76+
public boolean[] createEquivalent() {
77+
return new boolean[this.len];
78+
}
79+
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2021-2024 Team Galacticraft
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package dev.galacticraft.machinelib.impl.menu.sync;
24+
25+
import dev.galacticraft.machinelib.api.misc.DeltaPacketSerializable;
26+
import io.netty.buffer.ByteBuf;
27+
import org.jetbrains.annotations.NotNull;
28+
29+
import java.util.function.IntConsumer;
30+
import java.util.function.IntSupplier;
31+
32+
public record BytePacketSerializable(IntSupplier getter,
33+
IntConsumer setter) implements DeltaPacketSerializable<ByteBuf, int[]> {
34+
@Override
35+
public boolean hasChanged(int[] previous) {
36+
return previous[0] != this.getter.getAsInt();
37+
}
38+
39+
@Override
40+
public void copyInto(int[] other) {
41+
other[0] = this.getter.getAsInt();
42+
}
43+
44+
@Override
45+
public void readPacket(@NotNull ByteBuf buf) {
46+
this.setter.accept(buf.readByte());
47+
}
48+
49+
@Override
50+
public void writePacket(@NotNull ByteBuf buf) {
51+
buf.writeByte(this.getter.getAsInt());
52+
}
53+
54+
@Override
55+
public int[] createEquivalent() {
56+
return new int[1];
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2021-2024 Team Galacticraft
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package dev.galacticraft.machinelib.impl.menu.sync;
24+
25+
import dev.galacticraft.machinelib.api.misc.DeltaPacketSerializable;
26+
import io.netty.buffer.ByteBuf;
27+
import org.jetbrains.annotations.NotNull;
28+
29+
import java.util.function.DoubleConsumer;
30+
import java.util.function.DoubleSupplier;
31+
32+
public record DoublePacketSerializable(DoubleSupplier getter,
33+
DoubleConsumer setter) implements DeltaPacketSerializable<ByteBuf, double[]> {
34+
@Override
35+
public boolean hasChanged(double[] previous) {
36+
return previous[0] != this.getter.getAsDouble();
37+
}
38+
39+
@Override
40+
public void copyInto(double[] other) {
41+
other[0] = this.getter.getAsDouble();
42+
}
43+
44+
@Override
45+
public void readPacket(@NotNull ByteBuf buf) {
46+
this.setter.accept(buf.readDouble());
47+
}
48+
49+
@Override
50+
public void writePacket(@NotNull ByteBuf buf) {
51+
buf.writeDouble(this.getter.getAsDouble());
52+
}
53+
54+
@Override
55+
public double[] createEquivalent() {
56+
return new double[1];
57+
}
58+
}

0 commit comments

Comments
 (0)