use of net.minecraft.network.play.server.SPacketSetSlot in project SpongeCommon by SpongePowered.
the class PacketPhaseUtil method handlePlayerSlotRestore.
public static void handlePlayerSlotRestore(EntityPlayerMP player, ItemStack itemStack, EnumHand hand) {
if (itemStack.isEmpty() || itemStack == ItemTypeRegistryModule.NONE) {
return;
}
player.isChangingQuantityOnly = false;
int slotId = 0;
if (hand == EnumHand.OFF_HAND) {
player.inventory.offHandInventory.set(0, itemStack);
slotId = (player.inventory.mainInventory.size() + InventoryPlayer.getHotbarSize());
} else {
player.inventory.mainInventory.set(player.inventory.currentItem, itemStack);
final Slot slot = player.openContainer.getSlotFromInventory(player.inventory, player.inventory.currentItem);
slotId = slot.slotNumber;
}
player.openContainer.detectAndSendChanges();
player.isChangingQuantityOnly = false;
player.connection.sendPacket(new SPacketSetSlot(player.openContainer.windowId, slotId, itemStack));
}
use of net.minecraft.network.play.server.SPacketSetSlot in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method callCraftEventPost.
public static CraftItemEvent.Craft callCraftEventPost(EntityPlayer player, CraftingInventory inventory, ItemStackSnapshot result, @Nullable CraftingRecipe recipe, Container container, List<SlotTransaction> transactions) {
// Get previous cursor if captured
ItemStack previousCursor = ((IMixinContainer) container).getPreviousCursor();
if (previousCursor == null) {
// or get the current one
previousCursor = player.inventory.getItemStack();
}
Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(ItemStackUtil.snapshotOf(previousCursor), ItemStackUtil.snapshotOf(player.inventory.getItemStack()));
CraftItemEvent.Craft event = SpongeEventFactory.createCraftItemEventCraft(Sponge.getCauseStackManager().getCurrentCause(), result, inventory, cursorTransaction, Optional.ofNullable(recipe), ((org.spongepowered.api.item.inventory.Container) container), transactions);
SpongeImpl.postEvent(event);
((IMixinContainer) container).setCaptureInventory(false);
// handle slot-transactions
PacketPhaseUtil.handleSlotRestore(player, container, new ArrayList<>(transactions), event.isCancelled());
if (event.isCancelled() || !event.getCursorTransaction().isValid() || event.getCursorTransaction().getCustom().isPresent()) {
// handle cursor-transaction
ItemStackSnapshot newCursor = event.isCancelled() || event.getCursorTransaction().isValid() ? event.getCursorTransaction().getOriginal() : event.getCursorTransaction().getFinal();
player.inventory.setItemStack(ItemStackUtil.fromSnapshotToNative(newCursor));
if (player instanceof EntityPlayerMP) {
((EntityPlayerMP) player).connection.sendPacket(new SPacketSetSlot(-1, -1, player.inventory.getItemStack()));
}
}
transactions.clear();
((IMixinContainer) container).setCaptureInventory(true);
return event;
}
use of net.minecraft.network.play.server.SPacketSetSlot in project SpongeCommon by SpongePowered.
the class BookFaker method fakeBookView.
public static void fakeBookView(BookView bookView, Player player) {
EntityPlayerMP mcPlayer = (EntityPlayerMP) player;
NetHandlerPlayServer receiver = mcPlayer.connection;
// First we need to send a fake a Book ItemStack with the BookView's
// contents to the player's hand
ItemStack item = ItemStack.of(ItemTypes.WRITTEN_BOOK, 1);
item.offer(Keys.DISPLAY_NAME, bookView.getTitle());
item.offer(Keys.BOOK_AUTHOR, bookView.getAuthor());
item.offer(Keys.BOOK_PAGES, bookView.getPages());
InventoryPlayer inventory = mcPlayer.inventory;
int bookSlot = inventory.mainInventory.size() + inventory.currentItem;
receiver.sendPacket(new SPacketSetSlot(WINDOW_PLAYER_INVENTORY, bookSlot, ItemStackUtil.toNative(item)));
// Next we tell the client to open the Book GUI
PacketBuffer packetbuffer = new PacketBuffer(Unpooled.buffer());
packetbuffer.writeEnumValue(EnumHand.MAIN_HAND);
receiver.sendPacket(new SPacketCustomPayload("MC|BOpen", packetbuffer));
// Now we can remove the fake Book since it's contents will have already
// been transferred to the GUI
receiver.sendPacket(new SPacketSetSlot(WINDOW_PLAYER_INVENTORY, bookSlot, inventory.getCurrentItem()));
}
use of net.minecraft.network.play.server.SPacketSetSlot in project Charset by CharsetMC.
the class CharsetTweakAutoReplace method onPlayerDestroyItem.
@SubscribeEvent
public void onPlayerDestroyItem(PlayerDestroyItemEvent event) {
if (!(event.getEntity() instanceof EntityPlayerMP) || event.getEntity().world.isRemote || event.getOriginal().isEmpty()) {
return;
}
EntityPlayerMP player = (EntityPlayerMP) event.getEntityPlayer();
InventoryPlayer inv = player.inventory;
ItemStack currentItem = inv.getCurrentItem();
if (currentItem != event.getOriginal()) {
return;
}
int row, lastEmptiedSlot = -1;
boolean changed = false;
for (row = 2; row >= 0; row--) {
int slot = inv.currentItem + row * 9 + 9;
ItemStack stackAbove = inv.getStackInSlot(slot);
if (!canReplace(stackAbove, event.getOriginal()))
break;
int targetSlot = ((slot < 27) ? (slot + 9) : (slot - 27));
ItemStack stackTarget = stackAbove.copy();
inv.setInventorySlotContents(targetSlot, stackTarget);
inv.setInventorySlotContents(slot, ItemStack.EMPTY);
player.connection.sendPacket(new SPacketSetSlot(-1, targetSlot, stackTarget));
changed = true;
lastEmptiedSlot = slot;
}
if (changed) {
player.connection.sendPacket(new SPacketSetSlot(-1, lastEmptiedSlot, ItemStack.EMPTY));
}
inv.markDirty();
}
use of net.minecraft.network.play.server.SPacketSetSlot in project artisan-worktables by codetaylor.
the class SPacketWorktableTab method onMessage.
@Override
public IMessage onMessage(SPacketWorktableTab message, MessageContext ctx, TileEntity tileEntity) {
// Reference:
// https://github.com/SlimeKnights/TinkersConstruct/blob/master/src/main/java/slimeknights/tconstruct/tools/common/network/TinkerStationTabPacket.java
NetHandlerPlayServer serverHandler = ctx.getServerHandler();
EntityPlayerMP player = serverHandler.player;
ItemStack heldStack = player.inventory.getItemStack();
if (!heldStack.isEmpty()) {
player.inventory.setItemStack(ItemStack.EMPTY);
}
if (tileEntity instanceof TileEntityBase) {
player.openGui(ModuleWorktables.MOD_INSTANCE, 1, player.getEntityWorld(), message.blockPos.getX(), message.blockPos.getY(), message.blockPos.getZ());
}
if (!heldStack.isEmpty()) {
player.inventory.setItemStack(heldStack);
serverHandler.sendPacket(new SPacketSetSlot(-1, -1, heldStack));
}
return null;
}
Aggregations