Jump to content

Recommended Posts

Posted (edited)

Hello everyone

So my issue is here :

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

        // player.containerMenu.broadcastChanges(); with and without, same issue

        this.minecraft.setScreen(previous);
  }

i have this block of logic, the thing is it does the trick "visually" but instantly when i click on a slot everything goes back as first, i tried also to use player.containerMenu.broadcastChanges(); but it didnt help too :(

what should i use from AbstractContainer or something to be able the sync those values in server side thanks a lot in advance

Edited by RatCommit
Forgot tags
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.