use of net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket in project SpongeCommon by SpongePowered.
the class ContainerBasedTransaction method handleCraftingPreview.
private void handleCraftingPreview(final Player player, final ClickContainerEvent event) {
if (this.craftingInventory != null) {
// TODO push event to cause?
// TODO prevent event when there is no preview?
final SlotTransaction previewTransaction = this.getPreviewTransaction(this.craftingInventory.result(), event.transactions());
final Optional<CraftingRecipe> recipe = player.level.getRecipeManager().getRecipeFor(RecipeType.CRAFTING, this.craftingContainer, player.level).map(CraftingRecipe.class::cast);
final CraftItemEvent.Preview previewEvent = SpongeEventFactory.createCraftItemEventPreview(event.cause(), (Container) this.menu, this.craftingInventory, event.cursorTransaction(), previewTransaction, recipe, Optional.empty(), event.transactions());
SpongeCommon.post(previewEvent);
this.handleEventResults(player, previewEvent);
if (player instanceof ServerPlayer && previewEvent instanceof CraftItemEvent.Preview) {
final SlotTransaction preview = previewEvent.preview();
// Resend modified output if needed
if (!preview.isValid() || previewEvent.isCancelled()) {
((ServerPlayer) player).connection.send(new ClientboundContainerSetSlotPacket(0, 0, ItemStackUtil.fromSnapshotToNative(previewEvent.preview().original())));
// TODO handle preview event cancel during shift-crafting
} else if (preview.custom().isPresent()) {
((ServerPlayer) player).connection.send(new ClientboundContainerSetSlotPacket(0, 0, ItemStackUtil.fromSnapshotToNative(previewEvent.preview().finalReplacement())));
// TODO handle preview event modification during shift-crafting
}
}
}
}
use of net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket in project SpongeCommon by SpongePowered.
the class BookUtil method fakeBookView.
public static void fakeBookView(final Book book, final Collection<? extends Player> players) {
// First we need to send a fake a Book ItemStack with the BookView's
// contents to the player's hand
// These values are localized since sending item NBT doesn't trigger translation
final ItemStack item = ItemStack.of(ItemTypes.WRITTEN_BOOK, 1);
Locale lastLocale = BookUtil.STARTER_LOCALE;
for (final Player player : players) {
if (!Objects.equals(player.locale(), lastLocale)) {
lastLocale = player.locale();
item.offer(Keys.CUSTOM_NAME, GlobalTranslator.render(book.title(), lastLocale));
item.offer(Keys.AUTHOR, GlobalTranslator.render(book.author(), lastLocale));
final Locale finalLastLocale = lastLocale;
item.offer(Keys.PAGES, Lists.transform(book.pages(), page -> GlobalTranslator.render(page, finalLastLocale)));
}
final ServerPlayer mcPlayer = (ServerPlayer) player;
final ServerGamePacketListenerImpl receiver = mcPlayer.connection;
final Inventory inventory = mcPlayer.inventory;
final int bookSlot = inventory.items.size() + inventory.selected;
receiver.send(new ClientboundContainerSetSlotPacket(BookUtil.WINDOW_PLAYER_INVENTORY, bookSlot, ItemStackUtil.toNative(item)));
// Next we tell the client to open the Book GUI
receiver.send(new ClientboundOpenBookPacket(InteractionHand.MAIN_HAND));
// Now we can remove the fake Book since it's contents will have already
// been transferred to the GUI
receiver.send(new ClientboundContainerSetSlotPacket(BookUtil.WINDOW_PLAYER_INVENTORY, bookSlot, inventory.getSelected()));
}
}
use of net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket in project SpongeCommon by SpongePowered.
the class PacketPhaseUtil method handlePlayerSlotRestore.
public static void handlePlayerSlotRestore(final net.minecraft.server.level.ServerPlayer player, final ItemStack itemStack, final InteractionHand hand) {
if (itemStack.isEmpty()) {
// No need to check if it's NONE, NONE is checked by isEmpty.
return;
}
player.ignoreSlotUpdateHack = false;
int slotId = 0;
if (hand == InteractionHand.OFF_HAND) {
player.inventory.offhand.set(0, itemStack);
slotId = (player.inventory.items.size() + Inventory.getSelectionSize());
} else {
player.inventory.items.set(player.inventory.selected, itemStack);
// TODO check if window id -2 and slotid = player.inventory.currentItem works instead of this:
for (Slot containerSlot : player.containerMenu.slots) {
if (containerSlot.container == player.inventory && ((SlotAccessor) containerSlot).accessor$slot() == player.inventory.selected) {
slotId = containerSlot.index;
break;
}
}
}
player.containerMenu.broadcastChanges();
player.ignoreSlotUpdateHack = false;
player.connection.send(new ClientboundContainerSetSlotPacket(player.containerMenu.containerId, slotId, itemStack));
}
use of net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket in project SpongeCommon by SpongePowered.
the class PacketPhaseUtil method handleCursorRestore.
public static void handleCursorRestore(final Player player, final Transaction<ItemStackSnapshot> cursorTransaction) {
final ItemStackSnapshot cursorSnap;
if (!cursorTransaction.isValid()) {
cursorSnap = cursorTransaction.original();
} else if (cursorTransaction.custom().isPresent()) {
cursorSnap = cursorTransaction.finalReplacement();
} else {
return;
}
final ItemStack cursor = ItemStackUtil.fromSnapshotToNative(cursorSnap);
player.inventory.setCarried(cursor);
if (player instanceof net.minecraft.server.level.ServerPlayer) {
((net.minecraft.server.level.ServerPlayer) player).connection.send(new ClientboundContainerSetSlotPacket(-1, -1, cursor));
}
}
use of net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket in project SpongeCommon by SpongePowered.
the class PacketPhaseUtil method handleCustomCursor.
public static void handleCustomCursor(final Player player, final ItemStackSnapshot customCursor) {
final ItemStack cursor = ItemStackUtil.fromSnapshotToNative(customCursor);
player.inventory.setCarried(cursor);
if (player instanceof net.minecraft.server.level.ServerPlayer) {
((net.minecraft.server.level.ServerPlayer) player).connection.send(new ClientboundContainerSetSlotPacket(-1, -1, cursor));
}
}
Aggregations