Jump to content

Inventory slot syncing


RatCommit

Recommended Posts

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • https://drive.google.com/file/d/1UwJgcjIeH085xo08zXOvu3gC-JyGMntA/view?usp=sharing   Forge 47.3.0, ver: 1.20.1  
    • What version are you on? Look up on Global Loot Modifiers
    • I have found a rather simple solution to all this after actually combing through the source codes of Mr Crayfishs Gun Mod and Timeless and Classics Guns Zero. They execute the sounds and particles not in the custom packet class itself but externally. You simply create another class (put it anywhere in your project in main), create a method and put in the desired sounds and particles or whatever you want in it. Create multiple methods for each individual task if you need to. You execute your code in the C2S Packet as follows: public class SpittingC2SPacket { public SpittingC2SPacket(){ } public SpittingC2SPacket(FriendlyByteBuf buf){ } public void toBytes(FriendlyByteBuf buf){ } public boolean handle(Supplier<NetworkEvent.Context> supplier){ NetworkEvent.Context context = supplier.get(); context.enqueueWork(()-> { ServerPlayer player = context.getSender(); ServerLevel sevel = player.getLevel(); RandomSource rdm = RandomSource.create(); //Entity SpuckeEntity spit = new SpitEntity(sevel, player); float r = (float)rdm.nextInt(3500,5000)/10000; float y = player.getYRot(); float x = player.getXRot(); float z = 0f; spucke.shootFromRotation(player, x, y, z, r, 1f); sevel.addFreshEntity(spit); //External handler ///////////////////////////////////////////////////////////////// ServerPlayHandler.handleSpitting(player); ///////////////////////////////////////////////////////////////// }); return true; } } And your external handler class can look like this: public class ServerPlayHandler { public static void handleSpitting(ServerPlayer player){ if(player.isSpectator()) return; Level lvl = player.level; //Sounds double posX = player.getX(); double posY = player.getY() + player.getEyeHeight(); double posZ = player.getZ(); float r = 0.8f + lvl.random.nextFloat() * 0.3f; lvl.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.LLAMA_SPIT, SoundSource.BLOCKS, 1f, r); //Particles Vec3 vec3 = player.getViewVector(1f); Vec3 MausPos = player.getEyePosition(); Vec3 SchauWinkel = player.getLookAngle(); double x = player.getX() + vec3.x/4; double y = MausPos.y + vec3.y/4; double z = player.getZ() + vec3.z/4; if(lvl instanceof ServerLevel slevel) { slevel.sendParticles(ParticleTypes.SPIT, x, y, z, 3, 0d, 0d, 0d, 0.15d); } } } It works now. On client as well as on a server
    • 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
  • Topics

×
×
  • Create New...

Important Information

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