Jump to content

[Solved] Desynchronization of ItemStacks


Recommended Posts

Hi, I'm struggling with automated villager trading. I've extended GuiMerchant, added some buttons and now I'm implementing actual trading.


Method that should do the trading is:


    void StartTrading()
    	IMerchant villager = getMerchant();
    	EntityPlayer player = villager.getCustomer();
    	MerchantRecipeList trades = villager.getRecipes(player);
    	MerchantRecipe trade = trades.get(0);
ItemStack input = trade.getItemToBuy();
ItemStack materials = getFirstAvailableItemStack(player, input);
if(materials == null)
ContainerMerchant merchant = ((ContainerMerchant)inventorySlots);
merchant.getMerchantInventory().setInventorySlotContents(0, materials);
    	inventorySlots.transferStackInSlot(player, 2);
    	inventorySlots.transferStackInSlot(player, 0);


Method works as expected but as soon as I click on newly acquired emeralds, all changes are reverted. I assume I'm missing something but I can't find any tutorial that would address this problem.


getFirstAvailableItemStack method in case you are interested:



    ItemStack getFirstAvailableItemStack(EntityPlayer player, ItemStack itemStack){

    	for(ItemStack stack:player.inventory.mainInventory){
    		if(stack != null){
    				return stack;
    	return null;

Link to comment
Share on other sites

You sre handling the inventory on the clients s8de of things it needs to be handled server side, aince you are trying to make it only client side you should use some of the default packets if there are any.


I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Yesterday I found a solution to this. I could either send appropriate packets to a server or I could simulate user clicks. I went for second option and it's as simple as



    	handleMouseClick(slot30, slot30.slotNumber, 0, ClickType.PICKUP);
    	handleMouseClick(slot30, slot30.slotNumber, 0, ClickType.PICKUP_ALL);
    	handleMouseClick(slot0, slot0.slotNumber, 0, ClickType.PICKUP);

    	handleMouseClick(slot2, slot2.slotNumber, 0, ClickType.QUICK_MOVE);

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.

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.