@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();
  }
Пример #2
0
  @Override
  public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) {
    InventoryEnderChest enderChest = player.getInventoryEnderChest();
    IInventory inventory = enderChest;

    ItemStack extension = inventory.getStackInSlot(0);
    if (extension != null && extension.itemID == ModDaemon.instance.enderExtension.shiftedIndex) {
      UtilItem.setUniqueItem(extension, player);
      IInventory containedInventory =
          UtilItem.getInventory(player, extension, enderChest, enderChest.getSizeInventory());
      inventory = new InventoryMerged(inventory, containedInventory);
    }

    if (inventory != null && ModDaemon.proxy.isSimulating()) {
      enderChest.setAssociatedChest(new TileDaemonEnderChest());
      player.displayGUIChest(inventory);
    }
    return itemStack;
  }
Пример #3
0
  public ItemStack checkExtractGeneric(
      IInventory inventory, boolean doRemove, Orientations from, int start, int stop) {
    for (int k = start; k <= stop; ++k)
      if (inventory.getStackInSlot(k) != null && inventory.getStackInSlot(k).stackSize > 0) {

        ItemStack slot = inventory.getStackInSlot(k);

        if (slot != null)
          if (slot.stackSize > 0 && doRemove)
            return inventory.decrStackSize(
                k, (int) powerProvider.useEnergy(1, slot.stackSize, true));
          else if (slot.stackSize < 0) {
            ItemStack k = slot.copy();
            k.stackSize = 1;
            return k;
          } else if (!doRemove) return slot;
      }

    return null;
  }
Пример #4
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;
      }
    }
  }
  @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;
  }
  public ContainerReinforcedChest(
      EntityPlayer player, int columns, int rows, IInventory inventory) {
    this.inventory = inventory;
    inventory.openChest();

    for (int y = 0; y < rows; y++)
      for (int x = 0; x < columns; x++)
        addSlotToContainer(new Slot(inventory, x + y * columns, 8 + x * 18, 18 + y * 18));

    // Add player inventory
    for (int y = 0; y < 3; y++)
      for (int x = 0; x < 9; x++)
        addSlotToContainer(
            new Slot(
                player.inventory,
                9 + x + y * 9,
                8 + x * 18 + (columns - 9) * 9,
                rows * 18 + y * 18 + 32));
    for (int x = 0; x < 9; x++)
      addSlotToContainer(
          new Slot(player.inventory, x, 8 + x * 18 + (columns - 9) * 9, rows * 18 + 90));
  }
Пример #7
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;
  }
Пример #8
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);
        }
      }
    }
  }
Пример #9
0
  @Override
  public void doAction(IInventory matrix, DaemonEnergy energy) {
    int size = DaemonMatrix.MATRIX_SIZE;
    int orb = -1;
    ItemOrb orbItem = null;
    int lastSlot = 0;

    // find 1st orb
    for (; lastSlot < size; lastSlot++) {
      ItemStack stack = matrix.getStackInSlot(lastSlot);
      if (stack != null && stack.getItem() instanceof ItemOrb) {
        orb = lastSlot;
        orbItem = (ItemOrb) stack.getItem();
        break;
      }
    }
    if (orb < 0) return;

    // charge with glass shard if possible
    for (int i = 0; i < size; i++) {
      ItemStack stack = matrix.getStackInSlot(i);
      if (stack != null
          && stack.getItem() instanceof ItemShardGlass
          && stack.getItemDamage() >= ItemShardGlass.DAMAGE_CHARGED) {
        matrix.setInventorySlotContents(i, null);
        orbItem.chargeOrb(matrix.getStackInSlot(orb));
        return;
      }
    }

    // charge with crystal
    for (int i = 0; i < size; i++) {
      ItemStack stack = matrix.getStackInSlot(i);
      if (stack != null && stack.getItem() instanceof ItemCrystal) {
        matrix.setInventorySlotContents(i, null);
        DaemonEnergy de = UtilItem.getDaemonEnergy(matrix.getStackInSlot(orb));
        DaemonEnergy de2 = UtilItem.getDaemonEnergy(stack);
        de.merge(de2);
        UtilItem.setDaemonEnergy(matrix.getStackInSlot(orb), de);
        return;
      }
    }

    for (lastSlot++; lastSlot < size; lastSlot++) {
      ItemStack stack = matrix.getStackInSlot(lastSlot);
      if (stack != null && stack.getItem() instanceof ItemOrb) {
        matrix.setInventorySlotContents(orb, orbItem.mergeOrbs(matrix.getStackInSlot(orb), stack));
        matrix.setInventorySlotContents(lastSlot, null);
        return;
      }
    }
  }
 @Override
 public void onCraftGuiClosed(EntityPlayer player) {
   super.onCraftGuiClosed(player);
   inventory.closeChest();
 }
 @Override
 public boolean canInteractWith(EntityPlayer player) {
   return inventory.isUseableByPlayer(player);
 }