private void setClientPreviewLists() { Item baseItem = stacks[0].getItem(); int[] oreIds = OreDictionary.getOreIDs(stacks[0]); isOre = oreIds.length > 0; for (Object o : Item.itemRegistry) { Item item = (Item) o; boolean classMatch = classMatch(baseItem, item); List list = new LinkedList(); for (CreativeTabs tab : item.getCreativeTabs()) { item.getSubItems(item, tab, list); } if (list.size() > 0) { for (Object ol : list) { ItemStack stack = (ItemStack) ol; if (classMatch && relatedItems.size() <= 7 && !StackHelper.isMatchingItemOrList(stacks[0], stack)) { relatedItems.add(stack); } if (isOre && ores.size() <= 7 && !StackHelper.isMatchingItemOrList(stacks[0], stack) && oreMatch(stacks[0], stack)) { ores.add(stack); } } } } }
private int quantityMissing(ItemStack requirement, int amount) { int left = amount <= 0 ? requirement.stackSize : amount; for (IInvSlot slot : InventoryIterator.getIterable(this)) { if (slot.getStackInSlot() != null) { if (StackHelper.isMatchingItem(requirement, slot.getStackInSlot())) { if (slot.getStackInSlot().stackSize >= left) { return 0; } else { left -= slot.getStackInSlot().stackSize; } } } } return left; }
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); } } }
public boolean matches(ItemStack item) { if (subitemsWildcard) { if (stacks[0] == null) { return false; } return classMatch(stacks[0].getItem(), item.getItem()); } else if (oreWildcard) { if (stacks[0] == null) { return false; } return oreMatch(stacks[0], item); } else { for (ItemStack stack : stacks) { if (stack != null && StackHelper.isMatchingItem(stack, item, true, false)) { return true; } } return false; } }
@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); } } } }
@Override public void writeToWorld(IBuilderContext context) { if (mode == Mode.ClearIfInvalid) { if (!getSchematic().isAlreadyBuilt(context, x, y, z)) { if (BuildCraftBuilders.dropBrokenBlocks) { BlockUtils.breakBlock((WorldServer) context.world(), x, y, z); } else { context.world().setBlockToAir(x, y, z); } } } else { try { getSchematic().placeInWorld(context, x, y, z, stackConsumed); // This is slightly hackish, but it's a very important way to verify // the stored requirements. if (!context.world().isAirBlock(x, y, z) && getSchematic().getBuildingPermission() == BuildingPermission.ALL && getSchematic() instanceof SchematicBlock) { SchematicBlock sb = (SchematicBlock) getSchematic(); // Copy the old array of stored requirements. ItemStack[] oldRequirementsArray = sb.storedRequirements; List<ItemStack> oldRequirements = Arrays.asList(oldRequirementsArray); sb.storedRequirements = new ItemStack[0]; sb.storeRequirements(context, x, y, z); for (ItemStack s : sb.storedRequirements) { boolean contains = false; for (ItemStack ss : oldRequirements) { if (StackHelper.isMatchingItem(s, ss)) { contains = true; break; } } if (!contains) { BCLog.logger.warn( "Blueprint has MISMATCHING REQUIREMENTS! Potential corrupted/hacked blueprint! Removed mismatched block."); BCLog.logger.warn( "Location: " + x + ", " + y + ", " + z + " - ItemStack: " + s.toString()); context.world().removeTileEntity(x, y, z); context.world().setBlockToAir(x, y, z); return; } } // Restore the stored requirements. sb.storedRequirements = oldRequirementsArray; } // Once the schematic has been written, we're going to issue // calls // to various functions, in particular updating the tile entity. // If these calls issue problems, in order to avoid corrupting // the world, we're logging the problem and setting the block to // air. TileEntity e = context.world().getTileEntity(x, y, z); if (e != null) { e.updateEntity(); } } catch (Throwable t) { t.printStackTrace(); context.world().setBlockToAir(x, y, z); } } }