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

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

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

ItemStack.getDurability介绍

[英]Gets the durability of this item
[中]获取此项目的耐久性

代码示例

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

private boolean matchesWildcard(ItemStack expected, ItemStack actual) {
  return actual != null && expected.getType() == actual.getType() && (
      isWildcard(expected.getDurability()) || expected.getDurability() == actual
          .getDurability());
}

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

public static Potion fromItemStack(ItemStack item) {
  Validate.notNull(item, "item cannot be null");
  if (item.getType() != Material.POTION)
    throw new IllegalArgumentException("item is not a potion");
  return fromDamage(item.getDurability());
}

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

/**
 * Removes multiple instances of an ingredient from the list. If there are
 * less instances then specified, all will be removed. If the data value
 * is -1, only ingredients with a -1 data value will be removed.
 *
 * @param count The number of copies to remove.
 * @param ingredient The ingredient to remove.
 * @param rawdata The data value.
 * @return The changed recipe.
 * @deprecated Magic value
 */
@Deprecated
public ShapelessRecipe removeIngredient(int count, Material ingredient, int rawdata) {
  Iterator<ItemStack> iterator = ingredients.iterator();
  while (count > 0 && iterator.hasNext()) {
    ItemStack stack = iterator.next();
    if (stack.getType() == ingredient && stack.getDurability() == rawdata) {
      iterator.remove();
      count--;
    }
  }
  return this;
}

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

private boolean compareItems(ItemStack a, ItemStack b, boolean ignoreMeta) {
  if (ignoreMeta) {
    return a.getTypeId() == b.getTypeId() && a.getDurability() == b.getDurability();
  }
  return a.isSimilar(b);
}

代码示例来源: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

/**
 * Gets the MaterialData for this stack of items
 *
 * @return MaterialData for this item
 */
public MaterialData getData() {
  Material mat = getType();
  if (data == null && mat != null && mat.getData() != null) {
    data = mat.getNewData((byte) this.getDurability());
  }
  return data;
}

代码示例来源: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

int mapId = original.getDurability();

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

/**
 * Get a list of all recipes for a given item. The stack size is ignored in comparisons.
 * If the durability is -1, it will match any data value.
 *
 * @param result The item whose recipes you want
 * @return The list of recipes
 */
public List<Recipe> getRecipesFor(ItemStack result) {
  // handling for old-style wildcards
  if (result.getDurability() == -1) {
    result = result.clone();
    result.setDurability(Short.MAX_VALUE);
  }
  List<Recipe> recipes = new LinkedList<>();
  for (Recipe recipe : this) {
    if (matchesWildcard(result, recipe.getResult())) {
      recipes.add(recipe);
    }
  }
  return recipes;
}

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

/**
 * Write an item stack to the buffer.
 *
 * @param buf The buffer.
 * @param stack The stack to write, or null.
 */
public static void writeSlot(ByteBuf buf, ItemStack stack) {
  if (InventoryUtil.isEmpty(stack)) {
    buf.writeShort(-1);
  } else {
    buf.writeShort(stack.getTypeId());
    buf.writeByte(stack.getAmount());
    buf.writeShort(stack.getDurability());
    if (stack.hasItemMeta()) {
      CompoundTag tag = GlowItemFactory.instance().writeNbt(stack.getItemMeta());
      writeCompound(buf, tag);
    } else {
      writeCompound(buf, null);
    }
  }
}

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

@Override
public void placeBlock(GlowPlayer player, GlowBlockState state, BlockFace face,
  ItemStack holding, Vector clickedLoc) {
  super.placeBlock(player, state, face, holding, clickedLoc);
  // No Tree2 MaterialData
  MaterialData data = state.getData();
  data.setData(setTree(face, (byte) holding.getDurability()));
  state.setData(data);
}

代码示例来源: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
public void placeBlock(GlowPlayer player, GlowBlockState state, BlockFace face,
  ItemStack holding, Vector clickedLoc) {
  super.placeBlock(player, state, face, holding, clickedLoc);
  if (holding.getDurability() > 1) {
    switch (face) {
      case NORTH:
      case SOUTH:
        state.setRawData((byte) 4);
        break;
      case WEST:
      case EAST:
        state.setRawData((byte) 3);
        break;
      case UP:
      case DOWN:
        state.setRawData((byte) 2);
        break;
      default:
        // do nothing
        // TODO: should this raise a warning?
    }
  }
}

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

/**
 * This method is the same as equals, but does not consider stack size
 * (amount).
 *
 * @param stack the item stack to compare to
 * @return true if the two stacks are equal, ignoring the amount
 */
@Utility
public boolean isSimilar(ItemStack stack) {
  if (stack == null) {
    return false;
  }
  if (stack == this) {
    return true;
  }
  return getTypeId() == stack.getTypeId() && getDurability() == stack.getDurability() && hasItemMeta() == stack.hasItemMeta() && (hasItemMeta() ? Bukkit.getItemFactory().equals(getItemMeta(), stack.getItemMeta()) : true);
}

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

/**
 * Inflicts damage to an item. Unbreaking enchantment is applied if present.
 *
 * @param player the player holding the item, or null if held by a dispenser
 * @param holding the item
 *
 * @return the updated item stack
 */
public static ItemStack damageItem(GlowPlayer player, ItemStack holding) {
  if (player != null && player.getGameMode() == GameMode.CREATIVE) {
    return holding;
  }
  // Apply unbreaking enchantment.
  // TODO: Armor has a different formula for chance to avoid damage
  int durability = holding.getEnchantmentLevel(Enchantment.DURABILITY);
  if (durability > 0 && ThreadLocalRandom.current().nextDouble() < 1 / (durability + 1)) {
    return holding;
  }
  holding.setDurability((short) (holding.getDurability() + 1));
  if (holding.getDurability() == holding.getType().getMaxDurability() + 1) {
    if (player != null) {
      EventFactory.getInstance()
          .callEvent(new PlayerItemBreakEvent(player, holding));
    }
    return createEmptyStack();
  }
  return holding;
}

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

@Override
public void afterPlace(GlowPlayer player, GlowBlock block, ItemStack holding,
  GlowBlockState oldState) {
  GlowBanner banner = (GlowBanner) block.getState();
  banner.setBaseColor(DyeColor.getByDyeData((byte) holding.getDurability()));
  BannerMeta meta = (BannerMeta) holding.getItemMeta();
  meta.setPatterns(meta.getPatterns());
  banner.update();
}

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

@Override
public void placeBlock(GlowPlayer player, GlowBlockState state, BlockFace face,
  ItemStack holding, Vector clickedLoc) {
  super.placeBlock(player, state, face, holding, clickedLoc);
  MaterialData data = state.getData();
  if (data instanceof Tree) {
    ((Tree) data).setDirection(face);
    ((Tree) data).setSpecies(TreeSpecies.getByData((byte) holding.getDurability()));
  } else {
    warnMaterialData(Tree.class, data);
  }
  state.setData(data);
}

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

/**
 * Resets the enchantments.
 */
public void invalidate() {
  ItemStack item = inventory.getItem();
  ItemStack resource = inventory.getSecondary();
  if (item == null || !canEnchant(item) || player.getGameMode() != GameMode.CREATIVE && (
    resource == null || resource.getType() != Material.INK_SACK
      || resource.getDurability() != 4)) {
    clearEnch();
  } else {
    calculateNewEnchantsAndLevels();
  }
}

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

/**
 * Write an item stack to an NBT tag.
 *
 * <p>Null stacks produce an empty tag, and if slot is negative it is omitted from the result.
 *
 * @param stack The stack to write, or null.
 * @param slot The slot, or negative to omit.
 * @return The resulting tag.
 */
public static CompoundTag writeItem(ItemStack stack, int slot) {
  CompoundTag tag = new CompoundTag();
  if (stack == null || stack.getType() == Material.AIR) {
    return tag;
  }
  tag.putString("id", ItemIds.getName(stack.getType()));
  tag.putShort("Damage", stack.getDurability());
  tag.putByte("Count", stack.getAmount());
  tag.putByte("Slot", slot);
  CompoundTag meta = GlowItemFactory.instance().writeNbt(stack.getItemMeta());
  if (meta != null) {
    tag.putCompound("tag", meta);
  }
  return tag;
}

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

@Override
public void afterPlace(GlowPlayer player, GlowBlock block, ItemStack holding,
  GlowBlockState oldState) {
  GlowSkull skull = (GlowSkull) block.getState();
  skull.setSkullType(getType(holding.getDurability()));
  if (skull.getSkullType() == SkullType.PLAYER) {
    SkullMeta meta = (SkullMeta) holding.getItemMeta();
    if (meta != null) {
      skull.setOwner(meta.getOwner());
    }
  }
  MaterialData data = skull.getData();
  if (!(data instanceof Skull)) {
    warnMaterialData(Skull.class, data);
    return;
  }
  Skull skullData = (Skull) data;
  if (canRotate(skullData)) { // Can be rotated
    skull.setRotation(player.getFacing().getOppositeFace());
  }
  skull.update();
  // Wither
  for (int i = 0; i < 3; i++) {
    if (WITHER_PATTERN.matches(block.getLocation().clone(), true, i, 0)) {
      block.getWorld()
        .spawnEntity(block.getLocation().clone().subtract(0, 2, 0), EntityType.WITHER);
      break;
    }
  }
}

相关文章