broader inventory syncing fixes #1515

This commit is contained in:
gnembon committed 2023-10-11 15:48:12 +02:00
1 parent 9039bbaa16
commit 52489bab00
1 file changed
+8 -15
@@ -29,15 +29,12 @@ import java.util.Optional;
import java.util.Set; import java.util.Set;
import net.minecraft.commands.arguments.item.ItemInput; import net.minecraft.commands.arguments.item.ItemInput;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet; import net.minecraft.core.HolderSet;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess; import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.Registries; import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.tags.TagKey; import net.minecraft.tags.TagKey;
@@ -258,7 +255,7 @@ public class Inventories
{ {
// clear slot // clear slot
ItemStack removedStack = inventoryLocator.inventory().removeItemNoUpdate(slot); ItemStack removedStack = inventoryLocator.inventory().removeItemNoUpdate(slot);
syncPlayerInventory(inventoryLocator, slot); syncPlayerInventory(inventoryLocator);
return ValueConversions.of(removedStack, regs); return ValueConversions.of(removedStack, regs);
} }
if (lv.size() < inventoryLocator.offset() + 3) if (lv.size() < inventoryLocator.offset() + 3)
@@ -267,7 +264,7 @@ public class Inventories
ItemStack newStack = previousStack.copy(); ItemStack newStack = previousStack.copy();
newStack.setCount(count); newStack.setCount(count);
inventoryLocator.inventory().setItem(slot, newStack); inventoryLocator.inventory().setItem(slot, newStack);
syncPlayerInventory(inventoryLocator, slot); syncPlayerInventory(inventoryLocator);
return ValueConversions.of(previousStack, regs); return ValueConversions.of(previousStack, regs);
} }
CompoundTag nbt = null; // skipping one argument CompoundTag nbt = null; // skipping one argument
@@ -288,7 +285,7 @@ public class Inventories
try try
{ {
inventoryLocator.inventory().setItem(slot, newitem.createItemStack(count, false)); inventoryLocator.inventory().setItem(slot, newitem.createItemStack(count, false));
syncPlayerInventory(inventoryLocator, slot); syncPlayerInventory(inventoryLocator);
} }
catch (CommandSyntaxException e) catch (CommandSyntaxException e)
{ {
@@ -369,11 +366,11 @@ public class Inventories
{ {
stack.setCount(left); stack.setCount(left);
inventoryLocator.inventory().setItem(i, stack); inventoryLocator.inventory().setItem(i, stack);
syncPlayerInventory(inventoryLocator, i); syncPlayerInventory(inventoryLocator);
return Value.TRUE; return Value.TRUE;
} }
inventoryLocator.inventory().removeItemNoUpdate(i); inventoryLocator.inventory().removeItemNoUpdate(i);
syncPlayerInventory(inventoryLocator, i); syncPlayerInventory(inventoryLocator);
amount -= stack.getCount(); amount -= stack.getCount();
} }
if (amount > 0) if (amount > 0)
@@ -510,15 +507,11 @@ public class Inventories
}); });
} }
private static void syncPlayerInventory(NBTSerializableValue.InventoryLocator inventory, int slot) private static void syncPlayerInventory(NBTSerializableValue.InventoryLocator inventory)
{ {
if (inventory.owner() instanceof final ServerPlayer player && !inventory.isEnder() && !(inventory.inventory() instanceof ScreenValue.ScreenHandlerInventory)) if (inventory.owner() instanceof ServerPlayer player && !inventory.isEnder() && !(inventory.inventory() instanceof ScreenValue.ScreenHandlerInventory))
{ {
player.connection.send(new ClientboundContainerSetSlotPacket( player.containerMenu.broadcastChanges();
-2, 0, // resolve mystery argument
slot,
inventory.inventory().getItem(slot)
));
} }
} }
} }