Bukkit/Bukkit — pull request #1029 by stuntguy3000 (2014-02-10 12:31:20+00:00)
Title: [B] Add way to check of an Enchantment level is deemed "safe"

**The Issue:**
There is no proper way for developers to verify if an Enchantment is unsafe (or safe).

**PR Breakdown:**
This PR adds a static method, "isSafe(Enchantment ench, int level)" to the "Enchantment" class. The method checks if the specified level is within the "safe" boundaries. It checks this by performing two checks.
1) Is the level lower than the min?
2) Is the level higher than the max?

If any of these questions are false, the method returns false, specifying the Enchantment level **is not** safe. If both of these questions are answered **true** then this indicated the Enchantment level **is** safe.

**Justification for this PR:**
As said above, There is no proper method for verifying if an Enchantment is unsafe or safe. The only way to do this, is to try to add the enchantment to the ItemStack and catch for the IllegalArgumentException. This method clearly is not efficient, and should be replaced with a proper method for doing so.

**Use Case Example: #1**
A simple plugin that allows Custom ItemStacks to be created, with Lores, Display-names, and Enchantments.

Currently, the plugin would have to catch for the Exception to be fired if an invalid Enchantment was specified, and then alert the player that it was invalid. This is not an efficient method, and should be replaced with the proposed code above, which is more efficient.

**Use Case Example: #2**
A plugin randomly gives every player on the server a random Item, with a random Material and a random enchantment/random enchantment level. **I believe** Some enchants have different maximums than others. The plugin would have to do two things to ensure it does not fail

1) Limit the enchantment level to one that works for all enchantments
2) Catch exceptions, and manually lower (or do something i.e. add unsafeEnchantment) to prevent the player from not receiving the item.

*I hope both use cases explain the PR in great detail, if not please ask for more clarification*

**JIRA Ticket:** https://bukkit.atlassian.net/browse/BUKKIT-5397

**Associated PR:**

**Testing Material:**

**Code**

```
package me.test;

import org.bukkit.enchantments.Enchantment;
import org.bukkit.plugin.java.JavaPlugin;

public class Test extends JavaPlugin {

public void onEnable() {
// Is Not (Un-safe)
System.out.println(Enchantment.isSafe(Enchantment.ARROW_KNOCKBACK, 66));
// Is Valid (Safe)
System.out.println(Enchantment.isSafe(Enchantment.DAMAGE_ALL, 1));
// Is Not (Un-safe)
System.out.println(Enchantment.isSafe(Enchantment.LOOT_BONUS_MOBS, 7));
// Is Not (Un-safe)
System.out.println(Enchantment.isSafe(Enchantment.PROTECTION_FALL, -55));
}
}
```

**Console Output**

```
[INFO]: [Test] Enabling Test v1.0
[INFO]: false
[INFO]: true
[INFO]: false
[INFO]: false
```

The provided materials above show the method in a working practice, by showing the method being executed and the results returned from the method.