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

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

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

ItemStack.addUnsafeEnchantment介绍

[英]Adds the specified Enchantment to this item stack.

If this item stack already contained the given enchantment (at any level), it will be replaced.

This method is unsafe and will ignore level restrictions or item type. Use at your own discretion.
[中]将指定的附魔添加到此项目堆栈。
如果此物品堆栈已经包含给定的附魔(在任何级别),它将被替换。
此方法不安全,将忽略级别限制或项目类型。请自行决定使用。

代码示例

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

/**
 * Adds the specified enchantments to this item stack in an unsafe manner.
 * <p>
 * This method is the same as calling {@link
 * #addUnsafeEnchantment(org.bukkit.enchantments.Enchantment, int)} for
 * each element of the map.
 *
 * @param enchantments Enchantments to add
 */
@Utility
public void addUnsafeEnchantments(Map<Enchantment, Integer> enchantments) {
  for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
    addUnsafeEnchantment(entry.getKey(), entry.getValue());
  }
}

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

result.addUnsafeEnchantment(enchantment, (Integer) entry.getValue());

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

/**
 * Adds the specified {@link Enchantment} to this item stack.
 * <p>
 * If this item stack already contained the given enchantment (at any
 * level), it will be replaced.
 *
 * @param ench Enchantment to add
 * @param level Level of the enchantment
 * @throws IllegalArgumentException if enchantment null, or enchantment is
 *     not applicable
 */
@Utility
public void addEnchantment(Enchantment ench, int level) {
  Validate.notNull(ench, "Enchantment cannot be null");
  if ((level < ench.getStartLevel()) || (level > ench.getMaxLevel())) {
    throw new IllegalArgumentException("Enchantment level is either too low or too high (given " + level + ", bounds are " + ench.getStartLevel() + " to " + ench.getMaxLevel() + ")");
  } else if (!ench.canEnchantItem(this)) {
    throw new IllegalArgumentException("Specified enchantment cannot be applied to this itemstack");
  }
  addUnsafeEnchantment(ench, level);
}

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

item.addUnsafeEnchantment(enchantment, level);

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

.forEach(player -> {
  ItemStack itemInHand = player.getItemInHand();
  itemInHand.addUnsafeEnchantment(enchantment, level);
  player.setItemInHand(itemInHand);
  successMessage.send(sender);

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

item.setItemMeta(meta);
} else {
  item.addUnsafeEnchantment(enchantment.getKey(), enchantment.getValue());

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

/**
 * Returns the item that will result when this anvil is applied to the currently loaded items,
 * combining them, provided that two items which can be combined are loaded. A return of null
 * doesn't imply that the anvil cannot be activated, since it may still be able to repair/name a
 * single item.
 *
 * @return the resulting item, or null if two items that can be combined are not loaded
 */
public ItemStack getForged() {
  if (InventoryUtil.isEmpty(getFirstItem()) || InventoryUtil.isEmpty(getSecondItem())) {
    return null;
  }
  if (getSecondItem().getType() == Material.ENCHANTED_BOOK) {
    EnchantmentStorageMeta book = (EnchantmentStorageMeta) getSecondItem().getItemMeta();
    ItemStack result;
    if (InventoryUtil.isEmpty(getResultItem())) {
      result = getFirstItem().clone();
    } else {
      result = getResultItem();
    }
    book.getStoredEnchants().forEach((enchantment, level) -> {
      if (enchantment.canEnchantItem(result)
        || result.getType() == Material.ENCHANTED_BOOK) {
        result.addUnsafeEnchantment(enchantment, level);
      }
    });
    return result;
  }
  return null;
}

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

@Override
public void addUnsafeEnchantment(MCEnchantment e, int level) {
  if(is == null) {
    return;
  }
  is.addUnsafeEnchantment(((BukkitMCEnchantment) e).__Enchantment(), level);
}

代码示例来源:origin: lucko/helper

public ItemStackBuilder enchant(Enchantment enchantment) {
  return transform(itemStack -> itemStack.addUnsafeEnchantment(enchantment, 1));
}

代码示例来源:origin: me.lucko/helper

public ItemStackBuilder enchant(Enchantment enchantment, int level) {
  return transform(itemStack -> itemStack.addUnsafeEnchantment(enchantment, level));
}

代码示例来源:origin: me.lucko/helper

public ItemStackBuilder enchant(Enchantment enchantment) {
  return transform(itemStack -> itemStack.addUnsafeEnchantment(enchantment, 1));
}

代码示例来源:origin: lucko/helper

public ItemStackBuilder enchant(Enchantment enchantment, int level) {
  return transform(itemStack -> itemStack.addUnsafeEnchantment(enchantment, level));
}

代码示例来源:origin: SpigotMC/Spigot-API

/**
 * Adds the specified enchantments to this item stack in an unsafe manner.
 * <p>
 * This method is the same as calling {@link
 * #addUnsafeEnchantment(org.bukkit.enchantments.Enchantment, int)} for
 * each element of the map.
 *
 * @param enchantments Enchantments to add
 */
@Utility
public void addUnsafeEnchantments(Map<Enchantment, Integer> enchantments) {
  for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
    addUnsafeEnchantment(entry.getKey(), entry.getValue());
  }
}

代码示例来源:origin: marcelo-mason/PreciousStones

/**
 * Returns a real itemstack
 *
 * @return
 */
public ItemStack toItemStack() {
  ItemStack is = new ItemStack(getTypeId(), getAmount(), getDurability(), getData());
  for (Entry<Enchantment, Integer> ench : enchantments.entrySet()) {
    is.addUnsafeEnchantment(ench.getKey(), Math.min(ench.getValue(), ench.getKey().getMaxLevel()));
  }
  return is;
}

代码示例来源:origin: elBukkit/MagicPlugin

public static void applyEnchantments(ItemStack item, ConfigurationSection enchantConfig) {
  if (item == null || enchantConfig == null) return;
  Collection<String> enchantKeys = enchantConfig.getKeys(false);
  for (String enchantKey : enchantKeys)
  {
    try {
      Enchantment enchantment = Enchantment.getByName(enchantKey.toUpperCase());
      item.addUnsafeEnchantment(enchantment, enchantConfig.getInt(enchantKey));
    } catch (Exception ex) {
      Bukkit.getLogger().warning("Invalid enchantment: " + enchantKey);
    }
  }
}

代码示例来源:origin: garbagemule/MobArena

private static void addEnchantment(ItemStack stack, String ench) {
  String[] parts = ench.split(":");
  if (parts.length != 2) {
    return;
  }
  Enchantment enchantment = getEnchantment(parts[0]);
  if (enchantment == null) {
    return;
  }
  int lvl = Integer.parseInt(parts[1]);
  if (stack.getType() == Material.ENCHANTED_BOOK) {
    EnchantmentStorageMeta esm = (EnchantmentStorageMeta) stack.getItemMeta();
    esm.addStoredEnchant(enchantment, lvl, true);
    stack.setItemMeta(esm);
  } else {
    stack.addUnsafeEnchantment(enchantment, lvl);
  }
}

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

@SuppressWarnings("deprecation")
private void parseEnchants() {
 if (this.isMetarizable()) {
  Enchantment en = null;
  int level = 0;
  ConfigurationSection newSection = (ConfigurationSection) (this.configSection);
  ConfigurationSection enchantSection = (ConfigurationSection) newSection.get("enchants");
  for (String key : enchantSection.getKeys(false)) {
   if (Utils.isNumber(key)) {
    en = Enchantment.getById(Integer.parseInt(key));
    level = Integer.parseInt(enchantSection.get(key).toString());
   } else {
    en = Enchantment.getByName(key.toUpperCase());
    level = Integer.parseInt(enchantSection.get(key).toString());
   }
   if (en == null) {
    continue;
   }
   this.finalStack.addUnsafeEnchantment(en, level);
  }
 }
}

代码示例来源:origin: io.github.bedwarsrel/BedwarsRel-Common

@SuppressWarnings("deprecation")
private void parseEnchants() {
 if (this.isMetarizable()) {
  Enchantment en = null;
  int level = 0;
  ConfigurationSection newSection = (ConfigurationSection) (this.configSection);
  ConfigurationSection enchantSection = (ConfigurationSection) newSection.get("enchants");
  for (String key : enchantSection.getKeys(false)) {
   if (Utils.isNumber(key)) {
    en = Enchantment.getById(Integer.parseInt(key));
    level = Integer.parseInt(enchantSection.get(key).toString());
   } else {
    en = Enchantment.getByName(key.toUpperCase());
    level = Integer.parseInt(enchantSection.get(key).toString());
   }
   if (en == null) {
    continue;
   }
   this.finalStack.addUnsafeEnchantment(en, level);
  }
 }
}

代码示例来源:origin: SpigotMC/Spigot-API

/**
 * Adds the specified {@link Enchantment} to this item stack.
 * <p>
 * If this item stack already contained the given enchantment (at any
 * level), it will be replaced.
 *
 * @param ench Enchantment to add
 * @param level Level of the enchantment
 * @throws IllegalArgumentException if enchantment null, or enchantment is
 *     not applicable
 */
@Utility
public void addEnchantment(Enchantment ench, int level) {
  Validate.notNull(ench, "Enchantment cannot be null");
  if ((level < ench.getStartLevel()) || (level > ench.getMaxLevel())) {
    throw new IllegalArgumentException("Enchantment level is either too low or too high (given " + level + ", bounds are " + ench.getStartLevel() + " to " + ench.getMaxLevel() + ")");
  } else if (!ench.canEnchantItem(this)) {
    throw new IllegalArgumentException("Specified enchantment cannot be applied to this itemstack");
  }
  addUnsafeEnchantment(ench, level);
}

代码示例来源:origin: TheBusyBiscuit/Slimefun4

@EventHandler
public void onEnchant(EnchantItemEvent e) {
  if (Talisman.checkFor(e, SlimefunItem.getByID("MAGICIAN_TALISMAN"))) {
    List<String> enchantments = new ArrayList<String>();
    for (Enchantment en: Enchantment.values()) {
      for (int i = 1; i <= en.getMaxLevel(); i++) {
        if ((Boolean) Slimefun.getItemValue("MAGICIAN_TALISMAN", "allow-enchantments." + en.getName() + ".level." + i) && en.canEnchantItem(e.getItem())) enchantments.add(en.getName() + "-" + i);
      }
    }
    String enchant = enchantments.get(SlimefunStartup.randomize(enchantments.size()));
    e.getEnchantsToAdd().put(Enchantment.getByName(enchant.split("-")[0]), Integer.parseInt(enchant.split("-")[1]));
    
  }
  if (!e.getEnchantsToAdd().containsKey(Enchantment.SILK_TOUCH) && Enchantment.LOOT_BONUS_BLOCKS.canEnchantItem(e.getItem())) {
    if (Talisman.checkFor(e, SlimefunItem.getByID("WIZARD_TALISMAN"))) {
      if (e.getEnchantsToAdd().containsKey(Enchantment.LOOT_BONUS_BLOCKS)) e.getEnchantsToAdd().remove(Enchantment.LOOT_BONUS_BLOCKS);
      Set<Enchantment> enchantments = e.getEnchantsToAdd().keySet();
      for (Enchantment en: enchantments) {
        if (SlimefunStartup.chance(100, 40)) e.getEnchantsToAdd().put(en, SlimefunStartup.randomize(3) + 1);
      }
      
      e.getItem().addUnsafeEnchantment(Enchantment.LOOT_BONUS_BLOCKS, SlimefunStartup.randomize(3) + 3);
    }
  }
}

相关文章