當前位置: 首頁>>代碼示例>>Java>>正文


Java Enchantment.canApply方法代碼示例

本文整理匯總了Java中net.minecraft.enchantment.Enchantment.canApply方法的典型用法代碼示例。如果您正苦於以下問題:Java Enchantment.canApply方法的具體用法?Java Enchantment.canApply怎麽用?Java Enchantment.canApply使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在net.minecraft.enchantment.Enchantment的用法示例。


在下文中一共展示了Enchantment.canApply方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: execute

import net.minecraft.enchantment.Enchantment; //導入方法依賴的package包/類
/**
 * Callback for when the command is executed
 */
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException
{
    if (args.length < 2)
    {
        throw new WrongUsageException("commands.enchant.usage", new Object[0]);
    }
    else
    {
        EntityLivingBase entitylivingbase = (EntityLivingBase)getEntity(server, sender, args[0], EntityLivingBase.class);
        sender.setCommandStat(CommandResultStats.Type.AFFECTED_ITEMS, 0);
        Enchantment enchantment;

        try
        {
            enchantment = Enchantment.getEnchantmentByID(parseInt(args[1], 0));
        }
        catch (NumberInvalidException var12)
        {
            enchantment = Enchantment.getEnchantmentByLocation(args[1]);
        }

        if (enchantment == null)
        {
            throw new NumberInvalidException("commands.enchant.notFound", new Object[] {args[1]});
        }
        else
        {
            int i = 1;
            ItemStack itemstack = entitylivingbase.getHeldItemMainhand();

            if (itemstack.func_190926_b())
            {
                throw new CommandException("commands.enchant.noItem", new Object[0]);
            }
            else if (!enchantment.canApply(itemstack))
            {
                throw new CommandException("commands.enchant.cantEnchant", new Object[0]);
            }
            else
            {
                if (args.length >= 3)
                {
                    i = parseInt(args[2], enchantment.getMinLevel(), enchantment.getMaxLevel());
                }

                if (itemstack.hasTagCompound())
                {
                    NBTTagList nbttaglist = itemstack.getEnchantmentTagList();

                    if (nbttaglist != null)
                    {
                        for (int j = 0; j < nbttaglist.tagCount(); ++j)
                        {
                            int k = nbttaglist.getCompoundTagAt(j).getShort("id");

                            if (Enchantment.getEnchantmentByID(k) != null)
                            {
                                Enchantment enchantment1 = Enchantment.getEnchantmentByID(k);

                                if (!enchantment.func_191560_c(enchantment1))
                                {
                                    throw new CommandException("commands.enchant.cantCombine", new Object[] {enchantment.getTranslatedName(i), enchantment1.getTranslatedName(nbttaglist.getCompoundTagAt(j).getShort("lvl"))});
                                }
                            }
                        }
                    }
                }

                itemstack.addEnchantment(enchantment, i);
                notifyCommandListener(sender, this, "commands.enchant.success", new Object[0]);
                sender.setCommandStat(CommandResultStats.Type.AFFECTED_ITEMS, 1);
            }
        }
    }
}
 
開發者ID:sudofox,項目名稱:Backmemed,代碼行數:79,代碼來源:CommandEnchant.java

示例2: execute

import net.minecraft.enchantment.Enchantment; //導入方法依賴的package包/類
/**
 * Callback for when the command is executed
 */
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException
{
    if (args.length < 2)
    {
        throw new WrongUsageException("commands.enchant.usage", new Object[0]);
    }
    else
    {
        EntityLivingBase entitylivingbase = (EntityLivingBase)getEntity(server, sender, args[0], EntityLivingBase.class);
        sender.setCommandStat(CommandResultStats.Type.AFFECTED_ITEMS, 0);
        Enchantment enchantment;

        try
        {
            enchantment = Enchantment.getEnchantmentByID(parseInt(args[1], 0));
        }
        catch (NumberInvalidException var12)
        {
            enchantment = Enchantment.getEnchantmentByLocation(args[1]);
        }

        if (enchantment == null)
        {
            throw new NumberInvalidException("commands.enchant.notFound", new Object[] {Integer.valueOf(Enchantment.getEnchantmentID(enchantment))});
        }
        else
        {
            int i = 1;
            ItemStack itemstack = entitylivingbase.getHeldItemMainhand();

            if (itemstack == null)
            {
                throw new CommandException("commands.enchant.noItem", new Object[0]);
            }
            else if (!enchantment.canApply(itemstack))
            {
                throw new CommandException("commands.enchant.cantEnchant", new Object[0]);
            }
            else
            {
                if (args.length >= 3)
                {
                    i = parseInt(args[2], enchantment.getMinLevel(), enchantment.getMaxLevel());
                }

                if (itemstack.hasTagCompound())
                {
                    NBTTagList nbttaglist = itemstack.getEnchantmentTagList();

                    if (nbttaglist != null)
                    {
                        for (int j = 0; j < nbttaglist.tagCount(); ++j)
                        {
                            int k = nbttaglist.getCompoundTagAt(j).getShort("id");

                            if (Enchantment.getEnchantmentByID(k) != null)
                            {
                                Enchantment enchantment1 = Enchantment.getEnchantmentByID(k);

                                if (!enchantment.canApplyTogether(enchantment1) || !enchantment1.canApplyTogether(enchantment)) //Forge BugFix: Let Both enchantments veto being together
                                {
                                    throw new CommandException("commands.enchant.cantCombine", new Object[] {enchantment.getTranslatedName(i), enchantment1.getTranslatedName(nbttaglist.getCompoundTagAt(j).getShort("lvl"))});
                                }
                            }
                        }
                    }
                }

                itemstack.addEnchantment(enchantment, i);
                notifyCommandListener(sender, this, "commands.enchant.success", new Object[0]);
                sender.setCommandStat(CommandResultStats.Type.AFFECTED_ITEMS, 1);
            }
        }
    }
}
 
開發者ID:F1r3w477,項目名稱:CustomWorldGen,代碼行數:79,代碼來源:CommandEnchant.java


注:本文中的net.minecraft.enchantment.Enchantment.canApply方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。