Jump to content

SonicScrew12

Members
  • Posts

    4
  • Joined

  • Last visited

Everything posted by SonicScrew12

  1. It appears I have fixed the issue by changing the NonNullList into an ArrayList. I have no idea why this worked, but it also allowed me to use ArrayList.equals() to compare the two inventories. It has something to do with Minecraft's NonNullList, but I don't know exactly what. Here's the code if anyone is interested: ArrayList<ItemStack> currentInventory = new ArrayList<>(e.player.inventory.mainInventory); if (!currentInventory.equals(previousInventory)) System.out.println("Inventory changed"); previousInventory = currentInventory; Thanks to DavidM for the insight
  2. Okay, I've modified the code to not use equals and instead use ItemStack's built-in comparing method NonNullList<ItemStack> currentInventory = e.player.inventory.mainInventory; for (int i = 0; i < currentInventory.size(); i++) { if (!ItemStack.areItemStacksEqual(currentInventory.get(i).getStack(), previousInventory.get(i).getStack())) { System.out.println("Inventory changed"); break; } } previousInventory = currentInventory; This still does not log the inventory change, because I've found that the previous inventory's contents and the current inventory's contents are always the same by logging it to the console.
  3. That makes sense. However, the problem that I ran into was that although I iterated through both inventories, their contents would still end up being the same, as demonstrated by the comment on my original post. This leads me to believe that something is happening on Forge or Minecraft’s end where one tick isn’t actually performing as I expect it to; that is, run top-to-bottom twice because of start and end phases. Making it only run on one phase also doesn’t seem to fix the issue. It’s certainly not exhibiting similar behaviour to Unity3D’s Update() function, which happens every frame. To answer your question, I’m trying to create a mod that shares a single inventory among every player. I saw it on a YouTube video and thought it would be a fun exercise. I was mistaken. I wish there were more resources on how the game tick works beyond that of Forge’s docs. I can’t figure out the order of how things happen. I figured each player would check for changes in their inventories and other players would call copyInventory() when a change was detected by looping through the player list in the server.
  4. Hey there, I'm trying to check if a player's inventory has changed by comparing their current inventory to the inventory of the previous tick. I'm currently using PlayerTickEvent. Here's what I have tried: private NonNullList<ItemStack> previousInventory = ...; @SubscribeEvent public void onPlayerTick(TickEvent.PlayerTickEvent e) { if (!e.player.world.isRemote) return; NonNullList<ItemStack> currentInventory = e.player.inventory.mainInventory; /* Comparing the previous inventory's first ItemStack is always the same as the current inventory's first ItemStack. It appears like this: Previous first item: 6 dirt Current first item: 6 dirt But the desired effect would be: Previous first item: 1 air Current first item: 6 dirt */ System.out.println("Previous first item: " + previousInventory.get(0)); System.out.println("Current first item: " + currentInventory.get(0)); if (!currentInventory.equals(previousInventory)) System.out.println("Inventory changed"); // This never runs previousInventory = currentInventory; } However, I'm running into an issue where the previous inventory is ALWAYS equal to the current inventory. To prove this, I compared the first ItemStack of the previous and current inventory, and they're always the same. Why doesn't this work, and how would I get the player's inventory from the previous frame and compare it with the player's inventory of the current frame? (I know System.out.println() severely slows down performance. I don't care about it right now)
  5. Hi there, I’m fairly new to modding with Forge, and I want to create a mod that synchronizes every player’s inventories so that every player shares the same inventory. I did some research and I believe the best way to do this would be to make a container on the server and use Container.detectAndSendChanges() to send a packet whenever a player’s inventory gets updated. I see that Container.detectAndSendChanges() sends the packet to all Container listeners, but how do I create a ContainerListener on the server?
×
×
  • Create New...

Important Information

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