Jump to content

[1.14.4] Forcing client to update mouse slot contents


_Bedrockbreaker_

Recommended Posts

I have a certain event called whenever a player ctrl+clicks two fluid tanks in an inventory to swap their contents. When the player does so, the tank they had in their mouse slot stays in their mouse slot, and the tank in the inventory/chest/whatever stays in the inventory (but their contents are swapped, obviously). However, the client visually looks like it has a duplicated chest-tank in the mouse, until you put it in a slot and pick it up again, by which it realizes something has changed and correctly shows the original mouse-tank with its contents swapped. (the chest-tank the entire time is always correct.) Is there a way to force the client to realize this earlier?

Professional Hot Garbage Programmer.

https://github.com/Bedrockbreaker/

Link to comment
Share on other sites

I guess I probably should've realized that beforehand.
I've simplified the code down to the bare minimum, (which still describes the exact behavior which I mentioned.)
I realize I'm probably doing something jank and not good, but I'm not sure what.
 

//Events.java
@SubscribeEvent
  public static void onContainerOpen(PlayerContainerEvent.Open event) {
  listener = new InventorySwap(event.getPlayer());
  event.getContainer().addListener(listener);
}

 

//InventorySwap.java
private PlayerEntity player;
private int slot;

public InventoryFluidSwap(PlayerEntity player) {
  this.player = player;
}

@Override
  public void sendSlotContents(Container containerToSend, int slotInd, ItemStack stack) {
  if (player.inventory.getItemStack().isEmpty() || stack.isEmpty()) return;
  // Makes sure that this doesn't fire its own event again.
  if (this.slot != slotInd) {
    this.slot = slotInd;
  } else {
    this.slot= -1;
    return;
  }
  containerToSend.putStackInSlot(slotInd, player.inventory.getItemStack());
  player.inventory.setItemStack(stack);
}

 

Professional Hot Garbage Programmer.

https://github.com/Bedrockbreaker/

Link to comment
Share on other sites

Aaand, that confirms my suspicion of really bad, jank code.

In Events.java, I subscribe to the open container event, and attach a new listener to the container (the "InventorySwap" listener). Inside that, I override the sendSlotContents method, which fires any time a slot in the container changes. Whenever the slot contents changes, I set the container slot to the mouse's new stack (which was the container's old stack), and the mouse's slot to the container's new stack (which was the mouse's old stack.) This effectively prevents me from picking anything up, since the stacks are put back to where they were.

I also save a copy of the changed container's slot id, because when I put the container's stack back to where it was, it fires the sendSlotContents event again, and I need to check to make sure that I'm not firing my code twice.

Since I'm obviously doing about nothing correctly, what should I be doing instead?

Professional Hot Garbage Programmer.

https://github.com/Bedrockbreaker/

Link to comment
Share on other sites

I'm not trying to. I know that the actual moving of the itemstacks should be server-side. 

I'm pretty sure that the PlayerContainerEvent is server-side, which should mean that the attached listener operates server-side as well (at least I think it is). As far as I've tested, the server thinks the items are correct, but the client hasn't been notified that the changes have occured, hence the visual glitch of supposed duplicated items. But upon updating their inventory, everything appears correctly.

...
After quickly adding a println, the PlayerContainerEvent is indeed server-side.

Professional Hot Garbage Programmer.

https://github.com/Bedrockbreaker/

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.



×
×
  • Create New...

Important Information

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