Skip to content

Commit

Permalink
fix: add some additional sync handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus8448 committed Jul 11, 2024
1 parent 935c477 commit 6384adc
Show file tree
Hide file tree
Showing 9 changed files with 401 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,13 @@ protected void setActiveRecipe(@Nullable RecipeHolder<R> recipe) {
}

@Override
protected void saveAdditional(@NotNull CompoundTag tag, HolderLookup.Provider lookup) {
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider lookup) {
super.saveAdditional(tag, lookup);
tag.putInt(Constant.Nbt.PROGRESS, this.getProgress());
}

@Override
public void loadAdditional(@NotNull CompoundTag tag, HolderLookup.Provider lookup) {
public void loadAdditional(CompoundTag tag, HolderLookup.Provider lookup) {
super.loadAdditional(tag, lookup);
this.progress = tag.getInt(Constant.Nbt.PROGRESS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class MachineMenu<Machine extends MachineBlockEntity> extends ConfiguredM
* Constructs a new menu for a machine.
* Called on the logical server
*
* @param type The type of menu this is.
* @param syncId The sync id for this menu.
* @param player The player who is interacting with this menu.
* @param machine The machine this menu is for.
Expand All @@ -82,10 +83,10 @@ public MachineMenu(MenuType<? extends MachineMenu<Machine>> type, int syncId, @N
* Called on the logical client.
* You must add the player inventory slots manually.
*
* @param type The type of menu this is.
* @param syncId The sync id for this menu.
* @param buf The synchronization buffer from the server.
* @param inventory The inventory of the player interacting with this menu.
* @param type The type of menu this is.
*/
protected MachineMenu(MenuType<? extends MachineMenu<Machine>> type, int syncId, @NotNull Inventory inventory, @NotNull RegistryFriendlyByteBuf buf) {
super(type, syncId, inventory, buf);
Expand Down
78 changes: 69 additions & 9 deletions src/main/java/dev/galacticraft/machinelib/api/menu/MenuData.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@

package dev.galacticraft.machinelib.api.menu;

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

/**
* Registers a byte field to be synchronized.
*
* @param getter provides the current value of the data
* @param setter sets the value of the data
*/
public void registerBoolean(BooleanSupplier getter, BooleanConsumer setter) {
this.register(new BytePacketSerializable(() -> getter.getAsBoolean() ? 1 : 0, b -> setter.accept(b != 0)));
}

/**
* Registers a byte field to be synchronized.
*
* @param getter provides the current value of the data
* @param setter sets the value of the data
*/
public void registerByte(IntSupplier getter, IntConsumer setter) {
this.register(new BytePacketSerializable(getter, setter));
}

/**
* Registers a short field to be synchronized.
*
* @param getter provides the current value of the data
* @param setter sets the value of the data
*/
public void registerShort(IntSupplier getter, IntConsumer setter) {
this.register(new ShortPacketSerializable(getter, setter));
}

/**
* Registers an integer field to be synchronized.
*
Expand All @@ -88,6 +118,36 @@ public void registerInt(IntSupplier getter, IntConsumer setter) {
this.register(new IntPacketSerializable(getter, setter));
}

/**
* Registers a long field to be synchronized.
*
* @param getter provides the current value of the data
* @param setter sets the value of the data
*/
public void registerLong(LongSupplier getter, LongConsumer setter) {
this.register(new LongPacketSerializable(getter, setter));
}

/**
* Registers a float field to be synchronized.
*
* @param getter provides the current value of the data
* @param setter sets the value of the data
*/
public void registerFloat(FloatSupplier getter, FloatConsumer setter) {
this.register(new FloatPacketSerializable(getter, setter));
}

/**
* Registers a double field to be synchronized.
*
* @param getter provides the current value of the data
* @param setter sets the value of the data
*/
public void registerDouble(DoubleSupplier getter, DoubleConsumer setter) {
this.register(new DoublePacketSerializable(getter, setter));
}

/**
* Registers an enum field to be synchronized.
*
Expand All @@ -101,13 +161,13 @@ public <E extends Enum<E>> void registerEnum(E[] world, Supplier<E> getter, Cons
}

/**
* Registers a long field to be synchronized.
* Registers an array of booleans to be synchronized.
*
* @param getter provides the current value of the data
* @param setter sets the value of the data
* @param source the source array
* @param dest the destination array
*/
public void registerLong(LongSupplier getter, LongConsumer setter) {
this.register(new LongPacketSerializable(getter, setter));
public void registerBits(int len, boolean[] source, boolean[] dest) {
this.register(new BitsPacketSerializable(len, source, dest));
}

@ApiStatus.Internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @param <I> The type of storage the recipe uses
* @see MachineMenu
*/
public abstract class RecipeMachineMenu<I extends RecipeInput, R extends Recipe<I>, Machine extends RecipeMachineBlockEntity<I, R>> extends MachineMenu<Machine> {
public class RecipeMachineMenu<I extends RecipeInput, R extends Recipe<I>, Machine extends RecipeMachineBlockEntity<I, R>> extends MachineMenu<Machine> {
/**
* The amount of progress the machine has made in crafting a recipe.
* Counts from zero to {@link #maxProgress}, if {@link #maxProgress} > 0.
Expand All @@ -55,6 +55,7 @@ public abstract class RecipeMachineMenu<I extends RecipeInput, R extends Recipe<
/**
* Constructs a new recipe menu.
*
* @param type The type of machine associated with this menu.
* @param syncId The sync id for this menu.
* @param player The player who is interacting with this menu.
* @param machine The machine this menu is for.
Expand All @@ -66,15 +67,29 @@ public RecipeMachineMenu(MenuType<? extends RecipeMachineMenu<I, R, Machine>> ty
/**
* Constructs a new recipe menu for a machine.
*
* @param type The type of machine associated with this menu.
* @param syncId The sync id for this menu.
* @param inventory The inventory of the player interacting with this menu.
* @param buf The data buffer containing the information needed to initialize the menu.
* @param type The type of machine associated with this menu.
*/
protected RecipeMachineMenu(MenuType<? extends RecipeMachineMenu<I, R, Machine>> type, int syncId, @NotNull Inventory inventory, @NotNull RegistryFriendlyByteBuf buf) {
super(type, syncId, inventory, buf);
}

/**
* Constructs a new recipe menu for a machine.
*
* @param type The type of machine associated with this menu.
* @param syncId The sync id for this menu.
* @param inventory The inventory of the player interacting with this menu.
* @param buf The data buffer containing the information needed to initialize the menu.
* @param invX The x position of the inventory in the menu.
* @param invY The y position of the inventory in the menu.
*/
public RecipeMachineMenu(MenuType<? extends RecipeMachineMenu<I, R, Machine>> type, int syncId, @NotNull Inventory inventory, @NotNull RegistryFriendlyByteBuf buf, int invX, int invY) {
super(type, syncId, inventory, buf, invX, invY);
}

@Override
public void registerData(@NotNull MenuData data) {
super.registerData(data);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2021-2024 Team Galacticraft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package dev.galacticraft.machinelib.impl.menu.sync;

import dev.galacticraft.machinelib.api.misc.DeltaPacketSerializable;
import io.netty.buffer.ByteBuf;
import org.jetbrains.annotations.NotNull;

public record BitsPacketSerializable(int len, boolean[] source, boolean[] dest) implements DeltaPacketSerializable<ByteBuf, boolean[]> {
public BitsPacketSerializable {
assert len == source.length;
assert len == dest.length;
}

@Override
public boolean hasChanged(boolean[] previous) {
for (int i = 0; i < this.len; i++) {
if (previous[i] != this.source[i]) {
return true;
}
}
return false;
}

@Override
public void copyInto(boolean[] other) {
System.arraycopy(this.source, 0, other, 0, this.len);
}

@Override
public void readPacket(@NotNull ByteBuf buf) {
int bytes = (this.len / 8) + 1;
for (int i = 0; i < bytes; i++) {
byte b = buf.readByte();
for (int j = 0; j < 8 && i * 8 + j < this.len; j++) {
this.dest[i * 8 + j] = (b & (0b1 << j)) != 0;
}
}
}

@Override
public void writePacket(@NotNull ByteBuf buf) {
int bytes = (this.len / 8) + 1;
for (int i = 0; i < bytes; i++) {
byte b = 0;
for (int j = 0; j < 8 && i * 8 + j < this.len; j++) {
if (this.source[i * 8 + j]) {
b |= (byte) (0b1 << j);
}
}
buf.writeByte(b);
}
}

@Override
public boolean[] createEquivalent() {
return new boolean[this.len];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2021-2024 Team Galacticraft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package dev.galacticraft.machinelib.impl.menu.sync;

import dev.galacticraft.machinelib.api.misc.DeltaPacketSerializable;
import io.netty.buffer.ByteBuf;
import org.jetbrains.annotations.NotNull;

import java.util.function.IntConsumer;
import java.util.function.IntSupplier;

public record BytePacketSerializable(IntSupplier getter,
IntConsumer setter) implements DeltaPacketSerializable<ByteBuf, int[]> {
@Override
public boolean hasChanged(int[] previous) {
return previous[0] != this.getter.getAsInt();
}

@Override
public void copyInto(int[] other) {
other[0] = this.getter.getAsInt();
}

@Override
public void readPacket(@NotNull ByteBuf buf) {
this.setter.accept(buf.readByte());
}

@Override
public void writePacket(@NotNull ByteBuf buf) {
buf.writeByte(this.getter.getAsInt());
}

@Override
public int[] createEquivalent() {
return new int[1];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2021-2024 Team Galacticraft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package dev.galacticraft.machinelib.impl.menu.sync;

import dev.galacticraft.machinelib.api.misc.DeltaPacketSerializable;
import io.netty.buffer.ByteBuf;
import org.jetbrains.annotations.NotNull;

import java.util.function.DoubleConsumer;
import java.util.function.DoubleSupplier;

public record DoublePacketSerializable(DoubleSupplier getter,
DoubleConsumer setter) implements DeltaPacketSerializable<ByteBuf, double[]> {
@Override
public boolean hasChanged(double[] previous) {
return previous[0] != this.getter.getAsDouble();
}

@Override
public void copyInto(double[] other) {
other[0] = this.getter.getAsDouble();
}

@Override
public void readPacket(@NotNull ByteBuf buf) {
this.setter.accept(buf.readDouble());
}

@Override
public void writePacket(@NotNull ByteBuf buf) {
buf.writeDouble(this.getter.getAsDouble());
}

@Override
public double[] createEquivalent() {
return new double[1];
}
}
Loading

0 comments on commit 6384adc

Please sign in to comment.