示例#1
0
  /**
   * Registers an enchantment with the given ID and object.
   *
   * <p>Generally not to be used from within a plugin.
   *
   * @param id ID of the enchantment
   * @param enchantment Enchantment to register
   */
  public static void registerEnchantment(Enchantment enchantment) {
    if (byId.containsKey(enchantment.id) || byName.containsKey(enchantment.getName())) {
      throw new IllegalArgumentException("Cannot set already-set enchantment");
    } else if (!isAcceptingRegistrations()) {
      throw new IllegalStateException(
          "No longer accepting new enchantments (can only be done by the server implementation)");
    }

    byId.put(enchantment.id, enchantment);
    byName.put(enchantment.getName(), enchantment);
  }
示例#2
0
 public String getEnchantmentTypeName(ItemStack itemStack) {
   Enchantment enchantment = null;
   Integer level = 0;
   for (Map.Entry<Enchantment, Integer> e : itemStack.getEnchantments().entrySet()) {
     if (e.getValue() > level) {
       enchantment = e.getKey();
       level = e.getValue();
     }
   }
   if (enchantment == null) {
     return getPlugin()
         .getConfigurationManager()
         .getConfiguration(MythicConfigurationFile.LANGUAGE)
         .getString("Ordinary");
   }
   String ench =
       getPlugin()
           .getConfigurationManager()
           .getConfiguration(MythicConfigurationFile.LANGUAGE)
           .getString(enchantment.getName());
   if (ench != null) {
     return ench;
   }
   return "Ordinary";
 }
 @Override
 public String toString() {
   if (ce != null) {
     return ce.getName() + "-" + level;
   } else if (e != null) {
     return e.getName() + "-" + level;
   } else {
     return null;
   }
 }
示例#4
0
 public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
   if (sender.hasPermission("bukkit.command.enchant")) {
     Player p = Bukkit.getPlayerExact(sender.getName());
     if (args.length == 0) {
       List<String> names = new ArrayList<String>();
       for (Enchantment enchant : Enchantment.values()) names.add(enchant.getName().toUpperCase());
       sender.sendMessage(
           ChatColor.GREEN
               + "Enchants: |"
               + ChatColor.RED
               + StringUtils.join(names, ChatColor.GREEN + "|  |" + ChatColor.RED)
               + ChatColor.GREEN
               + "|");
     } else {
       Enchantment enchant = Enchantment.getByName(args[0].toUpperCase());
       if (enchant == null) {
         for (Enchantment enchantment : Enchantment.values())
           if (enchantment.getName().equalsIgnoreCase(args[0])) enchant = enchantment;
         if (enchant == null) {
           sender.sendMessage(ChatColor.GREEN + "Enchant: " + args[0] + " - Not found");
           return true;
         }
       }
       int level = enchant.getMaxLevel();
       if (args.length > 1) {
         if (lib.isNumeric(args[1])) level = Integer.parseInt(args[1]);
       }
       if (p.getItemInHand() != null && p.getItemInHand().getType() != Material.AIR) {
         p.getItemInHand().addUnsafeEnchantment(enchant, level);
         p.sendMessage(
             ChatColor.GREEN
                 + "Item enchanted with "
                 + enchant.getName()
                 + " with level "
                 + level);
         updateEnchants(p.getItemInHand());
       } else p.sendMessage(ChatColor.GREEN + "Unable to enchant item");
     }
   } else sender.sendMessage(ChatColor.RED + "You do not have permission to use this command");
   return true;
 }
示例#5
0
 ItemStack updateEnchants(ItemStack item) {
   ArrayList<String> enchants = new ArrayList<String>();
   for (Enchantment ench : item.getEnchantments().keySet()) {
     if (!isNatural(ench)) {
       enchants.add(
           ChatColor.GRAY + ench.getName() + " " + toRoman(item.getEnchantments().get(ench)));
     }
   }
   ItemMeta meta = item.getItemMeta();
   meta.setLore(enchants);
   item.setItemMeta(meta);
   return item;
 }
示例#6
0
	public static Enchantment getEnchantment(final String name, final IUser user) throws Exception
	{

		final Enchantment enchantment = Enchantments.getByName(name);
		if (enchantment == null)
		{
			throw new Exception(_("enchantmentNotFound"));
		}
		final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
		if (user != null && !Permissions.ENCHANT.isAuthorized(user, enchantmentName))
		{
			throw new Exception(_("enchantmentPerm", enchantmentName));
		}
		return enchantment;
	}
示例#7
0
 private double getEnchantmentBonus(ItemStack item, Stat stat) {
   double bonus = 0D;
   if (item.hasItemMeta()) {
     ItemMeta meta = item.getItemMeta();
     if (meta.hasEnchants()) {
       for (Enchantment enchantment : meta.getEnchants().keySet()) {
         bonus +=
             meta.getEnchants().get(enchantment)
                 * plugin
                     .getConfig()
                     .getDouble("rolls.enchants." + enchantment.getName() + "." + stat.toString());
       }
     }
   }
   return bonus;
 }
示例#8
0
	//TODO: Implement charge costs: final Trade charge = new Trade("enchant-" + enchantmentName, ess);
	@Override
	protected void run(final IUser user, final String commandLabel, final String[] args) throws Exception
	{
		final ItemStack stack = user.getPlayer().getItemInHand();
		if (stack == null)
		{
			throw new Exception(_("nothingInHand"));
		}
		if (args.length == 0)
		{
			final Set<String> enchantmentslist = new TreeSet<String>();
			for (Map.Entry<String, Enchantment> entry : Enchantments.entrySet())
			{
				final String enchantmentName = entry.getValue().getName().toLowerCase(Locale.ENGLISH);
				if (enchantmentslist.contains(enchantmentName) || Permissions.ENCHANT.isAuthorized(user, enchantmentName))
				{
					enchantmentslist.add(entry.getKey());
					//enchantmentslist.add(enchantmentName);
				}
			}
			throw new NotEnoughArgumentsException(_("enchantments", Util.joinList(enchantmentslist.toArray())));
		}
		int level = -1;
		if (args.length > 1)
		{
			try
			{
				level = Integer.parseInt(args[1]);
			}
			catch (NumberFormatException ex)
			{
				level = -1;
			}
		}
		final boolean allowUnsafe = Permissions.ENCHANT_UNSAFE.isAuthorized(user);
		final Enchantment enchantment = getEnchantment(args[0], user);
		if (level < 0 || (!allowUnsafe && level > enchantment.getMaxLevel()))
		{
			level = enchantment.getMaxLevel();
		}
		if (level == 0)
		{
			stack.removeEnchantment(enchantment);
		}
		else
		{
			if (allowUnsafe)
			{
				stack.addUnsafeEnchantment(enchantment, level);
			}
			else
			{
				stack.addEnchantment(enchantment, level);
			}
		}
		user.getPlayer().getInventory().setItemInHand(stack);
		user.getPlayer().updateInventory();
		final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
		if (level == 0)
		{
			user.sendMessage(_("enchantmentRemoved", enchantmentName.replace('_', ' ')));
		}
		else
		{
			user.sendMessage(_("enchantmentApplied", enchantmentName.replace('_', ' ')));
		}
	}
示例#9
0
  public void createCache(
      String playername, DataFileCache playerData, String group, boolean operator) {
    final File file = new File("cache/" + playername + ".cache");

    if (file.exists()) {
      return;
    }

    FileWriter writer = null;
    try {
      file.createNewFile();

      writer = new FileWriter(file);

      // put player group in cache
      // put if player is an op or not 1: is op 0: isnet op!
      // line format Group|OperatorStatus

      if (operator) writer.write(group + ";1\r\n");
      else writer.write(group + ";0\r\n");

      writer.flush();

      ItemStack[] invstack = playerData.getInventory();

      for (int i = 0; i < invstack.length; i++) {

        int itemid = 0;
        int amount = 0;
        int durability = 0;
        String enchList = "";
        // ench = new HashMap<Enchantment, Integer>();

        if (invstack[i] != null) {
          itemid = invstack[i].getTypeId();
          amount = invstack[i].getAmount();
          durability = invstack[i].getDurability();

          for (Enchantment e : invstack[i].getEnchantments().keySet()) {
            try {
              enchList =
                  enchList.concat(e.getName() + ":" + invstack[i].getEnchantmentLevel(e) + ":");
            } catch (NullPointerException npe) {
              ConsoleLogger.showError(npe.getMessage());
              ConsoleLogger.showError(
                  "The player " + playername + " has an illegaly enchant, Check Him !");
            }
          }
        }

        writer.write(
            "i" + ":" + itemid + ":" + amount + ":" + durability + ":" + enchList + "\r\n");
        writer.flush();
      }

      ItemStack[] armorstack = playerData.getArmour();

      for (int i = 0; i < armorstack.length; i++) {
        int itemid = 0;
        int amount = 0;
        int durability = 0;
        String enchList = "";

        if (armorstack[i] != null) {
          itemid = armorstack[i].getTypeId();
          amount = armorstack[i].getAmount();
          durability = armorstack[i].getDurability();

          for (Enchantment e : armorstack[i].getEnchantments().keySet()) {
            try {
              enchList =
                  enchList.concat(e.getName() + ":" + armorstack[i].getEnchantmentLevel(e) + ":");
            } catch (NullPointerException npe) {
              ConsoleLogger.showError(npe.getMessage());
              ConsoleLogger.showError(
                  "The player " + playername + " has an illegaly enchant, Check Him !");
            }
          }
        }

        writer.write(
            "w" + ":" + itemid + ":" + amount + ":" + durability + ":" + enchList + "\r\n");
        writer.flush();
      }

      writer.close();
    } catch (final Exception e) {
      e.printStackTrace();
    }
  }
示例#10
0
 private boolean isNatural(Enchantment ench) {
   return (ench.getName().equals(ench.getName().toUpperCase()) && !ench.getName().contains(" "));
 }
 @Override
 protected Enchantment matchNonAlias(String key) {
   for (Enchantment enchantment : Enchantment.values())
     if (key.equalsIgnoreCase(enchantment.getName())) return enchantment;
   return null;
 }