Jump to content

I need help with inventory syncing


QuantumSoul

Recommended Posts

Hey,

I don't know what to do:

I have a custom GUI which changes the content of the player's inventory.

I first tried sending a packet, but change was done in server and not in client.

I was then told using the container would not require packets, so that's what I've done but it was not done in server.

I finally combined both and I still have some weird item-disappearing bugs.

 

When I click in the GUI

Spoiler

                    if (buyRecipe(recipe, container.playerInv))
                    {
                        NetworkInit.CHANNEL.sendToServer(new BtcBuyPacket(recipe));
                        getMinecraft().displayGuiScreen(null);
                        return true;
                    }

 

BuyRecipe function

Spoiler

public static boolean buyRecipe(DarkWebRecipe recipe, PlayerInventory inv)
    {
        double allMoney = evaluateInventory(inv);
        int toPay = recipe.getPrice();
        if (allMoney >= toPay)
        {
            for(int i = 0; i < inv.getSizeInventory(); i++)
            {
                if(toPay <= 0)
                    break;

                ItemStack stack = inv.getStackInSlot(i);
                if(stack.getItem() instanceof BitcoinItem)
                {
                    int money = stack.getCount() * ((BitcoinItem) stack.getItem()).getValue();
                    toPay -= money;
                    inv.setInventorySlotContents(i, ItemStack.EMPTY);

                    if(toPay < 0)
                        for(ItemStack rem : getBitcoinStacks(-toPay))
                            inv.addItemStackToInventory(rem);
                }
            }

            inv.addItemStackToInventory(recipe.getCraftingResult(inv));
            return true;
        }

        return false;
    }

 

BtcBuyPacket

Spoiler

public class BtcBuyPacket
{
    private final ResourceLocation id;

    public BtcBuyPacket(DarkWebRecipe recipe)
    {
        this(recipe.getId());
    }

    public BtcBuyPacket(ResourceLocation recipeId)
    {
        id = recipeId;
    }

    public static void serialize(BtcBuyPacket packet, PacketBuffer buf)
    {
        buf.writeResourceLocation(packet.id);
    }

    public static BtcBuyPacket deserialize(PacketBuffer buf)
    {
        return new BtcBuyPacket(buf.readResourceLocation());
    }

    public static void handle(BtcBuyPacket packet, Supplier<NetworkEvent.Context> context)
    {
        context.get().enqueueWork(() ->
        {
            PlayerInventory inv = Objects.requireNonNull(context.get().getSender()).inventory;
            Optional<?> opt = inv.player.world.getRecipeManager().getRecipe(packet.id);
            if(opt.isPresent() && opt.get() instanceof DarkWebRecipe)
            {
                DarkWebRecipe recipe = (DarkWebRecipe) opt.get();
                buyRecipe(recipe, inv);
            }
        });

        context.get().setPacketHandled(true);
    }

 

 The container

Spoiler

public class ComputerContainer extends ProgrammerContainer
{
    private final ComputerTileEntity tileEntity;
    public final PlayerInventory playerInv;

    public ComputerContainer(int id, PlayerInventory playerInventory, PacketBuffer extraData)
    {
        this(id, playerInventory, (ComputerTileEntity) Minecraft.getInstance().world.getTileEntity(extraData.readBlockPos()));
    }

    public ComputerContainer(int id, PlayerInventory playerInventory, ComputerTileEntity tileEntity)
    {
        super(ContainerInit.COMPUTER.get(), id, ComputerTileEntity.SLOT_NUMBER);

        this.playerInv = playerInventory;
        this.tileEntity = tileEntity;
    }
}

 

 

Thanks in advance

Link to comment
Share on other sites

  • QuantumSoul changed the title to I need help with inventory syncing
1 hour ago, QuantumSoul said:

NetworkInit.CHANNEL.sendToServer(new BtcBuyPacket(recipe));

Isn't inventory data not synced to the CLIENT?

Edited by Novârch

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

Link to comment
Share on other sites

Just now, diesieben07 said:

How do you open your GUI?

Which kind of bugs do you see? Explain how to provoke the bugs.

I right click on my block, there's a button on the gui.

Well it adds the right items to my inventory, but when I drop them some don't come back in the inventory, others take half a second, and some come twice. I think the bug is because I call buyRecipe() twice.

I was told if I change the inventory in the container both client and server are updated but idk why it did only the job client side. That's why I also send a packet

Link to comment
Share on other sites

13 minutes ago, QuantumSoul said:

Well that's the point of this thread

Then why are you sending a packet to the server? Shouldn't you send a packet from the server to the client to sync the inventory?

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

Link to comment
Share on other sites

48 minutes ago, QuantumSoul said:

I right click on my block, there's a button on the gui.

what does this button do?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Can you just tell us what you want to acomplish? Helping you is much easier if we know what you want to do.

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

Link to comment
Share on other sites

1 hour ago, Draco18s said:

what does this button do?

 

3 hours ago, QuantumSoul said:

When I click in the GUI

  Hide contents


                    if (buyRecipe(recipe, container.playerInv))
                    {
                        NetworkInit.CHANNEL.sendToServer(new BtcBuyPacket(recipe));
                        getMinecraft().displayGuiScreen(null);
                        return true;
                    }

It buys an item using bitcoins you have in your inventory

Link to comment
Share on other sites

50 minutes ago, Novârch said:

Can you just tell us what you want to acomplish? Helping you is much easier if we know what you want to do.

Well there's a list of items, I click on it and it used bitcoins in the inventory to buy it. It add the item to the inventory as well as the remaining money

Link to comment
Share on other sites

7 minutes ago, QuantumSoul said:

Well there's a list of items, I click on it and it used bitcoins in the inventory to buy it. It add the item to the inventory as well as the remaining money

I would personally keep a static list of transactions and resolve the transactions in an event (likeServerTickEvent).

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

Link to comment
Share on other sites

1 hour ago, QuantumSoul said:

I pushed to github so you can see the problem:

https://github.com/BinaryQuantumSoul/BinaryMod

 

Take bitcoins in your inventory

Open a Computer, put a Dark Net and click load

Buy any item

 

Now drop the just bought item, and take it back, buy another one and mess around,

You'll see what I mean

I fixed it, I've made a pull request on GitHub if you're interested. I did exactly as I said you should (made a static transaction list and resolved it in ServerTickEvent). A comprehensive explanation of what you did wrong can be found in the pull request.

Edited by Novârch
  • Thanks 1

It's sad how much time mods spend saying "x is no longer supported on this forum. Please update to a modern version of Minecraft to receive support".

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



×
×
  • Create New...

Important Information

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