Hello guys : Im trying something else to sync my inventory data with the server so i wanted to use the PacketDistributor to achieve that :
// ClientPayloadHandler.class
public class ClientPayloadHandler {
private static Logger LOGGER = LogUtils.getLogger();
public static void handleDataOnMain(final SplitData data, final IPayloadContext context) { LOGGER.debug("ClientPayloadHandler.handleDataOnMain data = {}", data);
}
}
//ServerPayloadHandler
public class ServerPayloadHandler {
private static Logger LOGGER = LogUtils.getLogger();
public static void handleDataOnMain(final SplitData data, final IPayloadContext context) { LOGGER.debug("ServerPayloadHandler.handleDataOnMain data = {}", data);
}
}
// My Custon Data.class
public record SplitData(int splitSize, int remainingSize) implements CustomPacketPayload {
public static final CustomPacketPayload.Type<SplitData> TYPE =
new CustomPacketPayload.Type<>(ResourceLocation.fromNamespaceAndPath("perfect_stack_splitter",
"split_data"));
public static final StreamCodec<ByteBuf, SplitData> STREAM_CODEC = StreamCodec.composite(ByteBufCodecs.VAR_INT,
SplitData::splitSize, ByteBufCodecs.VAR_INT, SplitData::remainingSize, SplitData::new);
@Override
public CustomPacketPayload.Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}
//Register event
@SubscribeEvent
public static void register(final RegisterPayloadHandlersEvent event) {
final PayloadRegistrar registrar = event.registrar("1");
registrar.playBidirectional(
SplitData.TYPE,
SplitData.STREAM_CODEC,
new DirectionalPayloadHandler<>(
ClientPayloadHandler::handleDataOnMain,
ServerPayloadHandler::handleDataOnMain
)
);
}
when i shoot this :
private void onConfirm() {
int maxStackSize = stack.getMaxStackSize();
int carriedStackSize = Math.min(slider.getValueInt(), maxStackSize);
int totalLeft = totalSize - carriedStackSize;
ItemStack carriedStack = new ItemStack(stack.getItem(), carriedStackSize);
carriedStack.setCount((int) slider.getValue());
player.containerMenu.setCarried(carriedStack);
stack.setCount(totalSize - carriedStackSize);
PacketDistributor.sendToServer(new SplitData(carriedStackSize, totalLeft));
this.minecraft.setScreen(previous);
}
im getting Payload perfect_stack_splitter:split_data may not be sent to the server!
did i miss something ?
my goal is i wanna persist the setCount ...etc im my inventory to the server