Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.wsman217.eternal_enchants.enchantments;
- import static org.bukkit.ChatColor.*;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import org.bukkit.Material;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.meta.ItemMeta;
- public enum Enchants {
- // Display name, min level, max level, applicable items
- AUTO_SMELT("Auto Smelt", 1, 1, Arrays.asList(Material.DIAMOND_PICKAXE, Material.DIAMOND_AXE), -1);
- private String displayName;
- private int minLvl, maxLvl;
- private ArrayList<Material> applicableItems = new ArrayList<Material>();
- private int lvl;
- Enchants(String displayName, int minLvl, int maxLvl, List<Material> applicableItems, int lvl) {
- this.displayName = displayName;
- this.minLvl = minLvl;
- this.maxLvl = maxLvl;
- this.lvl = lvl;
- }
- public int getLvlCurrentLvl() {
- return lvl;
- }
- public Enchants setCurrentLvl(int newLvl) {
- this.lvl = newLvl;
- return this;
- }
- public String getDisplayName() {
- return displayName;
- }
- public int getMinLvl() {
- return minLvl;
- }
- public int getMaxLvl() {
- return maxLvl;
- }
- public ArrayList<Material> getApplicableItems() {
- return applicableItems;
- }
- public static boolean isApplicable(Enchants enchant, Material toApply) {
- for (Material appItem : enchant.getApplicableItems())
- if (appItem == toApply)
- return true;
- return false;
- }
- public static Enchants findByName(String name) {
- for (Enchants ench : Enchants.values())
- if (stripColor(ench.getDisplayName()).equals(name))
- return ench;
- return null;
- }
- /**
- * Adds an enchantment to the item that is in within the highest and lowest
- * level of the enchant.
- *
- * @param item
- * @param enchant
- * @param lvl
- * @return The item with the enchant.
- */
- public static ItemStack addSafeEnchant(ItemStack item, Enchants enchant, int lvl) {
- if (!isWithinLvl(enchant, lvl))
- lvl = enchant.getMaxLvl();
- ItemMeta im = item.getItemMeta();
- List<String> itemLore = new ArrayList<String>();
- if (im.hasLore())
- itemLore = im.getLore();
- String ench = GRAY + enchant.getDisplayName();
- if (lvl <= 10)
- ench += lvlToRoman(lvl);
- else
- ench += ".level." + lvl;
- int index = 0;
- for (String loreLine : itemLore) {
- index++;
- if (loreLine.startsWith(enchant.getDisplayName())) {
- itemLore.set(index, ench);
- im.setLore(itemLore);
- item.setItemMeta(im);
- return item;
- }
- }
- itemLore.add(ench);
- im.setLore(itemLore);
- item.setItemMeta(im);
- return item;
- }
- /**
- * Adds an enchantment to the item that can be out of the highest and lowest
- * level of the enchant.
- *
- * @param item
- * @param enchant
- * @param lvl
- * @return The item with the enchant.
- */
- public static ItemStack addUnsafeEnchant(ItemStack item, Enchants enchant, int lvl) {
- ItemMeta im = item.getItemMeta();
- List<String> itemLore = new ArrayList<String>();
- if (im.hasLore())
- itemLore = im.getLore();
- String ench = GRAY + enchant.getDisplayName();
- if (lvl <= 10)
- ench += lvlToRoman(lvl);
- else
- ench += ".level." + lvl;
- int index = 0;
- for (String loreLine : itemLore) {
- index++;
- if (loreLine.startsWith(enchant.getDisplayName())) {
- itemLore.set(index, ench);
- im.setLore(itemLore);
- item.setItemMeta(im);
- return item;
- }
- }
- itemLore.add(ench);
- im.setLore(itemLore);
- item.setItemMeta(im);
- return item;
- }
- public static ArrayList<Enchants> getCustomEnchantsFromItem(ItemStack item) {
- ItemMeta im = item.getItemMeta();
- if (!im.hasLore())
- return new ArrayList<Enchants>();
- List<String> itemLore = im.getLore();
- ArrayList<Enchants> customEnchantsOnItem = new ArrayList<Enchants>();
- for (String loreLine : itemLore)
- for (Enchants enchants : Enchants.values())
- if (loreLine.startsWith(enchants.getDisplayName()))
- customEnchantsOnItem.add(enchants.setCurrentLvl(romanToLvl(loreLine.substring(loreLine.indexOf(" ")))));
- return customEnchantsOnItem;
- }
- private static int romanToLvl(String roman) {
- switch (roman) {
- case "I":
- return 1;
- case "II":
- return 2;
- case "III":
- return 3;
- case "IV":
- return 4;
- case "V":
- return 5;
- case "VI":
- return 6;
- case "VII":
- return 7;
- case "VIII":
- return 8;
- case "IX":
- return 9;
- case "X":
- return 10;
- default:
- return 0;
- }
- }
- private static String lvlToRoman(int lvl) {
- switch (lvl) {
- case 1:
- return " I";
- case 2:
- return " II";
- case 3:
- return " III";
- case 4:
- return " IV";
- case 5:
- return " V";
- case 6:
- return " VI";
- case 7:
- return " VII";
- case 8:
- return " VIII";
- case 9:
- return " IX";
- default:
- return " X";
- }
- }
- private static boolean isWithinLvl(Enchants enchant, int lvl) {
- return enchant.getMinLvl() <= lvl && enchant.getMaxLvl() >= lvl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement