Java Code Examples for org.bukkit.enchantments.Enchantment#values()

The following examples show how to use org.bukkit.enchantments.Enchantment#values() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Methods.java    From Crazy-Crates with MIT License 6 votes vote down vote up
public static Enchantment getEnchantment(String enchantmentName) {
    HashMap<String, String> enchantments = getEnchantmentList();
    enchantmentName = stripEnchantmentName(enchantmentName);
    for (Enchantment enchantment : Enchantment.values()) {
        try {
            //MC 1.13+ has the correct names.
            if (Version.getCurrentVersion().isNewer(Version.v1_12_R1)) {
                if (stripEnchantmentName(enchantment.getKey().getKey()).equalsIgnoreCase(enchantmentName)) {
                    return enchantment;
                }
            }
            if (stripEnchantmentName(enchantment.getName()).equalsIgnoreCase(enchantmentName) || (enchantments.get(enchantment.getName()) != null &&
            stripEnchantmentName(enchantments.get(enchantment.getName())).equalsIgnoreCase(enchantmentName))) {
                return enchantment;
            }
        } catch (Exception ignore) {//If any null enchantments are found they may cause errors.
        }
    }
    return null;
}
 
Example 2
Source File: ItemBuilder.java    From Crazy-Crates with MIT License 6 votes vote down vote up
private static Enchantment getEnchantment(String enchantmentName) {
    enchantmentName = stripEnchantmentName(enchantmentName);
    for (Enchantment enchantment : Enchantment.values()) {
        try {
            //MC 1.13+ has the correct names.
            if (version.isNewer(Version.v1_12_R1)) {
                if (stripEnchantmentName(enchantment.getKey().getKey()).equalsIgnoreCase(enchantmentName)) {
                    return enchantment;
                }
            }
            HashMap<String, String> enchantments = getEnchantmentList();
            if (stripEnchantmentName(enchantment.getName()).equalsIgnoreCase(enchantmentName) || (enchantments.get(enchantment.getName()) != null &&
            stripEnchantmentName(enchantments.get(enchantment.getName())).equalsIgnoreCase(enchantmentName))) {
                return enchantment;
            }
        } catch (Exception ignore) {//If any null enchantments are found they may cause errors.
        }
    }
    return null;
}
 
Example 3
Source File: Configuration.java    From NyaaUtils with MIT License 6 votes vote down vote up
@SuppressWarnings("deprecation")
public Configuration(NyaaUtils plugin) {
    this.plugin = plugin;
    this.mailbox = new MailboxLocations(plugin);
    this.repair = new RepairConfig(plugin);
    this.globalLoreBlacklist = new GlobalLoreBlacklist(plugin);
    this.enchantSrcConfig = new EnchantSrcConfig(plugin);
    this.fuelConfig = new FuelConfig(plugin);
    this.timerConfig = new TimerConfig(plugin);
    this.realmConfig = new RealmConfig(plugin);
    this.particleConfig = new ParticleConfig(plugin);
    //TODO: Key based enchantment store
    for (Enchantment e : Enchantment.values()) {
        if (e == null) {
            plugin.getLogger().warning("Bad enchantment: null");
        } else if (e.getName() == null) {
            plugin.getLogger().warning(String.format("Bad enchantment: %s: %s", e.getClass().getName(), e.toString()));
        } else {
            enchantMaxLevel.put(e, e.getMaxLevel());
        }
    }
}
 
Example 4
Source File: AdminResCommand.java    From civcraft with GNU General Public License v2.0 6 votes vote down vote up
public void enchant_cmd() throws CivException {
	Player player = getPlayer();
	String enchant = getNamedString(1, "Enchant name");
	int level = getNamedInteger(2);
	
	
	ItemStack stack = player.getItemInHand();
	Enchantment ench = Enchantment.getByName(enchant);
	if (ench == null) {
		String out ="";
		for (Enchantment ench2 : Enchantment.values()) {
			out += ench2.getName()+",";
		}
		throw new CivException("No enchantment called "+enchant+" Options:"+out);
	}
	
	stack.addUnsafeEnchantment(ench, level);
	CivMessage.sendSuccess(sender, "Enchanted.");
}
 
Example 5
Source File: MagicianTalisman.java    From Slimefun4 with GNU General Public License v3.0 6 votes vote down vote up
public MagicianTalisman(SlimefunItemStack item, ItemStack[] recipe) {
    super(item, recipe, false, false, "magician", 80);

    for (Enchantment enchantment : Enchantment.values()) {
        try {
            for (int i = 1; i <= enchantment.getMaxLevel(); i++) {
                enchantments.add(new TalismanEnchantment(enchantment, i));
            }
        }
        catch (Exception x) {
            Slimefun.getLogger().log(Level.SEVERE, x, () -> "The following Exception occurred while trying to register the following Enchantment: " + enchantment);
        }
    }

    if (!enchantments.isEmpty()) {
        addItemSetting(enchantments.toArray(new ItemSetting[0]));
    }
}
 
Example 6
Source File: MsgUtil.java    From QuickShop-Reremake with GNU General Public License v3.0 5 votes vote down vote up
public static void loadEnchi18n() {
    plugin.getLogger().info("Starting loading enchantments translation...");
    File enchi18nFile = new File(plugin.getDataFolder(), "enchi18n.yml");
    if (!enchi18nFile.exists()) {
        plugin.getLogger().info("Creating enchi18n.yml");
        plugin.saveResource("enchi18n.yml", false);
    }
    // Store it
    enchi18n = YamlConfiguration.loadConfiguration(enchi18nFile);
    enchi18n.options().copyDefaults(false);
    YamlConfiguration enchi18nYAML =
            YamlConfiguration.loadConfiguration(
                    new InputStreamReader(Objects.requireNonNull(plugin.getResource("enchi18n.yml"))));
    enchi18n.setDefaults(enchi18nYAML);
    Util.parseColours(enchi18n);
    Enchantment[] enchsi18n = Enchantment.values();
    for (Enchantment ench : enchsi18n) {
        String enchi18nString = enchi18n.getString("enchi18n." + ench.getKey().getKey().trim());
        if (enchi18nString != null && !enchi18nString.isEmpty()) {
            continue;
        }
        String enchName = gameLanguage.getEnchantment(ench);
        enchi18n.set("enchi18n." + ench.getKey().getKey(), enchName);
        plugin.getLogger().info("Found new ench [" + enchName + "] , adding it to the config...");
    }
    try {
        enchi18n.save(enchi18nFile);
    } catch (IOException e) {
        e.printStackTrace();
        plugin
                .getLogger()
                .log(
                        Level.WARNING,
                        "Could not load/save transaction enchname from enchi18n.yml. Skipping.");
    }
    plugin.getLogger().info("Complete to load enchantments translation.");
}
 
Example 7
Source File: ItemReader.java    From Survival-Games with GNU General Public License v3.0 4 votes vote down vote up
private static void loadIds(){
	
	encids =  new HashMap<String, Enchantment>();
	
	
	for(Enchantment e:Enchantment.values()){
		encids.put(e.toString().toLowerCase().replace("_", ""), e);
	}
	
	//Anything enchants
	encids.put("unbreaking", Enchantment.DURABILITY);
	encids.put("mending", Enchantment.MENDING);
	
	//Armor Enchants
	encids.put("prot", Enchantment.PROTECTION_ENVIRONMENTAL);
	encids.put("protection", Enchantment.PROTECTION_ENVIRONMENTAL);
	encids.put("fireprot", Enchantment.PROTECTION_FIRE);
	encids.put("fireprotection", Enchantment.PROTECTION_FIRE);
	encids.put("featherfall", Enchantment.PROTECTION_FALL);
	encids.put("featherfalling", Enchantment.PROTECTION_FALL);
	encids.put("blastprot", Enchantment.PROTECTION_EXPLOSIONS);
	encids.put("blastprotection", Enchantment.PROTECTION_EXPLOSIONS);
	encids.put("projectileprot", Enchantment.PROTECTION_PROJECTILE);
	encids.put("projectileprotection", Enchantment.PROTECTION_PROJECTILE);
	encids.put("aquaaffinity", Enchantment.WATER_WORKER);
	encids.put("respiration", Enchantment.OXYGEN);
	encids.put("thorns", Enchantment.THORNS);
	encids.put("depthstrider", Enchantment.DEPTH_STRIDER);
	encids.put("frostwalker", Enchantment.FROST_WALKER);
	
	//Weapon Enchants
	encids.put("knockback", Enchantment.KNOCKBACK);
	encids.put("smite", Enchantment.DAMAGE_UNDEAD);
	encids.put("baneofarthropods", Enchantment.DAMAGE_ARTHROPODS);
	encids.put("sharpness", Enchantment.DAMAGE_ALL);
	encids.put("dmg", Enchantment.DAMAGE_ALL);
	encids.put("fire", Enchantment.FIRE_ASPECT);
	encids.put("looting", Enchantment.LOOT_BONUS_MOBS);
	encids.put("loot", Enchantment.LOOT_BONUS_MOBS);
	encids.put("sweepingedge", Enchantment.SWEEPING_EDGE);
	
	//Tool enchants (Silk Touch's enchantment name is Silk_Touch, so it's covered above)
	encids.put("silktouch", Enchantment.SILK_TOUCH);
	encids.put("efficiency", Enchantment.DIG_SPEED);
	encids.put("fort", Enchantment.LOOT_BONUS_BLOCKS);
	encids.put("fortune", Enchantment.LOOT_BONUS_BLOCKS);
	
	//Bow specific enchants
	encids.put("punch", Enchantment.ARROW_KNOCKBACK);
	encids.put("power", Enchantment.ARROW_DAMAGE);
	encids.put("infinity", Enchantment.ARROW_INFINITE);
	encids.put("flame", Enchantment.ARROW_FIRE);

	//Fishing Rod specific enchants
	encids.put("luckofthesea", Enchantment.LUCK);
	encids.put("lure", Enchantment.LURE);
}
 
Example 8
Source File: CustomItemSerializer.java    From CS-CoreLib with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
public static String serialize(ItemStack item, ItemFlag... flags) {
	if (item == null) return "NULL";
	List<ItemFlag> flaglist = Arrays.asList(flags);
	
	Collections.sort(flaglist, comparator);
	
	StringBuilder builder = new StringBuilder();
	
	int i = 0;
	for (ItemFlag flag: flags) {
		if (i > 0) builder.append(" </sep> ");
		builder.append(flag.toString() + "=");
		
		switch (flag) {
		case AMOUNT: {
			builder.append(item.getAmount());
			break;
		}
		case DATA: {
			builder.append((int) item.getData().getData());
			break;
		}
		case DURABILITY: {
			builder.append((int) item.getDurability());
			break;
		}
		case ENCHANTMENTS:
			for (Enchantment enchantment: Enchantment.values()) {
				if (item.getEnchantments().containsKey(enchantment)) {
					builder.append(enchantment.getName() + ":" + item.getEnchantmentLevel(enchantment));
				}
				else {
					builder.append(enchantment.getName() + ":0");
				}
			}
			break;
		case ITEMMETA_DISPLAY_NAME: {
			if (item.hasItemMeta() && item.getItemMeta().hasDisplayName()) {
				builder.append(item.getItemMeta().getDisplayName().replaceAll("\\u00a7", "&"));
			}
			else {
				builder.append("NONE");
			}
			break;
		}
		case ITEMMETA_LORE: {
			if (item.hasItemMeta() && item.getItemMeta().hasLore()) {
				builder.append(item.getItemMeta().getLore().toString().replaceAll("\\u00a7", "&"));
			}
			else {
				builder.append("NONE");
			}
			break;
		}
		case MATERIAL: {
			builder.append(item.getType().toString());
			break;
		}
		default:
			break;
		}
		
		i++;
	}
	
	return builder.toString();
}
 
Example 9
Source File: RepairInstance.java    From NyaaUtils with MIT License 4 votes vote down vote up
public RepairInstance(ItemStack item, RepairConfig config, NyaaUtils plugin) {
    if (item == null || item.getType() == Material.AIR) return;
    RepairConfig.RepairConfigItem cfg = config.getRepairConfig(item.getType());
    if (cfg == null) return;
    if (!(item.getItemMeta() instanceof Repairable)) return;
    if (item.hasItemMeta() && item.getItemMeta().hasLore()) {
        if (!plugin.cfg.globalLoreBlacklist.canRepair(item.getItemMeta().getLore())) {
            stat = RepairStat.UNREPAIRABLE;
            return;
        }
    }
    stat = RepairStat.REPAIRABLE;
    if (item.getItemMeta().isUnbreakable()) {
        stat = RepairStat.UNREPAIRABLE_UNBREAKABLE;
    }
    Repairable repairableMeta = (Repairable) item.getItemMeta();
    Damageable damageableMeta = (Damageable) item.getItemMeta();
    repairLimit = cfg.repairLimit;
    if (repairLimit > 0 && repairableMeta.getRepairCost() >= repairLimit) {
        stat = RepairStat.UNREPAIRABLE_RLE;
    }

    Material toolMaterial = item.getType();
    repairMaterial = cfg.material;
    int currentDurability = damageableMeta.getDamage();
    if (currentDurability <= 0) {
        stat = RepairStat.UNREPAIRABLE_REPAIRED;
    }

    int enchLevel = 0;
    for (Enchantment ench : Enchantment.values()) {
        enchLevel += Math.max(item.getEnchantmentLevel(ench), 0);
    }

    int fullDurability = toolMaterial.getMaxDurability();
    durRecovered = (int) Math.floor((double) fullDurability / ((double) cfg.fullRepairCost + (double) enchLevel * cfg.enchantCostPerLv));
    expConsumption = (int) Math.floor(cfg.expCost + cfg.enchantCostPerLv * enchLevel);
    if (durRecovered <= 0) {
        stat = RepairStat.UNREPAIRABLE_LOWRECOVER;
    }
}