org.bukkit.inventory.ItemStack.getAmount()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(319)

本文整理了Java中org.bukkit.inventory.ItemStack.getAmount()方法的一些代码示例,展示了ItemStack.getAmount()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ItemStack.getAmount()方法的具体详情如下:
包路径:org.bukkit.inventory.ItemStack
类名称:ItemStack
方法名:getAmount

ItemStack.getAmount介绍

[英]Gets the amount of items in this stack
[中]获取此堆栈中的项目数

代码示例

代码示例来源:origin: GlowstoneMC/Glowstone

/**
 * Get the amount of layers in the crafting matrix. This assumes all Minecraft recipes have an
 * item stack of 1 for all items in the recipe.
 *
 * @param items The items in the crafting matrix.
 * @return The number of stacks for a recipe.
 */
public static int getLayers(ItemStack... items) {
  int layers = 0;
  for (ItemStack item : items) {
    if (!InventoryUtil.isEmpty(item) && (item.getAmount() < layers || layers == 0)) {
      layers = item.getAmount();
    }
  }
  return layers;
}

代码示例来源:origin: Bukkit/Bukkit

@Override
@Utility
public boolean equals(Object obj) {
  if (this == obj) {
    return true;
  }
  if (!(obj instanceof ItemStack)) {
    return false;
  }
  ItemStack stack = (ItemStack) obj;
  return getAmount() == stack.getAmount() && isSimilar(stack);
}

代码示例来源:origin: GlowstoneMC/Glowstone

@Override
public boolean contains(int materialId, int amount) {
  HashMap<Integer, ? extends ItemStack> found = all(materialId);
  int total = 0;
  for (ItemStack stack : found.values()) {
    total += stack.getAmount();
  }
  return total >= amount;
}

代码示例来源:origin: GlowstoneMC/Glowstone

/**
 * Checks whether the given ItemStack is empty.
 *
 * @param stack the ItemStack to check
 * @return whether the given ItemStack is empty
 */
public static boolean isEmpty(ItemStack stack) {
  return stack == null || stack.getType() == Material.AIR || stack.getAmount() == 0;
}

代码示例来源:origin: Bukkit/Bukkit

@Override
@Utility
public String toString() {
  StringBuilder toString = new StringBuilder("ItemStack{").append(getType().name()).append(" x ").append(getAmount());
  if (hasItemMeta()) {
    toString.append(", ").append(getItemMeta());
  }
  return toString.append('}').toString();
}

代码示例来源:origin: Bukkit/Bukkit

@Utility
public Map<String, Object> serialize() {
  Map<String, Object> result = new LinkedHashMap<String, Object>();
  result.put("type", getType().name());
  if (getDurability() != 0) {
    result.put("damage", getDurability());
  }
  if (getAmount() != 1) {
    result.put("amount", getAmount());
  }
  ItemMeta meta = getItemMeta();
  if (!Bukkit.getItemFactory().equals(meta, null)) {
    result.put("meta", meta);
  }
  return result;
}

代码示例来源:origin: Bukkit/Bukkit

@Override
@Utility
public final int hashCode() {
  int hash = 1;
  hash = hash * 31 + getTypeId();
  hash = hash * 31 + getAmount();
  hash = hash * 31 + (getDurability() & 0xffff);
  hash = hash * 31 + (hasItemMeta() ? (meta == null ? getItemMeta().hashCode() : meta.hashCode()) : 0);
  return hash;
}

代码示例来源:origin: GlowstoneMC/Glowstone

protected ItemStack dispenseStack(GlowBlock block, ItemStack stack) {
  BlockFace facing = BlockDispenser.getFacing(block);
  Vector dispensePosition = BlockDispenser.getDispensePosition(block);
  ItemStack items = new ItemStack(stack.getType(), 1);
  stack.setAmount(stack.getAmount() - 1);
  doDispense(block, items, 6, facing, dispensePosition);
  return stack.getAmount() > 0 ? stack : null;
}

代码示例来源:origin: EngineHub/WorldEdit

/**
 * Create a WorldEdit BaseItemStack from a Bukkit ItemStack
 *
 * @param itemStack The Bukkit ItemStack
 * @return The WorldEdit BaseItemStack
 */
public static BaseItemStack adapt(ItemStack itemStack) {
  checkNotNull(itemStack);
  return new BaseItemStack(ItemTypes.get(itemStack.getType().getKey().toString()), itemStack.getAmount());
}

代码示例来源:origin: GlowstoneMC/Glowstone

private ItemStack combine(ItemStack slotItem, ItemStack cursor, int amount) {
  if (InventoryUtil.isEmpty(slotItem)) {
    ItemStack stack = cursor.clone();
    stack.setAmount(amount);
    return stack;
  } else if (slotItem.isSimilar(cursor)) {
    slotItem.setAmount(slotItem.getAmount() + amount);
    return slotItem;
  } else {
    throw new IllegalArgumentException(
        "Trying to combine dissimilar " + slotItem + " and " + cursor);
  }
}

代码示例来源:origin: GlowstoneMC/Glowstone

private static boolean canEnchant(ItemStack item) {
  Material type = item.getType();
  switch (type) {
    case ENCHANTED_BOOK:
      return false;
    case BOOK:
      return item.getAmount() == 1;
    case FISHING_ROD:
    case BOW:
      return item.getEnchantments().isEmpty();
    default:
      return (isEnchantableTool(type) || isCloth(type) || ToolType.SWORD.matches(type))
        && item.getEnchantments().isEmpty();
  }
}

代码示例来源:origin: GlowstoneMC/Glowstone

@Override
  protected ItemStack dispenseStack(GlowBlock block, ItemStack stack) {
    GlowWorld world = block.getWorld();
    GlowBlock target = block.getRelative(BlockDispenser.getFacing(block));
    GlowTntPrimed tnt = (GlowTntPrimed) world
      .spawnEntity(target.getLocation().add(0.5, 0, 0.5), EntityType.PRIMED_TNT);
    world.playSound(tnt.getLocation(), Sound.ENTITY_TNT_PRIMED, 1, 1);
    stack.setAmount(stack.getAmount() - 1);
    return stack.getAmount() > 0 ? stack : null;
  }
}

代码示例来源:origin: GlowstoneMC/Glowstone

private void spawnFirework(GlowPlayer player, ItemStack item, Location location, UUID spawner,
  LivingEntity boostedEntity) {
  if (item.getType() != Material.FIREWORK || !(item.getItemMeta() instanceof FireworkMeta)) {
    return;
  }
  new GlowFirework(location, spawner, boostedEntity, item);
  if (player.getGameMode() != GameMode.CREATIVE) {
    item.setAmount(item.getAmount() - 1);
  }
}

代码示例来源:origin: GlowstoneMC/Glowstone

private void throwEnderPearl(GlowPlayer player, ItemStack holding) {
  if (player.getEnderPearlCooldown() == 0) {
    if (!player.getGameMode().equals(GameMode.CREATIVE)) {
      holding.setAmount(holding.getAmount() - 1);
    }
    throwEnderPearl(player);
  }
}

代码示例来源:origin: Bukkit/Bukkit

/**
 * Creates a new item stack derived from the specified stack
 *
 * @param stack the stack to copy
 * @throws IllegalArgumentException if the specified stack is null or
 *     returns an item meta not created by the item factory
 */
public ItemStack(final ItemStack stack) throws IllegalArgumentException {
  Validate.notNull(stack, "Cannot copy null stack");
  this.type = stack.getTypeId();
  this.amount = stack.getAmount();
  this.durability = stack.getDurability();
  this.data = stack.getData();
  if (stack.hasItemMeta()) {
    setItemMeta0(stack.getItemMeta(), getType0());
  }
}

代码示例来源:origin: GlowstoneMC/Glowstone

@Override
protected ItemStack dispenseStack(GlowBlock block, ItemStack stack) {
  GlowBlock target = block.getRelative(BlockDispenser.getFacing(block));
  successful = true;
  if (target.getType() == Material.AIR) {
    target.setType(Material.FIRE);
    // TODO: Find the slot, so we can damage the flint and steel and write it back to the
    // inventory
    // InventoryUtil.damageItem(null, stack);
  } else if (target.getType() == Material.TNT) {
    BlockTnt.igniteBlock(target, false);
  } else {
    successful = false;
  }
  return stack.getAmount() > 0 ? stack : null;
}

代码示例来源:origin: GlowstoneMC/Glowstone

private void giveItem(CommandSender sender, Player player, ItemStack stack,
    ResourceBundle resourceBundle) {
  player.getInventory().addItem(stack);
  new LocalizedStringImpl("give.done", resourceBundle)
      .send(sender, ItemIds.getName(stack.getType()), stack.getAmount(),
          player.getName());
}

代码示例来源:origin: GlowstoneMC/Glowstone

@Override
protected ItemStack dispenseStack(GlowBlock block, ItemStack stack) {
  GlowWorld world = block.getWorld();
  Vector position = BlockDispenser.getDispensePosition(block);
  BlockFace face = BlockDispenser.getFacing(block);
  Projectile entity = projectileCreator.apply(
      new Location(world, position.getX(), position.getY(), position.getZ()), stack);
  entity.setShooter(new GlowDispenser(block));
  entity.setVelocity(
    new Vector(face.getModX(), face.getModY() + 0.1f, face.getModZ()).multiply(6));
  stack.setAmount(stack.getAmount() - 1);
  if (stack.getAmount() < 1) {
    return null;
  }
  return stack;
}

代码示例来源:origin: GlowstoneMC/Glowstone

@Override
public GlowItem drop(ItemStack stack) {
  GlowItem dropping = super.drop(stack);
  if (dropping != null) {
    PlayerDropItemEvent event = new PlayerDropItemEvent(this, dropping);
    EventFactory.getInstance().callEvent(event);
    if (event.isCancelled()) {
      dropping.remove();
      dropping = null;
    } else {
      incrementStatistic(Statistic.DROP, stack.getAmount());
    }
  }
  return dropping;
}

代码示例来源:origin: GlowstoneMC/Glowstone

@Override
  public void rightClickBlock(GlowPlayer player, GlowBlock target, BlockFace face,
    ItemStack holding, Vector clickedLoc, EquipmentSlot hand) {
    if (target.getType() == soilType
      && target.getRelative(BlockFace.UP).getType() == Material.AIR && face == BlockFace.UP) {
      GlowBlockState state = target.getRelative(BlockFace.UP).getState();
      state.setType(cropsType);
      state.setRawData((byte) 0);
      state.update(true);

      // deduct from stack if not in creative mode
      if (player.getGameMode() != GameMode.CREATIVE) {
        holding.setAmount(holding.getAmount() - 1);
      }
    }
  }
}

相关文章