private void craftItem() {
   ItemStack recipeOutput = getRecipeOutput();
   craftSlot.onPickupFromSlot(internalPlayer, recipeOutput);
   ItemStack[] tempStorage = internalInventoryCrafting.tempStacks;
   for (int i = 0; i < tempStorage.length; i++) {
     if (tempStorage[i] != null && tempStorage[i].stackSize <= 0) {
       tempStorage[i] = null;
     }
     inv.getItemStacks()[i] = tempStorage[i];
   }
   subtractEnergy(getRequiredEnergy());
   List<ItemStack> outputs = Lists.newArrayList(recipeOutput.copy());
   for (int i = 0; i < internalPlayer.inventory.mainInventory.length; i++) {
     if (internalPlayer.inventory.mainInventory[i] != null) {
       outputs.add(internalPlayer.inventory.mainInventory[i]);
       internalPlayer.inventory.mainInventory[i] = null;
     }
   }
   for (ItemStack output : outputs) {
     output.stackSize -=
         Transactor.getTransactorFor(invOutput).add(output, ForgeDirection.UP, true).stackSize;
     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);
     }
   }
 }
Exemple #2
0
  @Override
  public void onPickupFromSlot(EntityPlayer player, ItemStack craftedItem) {
    if (player.capabilities.isCreativeMode && ConfigurationManager.ENABLE_FREECRAFTING
        || craftedItem == null) {
      FMLCommonHandler.instance().firePlayerCraftingEvent(player, craftedItem, matrix);
      super.onCrafting(craftedItem);
      return;
    }

    CraftRecipe recipe = getRecipe();
    if (recipe == null) return;

    handler.doCraft(recipe, player, craftedItem);
    FMLCommonHandler.instance().firePlayerCraftingEvent(player, craftedItem, matrix);
    super.onCrafting(craftedItem);
  }
  public void craftOrGetItem(final PacketPatternSlot packetPatternSlot) {
    if (packetPatternSlot.slotItem != null && this.getCellInventory() != null) {
      final IAEItemStack out = packetPatternSlot.slotItem.copy();
      InventoryAdaptor inv = new AdaptorPlayerHand(this.getPlayerInv().player);
      final InventoryAdaptor playerInv =
          InventoryAdaptor.getAdaptor(this.getPlayerInv().player, ForgeDirection.UNKNOWN);

      if (packetPatternSlot.shift) {
        inv = playerInv;
      }

      if (inv.simulateAdd(out.getItemStack()) != null) {
        return;
      }

      final IAEItemStack extracted =
          Platform.poweredExtraction(
              this.getPowerSource(), this.getCellInventory(), out, this.getActionSource());
      final EntityPlayer p = this.getPlayerInv().player;

      if (extracted != null) {
        inv.addItems(extracted.getItemStack());
        if (p instanceof EntityPlayerMP) {
          this.updateHeld((EntityPlayerMP) p);
        }
        this.detectAndSendChanges();
        return;
      }

      final InventoryCrafting ic = new InventoryCrafting(new ContainerNull(), 3, 3);
      final InventoryCrafting real = new InventoryCrafting(new ContainerNull(), 3, 3);

      for (int x = 0; x < 9; x++) {
        ic.setInventorySlotContents(
            x,
            packetPatternSlot.pattern[x] == null
                ? null
                : packetPatternSlot.pattern[x].getItemStack());
      }

      final IRecipe r = Platform.findMatchingRecipe(ic, p.worldObj);

      if (r == null) {
        return;
      }

      final IMEMonitor<IAEItemStack> storage = this.getPatternTerminal().getItemInventory();
      final IItemList<IAEItemStack> all = storage.getStorageList();

      final ItemStack is = r.getCraftingResult(ic);

      for (int x = 0; x < ic.getSizeInventory(); x++) {
        if (ic.getStackInSlot(x) != null) {
          final ItemStack pulled =
              Platform.extractItemsByRecipe(
                  this.getPowerSource(),
                  this.getActionSource(),
                  storage,
                  p.worldObj,
                  r,
                  is,
                  ic,
                  ic.getStackInSlot(x),
                  x,
                  all,
                  Actionable.MODULATE,
                  ItemViewCell.createFilter(this.getViewCells()));
          real.setInventorySlotContents(x, pulled);
        }
      }

      final IRecipe rr = Platform.findMatchingRecipe(real, p.worldObj);

      if (rr == r && Platform.isSameItemPrecise(rr.getCraftingResult(real), is)) {
        final SlotCrafting sc = new SlotCrafting(p, real, this.cOut, 0, 0, 0);
        sc.onPickupFromSlot(p, is);

        for (int x = 0; x < real.getSizeInventory(); x++) {
          final ItemStack failed = playerInv.addItems(real.getStackInSlot(x));

          if (failed != null) {
            p.dropPlayerItemWithRandomChoice(failed, false);
          }
        }

        inv.addItems(is);
        if (p instanceof EntityPlayerMP) {
          this.updateHeld((EntityPlayerMP) p);
        }
        this.detectAndSendChanges();
      } else {
        for (int x = 0; x < real.getSizeInventory(); x++) {
          final ItemStack failed = real.getStackInSlot(x);
          if (failed != null) {
            this.getCellInventory()
                .injectItems(
                    AEItemStack.create(failed),
                    Actionable.MODULATE,
                    new MachineSource(this.getPatternTerminal()));
          }
        }
      }
    }
  }
  @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);
        }
      }
    }
  }