public void onCraftMatrixChanged(IInventory p_75130_1_) { // CraftBukkit start (Note: the following line would cause an error if called during construction) CraftingManager.getInstance().lastCraftView = getBukkitView(); ItemStack craftResult = CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.thePlayer.worldObj); this.craftResult.setInventorySlotContents(0, craftResult); if (super.crafters.size() < 1) { return; } EntityPlayerMP player = (EntityPlayerMP) super.crafters.get(0); // TODO: Is this _always_ correct? Seems like it. player.playerNetServerHandler.sendPacket(new S2FPacketSetSlot(player.openContainer.windowId, 0, craftResult)); // CraftBukkit end }
public void onCraftMatrixChanged(IInventory p_75130_1_) { // CraftBukkit start CraftingManager.getInstance().lastCraftView = getBukkitView(); ItemStack craftResult = CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.worldObj); this.craftResult.setInventorySlotContents(0, craftResult); if (super.crafters.size() < 1) { return; } EntityPlayerMP player = (EntityPlayerMP) super.crafters.get(0); // TODO: Is this _always_ correct? Seems like it. player.playerNetServerHandler.sendPacket(new S2FPacketSetSlot(player.openContainer.windowId, 0, craftResult)); // CraftBukkit end }
/** * Gives the player an ItemStack */ public static void giveItemStackToPlayer(EntityPlayer player, ItemStack itemStack) { int i = -1; for (int j = 0; j < player.inventory.mainInventory.length; j++) { if (player.inventory.mainInventory[j] == null) { i = j; break; } } if (i != -1) { player.inventory.mainInventory[i] = itemStack; // get the actual inventory Slot: Slot slot = player.openContainer.getSlotFromInventory(player.inventory, i); // send S2FPacketSetSlot to the player with the new / changed stack (or null) ((EntityPlayerMP) player).playerNetServerHandler.sendPacket(new S2FPacketSetSlot(player.openContainer.windowId, slot.slotNumber, player.inventory.mainInventory[i])); } else { WorldUtils.dropAsEntity(player.getEntityWorld(), (int) player.posX, (int) player.posY, (int) player.posZ, itemStack); } }
/** * Sends the contents of an inventory slot to the client-side Container. This doesn't have to match the actual * contents of that slot. Args: Container, slot number, slot contents */ public void sendSlotContents(Container containerToSend, int slotInd, ItemStack stack) { if (!(containerToSend.getSlot(slotInd) instanceof SlotCrafting)) { if (!this.isChangingQuantityOnly) { this.playerNetServerHandler.sendPacket(new S2FPacketSetSlot(containerToSend.windowId, slotInd, stack)); } } }
/** * updates item held by mouse */ public void updateHeldItem() { if (!this.isChangingQuantityOnly) { this.playerNetServerHandler.sendPacket(new S2FPacketSetSlot(-1, -1, this.inventory.getItemStack())); } }
/** * Handles pickin up an ItemStack or dropping one in your inventory or an open (non-creative) container */ public void handleSetSlot(S2FPacketSetSlot packetIn) { PacketThreadUtil.checkThreadAndEnqueue(packetIn, this, this.gameController); EntityPlayer entityplayer = this.gameController.thePlayer; if (packetIn.func_149175_c() == -1) { entityplayer.inventory.setItemStack(packetIn.func_149174_e()); } else { boolean flag = false; if (this.gameController.currentScreen instanceof GuiContainerCreative) { GuiContainerCreative guicontainercreative = (GuiContainerCreative)this.gameController.currentScreen; flag = guicontainercreative.getSelectedTabIndex() != CreativeTabs.tabInventory.getTabIndex(); } if (packetIn.func_149175_c() == 0 && packetIn.func_149173_d() >= 36 && packetIn.func_149173_d() < 45) { ItemStack itemstack = entityplayer.inventoryContainer.getSlot(packetIn.func_149173_d()).getStack(); if (packetIn.func_149174_e() != null && (itemstack == null || itemstack.stackSize < packetIn.func_149174_e().stackSize)) { packetIn.func_149174_e().animationsToGo = 5; } entityplayer.inventoryContainer.putStackInSlot(packetIn.func_149173_d(), packetIn.func_149174_e()); } else if (packetIn.func_149175_c() == entityplayer.openContainer.windowId && (packetIn.func_149175_c() != 0 || !flag)) { entityplayer.openContainer.putStackInSlot(packetIn.func_149173_d(), packetIn.func_149174_e()); } } }
/** * Handles pickin up an ItemStack or dropping one in your inventory or an open * (non-creative) container */ public void handleSetSlot(S2FPacketSetSlot packetIn) { PacketThreadUtil.checkThreadAndEnqueue(packetIn, this, this.gameController); EntityPlayer entityplayer = this.gameController.thePlayer; if (packetIn.func_149175_c() == -1) { entityplayer.inventory.setItemStack(packetIn.func_149174_e()); } else { boolean flag = false; if (this.gameController.currentScreen instanceof GuiContainerCreative) { GuiContainerCreative guicontainercreative = (GuiContainerCreative) this.gameController.currentScreen; flag = guicontainercreative.getSelectedTabIndex() != CreativeTabs.tabInventory.getTabIndex(); } if (packetIn.func_149175_c() == 0 && packetIn.func_149173_d() >= 36 && packetIn.func_149173_d() < 45) { ItemStack itemstack = entityplayer.inventoryContainer.getSlot(packetIn.func_149173_d()).getStack(); if (packetIn.func_149174_e() != null && (itemstack == null || itemstack.stackSize < packetIn.func_149174_e().stackSize)) { packetIn.func_149174_e().animationsToGo = 5; } entityplayer.inventoryContainer.putStackInSlot(packetIn.func_149173_d(), packetIn.func_149174_e()); } else if (packetIn.func_149175_c() == entityplayer.openContainer.windowId && (packetIn.func_149175_c() != 0 || !flag)) { entityplayer.openContainer.putStackInSlot(packetIn.func_149173_d(), packetIn.func_149174_e()); } } }
private void resolveContents(ItemStack stack, EntityPlayer player) { if (stack != null && stack.getTagCompound() != null) { NBTTagCompound nbttagcompound = stack.getTagCompound(); if (!nbttagcompound.getBoolean("resolved")) { nbttagcompound.setBoolean("resolved", true); if (validBookTagContents(nbttagcompound)) { NBTTagList nbttaglist = nbttagcompound.getTagList("pages", 8); for (int i = 0; i < nbttaglist.tagCount(); ++i) { String s = nbttaglist.getStringTagAt(i); Object object; try { IChatComponent ichatcomponent = IChatComponent.Serializer.jsonToComponent(s); object = ChatComponentProcessor.func_179985_a(player, ichatcomponent, player); } catch (Exception exception) { object = new ChatComponentText(s); } nbttaglist.set(i, new NBTTagString(IChatComponent.Serializer.componentToJson((IChatComponent) object))); } nbttagcompound.setTag("pages", nbttaglist); if (player instanceof EntityPlayerMP && player.getCurrentEquippedItem() == stack) { Slot slot = player.openContainer.getSlotFromInventory(player.inventory, player.inventory.currentItem); ((EntityPlayerMP) player).playerNetServerHandler .sendPacket(new S2FPacketSetSlot(0, slot.slotNumber, stack)); } } } } }
public void placeAnything(PlayerInteractEvent event) { // Server side and on block only. if (event.isCanceled() || event.world.isRemote || event.action != RIGHT_CLICK_BLOCK) return; ItemStack heldItem = event.entityPlayer.inventory.getCurrentItem(); // Only tools, not null if (heldItem == null || !(heldItem.getItem() instanceof ItemTool)) return; if (event.world.getTileEntity(event.x, event.y, event.z) != null) return; // old slot int oldSlot = event.entityPlayer.inventory.currentItem; // Avoid invalid array indexes if (oldSlot < 0 || oldSlot > 8) return; // new slot int newSlot = slots[oldSlot]; // Avoid invalid slots indexes if (newSlot < 0 || newSlot > 8) return; // Get new item ItemStack slotStack = event.entityPlayer.inventory.getStackInSlot(newSlot); // No null please if (slotStack == null) return; // Set current slot to new slot to fool Minecraft event.entityPlayer.inventory.currentItem = newSlot; // Place block boolean b = ((EntityPlayerMP) event.entityPlayer).theItemInWorldManager.activateBlockOrUseItem(event.entityPlayer, event.world, slotStack, event.x, event.y, event.z, event.face, 0.5f, 0.5f, 0.5f); // Remove empty stacks if (slotStack.stackSize <= 0) slotStack = null; // Set old slot back properly event.entityPlayer.inventory.currentItem = oldSlot; // Update client event.entityPlayer.inventory.setInventorySlotContents(newSlot, slotStack); ((EntityPlayerMP) event.entityPlayer).playerNetServerHandler.sendPacket(new S2FPacketSetSlot(0, newSlot + 36, slotStack)); // Prevent derpy doors event.setCanceled(true); }
/** * Sends the contents of an inventory slot to the client-side Container. This doesn't have to match the actual * contents of that slot. Args: Container, slot number, slot contents */ public void sendSlotContents(Container par1Container, int par2, ItemStack par3ItemStack) { if (!(par1Container.getSlot(par2) instanceof SlotCrafting)) { if (!this.isChangingQuantityOnly) { this.playerNetServerHandler.sendPacket(new S2FPacketSetSlot(par1Container.windowId, par2, par3ItemStack)); } } }
/** * Handles pickin up an ItemStack or dropping one in your inventory or an open (non-creative) container */ public void handleSetSlot(S2FPacketSetSlot p_147266_1_) { EntityClientPlayerMP var2 = this.gameController.thePlayer; if (p_147266_1_.func_149175_c() == -1) { var2.inventory.setItemStack(p_147266_1_.func_149174_e()); } else { boolean var3 = false; if (this.gameController.currentScreen instanceof GuiContainerCreative) { GuiContainerCreative var4 = (GuiContainerCreative)this.gameController.currentScreen; var3 = var4.func_147056_g() != CreativeTabs.tabInventory.getTabIndex(); } if (p_147266_1_.func_149175_c() == 0 && p_147266_1_.func_149173_d() >= 36 && p_147266_1_.func_149173_d() < 45) { ItemStack var5 = var2.inventoryContainer.getSlot(p_147266_1_.func_149173_d()).getStack(); if (p_147266_1_.func_149174_e() != null && (var5 == null || var5.stackSize < p_147266_1_.func_149174_e().stackSize)) { p_147266_1_.func_149174_e().animationsToGo = 5; } var2.inventoryContainer.putStackInSlot(p_147266_1_.func_149173_d(), p_147266_1_.func_149174_e()); } else if (p_147266_1_.func_149175_c() == var2.openContainer.windowId && (p_147266_1_.func_149175_c() != 0 || !var3)) { var2.openContainer.putStackInSlot(p_147266_1_.func_149173_d(), p_147266_1_.func_149174_e()); } } }
@SubscribeEvent public void onPlayerDestroyItem(PlayerDestroyItemEvent event) { if (!(event.entity instanceof EntityPlayerMP) || event.entity.worldObj.isRemote) return; EntityPlayerMP player = (EntityPlayerMP)event.entityPlayer; InventoryPlayer inv = player.inventory; ItemStack currentItem = inv.getCurrentItem(); if ((currentItem != null) && (currentItem != event.original)) return; int row; for (row = 2; row >= 0; row--) { int slot = inv.currentItem + row * 9 + 9; ItemStack stackAbove = inv.getStackInSlot(slot); if (!canReplace(stackAbove, event.original)) break; int targetSlot = ((slot < 27) ? (slot + 9) : (slot - 27)); inv.setInventorySlotContents(targetSlot, stackAbove); inv.setInventorySlotContents(slot, null); player.playerNetServerHandler.sendPacket( new S2FPacketSetSlot(0, slot + 9, stackAbove)); } if (row < 2) player.playerNetServerHandler.sendPacket( new S2FPacketSetSlot(0, inv.currentItem + row * 9 + 18, null)); }
public void sendSlotContents(Container p_71111_1_, int p_71111_2_, ItemStack p_71111_3_) { if (!(p_71111_1_.getSlot(p_71111_2_) instanceof SlotCrafting)) { if (!this.isChangingQuantityOnly) { this.playerNetServerHandler.sendPacket(new S2FPacketSetSlot(p_71111_1_.windowId, p_71111_2_, p_71111_3_)); } } }
public void sendContainerAndContentsToPlayer(Container p_71110_1_, List p_71110_2_) { this.playerNetServerHandler.sendPacket(new S30PacketWindowItems(p_71110_1_.windowId, p_71110_2_)); this.playerNetServerHandler.sendPacket(new S2FPacketSetSlot(-1, -1, this.inventory.getItemStack())); if (p_71110_1_.getBukkitView() == null) return; // Cauldron - allow vanilla mods to bypass // CraftBukkit start - Send a Set Slot to update the crafting result slot if (java.util.EnumSet.of(InventoryType.CRAFTING, InventoryType.WORKBENCH).contains(p_71110_1_.getBukkitView().getType())) { this.playerNetServerHandler.sendPacket(new S2FPacketSetSlot(p_71110_1_.windowId, 0, p_71110_1_.getSlot(0).getStack())); } // CraftBukkit end }
public void updateHeldItem() { if (!this.isChangingQuantityOnly) { this.playerNetServerHandler.sendPacket(new S2FPacketSetSlot(-1, -1, this.inventory.getItemStack())); } }
public void handleSetSlot(S2FPacketSetSlot p_147266_1_) { EntityClientPlayerMP entityclientplayermp = this.gameController.thePlayer; if (p_147266_1_.func_149175_c() == -1) { entityclientplayermp.inventory.setItemStack(p_147266_1_.func_149174_e()); } else { boolean flag = false; if (this.gameController.currentScreen instanceof GuiContainerCreative) { GuiContainerCreative guicontainercreative = (GuiContainerCreative)this.gameController.currentScreen; flag = guicontainercreative.func_147056_g() != CreativeTabs.tabInventory.getTabIndex(); } if (p_147266_1_.func_149175_c() == 0 && p_147266_1_.func_149173_d() >= 36 && p_147266_1_.func_149173_d() < 45) { ItemStack itemstack = entityclientplayermp.inventoryContainer.getSlot(p_147266_1_.func_149173_d()).getStack(); if (p_147266_1_.func_149174_e() != null && (itemstack == null || itemstack.stackSize < p_147266_1_.func_149174_e().stackSize)) { p_147266_1_.func_149174_e().animationsToGo = 5; } entityclientplayermp.inventoryContainer.putStackInSlot(p_147266_1_.func_149173_d(), p_147266_1_.func_149174_e()); } else if (p_147266_1_.func_149175_c() == entityclientplayermp.openContainer.windowId && (p_147266_1_.func_149175_c() != 0 || !flag)) { entityclientplayermp.openContainer.putStackInSlot(p_147266_1_.func_149173_d(), p_147266_1_.func_149174_e()); } } }
@Override public void onUpdate(ItemStack itemStack, World world, Entity holdingEntity, int p_77663_4_, boolean inHand) { if (!world.isRemote && holdingEntity instanceof EntityPlayer) { EntityPlayer player = (EntityPlayer) holdingEntity; ItemStack stack = player.getHeldItem(); if (stack != null && stack.getItem() instanceof ToolBase && player.experienceLevel > 0) { ToolProperties properties = ((ToolBase) stack.getItem()) .getToolProperties(stack); if (!properties.isBroken() && properties.getMaterialType() == MaterialType.METAL_ARCANE && stack.getItemDamage() > DUR_PER_LEVEL) { player.addExperienceLevel(-1); stack.setItemDamage(stack.getItemDamage() - DUR_PER_LEVEL); // get the actual inventory Slot: Slot slot = player.openContainer.getSlotFromInventory( player.inventory, player.inventory.currentItem); // send S2FPacketSetSlot to the player with the new / // changed stack (or null) ((EntityPlayerMP) player).playerNetServerHandler .sendPacket(new S2FPacketSetSlot( player.openContainer.windowId, slot.slotNumber, stack)); } } } }
/** * update the crafting window inventory with the items in the list */ public void updateCraftingInventory(Container containerToSend, List<ItemStack> itemsList) { this.playerNetServerHandler.sendPacket(new S30PacketWindowItems(containerToSend.windowId, itemsList)); this.playerNetServerHandler.sendPacket(new S2FPacketSetSlot(-1, -1, this.inventory.getItemStack())); }
/** * Processes block placement and block activation (anvil, furnace, etc.) */ public void processPlayerBlockPlacement(C08PacketPlayerBlockPlacement packetIn) { PacketThreadUtil.checkThreadAndEnqueue(packetIn, this, this.playerEntity.getServerForPlayer()); WorldServer worldserver = this.serverController.worldServerForDimension(this.playerEntity.dimension); ItemStack itemstack = this.playerEntity.inventory.getCurrentItem(); boolean flag = false; BlockPos blockpos = packetIn.getPosition(); EnumFacing enumfacing = EnumFacing.getFront(packetIn.getPlacedBlockDirection()); this.playerEntity.markPlayerActive(); if (packetIn.getPlacedBlockDirection() == 255) { if (itemstack == null) { return; } this.playerEntity.theItemInWorldManager.tryUseItem(this.playerEntity, worldserver, itemstack); } else if (blockpos.getY() < this.serverController.getBuildLimit() - 1 || enumfacing != EnumFacing.UP && blockpos.getY() < this.serverController.getBuildLimit()) { if (this.hasMoved && this.playerEntity.getDistanceSq((double)blockpos.getX() + 0.5D, (double)blockpos.getY() + 0.5D, (double)blockpos.getZ() + 0.5D) < 64.0D && !this.serverController.isBlockProtected(worldserver, blockpos, this.playerEntity) && worldserver.getWorldBorder().contains(blockpos)) { this.playerEntity.theItemInWorldManager.activateBlockOrUseItem(this.playerEntity, worldserver, itemstack, blockpos, enumfacing, packetIn.getPlacedBlockOffsetX(), packetIn.getPlacedBlockOffsetY(), packetIn.getPlacedBlockOffsetZ()); } flag = true; } else { ChatComponentTranslation chatcomponenttranslation = new ChatComponentTranslation("build.tooHigh", new Object[] {Integer.valueOf(this.serverController.getBuildLimit())}); chatcomponenttranslation.getChatStyle().setColor(EnumChatFormatting.RED); this.playerEntity.playerNetServerHandler.sendPacket(new S02PacketChat(chatcomponenttranslation)); flag = true; } if (flag) { this.playerEntity.playerNetServerHandler.sendPacket(new S23PacketBlockChange(worldserver, blockpos)); this.playerEntity.playerNetServerHandler.sendPacket(new S23PacketBlockChange(worldserver, blockpos.offset(enumfacing))); } itemstack = this.playerEntity.inventory.getCurrentItem(); if (itemstack != null && itemstack.stackSize == 0) { this.playerEntity.inventory.mainInventory[this.playerEntity.inventory.currentItem] = null; itemstack = null; } if (itemstack == null || itemstack.getMaxItemUseDuration() == 0) { this.playerEntity.isChangingQuantityOnly = true; this.playerEntity.inventory.mainInventory[this.playerEntity.inventory.currentItem] = ItemStack.copyItemStack(this.playerEntity.inventory.mainInventory[this.playerEntity.inventory.currentItem]); Slot slot = this.playerEntity.openContainer.getSlotFromInventory(this.playerEntity.inventory, this.playerEntity.inventory.currentItem); this.playerEntity.openContainer.detectAndSendChanges(); this.playerEntity.isChangingQuantityOnly = false; if (!ItemStack.areItemStacksEqual(this.playerEntity.inventory.getCurrentItem(), packetIn.getStack())) { this.sendPacket(new S2FPacketSetSlot(this.playerEntity.openContainer.windowId, slot.slotNumber, this.playerEntity.inventory.getCurrentItem())); } } }
public void sendContainerAndContentsToPlayer(Container par1Container, List par2List) { this.playerNetServerHandler.sendPacket(new S30PacketWindowItems(par1Container.windowId, par2List)); this.playerNetServerHandler.sendPacket(new S2FPacketSetSlot(-1, -1, this.inventory.getItemStack())); }
@Override public Packet setSlot(int windowId, int slotId, Item item) { return (Packet) new S2FPacketSetSlot(windowId, slotId, (ItemStack) item); }
/** * This method is the mod. Everything else is extra */ @SubscribeEvent public void playerInteractEventHandler(PlayerInteractEvent event) { // Server side and on block only. if (event.isCanceled() || event.world.isRemote || event.action != RIGHT_CLICK_BLOCK) return; ItemStack heldItem = event.entityPlayer.inventory.getCurrentItem(); // Only tools, not null if (heldItem == null || !(heldItem.getItem() instanceof ItemTool)) return; // disableTE code if (disableTE && event.world.getTileEntity(event.x, event.y, event.z) != null) return; // Save old slot id int oldSlot = event.entityPlayer.inventory.currentItem; // Avoid invalid array indexes if (oldSlot < 0 || oldSlot > 8) return; // Get the new slot id int newSlot = slots[oldSlot]; // Avoid invalid slots indexes if (newSlot < 0 || newSlot > 8) return; // Get new item ItemStack slotStack = event.entityPlayer.inventory.getStackInSlot(newSlot); // No null please if (slotStack == null) return; // Set current slot to new slot to fool Minecraft event.entityPlayer.inventory.currentItem = newSlot; // Debug info if (D3Core.debug()) logger.info("Player: " + event.entityPlayer.getDisplayName() + "\tOldSlot: " + oldSlot + "\tOldStack: " + slotStack); // Fake right click Oh look fake values :p boolean b = ((EntityPlayerMP) event.entityPlayer).theItemInWorldManager.activateBlockOrUseItem(event.entityPlayer, event.world, slotStack, event.x, event.y, event.z, event.face, 0.5f, 0.5f, 0.5f); // Remove empty stacks if (slotStack.stackSize <= 0) slotStack = null; // Debug info if (D3Core.debug()) logger.info("Player: " + event.entityPlayer.getDisplayName() + "\tNewSlot: " + newSlot + "\tNewStack: " + slotStack + "\tResult: " + b); // Set old slot back properly event.entityPlayer.inventory.currentItem = oldSlot; // Update client event.entityPlayer.inventory.setInventorySlotContents(newSlot, slotStack); ((EntityPlayerMP) event.entityPlayer).playerNetServerHandler.sendPacket(new S2FPacketSetSlot(0, newSlot + 36, slotStack)); // Prevent derpy doors event.setCanceled(true); }
@SubscribeEvent(priority = EventPriority.LOWEST) public void onPlayerInteractEvent(PlayerInteractEvent event) { if (event.isCanceled()) { // Serverside return; } if (event.action != Action.RIGHT_CLICK_BLOCK) { // Right clicking only (and on a block) return; } ItemStack current = event.entityPlayer.inventory.getCurrentItem(); if (current != null && current.getItem() instanceof ItemTool && checkIfCanPlace(current)) { EntityPlayer player = event.entityPlayer; int currentSlot = player.inventory.currentItem; int newSlot = (currentSlot == 0) ? 8 : currentSlot + 1; // Loops over the top. if (newSlot > 8 || player.inventory.getStackInSlot(newSlot) == null) { return; } player.inventory.currentItem = newSlot; ItemStack useStack = player.inventory.getStackInSlot(newSlot); if (!event.world.isRemote) { // Inspiration from TiC and TorchTools here EntityPlayerMP playerMP = (EntityPlayerMP) event.entityPlayer; // Use item if (canUseItem(useStack)) { playerMP.theItemInWorldManager.activateBlockOrUseItem(player, event.world, useStack, event.x, event.y, event.z, event.face, 0.5F, 0.5F, 0.5F); // Assume looking halfway at block // // until // ray tracing } if (useStack.stackSize <= 0) { useStack = null; } // And update (TorchTools) playerMP.playerNetServerHandler.sendPacket(new S2FPacketSetSlot(0, newSlot + 36, useStack)); // And end event.setCanceled(true); } else if (canUseItem(useStack)) { player.swingItem(); } player.inventory.currentItem = currentSlot; } }