Skip to content

Commit

Permalink
Update slot packets
Browse files Browse the repository at this point in the history
  • Loading branch information
aromaa committed Nov 22, 2024
1 parent 1fff094 commit d878472
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.common.ServerboundClientInformationPacket;
import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket;
import net.minecraft.network.protocol.game.ClientboundSetCursorItemPacket;
import net.minecraft.network.protocol.game.ClientboundSetPlayerInventoryPacket;
import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.server.level.ServerLevel;
Expand Down Expand Up @@ -103,7 +105,7 @@ public static void handleSlotRestore(@Nullable final Player player, final @Nulla
final org.spongepowered.api.item.inventory.ItemStack stack = snapshot.asMutable();
slot.set(stack);
((net.minecraft.server.level.ServerPlayer) player).connection.send(
new ClientboundContainerSetSlotPacket(-2, player.inventoryMenu.getStateId(), ((SlotAdapter) slot).getOrdinal(), ItemStackUtil.toNative(stack)));
new ClientboundSetPlayerInventoryPacket(((SlotAdapter) slot).getOrdinal(), ItemStackUtil.toNative(stack)));
} else {
final int slotNumber = ((SlotAdapter) slot).getOrdinal();
final Slot nmsSlot = containerMenu.getSlot(slotNumber);
Expand Down Expand Up @@ -138,15 +140,15 @@ public static void handleCursorRestore(final Player player, final Transaction<It
player.containerMenu.setCarried(cursor);
player.containerMenu.setRemoteCarried(cursor);
if (player instanceof net.minecraft.server.level.ServerPlayer) {
((net.minecraft.server.level.ServerPlayer) player).connection.send(new ClientboundContainerSetSlotPacket(-1, player.containerMenu.getStateId(), -1, cursor));
((net.minecraft.server.level.ServerPlayer) player).connection.send(new ClientboundSetCursorItemPacket(cursor));
}
}

public static void handleCustomCursor(final Player player, final ItemStackSnapshot customCursor) {
final ItemStack cursor = ItemStackUtil.fromSnapshotToNative(customCursor);
player.containerMenu.setCarried(cursor);
if (player instanceof net.minecraft.server.level.ServerPlayer) {
((net.minecraft.server.level.ServerPlayer) player).connection.send(new ClientboundContainerSetSlotPacket(-1, -1, -1, cursor));
((net.minecraft.server.level.ServerPlayer) player).connection.send(new ClientboundSetCursorItemPacket(cursor));
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/main/java/org/spongepowered/common/util/BookUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBundlePacket;
import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket;
import net.minecraft.network.protocol.game.ClientboundOpenBookPacket;
import net.minecraft.network.protocol.game.ClientboundSetPlayerInventoryPacket;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Inventory;
import org.spongepowered.api.data.Keys;
Expand All @@ -43,8 +43,6 @@

public final class BookUtil {

public static final int PLAYER_INVENTORY = -2;

public static ClientboundBundlePacket createFakeBookViewPacket(final Player player, final Book book) {
final ItemStack item = ItemStack.of(ItemTypes.WRITTEN_BOOK);
item.offer(Keys.CUSTOM_NAME, book.title());
Expand All @@ -59,14 +57,14 @@ public static ClientboundBundlePacket createFakeBookViewPacket(final Player play

// First we need to send a fake a Book ItemStack with the BookView's
// contents to the player's hand
packets.add(new ClientboundContainerSetSlotPacket(BookUtil.PLAYER_INVENTORY, 0, bookSlot, ItemStackUtil.toNative(item)));
packets.add(new ClientboundSetPlayerInventoryPacket(bookSlot, ItemStackUtil.toNative(item)));

// Next we tell the client to open the Book GUI
packets.add(new ClientboundOpenBookPacket(InteractionHand.MAIN_HAND));

// Now we can remove the fake Book since it's contents will have already
// been transferred to the GUI
packets.add(new ClientboundContainerSetSlotPacket(BookUtil.PLAYER_INVENTORY, 0, bookSlot, oldItem));
packets.add(new ClientboundSetPlayerInventoryPacket(bookSlot, oldItem));

return new ClientboundBundlePacket(packets);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
package org.spongepowered.common.mixin.inventory.impl.server.level;

import net.minecraft.core.NonNullList;
import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket;
import net.minecraft.network.protocol.game.ClientboundSetPlayerInventoryPacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -54,6 +54,6 @@ public abstract class ServerPlayer_Mixin_Inventory {
final org.spongepowered.api.item.inventory.Slot offhand
= ((org.spongepowered.api.entity.living.player.server.ServerPlayer) this.this$0).inventory().offhand();
this.this$0.connection.send(
new ClientboundContainerSetSlotPacket(-2, 0, ((SlotAdapter) offhand).getOrdinal(), ItemStackUtil.toNative(offhand.peek())));
new ClientboundSetPlayerInventoryPacket(((SlotAdapter) offhand).getOrdinal(), ItemStackUtil.toNative(offhand.peek())));
}
}

0 comments on commit d878472

Please sign in to comment.