コード例 #1
0
  public static InvStack takeTopStack(IInventory inventory, int side) {
    if (!(inventory instanceof ISidedInventory)) {
      inventory = checkChestInv(inventory);

      for (int i = inventory.getSizeInventory() - 1; i >= 0; i--) {
        if (inventory.getStackInSlot(i) != null) {
          ItemStack toSend = inventory.getStackInSlot(i).copy();
          return new InvStack(inventory, i, toSend);
        }
      }
    } else {
      ISidedInventory sidedInventory = (ISidedInventory) inventory;
      int[] slots =
          sidedInventory.getAccessibleSlotsFromSide(
              ForgeDirection.getOrientation(side).getOpposite().ordinal());

      if (slots != null && slots.length != 0) {
        for (int get = slots.length - 1; get >= 0; get--) {
          int slotID = slots[get];

          if (sidedInventory.getStackInSlot(slotID) != null) {
            ItemStack toSend = sidedInventory.getStackInSlot(slotID);

            if (sidedInventory.canExtractItem(
                slotID, toSend, ForgeDirection.getOrientation(side).getOpposite().ordinal())) {
              return new InvStack(inventory, slotID, toSend);
            }
          }
        }
      }
    }

    return null;
  }
コード例 #2
0
 public static ItemStack insertStackIntoInventory(
     IInventory inventory, ItemStack stack, int side) {
   if (stack == null || inventory == null) return null;
   int stackSize = stack.stackSize;
   if (inventory instanceof ISidedInventory) {
     ISidedInventory sidedInv = (ISidedInventory) inventory;
     int slots[] = sidedInv.getAccessibleSlotsFromSide(side);
     if (slots == null) return stack;
     for (int i = 0; i < slots.length && stack != null; i++) {
       if (sidedInv.canInsertItem(slots[i], stack, side)) {
         ItemStack existingStack = inventory.getStackInSlot(slots[i]);
         if (OreDictionary.itemMatches(existingStack, stack, true)
             && ItemStack.areItemStackTagsEqual(stack, existingStack))
           stack = addToOccupiedSlot(sidedInv, slots[i], stack, existingStack);
       }
     }
     for (int i = 0; i < slots.length && stack != null; i++)
       if (inventory.getStackInSlot(slots[i]) == null
           && sidedInv.canInsertItem(slots[i], stack, side))
         stack = addToEmptyInventorySlot(sidedInv, slots[i], stack);
   } else {
     int invSize = inventory.getSizeInventory();
     for (int i = 0; i < invSize && stack != null; i++) {
       ItemStack existingStack = inventory.getStackInSlot(i);
       if (OreDictionary.itemMatches(existingStack, stack, true)
           && ItemStack.areItemStackTagsEqual(stack, existingStack))
         stack = addToOccupiedSlot(inventory, i, stack, existingStack);
     }
     for (int i = 0; i < invSize && stack != null; i++)
       if (inventory.getStackInSlot(i) == null)
         stack = addToEmptyInventorySlot(inventory, i, stack);
   }
   if (stack == null || stack.stackSize != stackSize) inventory.markDirty();
   return stack;
 }
コード例 #3
0
 public static boolean canInsertStackIntoInventory(
     IInventory inventory, ItemStack stack, int side) {
   if (stack == null || inventory == null) return false;
   if (inventory instanceof ISidedInventory) {
     ISidedInventory sidedInv = (ISidedInventory) inventory;
     int slots[] = sidedInv.getAccessibleSlotsFromSide(side);
     if (slots == null) return false;
     for (int i = 0; i < slots.length && stack != null; i++) {
       if (sidedInv.canInsertItem(slots[i], stack, side)
           && sidedInv.isItemValidForSlot(slots[i], stack)) {
         ItemStack existingStack = inventory.getStackInSlot(slots[i]);
         if (existingStack == null) return true;
         else if (OreDictionary.itemMatches(existingStack, stack, true)
             && ItemStack.areItemStackTagsEqual(stack, existingStack))
           if (existingStack.stackSize + stack.stackSize < inventory.getInventoryStackLimit()
               && existingStack.stackSize + stack.stackSize < existingStack.getMaxStackSize())
             return true;
       }
     }
   } else {
     int invSize = inventory.getSizeInventory();
     for (int i = 0; i < invSize && stack != null; i++)
       if (inventory.isItemValidForSlot(i, stack)) {
         ItemStack existingStack = inventory.getStackInSlot(i);
         if (existingStack == null) return true;
         else if (OreDictionary.itemMatches(existingStack, stack, true)
             && ItemStack.areItemStackTagsEqual(stack, existingStack))
           if (existingStack.stackSize + stack.stackSize < inventory.getInventoryStackLimit()
               && existingStack.stackSize + stack.stackSize < existingStack.getMaxStackSize())
             return true;
       }
   }
   return false;
 }
コード例 #4
0
 public boolean getHasError() {
   if (idea.getStackInSlot(0) == null) return true;
   if (idea.getStackInSlot(0).stackTagCompound.hasKey(Constants.NBT_IDEA) == false) return true;
   if (idea.getStackInSlot(0).stackTagCompound.hasKey(Constants.NBT_REFINED) == false) return true;
   if (idea.getStackInSlot(0).stackTagCompound.getInteger(Constants.NBT_REFINED) < 3) return true;
   return false;
 }
コード例 #5
0
ファイル: CraftHandler.java プロジェクト: huter/Camping-Mod
  @Override
  public void onCrafting(EntityPlayer player, ItemStack item, IInventory inv) {
    if (item.itemID == ModItems.Marshmallow.itemID) {
      if (item.getItemDamage() == 0) {
        ItemStack bowl = new ItemStack(Item.bowlEmpty);
        player.inventory.addItemStackToInventory(bowl);
      }
    }

    for (int i = 0; i < inv.getSizeInventory(); i++) {
      if (inv.getStackInSlot(i) != null) {
        ItemStack j = inv.getStackInSlot(i);
        if (j.getItem() != null && ToolHelper.isTool(j) && !ToolHelper.isTool(item)) {
          ItemStack k = ToolHelper.addDamage(j, player);
          if (k != null) k.stackSize++;
          inv.setInventorySlotContents(i, k);
        }
      }
    }

    if (item.itemID == ModItems.CampingBag.itemID) {
      for (int i = 0; i < inv.getSizeInventory(); i++) {
        if (inv.getStackInSlot(i) != null) {
          ItemStack j = inv.getStackInSlot(i);
          if (j.getItem() != null && j.itemID == ModItems.CampingBag.itemID) {
            if (j.hasTagCompound()) {
              item.setTagCompound((NBTTagCompound) j.getTagCompound().copy());
            }
          }
        }
      }
    }

    if (item.itemID == ModBlocks.campfire.blockID) {
      if (item.getItemDamage() == campfire[0].getItemDamage())
        player.addStat(ModAchievements.Campfire, 1);
      if (item.getItemDamage() == campfire[1].getItemDamage())
        player.addStat(ModAchievements.CampfireMultiCook, 1);
      if (item.getItemDamage() == campfire[2].getItemDamage())
        player.addStat(ModAchievements.CampfireFastCook, 1);
      if (item.getItemDamage() == campfire[3].getItemDamage())
        player.addStat(ModAchievements.CampfireCheapCook, 1);
      if (item.getItemDamage() == campfire[4].getItemDamage())
        player.addStat(ModAchievements.CampfireInstaCook, 1);
    }

    if (item.itemID == ModItems.CampingBag.itemID) {
      if (item.getItemDamage() == bag[0].getItemDamage())
        player.addStat(ModAchievements.CampingBagSmall, 1);
      if (item.getItemDamage() == bag[1].getItemDamage())
        player.addStat(ModAchievements.CampingBagNormal, 1);
      if (item.getItemDamage() == bag[2].getItemDamage())
        player.addStat(ModAchievements.CampingBagLarge, 1);
    }

    if (item.itemID == ModItems.CampTool.itemID) player.addStat(ModAchievements.CampersTool, 1);
    if (item.itemID == ModItems.TentParts.itemID) player.addStat(ModAchievements.TentParts, 1);
    if (item.itemID == tent.itemID) player.addStat(ModAchievements.Tent, 1);
  }
コード例 #6
0
 public String getInfoMessage() {
   if (idea.getStackInSlot(0) == null) return "Need idea";
   if (idea.getStackInSlot(0).stackTagCompound.hasKey(Constants.NBT_IDEA) == false)
     return "Invalid idea";
   if (idea.getStackInSlot(0).stackTagCompound.hasKey(Constants.NBT_REFINED) == false)
     return "Please refine your idea";
   if (idea.getStackInSlot(0).stackTagCompound.getInteger(Constants.NBT_REFINED) < 3)
     return "Please refine your idea fully";
   return "All is good";
 }
コード例 #7
0
  private float percentOccupied(int startSlot, int countSlots) {
    int max = 0;
    int avail = 0;

    IInventory tradeInventory = this.getOrCreateTradeInventory();
    for (int i = startSlot; i < startSlot + countSlots; i++) {
      max += 64;
      if (tradeInventory.getStackInSlot(i) == null) continue;
      avail += tradeInventory.getStackInSlot(i).stackSize;
    }

    return ((float) avail / (float) max);
  }
コード例 #8
0
 @Override
 public void performEffect(IMasterRitualStone ritualStone) {
   String owner = ritualStone.getOwner();
   World world = ritualStone.getWorld();
   int x = ritualStone.getXCoord();
   int y = ritualStone.getYCoord();
   int z = ritualStone.getZCoord();
   TileEntity tile = world.getTileEntity(x, y + 1, z);
   IInventory tileEntity;
   if (tile instanceof IInventory) {
     tileEntity = (IInventory) tile;
   } else {
     return;
   }
   if (tileEntity.getSizeInventory() <= 0) {
     return;
   }
   int currentEssence = SoulNetworkHandler.getCurrentEssence(owner);
   if (currentEssence < this.getCostPerRefresh()) {
     EntityPlayer entityOwner = SpellHelper.getPlayerForUsername(owner);
     if (entityOwner == null) {
       return;
     }
     SoulNetworkHandler.causeNauseaToPlayer(owner);
   } else {
     for (int i = x - 2; i <= x + 2; i++) {
       for (int k = z - 2; k <= z + 2; k++) {
         for (int inv = 0; inv < tileEntity.getSizeInventory(); inv++) {
           if (world.getBlock(i, y, k).isReplaceable(world, i, y + 1, k)
               && tileEntity.getStackInSlot(inv) != null
               && tileEntity.getStackInSlot(inv).stackSize != 0) {
             if (tileEntity.getStackInSlot(inv).getItem() instanceof ItemBlock
                 && world.getBlock(i, y - 1, k) != null) {
               world.setBlock(
                   i,
                   y,
                   k,
                   Block.getBlockFromItem(tileEntity.getStackInSlot(inv).getItem()),
                   tileEntity.getStackInSlot(inv).getItemDamage(),
                   3);
               tileEntity.decrStackSize(inv, 1);
               tileEntity.markDirty();
               SoulNetworkHandler.syphonFromNetwork(owner, getCostPerRefresh());
             }
           }
         }
       }
     }
   }
 }
コード例 #9
0
 public static void saveInventory(TileEntity t, NBTTagCompound saveTag) {
   if (t instanceof IInventory) {
     IInventory tile = (IInventory) t;
     NBTTagList nbttaglist = new NBTTagList();
     for (int i = 0; i < tile.getSizeInventory(); ++i) {
       if (tile.getStackInSlot(i) != null) {
         NBTTagCompound nbttagcompound1 = new NBTTagCompound();
         nbttagcompound1.setByte("Slot", (byte) i);
         tile.getStackInSlot(i).writeToNBT(nbttagcompound1);
         nbttaglist.appendTag(nbttagcompound1);
       }
     }
     saveTag.setTag("Items", nbttaglist);
   }
 }
コード例 #10
0
ファイル: Util.java プロジェクト: umamama/MinecraftMods
 public static ItemStack insertItem(
     IInventory inventory, ItemStack itemStack, boolean test, boolean force) {
   if (test) {
     itemStack = itemStack.copy();
   }
   int size = inventory.getSizeInventory();
   for (int i = 0; i < size; i++) {
     if (inventory.isItemValidForSlot(i, itemStack) || force) {
       ItemStack storedItem = inventory.getStackInSlot(i);
       if (storedItem != null) {
         if (equalItemAndNBT(storedItem, itemStack)) {
           int maxStackSize =
               Math.min(itemStack.getMaxStackSize(), inventory.getInventoryStackLimit());
           int add = Math.min(itemStack.stackSize, maxStackSize - storedItem.stackSize);
           if (!test) {
             storedItem.stackSize += add;
           }
           itemStack.stackSize -= add;
           inventory.setInventorySlotContents(i, storedItem);
         }
       } else {
         storedItem = itemStack.copy();
         storedItem.stackSize = Math.min(itemStack.stackSize, itemStack.getMaxStackSize());
         if (!test) {
           inventory.setInventorySlotContents(i, storedItem);
         }
         itemStack.stackSize -= storedItem.stackSize;
       }
       if (itemStack.stackSize <= 0) {
         return null;
       }
     }
   }
   return itemStack;
 }
コード例 #11
0
  /* ISPECIALINVENTORY */
  @Override
  public int addItem(ItemStack stack, boolean doAdd, ForgeDirection from) {

    if (!this.isLinked()) return 0;

    IInventory inventory = getOrCreateTradeInventory();
    ItemStack tradegood = inventory.getStackInSlot(TradeStation.SLOT_TRADEGOOD);

    // Special handling for paper
    if (stack.getItem() == Items.paper)
      // Handle paper as resource if its not the trade good or pumped in from above or below
      if ((tradegood != null && tradegood.getItem() != Items.paper)
          || from == ForgeDirection.DOWN
          || from == ForgeDirection.UP)
        return StackUtils.addToInventory(
            stack, inventory, doAdd, TradeStation.SLOT_LETTERS_1, TradeStation.SLOT_LETTERS_COUNT);

    // Special handling for stamps
    if (stack.getItem() instanceof IStamps)
      // Handle stamps as resource if its not the trade good or pumped in from above or below
      if ((tradegood != null && !(tradegood.getItem() instanceof IStamps))
          || from == ForgeDirection.DOWN
          || from == ForgeDirection.UP)
        return StackUtils.addToInventory(
            stack, inventory, doAdd, TradeStation.SLOT_STAMPS_1, TradeStation.SLOT_STAMPS_COUNT);

    // Everything else
    if (tradegood == null) return 0;

    if (!tradegood.isItemEqual(stack)) return 0;

    return StackUtils.addToInventory(
        stack, inventory, doAdd, TradeStation.SLOT_INPUTBUF_1, TradeStation.SLOT_BUFFER_COUNT);
  }
コード例 #12
0
ファイル: vase_black.java プロジェクト: Techead13/ModJam
 private void dropItems(World world, int x, int y, int z) {
   Random rand = new Random();
   TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
   if (!(tileEntity instanceof IInventory)) {
     return;
   }
   IInventory inventory = (IInventory) tileEntity;
   for (int i = 0; i < inventory.getSizeInventory(); i++) {
     ItemStack item = inventory.getStackInSlot(i);
     if (item != null && item.stackSize > 0) {
       float rx = rand.nextFloat() * 0.8F + 0.1F;
       float ry = rand.nextFloat() * 0.8F + 0.1F;
       float rz = rand.nextFloat() * 0.8F + 0.1F;
       EntityItem entityItem =
           new EntityItem(
               world,
               x + rx,
               y + ry,
               z + rz,
               new ItemStack(item.itemID, item.stackSize, item.getItemDamage()));
       if (item.hasTagCompound()) {
         entityItem.getEntityItem().setTagCompound((NBTTagCompound) item.getTagCompound().copy());
       }
       float factor = 0.05F;
       entityItem.motionX = rand.nextGaussian() * factor;
       entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
       entityItem.motionZ = rand.nextGaussian() * factor;
       world.spawnEntityInWorld(entityItem);
       item.stackSize = 0;
     }
   }
 }
コード例 #13
0
  public static boolean requestElements(
      EntityPlayer player, ElementList list, int purityRequired, boolean doit) {
    if (player.capabilities.isCreativeMode) return true;

    IInventory mainInv = player.inventory;

    int invSize = mainInv.getSizeInventory();

    for (int i = 0; i < invSize; i++) {
      ItemStack stackInSlot = mainInv.getStackInSlot(i);

      if (stackInSlot != null && stackInSlot.getItem() instanceof IElementProvider) {
        IElementProvider elementItem = (IElementProvider) stackInSlot.getItem();
        if (elementItem.getPurity(player, stackInSlot) < purityRequired) continue;
        ElementList els = elementItem.getElements(player, stackInSlot);
        if (els.size() != 0) {
          ItemStack newStack = elementItem.consumeElements(player, stackInSlot, list, doit);
          if (newStack.stackSize <= 0) mainInv.setInventorySlotContents(i, null);
          else mainInv.setInventorySlotContents(i, newStack);

          if (list.size() == 0) return true;
        }
      }
    }
    return false;
  }
コード例 #14
0
  @Override
  public ItemStack getCraftingResult(IInventory inventory) {
    ItemStack skinStack = null;
    ItemStack blackStack = null;

    for (int slotId = 0; slotId < inventory.getSizeInventory(); slotId++) {
      ItemStack stack = inventory.getStackInSlot(slotId);
      if (stack != null) {
        Item item = stack.getItem();

        if (item == ModItems.equipmentSkin && EquipmentNBTHelper.stackHasSkinData(stack)) {
          if (skinStack != null) {
            return null;
          }
          skinStack = stack;
        } else if (item == ModItems.equipmentSkinTemplate
            & !EquipmentNBTHelper.stackHasSkinData(stack)) {
          if (blackStack != null) {
            return null;
          }
          blackStack = stack;
        } else {
          return null;
        }
      }
    }

    if (skinStack != null && blackStack != null) {
      ItemStack returnStack = new ItemStack(ModItems.equipmentSkin, 1);
      SkinPointer skinData = EquipmentNBTHelper.getSkinPointerFromStack(skinStack);
      EquipmentNBTHelper.addSkinDataToStack(returnStack, skinData.skinType, skinData.skinId, false);
      return returnStack;
    }
    return null;
  }
コード例 #15
0
 public float checkTemps(IInventory inv) {
   float temp = 0;
   float[] temp1 = new float[inv.getSizeInventory()];
   for (int i = 0; i < inv.getSizeInventory(); i++) {
     ItemStack is = inv.getStackInSlot(i);
     if (is != null
         && is.hasTagCompound()
         && !is.getItem().getUnlocalizedName(is).contains("Clay")) {
       if (is.getTagCompound().hasKey("temperature")) {
         temp1[i] = is.getTagCompound().getFloat("temperature");
         if (temp1[i] < TFC_ItemHeat.getMeltingPoint(is)) {
           return (float) -1;
         }
       } else {
         return (float) -1;
       }
     } else if (is == null) {
       temp1[i] = -1;
     }
   }
   int temp2 = 0;
   for (int i = 0; i < inv.getSizeInventory(); i++) {
     if (temp1[i] >= 0) {
       temp += temp1[i];
       temp2++;
     }
   }
   if (temp2 > 0) {
     temp /= temp2;
   }
   return temp;
 }
コード例 #16
0
  /**
   * Adds an ItemStack to the first available slot in the provided IInventory, continuing to
   * distribute the stack as necessary until the stacksize reaches 0, if possible
   *
   * @return the number of items remaining in the stack, zero if all were added
   */
  public static int addItemToInventory(ItemStack stack, IInventory inv) {
    int remaining = stack.stackSize;
    for (int i = 0; i < inv.getSizeInventory() && remaining > 0; ++i) {
      ItemStack slotstack = inv.getStackInSlot(i);
      if (slotstack == null && inv.isItemValidForSlot(i, stack)) {
        remaining -= inv.getInventoryStackLimit();
        stack.stackSize = (remaining > 0 ? inv.getInventoryStackLimit() : stack.stackSize);
        inv.setInventorySlotContents(i, stack);
        inv.markDirty();
      } else if (slotstack != null && stack.isStackable() && inv.isItemValidForSlot(i, stack)) {
        if (slotstack.getItem() == stack.getItem()
            && (!stack.getHasSubtypes() || stack.getItemDamage() == slotstack.getItemDamage())
            && ItemStack.areItemStackTagsEqual(stack, slotstack)) {
          int l = slotstack.stackSize + remaining;
          if (l <= stack.getMaxStackSize() && l <= inv.getInventoryStackLimit()) {
            remaining = 0;
            slotstack.stackSize = l;
            inv.markDirty();
          } else if (slotstack.stackSize < stack.getMaxStackSize()
              && stack.getMaxStackSize() <= inv.getInventoryStackLimit()) {
            remaining -= stack.getMaxStackSize() - slotstack.stackSize;
            slotstack.stackSize = stack.getMaxStackSize();
            inv.markDirty();
          }
        }
      }
    }

    return remaining;
  }
コード例 #17
0
ファイル: BlockFL.java プロジェクト: Vexatos/FloodLights
 protected void dropInventory(World world, int x, int y, int z) {
   TileEntity tileEntity = world.getTileEntity(x, y, z);
   if (!(tileEntity instanceof IInventory)) {
     return;
   }
   IInventory inventory = (IInventory) tileEntity;
   for (int i = 0; i < inventory.getSizeInventory(); i++) {
     ItemStack itemStack = inventory.getStackInSlot(i);
     if (itemStack != null && itemStack.stackSize > 0) {
       Random rand = new Random();
       float dX = rand.nextFloat() * 0.8F + 0.1F;
       float dY = rand.nextFloat() * 0.8F + 0.1F;
       float dZ = rand.nextFloat() * 0.8F + 0.1F;
       EntityItem entityItem = new EntityItem(world, x + dX, y + dY, z + dZ, itemStack.copy());
       if (itemStack.hasTagCompound()) {
         entityItem
             .getEntityItem()
             .setTagCompound((NBTTagCompound) itemStack.getTagCompound().copy());
       }
       float factor = 0.05F;
       entityItem.motionX = rand.nextGaussian() * factor;
       entityItem.motionY = rand.nextGaussian() * factor + 0.2F;
       entityItem.motionZ = rand.nextGaussian() * factor;
       world.spawnEntityInWorld(entityItem);
       itemStack.stackSize = 0;
     }
   }
 }
コード例 #18
0
  @Override
  public ItemStack pullItem(ForgeDirection side, boolean doPull) {
    int xo = xCoord + side.offsetX;
    int yo = yCoord + side.offsetY;
    int zo = zCoord + side.offsetZ;

    TileEntity t = worldObj.getBlockTileEntity(xo, yo, zo);

    if (t instanceof IInventory) {
      if (t instanceof ISpecialInventory) {
        ISpecialInventory isi = (ISpecialInventory) t;
        ItemStack[] items = isi.extractItem(doPull, side.getOpposite(), 1);
        if (items != null && items.length > 0) return items[0];
      } else if (t instanceof ISidedInventory) {
        ISidedInventory isi = (ISidedInventory) t;
        int[] slots = isi.getAccessibleSlotsFromSide(side.getOpposite().ordinal());

        for (int i = 0; i < slots.length; i++) {
          ItemStack pulled = isi.getStackInSlot(slots[i]);
          if (pulled != null
              && isi.canExtractItem(slots[i], pulled, side.getOpposite().ordinal())) {
            ItemStack result = null; // pulled.copy().splitStack(1);
            // pulled.stackSize--;
            // isi.setInventorySlotContents(slots[i], pulled);
            // if(pulled.stackSize <= 0) isi.setInventorySlotContents(slots[i], null);
            if (doPull) {
              result = isi.decrStackSize(slots[i], 1);
              isi.onInventoryChanged();
            } else {
              result = pulled.copy().splitStack(1);
            }
            return result;
          }
        }
      } else {
        IInventory ii = (IInventory) t;

        for (int i = 0; i < ii.getSizeInventory(); i++) {
          ItemStack pulled = ii.getStackInSlot(i);
          if (pulled != null) {
            ItemStack result = null;
            // pulled.stackSize--;
            // ii.setInventorySlotContents(i, pulled);
            // if(pulled.stackSize <= 0)ii.setInventorySlotContents(i, null);
            if (doPull) {
              result = ii.decrStackSize(i, 1);
              ii.onInventoryChanged();
            } else {
              result = pulled.copy().splitStack(1);
            }
            return result;
          }
        }
      }
    }

    return null;
  }
コード例 #19
0
  @Override
  public Map<Integer, ItemStack> getContents() {

    Map<Integer, ItemStack> contents = new HashMap<Integer, ItemStack>();
    for (int i : getSlots()) {
      contents.put(i, _inv.getStackInSlot(i));
    }
    return contents;
  }
コード例 #20
0
 @Override
 public void onCraft(IInventory inventory) {
   for (int slotId = 0; slotId < inventory.getSizeInventory(); slotId++) {
     ItemStack stack = inventory.getStackInSlot(slotId);
     Item item = stack.getItem();
     if (item == ModItems.equipmentSkinTemplate & !EquipmentNBTHelper.stackHasSkinData(stack)) {
       inventory.decrStackSize(slotId, 1);
     }
   }
 }
コード例 #21
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);
      }
    }
  }
コード例 #22
0
ファイル: FluidHelper.java プロジェクト: asiekierka/Railcraft
 public static boolean drainContainers(
     IFluidHandler fluidHandler, IInventory inv, int inputSlot, int outputSlot) {
   ItemStack input = inv.getStackInSlot(inputSlot);
   ItemStack output = inv.getStackInSlot(outputSlot);
   if (input != null) {
     FluidItemHelper.DrainReturn drain = FluidItemHelper.drainContainer(input, PROCESS_VOLUME);
     if (drain.fluidDrained != null
         && (drain.container == null || hasPlaceToPutContainer(output, drain.container))) {
       int used = fluidHandler.fill(ForgeDirection.UNKNOWN, drain.fluidDrained, false);
       if ((drain.isAtomic && used == drain.fluidDrained.amount)
           || (!drain.isAtomic && drain.fluidDrained.amount > 0)) {
         fluidHandler.fill(ForgeDirection.UNKNOWN, drain.fluidDrained, true);
         storeContainer(inv, inputSlot, outputSlot, drain.container);
         return true;
       }
     }
   }
   return false;
 }
コード例 #23
0
 /**
  * Attempts to add the itemstack to a random slot in the inventory; failing that, it will add to
  * the first available slot
  *
  * @param numAttempts number of times to attempt random placement
  * @return the number of items remaining in the stack, zero if all were added
  */
 public static int addItemToInventoryAtRandom(
     Random rand, ItemStack stack, IInventory inv, int numAttempts) {
   for (int i = 0; i < numAttempts; ++i) {
     int slot = rand.nextInt(inv.getSizeInventory());
     if (inv.getStackInSlot(slot) == null) {
       inv.setInventorySlotContents(slot, stack);
       return 0;
     }
   }
   return addItemToInventory(stack, inv);
 }
コード例 #24
0
 @LuaMethod(
     returnType = LuaType.TABLE,
     description = "Get a table with all the items of the chest")
 public ItemStack[] getAllStacks(IComputerAccess computer, IInventory target) {
   IInventory inventory = InventoryUtils.getInventory(target);
   ItemStack[] allStacks = new ItemStack[inventory.getSizeInventory()];
   for (int i = 0; i < inventory.getSizeInventory(); i++) {
     allStacks[i] = inventory.getStackInSlot(i);
   }
   return allStacks;
 }
コード例 #25
0
  public static ArrayList<ItemStack> dumpToIInventory(
      ArrayList<ItemStack> stacks, IInventory inventory) {

    boolean debug = false;
    ArrayList<ItemStack> remaining = new ArrayList<ItemStack>(); // returns the remainder

    ItemStack chestStack;

    for (ItemStack current : stacks) {
      if (current == null) {
        continue;
      }

      for (int i = 0; i < inventory.getSizeInventory(); i++) {

        if (current == null) {
          continue;
        }

        chestStack = inventory.getStackInSlot(i);

        if (chestStack == null) {
          if (debug) System.out.println("DUMP " + i);

          inventory.setInventorySlotContents(i, current);
          // and dont add current ot remainder at all ! sweet!
          current = null;
        } else if (chestStack.isItemEqual(current)) {

          int space = chestStack.getMaxStackSize() - chestStack.stackSize;

          int toDeposit = Math.min(space, current.stackSize);

          if (toDeposit > 0) {

            if (debug) System.out.println("merge " + i + " ; toDeposit =  " + toDeposit);
            current.stackSize -= toDeposit;
            chestStack.stackSize += toDeposit;

            if (current.stackSize == 0) {
              current = null;
            }
          }
        }
      } // finished current pass over inventory
      if (current != null) {
        if (debug) System.out.println("remaining.add : stackSize = " + current.stackSize);
        remaining.add(current);
      }
    }

    if (debug) System.out.println("remaining" + remaining.size());
    return remaining;
  }
コード例 #26
0
ファイル: FluidHelper.java プロジェクト: asiekierka/Railcraft
 /**
  * We can assume that if null is passed for the container that the container was consumed by the
  * process and we should just remove the input container.
  *
  * @param inv
  * @param inputSlot
  * @param outputSlot
  * @param container
  */
 private static void storeContainer(
     IInventory inv, int inputSlot, int outputSlot, ItemStack container) {
   if (container == null) {
     inv.decrStackSize(inputSlot, 1);
     return;
   }
   ItemStack output = inv.getStackInSlot(outputSlot);
   if (output == null) inv.setInventorySlotContents(outputSlot, container);
   else output.stackSize++;
   inv.decrStackSize(inputSlot, 1);
 }
コード例 #27
0
 public void getDrops(World w, int x, int y, int z, ArrayList<ItemStack> drops) {
   if (this instanceof IInventory) {
     IInventory inventory = (IInventory) this;
     for (int l = 0; l < inventory.getSizeInventory(); l++) {
       ItemStack itemStack = inventory.getStackInSlot(l);
       if (itemStack != null) {
         drops.add(itemStack);
       }
     }
   }
 }
コード例 #28
0
  @Override
  public void onCrafting(EntityPlayer player, ItemStack output, IInventory craftMatrix) {
    ItemStack itemstack1 = craftMatrix.getStackInSlot(0);

    // Assumption: Only 1 input, will always be decreased by only 1
    if (itemstack1 != null) {
      craftMatrix.decrStackSize(0, 1);
    }

    updateResult();
  }
コード例 #29
0
ファイル: BlockUtils.java プロジェクト: kMaiSmith/OpenModsLib
 public static void dropInventory(
     IInventory inventory, World world, double x, double y, double z) {
   if (inventory == null) {
     return;
   }
   for (int i = 0; i < inventory.getSizeInventory(); ++i) {
     ItemStack itemStack = inventory.getStackInSlot(i);
     if (itemStack != null) {
       dropItemStackInWorld(world, x, y, z, itemStack);
     }
   }
 }
コード例 #30
0
  public static void dropItemsInRange(World world, int x, int y, int z, int l, int r) {
    TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
    if (!(tileEntity instanceof IInventory)) {
      return;
    }
    IInventory inventory = (IInventory) tileEntity;

    for (int i = l; i < Math.min(r, inventory.getSizeInventory()); i++) {
      ItemStack item = inventory.getStackInSlot(i);
      dropItem(item, world, x, y, z);
    }
  }