@Override
  public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par1) {
    ItemStack var2 = null;
    Slot var3 = (Slot) this.inventorySlots.get(par1);

    if (var3 != null && var3.getHasStack()) {
      ItemStack var4 = var3.getStack();
      var2 = var4.copy();

      if (par1 == 0) {
        if (!this.mergeItemStack(var4, 1, 37, true)) {
          return null;
        }

        var3.onSlotChange(var4, var2);
      } else if (par1 != 1 && par1 != 0) {
        if (var4.getItem() instanceof IItemElectric) {
          Slot temp = (Slot) this.inventorySlots.get(0);
          if (!temp.getHasStack()) {
            temp.putStack(var4);
            var3.decrStackSize(1);
          }
          if (!this.mergeItemStack(var4, 0, 0, false)) {
            return null;
          }
        } else if (par1 >= 1 && par1 < 28) {
          if (!this.mergeItemStack(var4, 32, 40, false)) {
            return null;
          }
        } else if (par1 >= 32 && par1 < 40 && !this.mergeItemStack(var4, 3, 30, false)) {
          return null;
        }
      } else if (!this.mergeItemStack(var4, 4, 31, false)) {
        return null;
      }

      if (var4.stackSize == 0) {
        var3.putStack((ItemStack) null);
      } else {
        var3.onSlotChanged();
      }

      if (var4.stackSize == var2.stackSize) {
        return null;
      }

      var3.onPickupFromSlot(par1EntityPlayer, var4);
    }

    return var2;
  }
Exemplo n.º 2
0
  public ItemStack slotClick(int par1, int par2, int par3, EntityPlayer par4EntityPlayer) {
    ItemStack var5 = null;
    InventoryPlayer var6 = par4EntityPlayer.inventory;
    Slot var7;
    ItemStack var8;
    int var10;
    ItemStack var11;

    if ((par3 == 0 || par3 == 1) && (par2 == 0 || par2 == 1)) {
      if (par1 == -999) {
        if (var6.getItemStack() != null && par1 == -999) {
          if (par2 == 0) {
            par4EntityPlayer.dropPlayerItem(var6.getItemStack());
            var6.setItemStack((ItemStack) null);
          }

          if (par2 == 1) {
            // CraftBukkit start - store a reference
            ItemStack itemstack3 = var6.getItemStack();
            par4EntityPlayer.dropPlayerItem(itemstack3.splitStack(1));

            if (itemstack3.stackSize == 0) {
              // CraftBukkit end
              var6.setItemStack((ItemStack) null);
            }
          }
        }
      } else if (par3 == 1) {
        var7 = (Slot) this.inventorySlots.get(par1);

        if (var7 != null && var7.canTakeStack(par4EntityPlayer)) {
          var8 = this.transferStackInSlot(par4EntityPlayer, par1);

          if (var8 != null) {
            int var12 = var8.itemID;
            var5 = var8.copy();

            if (var7 != null && var7.getStack() != null && var7.getStack().itemID == var12) {
              this.retrySlotClick(par1, par2, true, par4EntityPlayer);
            }
          }
        }
      } else {
        if (par1 < 0) {
          return null;
        }

        var7 = (Slot) this.inventorySlots.get(par1);

        if (var7 != null) {
          var8 = var7.getStack();
          ItemStack var13 = var6.getItemStack();

          if (var8 != null) {
            var5 = var8.copy();
          }

          if (var8 == null) {
            if (var13 != null && var7.isItemValid(var13)) {
              var10 = par2 == 0 ? var13.stackSize : 1;

              if (var10 > var7.getSlotStackLimit()) {
                var10 = var7.getSlotStackLimit();
              }

              if (var13.stackSize >= var10) // CraftBukkit
              var7.putStack(var13.splitStack(var10));

              if (var13.stackSize == 0) {
                var6.setItemStack((ItemStack) null);
              }
            }
          } else if (var7.canTakeStack(par4EntityPlayer)) {
            if (var13 == null) {
              var10 = par2 == 0 ? var8.stackSize : (var8.stackSize + 1) / 2;
              var11 = var7.decrStackSize(var10);
              var6.setItemStack(var11);

              if (var8.stackSize == 0) {
                var7.putStack((ItemStack) null);
              }

              var7.onPickupFromSlot(par4EntityPlayer, var6.getItemStack());
            } else if (var7.isItemValid(var13)) {
              if (var8.itemID == var13.itemID
                  && var8.getItemDamage() == var13.getItemDamage()
                  && ItemStack.areItemStackTagsEqual(var8, var13)) {
                var10 = par2 == 0 ? var13.stackSize : 1;

                if (var10 > var7.getSlotStackLimit() - var8.stackSize) {
                  var10 = var7.getSlotStackLimit() - var8.stackSize;
                }

                if (var10 > var13.getMaxStackSize() - var8.stackSize) {
                  var10 = var13.getMaxStackSize() - var8.stackSize;
                }

                var13.splitStack(var10);

                if (var13.stackSize == 0) {
                  var6.setItemStack((ItemStack) null);
                }

                var8.stackSize += var10;
              } else if (var13.stackSize <= var7.getSlotStackLimit()) {
                var7.putStack(var13);
                var6.setItemStack(var8);
              }
            } else if (var8.itemID == var13.itemID
                && var13.getMaxStackSize() > 1
                && (!var8.getHasSubtypes() || var8.getItemDamage() == var13.getItemDamage())
                && ItemStack.areItemStackTagsEqual(var8, var13)) {
              var10 = var8.stackSize;

              if (var10 > 0 && var10 + var13.stackSize <= var13.getMaxStackSize()) {
                var13.stackSize += var10;
                var8 = var7.decrStackSize(var10);

                if (var8.stackSize == 0) {
                  var7.putStack((ItemStack) null);
                }

                var7.onPickupFromSlot(par4EntityPlayer, var6.getItemStack());
              }
            }
          }

          var7.onSlotChanged();
        }
      }
    } else if (par3 == 2 && par2 >= 0 && par2 < 9) {
      var7 = (Slot) this.inventorySlots.get(par1);

      if (var7.canTakeStack(par4EntityPlayer)) {
        var8 = var6.getStackInSlot(par2);
        boolean var9 = var8 == null || var7.inventory == var6 && var7.isItemValid(var8);
        var10 = -1;

        if (!var9) {
          var10 = var6.getFirstEmptyStack();
          var9 |= var10 > -1;
        }

        if (var7.getHasStack() && var9) {
          var11 = var7.getStack();
          var6.setInventorySlotContents(par2, var11);

          if ((var7.inventory != var6 || !var7.isItemValid(var8)) && var8 != null) {
            if (var10 > -1) {
              var6.addItemStackToInventory(var8);
              var7.decrStackSize(var11.stackSize);
              var7.putStack((ItemStack) null);
              var7.onPickupFromSlot(par4EntityPlayer, var11);
            }
          } else {
            var7.decrStackSize(var11.stackSize);
            var7.putStack(var8);
            var7.onPickupFromSlot(par4EntityPlayer, var11);
          }
        } else if (!var7.getHasStack() && var8 != null && var7.isItemValid(var8)) {
          var6.setInventorySlotContents(par2, (ItemStack) null);
          var7.putStack(var8);
        }
      }
    } else if (par3 == 3
        && par4EntityPlayer.capabilities.isCreativeMode
        && var6.getItemStack() == null
        && par1 >= 0) {
      var7 = (Slot) this.inventorySlots.get(par1);

      if (var7 != null && var7.getHasStack()) {
        var8 = var7.getStack().copy();
        var8.stackSize = var8.getMaxStackSize();
        var6.setItemStack(var8);
      }
    }

    return var5;
  }
  // patch method
  @Overwrite
  public void processVanilla250Packet(C17PacketCustomPayload p_147349_1_) {
    PacketBuffer packetbuffer;
    ItemStack itemstack;
    ItemStack itemstack1;

    if ("MC|BEdit".equals(p_147349_1_.func_149559_c())) {
      packetbuffer = new PacketBuffer(Unpooled.wrappedBuffer(p_147349_1_.func_149558_e()));

      try {
        itemstack = packetbuffer.readItemStackFromBuffer();

        if (itemstack == null) {
          return;
        }

        if (!ItemWritableBook.func_150930_a(itemstack.getTagCompound())) {
          throw new IOException("Invalid book tag!");
        }

        itemstack1 = playerEntity.inventory.getCurrentItem();

        if (itemstack1 != null) {
          if (itemstack.getItem() == Items.writable_book
              && itemstack.getItem() == itemstack1.getItem()) {
            itemstack1.setTagInfo("pages", itemstack.getTagCompound().getTagList("pages", 8));
          }

          return;
        }
      } catch (Exception exception4) {
        logger.error("Couldn\'t handle book info", exception4);
        return;
      } finally {
        packetbuffer.release();
      }

      return;
    } else if ("MC|BSign".equals(p_147349_1_.func_149559_c())) {
      packetbuffer = new PacketBuffer(Unpooled.wrappedBuffer(p_147349_1_.func_149558_e()));

      try {
        itemstack = packetbuffer.readItemStackFromBuffer();

        if (itemstack != null) {
          if (!ItemEditableBook.validBookTagContents(itemstack.getTagCompound())) {
            throw new IOException("Invalid book tag!");
          }

          itemstack1 = playerEntity.inventory.getCurrentItem();

          if (itemstack1 == null) {
            return;
          }

          if (itemstack.getItem() == Items.written_book
              && itemstack1.getItem() == Items.writable_book) {
            itemstack1.setTagInfo("author", new NBTTagString(playerEntity.getCommandSenderName()));
            itemstack1.setTagInfo(
                "title", new NBTTagString(itemstack.getTagCompound().getString("title")));
            itemstack1.setTagInfo("pages", itemstack.getTagCompound().getTagList("pages", 8));
            itemstack1.func_150996_a(Items.written_book);
          }

          return;
        }
      } catch (Exception exception3) {
        logger.error("Couldn\'t sign book", exception3);
        return;
      } finally {
        packetbuffer.release();
      }

      return;
    } else {
      DataInputStream datainputstream;
      int i;

      if ("MC|TrSel".equals(p_147349_1_.func_149559_c())) {
        try {
          datainputstream =
              new DataInputStream(new ByteArrayInputStream(p_147349_1_.func_149558_e()));
          i = datainputstream.readInt();
          Container container = playerEntity.openContainer;

          if (container instanceof ContainerMerchant) {
            ((ContainerMerchant) container).setCurrentRecipeIndex(i);
          }
        } catch (Exception exception2) {
          logger.error("Couldn\'t select trade", exception2);
        }
      } else if ("MC|AdvCdm".equals(p_147349_1_.func_149559_c())) {
        if (!serverController.isCommandBlockEnabled()) {
          playerEntity.addChatMessage(
              new ChatComponentTranslation("advMode.notEnabled", new Object[0]));
        } else if (PermissionManager.checkPermission(playerEntity, "mc.cmdblocks")
            && playerEntity.capabilities.isCreativeMode) {
          packetbuffer = new PacketBuffer(Unpooled.wrappedBuffer(p_147349_1_.func_149558_e()));

          try {
            byte b0 = packetbuffer.readByte();
            CommandBlockLogic commandblocklogic = null;

            if (b0 == 0) {
              TileEntity tileentity =
                  playerEntity.worldObj.getTileEntity(
                      packetbuffer.readInt(), packetbuffer.readInt(), packetbuffer.readInt());

              if (tileentity instanceof TileEntityCommandBlock) {
                commandblocklogic = ((TileEntityCommandBlock) tileentity).func_145993_a();
              }
            } else if (b0 == 1) {
              Entity entity = playerEntity.worldObj.getEntityByID(packetbuffer.readInt());

              if (entity instanceof EntityMinecartCommandBlock) {
                commandblocklogic = ((EntityMinecartCommandBlock) entity).func_145822_e();
              }
            }

            String s1 = packetbuffer.readStringFromBuffer(packetbuffer.readableBytes());

            if (commandblocklogic != null) {
              commandblocklogic.func_145752_a(s1);
              commandblocklogic.func_145756_e();
              playerEntity.addChatMessage(
                  new ChatComponentTranslation("advMode.setCommand.success", new Object[] {s1}));
            }
          } catch (Exception exception1) {
            logger.error("Couldn\'t set command block", exception1);
          } finally {
            packetbuffer.release();
          }
        } else {
          playerEntity.addChatMessage(
              new ChatComponentTranslation("advMode.notAllowed", new Object[0]));
        }
      } else if ("MC|Beacon".equals(p_147349_1_.func_149559_c())) {
        if (playerEntity.openContainer instanceof ContainerBeacon) {
          try {
            datainputstream =
                new DataInputStream(new ByteArrayInputStream(p_147349_1_.func_149558_e()));
            i = datainputstream.readInt();
            int j = datainputstream.readInt();
            ContainerBeacon containerbeacon = (ContainerBeacon) playerEntity.openContainer;
            Slot slot = containerbeacon.getSlot(0);

            if (slot.getHasStack()) {
              slot.decrStackSize(1);
              TileEntityBeacon tileentitybeacon = containerbeacon.func_148327_e();
              tileentitybeacon.setPrimaryEffect(i);
              tileentitybeacon.setSecondaryEffect(j);
              tileentitybeacon.markDirty();
            }
          } catch (Exception exception) {
            logger.error("Couldn\'t set beacon", exception);
          }
        }
      } else if ("MC|ItemName".equals(p_147349_1_.func_149559_c())
          && playerEntity.openContainer instanceof ContainerRepair) {
        ContainerRepair containerrepair = (ContainerRepair) playerEntity.openContainer;

        if (p_147349_1_.func_149558_e() != null && p_147349_1_.func_149558_e().length >= 1) {
          String s =
              ChatAllowedCharacters.filerAllowedCharacters(
                  new String(p_147349_1_.func_149558_e(), Charsets.UTF_8));

          if (s.length() <= 30) {
            containerrepair.updateItemName(s);
          }
        } else {
          containerrepair.updateItemName("");
        }
      }
    }
  }