Jump to content

[SOLVED] 1.18.2 Items added through packet doesn't instantly show up on client


sfxprt2

Recommended Posts

Posted (edited)

SOLVED EDIT: The fix was to ensure AbstractContainerMenu#stillValid returned true IF the container/menu is being accessed. I guess it was some kind of sync race condition where the screen would be inactive but the container still active causing the packet to wait and ensure container was also closed. Since opening the inventory opens a new container that race condition would be met. Idk if its true just my guess

EDIT: Item instantly pops up if i open and then close the inventory.

I have a screen that sends a packet, that packet then calls a method that simply gives the player a gold ingot. In game the screen works fine and the packet does get sent but the item doesnt show up in my inventory. If I press Q the item will drop on the floor and when i pick it back up it still wont show in the inventory. The item usually shows up after 30-40 seconds or if i put it in a hopper/chest than back into my inventory.

ModMessages.sendToServer(new VillagerScreenC2S(1)); //send packet

public class VillagerScreenC2S {
    private int id;
    public VillagerScreenC2S(int rV) {
        this.id = rV;
    }

    public void encode(FriendlyByteBuf buffer) {
        buffer.writeInt(this.id);
    }

    public static VillagerScreenC2S decode(FriendlyByteBuf buffer) {
        return new VillagerScreenC2S(buffer.readInt());
    }

    public static void handle(VillagerScreenC2S msg, Supplier<NetworkEvent.Context> ctx) {
        AtomicBoolean success = new AtomicBoolean(false);
        ctx.get().enqueueWork(() -> {
            ServerPlayer p = ctx.get().getSender();
            if(msg.id == 1){
                DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> success.set(ClientAccess.villagerGetQuest(p)));
            }
        });
        ctx.get().setPacketHandled(success.get());
    }
}

public class ClientAccess {
    public static boolean villagerGetQuest(ServerPlayer p){
        AtomicBoolean success = new AtomicBoolean(false);
        p.addItem(new ItemStack(Items.GOLD_INGOT));
        return success.get();
    }
}

 

Edited by sfxprt2
Link to comment
Share on other sites

  • sfxprt2 changed the title to [SOLVED] 1.18.2 Items added through packet doesn't instantly show up on client

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.



×
×
  • Create New...

Important Information

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