@Override
  public int execute(
      EAI_Manager manager, EntityLiving entity, IInventory inventory, int slotnum, int maxcol) {
    super.execute(manager, entity, inventory, slotnum, maxcol);

    // 食べ物を持ってたら食べる。次へ進む
    //
    // ロジックをなるだけ壊さないように、検索は後ろから
    for (int i = inventory.getSizeInventory() - 1; i > 0; i--) {
      ItemStack stack = inventory.getStackInSlot(i);
      if (stack != null && stack.getItem() instanceof ItemFood) {
        UtilInventory.decrStackSize(inventory, i, 1); // アイテムを 1 つ減らす
        ItemFood food = (ItemFood) stack.getItem();

        entity.worldObj.playSoundEffect(
            entity.posX,
            entity.posY,
            entity.posZ,
            "random.eat",
            0.5F,
            entity.worldObj.rand.nextFloat() * 0.1F + 0.9F);

        entity.setEating(true);
        entity.setEntityHealth(entity.getHealth() + 1); // HP を 1 回復する
        EasyAIInterface.debugPrint("[EAI_TASK_eatFood] Eat Food " + stack + " " + slotnum);

        return this.returnTrue();
      }
    }
    return this.returnFalse();
  }
  @Override
  public ItemStack transferStackInSlot(EntityPlayer player, int slotId) {
    ItemStack stack = null;
    Slot slot = (Slot) inventorySlots.get(slotId);

    // If slot isn't empty and item can be stacked.
    if (slot != null && slot.getHasStack()) {
      ItemStack slotStack = slot.getStack();
      stack = slotStack.copy();
      // If slot is in the container inventory, try to transfer the item to the player.
      if (slotId < inventory.getSizeInventory()) {
        if (!mergeItemStack(slotStack, inventory.getSizeInventory(), inventorySlots.size(), true))
          return null;
        // If slot is in the player inventory, try to transfer the item to the container.
      } else if (!mergeItemStack(slotStack, 0, inventory.getSizeInventory(), false)) return null;
      if (slotStack.stackSize != 0) slot.onSlotChanged();
      else slot.putStack(null);
    }

    return stack;
  }
Exemplo n.º 3
0
  private void dropItems(World world, int x, int y, int z) {
    Random rand = new Random();

    TileEntity tile_entity = world.getBlockTileEntity(x, y, z);

    if (!(tile_entity instanceof IInventory)) {
      return;
    }

    IInventory inventory = (IInventory) tile_entity;

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

      if (item != null && item.stackSize > 0) {
        float rx = rand.nextFloat() * 0.6F + 0.1F;
        float ry = rand.nextFloat() * 0.6F + 0.1F;
        float rz = rand.nextFloat() * 0.6F + 0.1F;

        EntityItem entity_item =
            new EntityItem(
                world,
                x + rx,
                y + ry,
                z + rz,
                new ItemStack(item.itemID, item.stackSize, item.getItemDamage()));

        if (item.hasTagCompound()) {
          entity_item.item.setTagCompound((NBTTagCompound) item.getTagCompound().copy());
        }

        float factor = 0.5F;

        entity_item.motionX = rand.nextGaussian() * factor;
        entity_item.motionY = rand.nextGaussian() * factor + 0.2F;
        entity_item.motionZ = rand.nextGaussian() * factor;
        world.spawnEntityInWorld(entity_item);
        item.stackSize = 0;
      }
    }
  }
Exemplo n.º 4
0
  /**
   * Return the itemstack that can be if something can be extracted from this inventory, null if
   * none. On certain cases, the extractable slot depends on the position of the pipe.
   */
  public ItemStack[] checkExtract(IInventory inventory, boolean doRemove, Orientations from) {

    /// ISPECIALINVENTORY
    if (inventory instanceof ISpecialInventory)
      return ((ISpecialInventory) inventory).extractItem(doRemove, from, 1);

    if (inventory instanceof ISidedInventory) {
      ISidedInventory sidedInv = (ISidedInventory) inventory;

      int first = sidedInv.getStartInventorySide(from.toDirection());
      int last = first + sidedInv.getSizeInventorySide(from.toDirection()) - 1;

      IInventory inv = Utils.getInventory(inventory);

      ItemStack result = checkExtractGeneric(inv, doRemove, from, first, last);

      if (result != null) return new ItemStack[] {result};
    } else if (inventory.getSizeInventory() == 2) {
      // This is an input-output inventory

      int slotIndex = 0;

      if (from == Orientations.YNeg || from == Orientations.YPos) slotIndex = 0;
      else slotIndex = 1;

      ItemStack slot = inventory.getStackInSlot(slotIndex);

      if (slot != null && slot.stackSize > 0)
        if (doRemove)
          return new ItemStack[] {
            inventory.decrStackSize(
                slotIndex, (int) powerProvider.useEnergy(1, slot.stackSize, true))
          };
        else return new ItemStack[] {slot};
    } else if (inventory.getSizeInventory() == 3) {
      // This is a furnace-like inventory

      int slotIndex = 0;

      if (from == Orientations.YPos) slotIndex = 0;
      else if (from == Orientations.YNeg) slotIndex = 1;
      else slotIndex = 2;

      ItemStack slot = inventory.getStackInSlot(slotIndex);

      if (slot != null && slot.stackSize > 0)
        if (doRemove)
          return new ItemStack[] {
            inventory.decrStackSize(
                slotIndex, (int) powerProvider.useEnergy(1, slot.stackSize, true))
          };
        else return new ItemStack[] {slot};
    } else {
      // This is a generic inventory
      IInventory inv = Utils.getInventory(inventory);

      ItemStack result = checkExtractGeneric(inv, doRemove, from, 0, inv.getSizeInventory() - 1);

      if (result != null) return new ItemStack[] {result};
    }

    return null;
  }
Exemplo n.º 5
0
  @Override
  public void throttledUpdateEntity() {

    if (!((CoreRoutedPipe) this.container.pipe).isEnabled()) {
      return;
    }

    if (APIProxy.isClient(worldObj)) return;
    if (pause) return;
    super.throttledUpdateEntity();
    WorldUtil worldUtil = new WorldUtil(worldObj, xCoord, yCoord, zCoord);
    for (AdjacentTile tile : worldUtil.getAdjacentTileEntities()) {
      if (tile.tile instanceof TileGenericPipe) continue;
      if (!(tile.tile instanceof IInventory)) continue;

      // Do not attempt to supply redstone engines
      if (tile.tile instanceof TileEngine && ((TileEngine) tile.tile).engine instanceof EngineWood)
        continue;

      IInventory inv = Utils.getInventory((IInventory) tile.tile);
      if (inv.getSizeInventory() < 1) continue;
      InventoryUtil invUtil = _invUtilFactory.getInventoryUtil(inv);

      // How many do I want?
      HashMap<ItemIdentifier, Integer> needed = _dummyInvUtil.getItemsAndCount();

      // How many do I have?
      HashMap<ItemIdentifier, Integer> have = invUtil.getItemsAndCount();

      // Reduce what I have
      for (ItemIdentifier item : needed.keySet()) {
        if (have.containsKey(item)) {
          needed.put(item, needed.get(item) - have.get(item));
        }
      }

      // Reduce what have been requested already
      for (ItemIdentifier item : needed.keySet()) {
        if (_requestedItems.containsKey(item)) {
          needed.put(item, needed.get(item) - _requestedItems.get(item));
        }
      }

      ((PipeItemsSupplierLogistics) this.container.pipe).setRequestFailed(false);

      // Make request
      for (ItemIdentifier need : needed.keySet()) {
        if (needed.get(need) < 1) continue;
        int neededCount = needed.get(need);
        boolean success = false;
        do {
          success =
              LogisticsManager.Request(
                  new LogisticsRequest(need, neededCount, (IRequestItems) container.pipe),
                  getRouter().getRoutersByCost(),
                  null);
          if (success || neededCount == 1) {
            break;
          }
          neededCount = neededCount / 2;
        } while (_requestPartials && !success);

        if (success) {
          if (!_requestedItems.containsKey(need)) {
            _requestedItems.put(need, neededCount);
          } else {
            _requestedItems.put(need, _requestedItems.get(need) + neededCount);
          }
        } else {
          ((PipeItemsSupplierLogistics) this.container.pipe).setRequestFailed(true);
        }
      }
    }
  }