コード例 #1
0
  private void handleLeftoverItems(IInventory items) {
    for (int i = 0; i < items.getSizeInventory(); i++) {
      if (items.getStackInSlot(i) != null) {
        ItemStack output = items.getStackInSlot(i);

        if (output.stackSize <= 0) {
          items.setInventorySlotContents(i, null);
          continue;
        }

        boolean inserted = false;

        for (int j = 2; j <= 4; j++) {
          ItemStack target = getStackInSlot(j);

          if (target == null || target.stackSize <= 0) {
            setInventorySlotContents(j, output);
            inserted = true;
            break;
          } else {
            output.stackSize -= StackHelper.mergeStacks(output, target, true);
            if (output.stackSize == 0) {
              inserted = true;
              break;
            }
          }
        }

        if (!inserted) {
          if (output.stackSize > 0) {
            output.stackSize -=
                Utils.addToRandomInventoryAround(worldObj, xCoord, yCoord, zCoord, output);

            if (output.stackSize > 0) {
              InvUtils.dropItems(worldObj, output, xCoord, yCoord + 1, zCoord);
            }
          }
        }

        items.setInventorySlotContents(i, null);
      }
    }
  }
コード例 #2
0
  @Override
  public void updateEntity() {
    super.updateEntity();

    if (getEnergy() >= getRequiredEnergy() && getEnergy() > 0) {
      ItemStack input = this.getStackInSlot(0);

      if (input == null) {
        return;
      }

      EntityPlayer internalPlayer = getInternalPlayer().get();

      if (craftSlot == null) {
        craftSlot = new SlotCrafting(internalPlayer, crafting, this, 1, 0, 0);
      }

      if (input.getItem() instanceof ItemPackage) {
        // Try a recipe made out of the package's contents
        NBTTagCompound tag = NBTUtils.getItemData(input);
        for (int i = 0; i < 9; i++) {
          if (tag.hasKey("item" + i)) {
            ItemStack is = ItemStack.loadItemStackFromNBT(tag.getCompoundTag("item" + i));
            if (is != null) {
              crafting.setInventorySlotContents(i, is);
            } else {
              return;
            }
          } else {
            crafting.setInventorySlotContents(i, null);
          }
        }
      } else {
        // Try a shapeless recipe made from just that item
        ItemStack input2 = input.copy();
        input2.stackSize = 1;
        crafting.setInventorySlotContents(0, input2);
        for (int i = 1; i < 9; i++) {
          crafting.setInventorySlotContents(i, null);
        }
      }

      IRecipe recipe = crafting.findRecipe();
      ItemStack result = recipe != null ? recipe.getCraftingResult(crafting).copy() : null;

      addEnergy(-getRequiredEnergy());

      if (result != null) {
        craftSlot.onPickupFromSlot(internalPlayer, result);
        handleLeftoverItems(crafting);
        handleLeftoverItems(internalPlayer.inventory);

        for (int i = 1; i <= 4; i++) {
          ItemStack inside = inv.getStackInSlot(i);

          if (inside == null || inside.stackSize <= 0) {
            inv.setInventorySlotContents(i, result.copy());
            result.stackSize = 0;
            break;
          } else if (StackHelper.canStacksMerge(inside, result)) {
            result.stackSize -= StackHelper.mergeStacks(result, inside, true);

            if (result.stackSize == 0) {
              break;
            }
          }
        }

        if (result.stackSize > 0) {
          EntityItem entityitem =
              new EntityItem(worldObj, xCoord + 0.5, yCoord + 0.7, zCoord + 0.5, result.copy());

          worldObj.spawnEntityInWorld(entityitem);
          result.stackSize = 0;
        }
        decrStackSize(0, 1);
      } else {
        ItemStack outputSlot = getStackInSlot(1);
        if (outputSlot == null) {
          setInventorySlotContents(1, getStackInSlot(0));
          setInventorySlotContents(0, null);
        }
      }
    }
  }