private static ItemStack tryMoveInItem(IInventory iinventory, ItemStack itemstack, int i, int j) {
    ItemStack itemstack1 = iinventory.getItem(i);

    if (canPlaceItemInInventory(iinventory, itemstack, i, j)) {
      boolean flag = false;

      if (itemstack1 == null) {
        iinventory.setItem(i, itemstack);
        itemstack = null;
        flag = true;
      } else if (canMergeItems(itemstack1, itemstack)) {
        int k = itemstack.getMaxStackSize() - itemstack1.count;
        int l = Math.min(itemstack.count, k);

        itemstack.count -= l;
        itemstack1.count += l;
        flag = l > 0;
      }

      if (flag) {
        if (iinventory instanceof TileEntityHopper) {
          ((TileEntityHopper) iinventory)
              .c(((TileEntityHopper) iinventory).world.spigotConfig.hopperTransfer); // Spigot
          iinventory.update();
        }

        iinventory.update();
      }
    }

    return itemstack;
  }
  public void setItem(int i, ItemStack itemstack) {
    this.items[i] = itemstack;
    if (itemstack != null && itemstack.count > this.getMaxStackSize()) {
      itemstack.count = this.getMaxStackSize();
    }

    this.update();
  }
  /** Called whenever the block is removed. */
  @Override
  public void remove(World par1World, int par2, int par3, int par4) {
    if (!c) {
      TileEntityIronFurnace tileentityfurnace =
          (TileEntityIronFurnace) par1World.getTileEntity(par2, par3, par4);

      if (tileentityfurnace != null) {
        label0:
        for (int i = 0; i < tileentityfurnace.getSize(); i++) {
          ItemStack itemstack = tileentityfurnace.getItem(i);

          if (itemstack == null) {
            continue;
          }

          float f = a.nextFloat() * 0.8F + 0.1F;
          float f1 = a.nextFloat() * 0.8F + 0.1F;
          float f2 = a.nextFloat() * 0.8F + 0.1F;

          do {
            if (itemstack.count <= 0) {
              continue label0;
            }

            int j = a.nextInt(21) + 10;

            if (j > itemstack.count) {
              j = itemstack.count;
            }

            itemstack.count -= j;
            EntityItem entityitem =
                new EntityItem(
                    par1World,
                    par2 + f,
                    par3 + f1,
                    par4 + f2,
                    new ItemStack(itemstack.id, j, itemstack.getData()));

            if (itemstack.hasTag()) {
              entityitem.itemStack.setTag((NBTTagCompound) itemstack.getTag().clone());
            }

            float f3 = 0.05F;
            entityitem.motX = (float) a.nextGaussian() * f3;
            entityitem.motY = (float) a.nextGaussian() * f3 + 0.2F;
            entityitem.motZ = (float) a.nextGaussian() * f3;
            par1World.addEntity(entityitem);
          } while (true);
        }
      }
    }

    super.remove(par1World, par2, par3, par4);
  }
Exemple #4
0
  public void die() {
    if (this.h) {
      for (int i = 0; i < this.getSize(); ++i) {
        ItemStack itemstack = this.getItem(i);

        if (itemstack != null) {
          float f = this.random.nextFloat() * 0.8F + 0.1F;
          float f1 = this.random.nextFloat() * 0.8F + 0.1F;
          float f2 = this.random.nextFloat() * 0.8F + 0.1F;

          while (itemstack.count > 0) {
            int j = this.random.nextInt(21) + 10;

            if (j > itemstack.count) {
              j = itemstack.count;
            }

            itemstack.count -= j;
            EntityItem entityitem =
                new EntityItem(
                    this.world,
                    this.locX + (double) f,
                    this.locY + (double) f1,
                    this.locZ + (double) f2,
                    new ItemStack(itemstack.id, j, itemstack.getData()));

            if (itemstack.hasTag()) {
              entityitem.itemStack.setTag((NBTTagCompound) itemstack.getTag().clone());
            }

            float f3 = 0.05F;

            entityitem.motX = (double) ((float) this.random.nextGaussian() * f3);
            entityitem.motY = (double) ((float) this.random.nextGaussian() * f3 + 0.2F);
            entityitem.motZ = (double) ((float) this.random.nextGaussian() * f3);
            this.world.addEntity(entityitem);
          }
        }
      }
    }

    super.die();
    if (this.g != null) {
      this.g.a();
    }
  }
  public void remove(World world, int i, int j, int k) {
    TileEntityDispenser tileentitydispenser = (TileEntityDispenser) world.getTileEntity(i, j, k);

    if (tileentitydispenser != null) {
      for (int l = 0; l < tileentitydispenser.getSize(); ++l) {
        ItemStack itemstack = tileentitydispenser.getItem(l);

        if (itemstack != null) {
          float f = this.a.nextFloat() * 0.8F + 0.1F;
          float f1 = this.a.nextFloat() * 0.8F + 0.1F;
          float f2 = this.a.nextFloat() * 0.8F + 0.1F;

          while (itemstack.count > 0) {
            int i1 = this.a.nextInt(21) + 10;

            if (i1 > itemstack.count) {
              i1 = itemstack.count;
            }

            itemstack.count -= i1;
            EntityItem entityitem =
                new EntityItem(
                    world,
                    (double) ((float) i + f),
                    (double) ((float) j + f1),
                    (double) ((float) k + f2),
                    new ItemStack(itemstack.id, i1, itemstack.getData()));

            if (itemstack.hasTag()) {
              entityitem.itemStack.setTag((NBTTagCompound) itemstack.getTag().clone());
            }

            float f3 = 0.05F;

            entityitem.motX = (double) ((float) this.a.nextGaussian() * f3);
            entityitem.motY = (double) ((float) this.a.nextGaussian() * f3 + 0.2F);
            entityitem.motZ = (double) ((float) this.a.nextGaussian() * f3);
            world.addEntity(entityitem);
          }
        }
      }
    }

    super.remove(world, i, j, k);
  }
Exemple #6
0
  public static boolean addToRandomPipeEntry(TileEntity var0, Orientations var1, ItemStack var2) {
    World var3 = var0.world; // APIProxy.getWorld();
    LinkedList var4 = new LinkedList();

    int var5;
    Position var6;
    for (var5 = 0; var5 < 6; ++var5) {
      if (var1.reverse().ordinal() != var5) {
        var6 =
            new Position(
                (double) var0.x, (double) var0.y, (double) var0.z, Orientations.values()[var5]);
        var6.moveForwards(1.0D);
        TileEntity var7 = var3.getTileEntity((int) var6.x, (int) var6.y, (int) var6.z);

        if (var7 instanceof IPipeEntry && ((IPipeEntry) var7).acceptItems()) {
          var4.add(Orientations.values()[var5]);
        }
      }
    }

    if (var4.size() > 0) {
      var5 = var3.random.nextInt(var4.size());
      var6 =
          new Position(
              (double) var0.x, (double) var0.y, (double) var0.z, (Orientations) var4.get(var5));
      Position var10 =
          new Position(
              (double) var0.x, (double) var0.y, (double) var0.z, (Orientations) var4.get(var5));
      var6.x += 0.5D;
      var6.y += (double) getPipeFloorOf(var2);
      var6.z += 0.5D;
      var6.moveForwards(0.5D);
      var10.moveForwards(1.0D);
      IPipeEntry var8 =
          (IPipeEntry) var3.getTileEntity((int) var10.x, (int) var10.y, (int) var10.z);
      EntityPassiveItem var9 = new EntityPassiveItem(var3, var6.x, var6.y, var6.z, var2);
      var8.entityEntering(var9, var6.orientation);
      var2.count = 0;
      return true;
    } else {
      return false;
    }
  }
  public void remove(World world, int i, int j, int k, int l, int i1) {
    TileEntity tileentity = world.getTileEntity(i, j, k);

    if (tileentity instanceof TileEntityBrewingStand) {
      TileEntityBrewingStand tileentitybrewingstand = (TileEntityBrewingStand) tileentity;

      for (int j1 = 0; j1 < tileentitybrewingstand.getSize(); ++j1) {
        ItemStack itemstack = tileentitybrewingstand.getItem(j1);

        if (itemstack != null) {
          float f = this.a.nextFloat() * 0.8F + 0.1F;
          float f1 = this.a.nextFloat() * 0.8F + 0.1F;
          float f2 = this.a.nextFloat() * 0.8F + 0.1F;

          while (itemstack.count > 0) {
            int k1 = this.a.nextInt(21) + 10;

            if (k1 > itemstack.count) {
              k1 = itemstack.count;
            }

            itemstack.count -= k1;
            EntityItem entityitem =
                new EntityItem(
                    world,
                    (double) ((float) i + f),
                    (double) ((float) j + f1),
                    (double) ((float) k + f2),
                    new ItemStack(itemstack.id, k1, itemstack.getData()));
            float f3 = 0.05F;

            entityitem.motX = (double) ((float) this.a.nextGaussian() * f3);
            entityitem.motY = (double) ((float) this.a.nextGaussian() * f3 + 0.2F);
            entityitem.motZ = (double) ((float) this.a.nextGaussian() * f3);
            world.addEntity(entityitem);
          }
        }
      }
    }

    super.remove(world, i, j, k, l, i1);
  }
Exemple #8
0
  public void updateInventory(Player player) {

    CraftPlayer c = (CraftPlayer) player;

    for (int i = 0; i < 36; i++) {

      int nativeindex = i;

      if (i < 9) nativeindex = i + 36;

      ItemStack olditem = c.getInventory().getItem(i);
      net.minecraft.server.ItemStack item = null;

      if (olditem != null && olditem.getType() != Material.AIR) {
        item = new net.minecraft.server.ItemStack(0, 0, 0);
        item.id = olditem.getTypeId();
        item.count = olditem.getAmount();
      }

      Packet103SetSlot pack = new Packet103SetSlot(0, nativeindex, item);
      c.getHandle().netServerHandler.sendPacket(pack);
    }
  }
  @Override
  public boolean a(InventoryCrafting inventorycrafting, World world) {
    a = null;
    int i = 0;
    int j = 0;
    int k = 0;
    int l = 0;
    int i1 = 0;
    int j1 = 0;

    for (int k1 = 0; k1 < inventorycrafting.getSize(); ++k1) {
      ItemStack itemstack = inventorycrafting.getItem(k1);

      if (itemstack != null) {
        if (itemstack.getItem() == Items.SULPHUR) {
          ++j;
        } else if (itemstack.getItem() == Items.FIREWORKS_CHARGE) {
          ++l;
        } else if (itemstack.getItem() == Items.INK_SACK) {
          ++k;
        } else if (itemstack.getItem() == Items.PAPER) {
          ++i;
        } else if (itemstack.getItem() == Items.GLOWSTONE_DUST) {
          ++i1;
        } else if (itemstack.getItem() == Items.DIAMOND) {
          ++i1;
        } else if (itemstack.getItem() == Items.FIREBALL) {
          ++j1;
        } else if (itemstack.getItem() == Items.FEATHER) {
          ++j1;
        } else if (itemstack.getItem() == Items.GOLD_NUGGET) {
          ++j1;
        } else {
          if (itemstack.getItem() != Items.SKULL) return false;

          ++j1;
        }
      }
    }

    i1 += k + j1;
    if (j <= 3 && i <= 1) {
      NBTTagCompound nbttagcompound;
      NBTTagCompound nbttagcompound1;

      if (j >= 1 && i == 1 && i1 == 0) {
        a = new ItemStack(Items.FIREWORKS);
        if (l > 0) {
          nbttagcompound = new NBTTagCompound();
          nbttagcompound1 = new NBTTagCompound();
          NBTTagList nbttaglist = new NBTTagList();

          for (int l1 = 0; l1 < inventorycrafting.getSize(); ++l1) {
            ItemStack itemstack1 = inventorycrafting.getItem(l1);

            if (itemstack1 != null
                && itemstack1.getItem() == Items.FIREWORKS_CHARGE
                && itemstack1.hasTag()
                && itemstack1.getTag().hasKeyOfType("Explosion", 10)) {
              nbttaglist.add(itemstack1.getTag().getCompound("Explosion"));
            }
          }

          nbttagcompound1.set("Explosions", nbttaglist);
          nbttagcompound1.setByte("Flight", (byte) j);
          nbttagcompound.set("Fireworks", nbttagcompound1);
          a.setTag(nbttagcompound);
        }

        return true;
      } else if (j == 1 && i == 0 && l == 0 && k > 0 && j1 <= 1) {
        a = new ItemStack(Items.FIREWORKS_CHARGE);
        nbttagcompound = new NBTTagCompound();
        nbttagcompound1 = new NBTTagCompound();
        byte b0 = 0;
        ArrayList arraylist = new ArrayList();

        for (int i2 = 0; i2 < inventorycrafting.getSize(); ++i2) {
          ItemStack itemstack2 = inventorycrafting.getItem(i2);

          if (itemstack2 != null) {
            if (itemstack2.getItem() == Items.INK_SACK) {
              arraylist.add(Integer.valueOf(ItemDye.c[itemstack2.getData()]));
            } else if (itemstack2.getItem() == Items.GLOWSTONE_DUST) {
              nbttagcompound1.setBoolean("Flicker", true);
            } else if (itemstack2.getItem() == Items.DIAMOND) {
              nbttagcompound1.setBoolean("Trail", true);
            } else if (itemstack2.getItem() == Items.FIREBALL) {
              b0 = 1;
            } else if (itemstack2.getItem() == Items.FEATHER) {
              b0 = 4;
            } else if (itemstack2.getItem() == Items.GOLD_NUGGET) {
              b0 = 2;
            } else if (itemstack2.getItem() == Items.SKULL) {
              b0 = 3;
            }
          }
        }

        int[] aint = new int[arraylist.size()];

        for (int j2 = 0; j2 < aint.length; ++j2) {
          aint[j2] = ((Integer) arraylist.get(j2)).intValue();
        }

        nbttagcompound1.setIntArray("Colors", aint);
        nbttagcompound1.setByte("Type", b0);
        nbttagcompound.set("Explosion", nbttagcompound1);
        a.setTag(nbttagcompound);
        return true;
      } else if (j == 0 && i == 0 && l == 1 && k > 0 && k == i1) {
        ArrayList arraylist1 = new ArrayList();

        for (int k2 = 0; k2 < inventorycrafting.getSize(); ++k2) {
          ItemStack itemstack3 = inventorycrafting.getItem(k2);

          if (itemstack3 != null) {
            if (itemstack3.getItem() == Items.INK_SACK) {
              arraylist1.add(Integer.valueOf(ItemDye.c[itemstack3.getData()]));
            } else if (itemstack3.getItem() == Items.FIREWORKS_CHARGE) {
              a = itemstack3.cloneItemStack();
              a.count = 1;
            }
          }
        }

        int[] aint1 = new int[arraylist1.size()];

        for (int l2 = 0; l2 < aint1.length; ++l2) {
          aint1[l2] = ((Integer) arraylist1.get(l2)).intValue();
        }

        if (a != null && a.hasTag()) {
          NBTTagCompound nbttagcompound2 = a.getTag().getCompound("Explosion");

          if (nbttagcompound2 == null) return false;
          else {
            nbttagcompound2.setIntArray("FadeColors", aint1);
            return true;
          }
        } else return false;
      } else return false;
    } else return false;
  }
  public boolean handleInventoryClick(
      Packet102WindowClick packet,
      InventorySlotType type,
      SpoutCraftItemStack slot,
      SpoutCraftItemStack cursor,
      Inventory inventory) {
    InventoryClickEvent event = null;
    Result result = Result.DEFAULT;
    boolean success = false;
    final int LEFT_CLICK = 0;
    final int RIGHT_CLICK = 1;
    int click = packet.c;

    // clicked on bottom player inventory
    if (!(this.player.activeContainer instanceof ContainerPlayer)
        && this.player.defaultContainer instanceof ContainerPlayer
        && packet.b >= inventory.getSize()) {
      int activeSlot = packet.b - inventory.getSize() + 9;
      if (activeSlot >= this.getPlayer().getInventory().getSize()) {
        activeSlot -= this.getPlayer().getInventory().getSize();
      }
      type = getInventorySlotType(activeSlot);
      event =
          new InventoryPlayerClickEvent(
              this.getPlayer(),
              this.getPlayer().getInventory(),
              type,
              slot,
              cursor,
              activeSlot,
              click == LEFT_CLICK,
              packet.f,
              activeLocation);
    } else {
      event =
          new InventoryClickEvent(
              this.getPlayer(),
              inventory,
              type,
              slot,
              cursor,
              packet.b,
              click == LEFT_CLICK,
              packet.f,
              activeLocation);
    }

    if (event != null) {
      Bukkit.getServer().getPluginManager().callEvent(event);
      result = event.getResult();
      cursor = SpoutCraftItemStack.getCraftItemStack(event.getCursor());
      slot = SpoutCraftItemStack.getCraftItemStack(event.getItem());
    }

    // initialize setup
    ItemStack itemstack = slot != null ? slot.getHandle() : null;
    ItemStack cursorstack = cursor != null ? cursor.getHandle() : null;

    // NOTE: Successful means that its successful as-is; thus, only becomes true for default
    // behaviour

    switch (result) {
      case DEFAULT:
        itemstack = this.player.activeContainer.a(packet.b, packet.c, packet.f, this.player);
        success = ItemStack.equals(packet.e, itemstack);
        break;
      case DENY:
        if (packet.b != -999) { // Only swap if target is not OUTSIDE
          if (itemstack != null) {
            setActiveSlot(packet.b, itemstack);
            setCursorSlot((ItemStack) null);
          }
          if (event.getCursor() != null) {
            setActiveSlot(packet.b, itemstack);
            setCursorSlot(cursorstack);
          }
        }

        break;
      case ALLOW: // Allow the placement unconditionally
        if (packet.b == -999) { // Clicked outside, just defer to default
          itemstack = this.player.activeContainer.a(packet.b, packet.c, packet.f, this.player);
        } else {
          if (click == LEFT_CLICK
              && (itemstack != null
                  && cursorstack != null
                  && itemstack.doMaterialsMatch(cursorstack))) {
            // Left-click full slot with full cursor of same item; merge stacks
            itemstack.count += cursorstack.count;
            cursorstack = null;
          } else if (click == LEFT_CLICK
              || (itemstack != null
                  && cursorstack != null
                  && !itemstack.doMaterialsMatch(cursorstack))) {
            // Either left-click, or right-click full slot with full cursor of different item; just
            // swap contents
            ItemStack temp = itemstack;
            itemstack = cursorstack;
            cursorstack = temp;
          } else if (click == RIGHT_CLICK) { // Right-click with either slot or cursor empty
            if (itemstack == null) { // Slot empty; drop one
              if (cursorstack != null) {
                itemstack = cursorstack.a(1);
                if (cursorstack.count == 0) {
                  cursorstack = null;
                }
              }
            } else if (cursorstack == null) { // Cursor empty; take half
              cursorstack = itemstack.a((itemstack.count + 1) / 2);
            } else { // Neither empty, but same item; drop one
              ItemStack drop = cursorstack.a(1);
              itemstack.count += drop.count;
              if (cursorstack.count == 0) {
                cursorstack = null;
              }
            }
          }
          // update the stacks
          setActiveSlot(packet.b, itemstack);
          setCursorSlot(cursorstack);
        }
        break;
    }
    return success;
  }
  public ItemStack b(ISourceBlock isourceblock, ItemStack itemstack) {
    ItemBucket itembucket = (ItemBucket) itemstack.getItem();
    BlockPosition blockposition =
        isourceblock.getBlockPosition().shift(BlockDispenser.b(isourceblock.f()));

    // CraftBukkit start
    World world = isourceblock.i();
    int x = blockposition.getX();
    int y = blockposition.getY();
    int z = blockposition.getZ();
    if (world.isEmpty(blockposition)
        || !world.getType(blockposition).getBlock().getMaterial().isBuildable()) {
      org.bukkit.block.Block block =
          world
              .getWorld()
              .getBlockAt(
                  isourceblock.getBlockPosition().getX(),
                  isourceblock.getBlockPosition().getY(),
                  isourceblock.getBlockPosition().getZ());
      CraftItemStack craftItem = CraftItemStack.asCraftMirror(itemstack);

      BlockDispenseEvent event =
          new BlockDispenseEvent(block, craftItem.clone(), new org.bukkit.util.Vector(x, y, z));
      if (!BlockDispenser.eventFired) {
        world.getServer().getPluginManager().callEvent(event);
      }

      if (event.isCancelled()) {
        return itemstack;
      }

      if (!event.getItem().equals(craftItem)) {
        // Chain to handler for new item
        ItemStack eventStack = CraftItemStack.asNMSCopy(event.getItem());
        IDispenseBehavior idispensebehavior =
            (IDispenseBehavior) BlockDispenser.M.get(eventStack.getItem());
        if (idispensebehavior != IDispenseBehavior.a && idispensebehavior != this) {
          idispensebehavior.a(isourceblock, eventStack);
          return itemstack;
        }
      }

      itembucket = (ItemBucket) CraftItemStack.asNMSCopy(event.getItem()).getItem();
    }
    // CraftBukkit end

    if (itembucket.a(isourceblock.i(), blockposition)) {
      // CraftBukkit start - Handle stacked buckets
      Item item = Items.BUCKET;
      if (--itemstack.count == 0) {
        itemstack.setItem(Items.BUCKET);
        itemstack.count = 1;
      } else if (((TileEntityDispenser) isourceblock.getTileEntity()).addItem(new ItemStack(item))
          < 0) {
        this.b.a(isourceblock, new ItemStack(item));
      }
      // CraftBukkit end
      return itemstack;
    } else {
      return this.b.a(isourceblock, itemstack);
    }
  }
Exemple #12
0
  public boolean damageEntity(DamageSource damagesource, int i) {
    if (!this.world.isStatic && !this.dead) {
      if (this.isInvulnerable()) {
        return false;
      } else {
        this.i(-this.k());
        this.h(10);
        this.K();
        this.setDamage(this.getDamage() + i * 10);
        if (damagesource.getEntity() instanceof EntityHuman
            && ((EntityHuman) damagesource.getEntity()).abilities.canInstantlyBuild) {
          this.setDamage(100);
        }

        if (this.getDamage() > 40) {
          if (this.passenger != null) {
            this.passenger.mount(this);
          }

          this.die();
          this.a(Item.MINECART.id, 1, 0.0F);
          if (this.type == 1) {
            EntityMinecart entityminecart = this;

            for (int j = 0; j < entityminecart.getSize(); ++j) {
              ItemStack itemstack = entityminecart.getItem(j);

              if (itemstack != null) {
                float f = this.random.nextFloat() * 0.8F + 0.1F;
                float f1 = this.random.nextFloat() * 0.8F + 0.1F;
                float f2 = this.random.nextFloat() * 0.8F + 0.1F;

                while (itemstack.count > 0) {
                  int k = this.random.nextInt(21) + 10;

                  if (k > itemstack.count) {
                    k = itemstack.count;
                  }

                  itemstack.count -= k;
                  EntityItem entityitem =
                      new EntityItem(
                          this.world,
                          this.locX + (double) f,
                          this.locY + (double) f1,
                          this.locZ + (double) f2,
                          new ItemStack(itemstack.id, k, itemstack.getData()));
                  float f3 = 0.05F;

                  entityitem.motX = (double) ((float) this.random.nextGaussian() * f3);
                  entityitem.motY = (double) ((float) this.random.nextGaussian() * f3 + 0.2F);
                  entityitem.motZ = (double) ((float) this.random.nextGaussian() * f3);
                  this.world.addEntity(entityitem);
                }
              }
            }

            this.a(Block.CHEST.id, 1, 0.0F);
          } else if (this.type == 2) {
            this.a(Block.FURNACE.id, 1, 0.0F);
          }
        }

        return true;
      }
    } else {
      return true;
    }
  }
  protected boolean a(ItemStack itemstack, int i, int j, boolean flag) {
    boolean flag1 = false;
    int k = i;

    if (flag) {
      k = j - 1;
    }

    Slot slot;
    ItemStack itemstack1;

    if (itemstack.isStackable()) {
      while (itemstack.count > 0 && (!flag && k < j || flag && k >= i)) {
        slot = (Slot) this.c.get(k);
        itemstack1 = slot.getItem();
        if (itemstack1 != null
            && itemstack1.id == itemstack.id
            && (!itemstack.usesData() || itemstack.getData() == itemstack1.getData())
            && ItemStack.equals(itemstack, itemstack1)) {
          int l = itemstack1.count + itemstack.count;

          if (l <= itemstack.getMaxStackSize()) {
            itemstack.count = 0;
            itemstack1.count = l;
            slot.e();
            flag1 = true;
          } else if (itemstack1.count < itemstack.getMaxStackSize()) {
            itemstack.count -= itemstack.getMaxStackSize() - itemstack1.count;
            itemstack1.count = itemstack.getMaxStackSize();
            slot.e();
            flag1 = true;
          }
        }

        if (flag) {
          --k;
        } else {
          ++k;
        }
      }
    }

    if (itemstack.count > 0) {
      if (flag) {
        k = j - 1;
      } else {
        k = i;
      }

      while (!flag && k < j || flag && k >= i) {
        slot = (Slot) this.c.get(k);
        itemstack1 = slot.getItem();
        if (itemstack1 == null) {
          slot.set(itemstack.cloneItemStack());
          slot.e();
          itemstack.count = 0;
          flag1 = true;
          break;
        }

        if (flag) {
          --k;
        } else {
          ++k;
        }
      }
    }

    return flag1;
  }
  public ItemStack clickItem(int i, int j, int k, EntityHuman entityhuman) {
    ItemStack itemstack = null;
    PlayerInventory playerinventory = entityhuman.inventory;
    Slot slot;
    ItemStack itemstack1;
    int l;
    ItemStack itemstack2;

    if ((k == 0 || k == 1) && (j == 0 || j == 1)) {
      if (i == -999) {
        if (playerinventory.getCarried() != null && i == -999) {
          if (j == 0) {
            entityhuman.drop(playerinventory.getCarried());
            playerinventory.setCarried((ItemStack) null);
          }

          if (j == 1) {
            // CraftBukkit start - store a reference
            ItemStack itemstack3 = playerinventory.getCarried();
            if (itemstack3.count > 0) {
              entityhuman.drop(itemstack3.a(1));
            }

            if (itemstack3.count == 0) {
              // CraftBukkit end
              playerinventory.setCarried((ItemStack) null);
            }
          }
        }
      } else if (k == 1) {
        slot = (Slot) this.c.get(i);
        if (slot != null && slot.a(entityhuman)) {
          itemstack1 = this.b(entityhuman, i);
          if (itemstack1 != null) {
            int i1 = itemstack1.id;

            itemstack = itemstack1.cloneItemStack();
            if (slot != null && slot.getItem() != null && slot.getItem().id == i1) {
              this.a(i, j, true, entityhuman);
            }
          }
        }
      } else {
        if (i < 0) {
          return null;
        }

        slot = (Slot) this.c.get(i);
        if (slot != null) {
          itemstack1 = slot.getItem();
          ItemStack itemstack3 = playerinventory.getCarried();

          if (itemstack1 != null) {
            itemstack = itemstack1.cloneItemStack();
          }

          if (itemstack1 == null) {
            if (itemstack3 != null && slot.isAllowed(itemstack3)) {
              l = j == 0 ? itemstack3.count : 1;
              if (l > slot.a()) {
                l = slot.a();
              }

              // CraftBukkit start
              if (itemstack3.count >= l) {
                slot.set(itemstack3.a(l));
              }
              // CraftBukkit end

              if (itemstack3.count == 0) {
                playerinventory.setCarried((ItemStack) null);
              }
            }
          } else if (slot.a(entityhuman)) {
            if (itemstack3 == null) {
              l = j == 0 ? itemstack1.count : (itemstack1.count + 1) / 2;
              itemstack2 = slot.a(l);
              playerinventory.setCarried(itemstack2);
              if (itemstack1.count == 0) {
                slot.set((ItemStack) null);
              }

              slot.a(entityhuman, playerinventory.getCarried());
            } else if (slot.isAllowed(itemstack3)) {
              if (itemstack1.id == itemstack3.id
                  && (!itemstack1.usesData() || itemstack1.getData() == itemstack3.getData())
                  && ItemStack.equals(itemstack1, itemstack3)) {
                l = j == 0 ? itemstack3.count : 1;
                if (l > slot.a() - itemstack1.count) {
                  l = slot.a() - itemstack1.count;
                }

                if (l > itemstack3.getMaxStackSize() - itemstack1.count) {
                  l = itemstack3.getMaxStackSize() - itemstack1.count;
                }

                itemstack3.a(l);
                if (itemstack3.count == 0) {
                  playerinventory.setCarried((ItemStack) null);
                }

                itemstack1.count += l;
              } else if (itemstack3.count <= slot.a()) {
                slot.set(itemstack3);
                playerinventory.setCarried(itemstack1);
              }
            } else if (itemstack1.id == itemstack3.id
                && itemstack3.getMaxStackSize() > 1
                && (!itemstack1.usesData() || itemstack1.getData() == itemstack3.getData())
                && ItemStack.equals(itemstack1, itemstack3)) {
              l = itemstack1.count;
              if (l > 0 && l + itemstack3.count <= itemstack3.getMaxStackSize()) {
                itemstack3.count += l;
                itemstack1 = slot.a(l);
                if (itemstack1.count == 0) {
                  slot.set((ItemStack) null);
                }

                slot.a(entityhuman, playerinventory.getCarried());
              }
            }
          }

          slot.e();
        }
      }
    } else if (k == 2 && j >= 0 && j < 9) {
      slot = (Slot) this.c.get(i);
      if (slot.a(entityhuman)) {
        itemstack1 = playerinventory.getItem(j);
        boolean flag =
            itemstack1 == null || slot.inventory == playerinventory && slot.isAllowed(itemstack1);

        l = -1;
        if (!flag) {
          l = playerinventory.i();
          flag |= l > -1;
        }

        if (slot.d() && flag) {
          itemstack2 = slot.getItem();
          playerinventory.setItem(j, itemstack2);
          if ((slot.inventory != playerinventory || !slot.isAllowed(itemstack1))
              && itemstack1 != null) {
            if (l > -1) {
              playerinventory.pickup(itemstack1);
              slot.set((ItemStack) null);
              slot.a(entityhuman, itemstack2);
            }
          } else {
            slot.set(itemstack1);
            slot.a(entityhuman, itemstack2);
          }
        } else if (!slot.d() && itemstack1 != null && slot.isAllowed(itemstack1)) {
          playerinventory.setItem(j, (ItemStack) null);
          slot.set(itemstack1);
        }
      }
    } else if (k == 3
        && entityhuman.abilities.canInstantlyBuild
        && playerinventory.getCarried() == null
        && i >= 0) {
      slot = (Slot) this.c.get(i);
      if (slot != null && slot.d()) {
        itemstack1 = slot.getItem().cloneItemStack();
        itemstack1.count = itemstack1.getMaxStackSize();
        playerinventory.setCarried(itemstack1);
      }
    }

    return itemstack;
  }