ExchangeRule.java 文件源码

java
阅读 23 收藏 0 点赞 0 评论 0

项目:ItemExchange 作者:
public static ExchangeRule parseItemStack(ItemStack itemStack, RuleType ruleType) throws ExchangeRuleCreateException {
    Map<Enchantment, Integer> requiredEnchantments = new HashMap<Enchantment, Integer>();
    for (Enchantment enchantment : itemStack.getEnchantments().keySet()) {
        requiredEnchantments.put(enchantment, itemStack.getEnchantments().get(enchantment));
    }
    String displayName = "";
    String[] lore = new String[0];
    AdditionalMetadata additional = null;
    if (itemStack.hasItemMeta()) {
        ItemMeta itemMeta = itemStack.getItemMeta();
        if (itemMeta.hasDisplayName()) {
            displayName = itemMeta.getDisplayName();
        }
        if (itemMeta.hasLore()) {
            lore = itemMeta.getLore().toArray(new String[itemMeta.getLore().size()]);
        }

        if(itemMeta instanceof BookMeta) {
            additional = new BookMetadata((BookMeta) itemMeta);
        }
        else if(itemMeta instanceof EnchantmentStorageMeta) {
            additional = new EnchantmentStorageMetadata((EnchantmentStorageMeta) itemMeta);
        }
        else if(itemMeta instanceof PotionMeta) {
            additional = new PotionMetadata((PotionMeta) itemMeta);
        }
        //I've removed the PotionMeta block since it is not required if only vanilla potions are used, PotionMeta support should be added in the future
        if(itemMeta instanceof FireworkEffectMeta || itemMeta instanceof FireworkMeta || itemMeta instanceof LeatherArmorMeta || itemMeta instanceof MapMeta || itemMeta instanceof SkullMeta) {
            throw new ExchangeRuleCreateException("This item is not yet supported by ItemExchange.");
        }
    }

    ExchangeRule exchangeRule = new ExchangeRule(itemStack.getType(), itemStack.getAmount(), itemStack.getDurability(), requiredEnchantments, new ArrayList<Enchantment>(), false, displayName, lore, ruleType);

    exchangeRule.setAdditionalMetadata(additional);

    return exchangeRule;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号