Search in sources :

Example 11 with SPacketSetSlot

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));
}
Also used : Slot(net.minecraft.inventory.Slot) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot)

Example 12 with SPacketSetSlot

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;
}
Also used : IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) Container(net.minecraft.inventory.Container) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) SlotTransaction(org.spongepowered.api.item.inventory.transaction.SlotTransaction) Transaction(org.spongepowered.api.data.Transaction) CraftItemEvent(org.spongepowered.api.event.item.inventory.CraftItemEvent) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) IMixinEntityPlayerMP(org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayerMP) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) ItemStack(net.minecraft.item.ItemStack)

Example 13 with SPacketSetSlot

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()));
}
Also used : InventoryPlayer(net.minecraft.entity.player.InventoryPlayer) SPacketCustomPayload(net.minecraft.network.play.server.SPacketCustomPayload) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) NetHandlerPlayServer(net.minecraft.network.NetHandlerPlayServer) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 14 with SPacketSetSlot

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();
}
Also used : InventoryPlayer(net.minecraft.entity.player.InventoryPlayer) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 15 with SPacketSetSlot

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;
}
Also used : TileEntityBase(com.codetaylor.mc.artisanworktables.modules.worktables.tile.spi.TileEntityBase) SPacketTileEntityBase(com.codetaylor.mc.athenaeum.spi.packet.SPacketTileEntityBase) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) ItemStack(net.minecraft.item.ItemStack) NetHandlerPlayServer(net.minecraft.network.NetHandlerPlayServer)

Aggregations

SPacketSetSlot (net.minecraft.network.play.server.SPacketSetSlot)15 ItemStack (net.minecraft.item.ItemStack)11 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)6 EntityItem (net.minecraft.entity.item.EntityItem)5 Slot (net.minecraft.inventory.Slot)4 Redirect (org.spongepowered.asm.mixin.injection.Redirect)3 IMixinEntityPlayer (org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayer)3 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 InventoryPlayer (net.minecraft.entity.player.InventoryPlayer)2 NetHandlerPlayServer (net.minecraft.network.NetHandlerPlayServer)2 TileEntity (net.minecraft.tileentity.TileEntity)2 CraftItemEvent (org.spongepowered.api.event.item.inventory.CraftItemEvent)2 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)2 Overwrite (org.spongepowered.asm.mixin.Overwrite)2 IMixinEntityPlayerMP (org.spongepowered.common.interfaces.entity.player.IMixinEntityPlayerMP)2 TileEntityBase (com.codetaylor.mc.artisanworktables.modules.worktables.tile.spi.TileEntityBase)1 SPacketTileEntityBase (com.codetaylor.mc.athenaeum.spi.packet.SPacketTileEntityBase)1 Vector3d (com.flowpowered.math.vector.Vector3d)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1