Start using enum names for enchantments, fixes #566

This commit is contained in:
BuildTools 2018-12-11 16:41:43 -05:00
parent bf7a0849b1
commit 6e34954281
4 changed files with 58 additions and 8 deletions

View File

@ -3,7 +3,7 @@
<groupId>me.blackvein.quests</groupId>
<artifactId>quests</artifactId>
<version>3.5.0</version>
<version>3.5.1</version>
<name>quests</name>
<url>https://github.com/FlyingPikachu/Quests/</url>
<packaging>jar</packaging>

View File

@ -1644,12 +1644,24 @@ public class Quester {
return Lang.get("COLOR_" + color.name());
}
/**
* Gets player-friendly name from enchantment. 'FIRE_ASPECT' becomes 'Fire Aspect'
*
* @param e Enchantment to get pretty localized name of
* @return pretty localized name
*/
public static String prettyEnchantmentString(Enchantment e) {
String prettyString = enchantmentString(e);
prettyString = capitalsToSpaces(prettyString);
return prettyString;
}
/**
* Gets name of enchantment exactly as it appears in lang file
*
* @param e Enchantment to get localized name of
* @return localized name
*/
@SuppressWarnings("deprecation") // since 1.13
public static String enchantmentString(Enchantment e) {
try {
@ -1660,6 +1672,12 @@ public class Quester {
}
}
/**
* Adds a single space in front of all capital letters
*
* @param s string to process
* @return processed string
*/
public static String capitalsToSpaces(String s) {
int max = s.length();
for (int i = 1; i < max; i++) {
@ -1672,6 +1690,12 @@ public class Quester {
return s;
}
/**
* Capitalize character after space
*
* @param s string to process
* @return processed string
*/
public static String spaceToCapital(String s) {
int index = s.indexOf(' ');
if (index == -1) {

View File

@ -120,7 +120,7 @@ public class ItemUtil {
} catch (Exception e) {
try {
Bukkit.getLogger().warning(material + " is invalid! You may need to update your quests.yml or events.yml "
+ "in accordance with https://github.com/FlyingPikachu/Quests/wiki/Item-Formatting#list");
+ "in accordance with https://bit.ly/2BkBNNN");
return new ItemStack(Material.matchMaterial(material, true), amount, durability);
} catch (Exception e2) {
Bukkit.getLogger().severe("Unable to use LEGACY_" + material + " for as item name");
@ -168,12 +168,24 @@ public class ItemUtil {
} else if (arg.startsWith("enchantment-")) {
String[] temp = arg.substring(12).split(" ");
try {
if (Quests.getEnchantment(temp[0]) != null) {
enchs.put(Quests.getEnchantment(temp[0]), Integer.parseInt(temp[1]));
String key = Lang.getKey(temp[0]).replace(" ", "");
if (!key.equals("NULL")) {
// Legacy localized name
Enchantment e = Enchantment.getByName(key.replace("ENCHANTMENT_", ""));
if (e != null) {
enchs.put(e, Integer.parseInt(temp[1]));
} else {
Bukkit.getLogger().severe("Legacy enchantment name \'" + temp[0] + "\' on " + name + " is invalid. Make sure it is spelled correctly");
}
} else {
Bukkit.getLogger().severe("The enchantment name \'" + temp[0] + "\' on " + name + " is invalid. Make sure it is spelled correctly");
// Modern enum name
if (Enchantment.getByName(temp[0]) != null) {
enchs.put(Enchantment.getByName(temp[0]), Integer.parseInt(temp[1]));
} else {
Bukkit.getLogger().severe("Enum enchantment name \'" + temp[0] + "\' on " + name + " is invalid. Make sure it is spelled correctly");
}
}
} catch (IllegalArgumentException e) {
} catch (Exception e) {
Bukkit.getLogger().severe("The enchantment name \'" + temp[0] + "\' on " + name + " is invalid. Make sure quests.yml is UTF-8 encoded");
return null;
}
@ -251,7 +263,11 @@ public class ItemUtil {
}
if (!enchs.isEmpty()) {
for (Enchantment e : enchs.keySet()) {
meta.addEnchant(e, enchs.get(e), true);
try {
meta.addEnchant(e, enchs.get(e), true);
} catch (IllegalArgumentException iae) {
Bukkit.getLogger().severe("Enchantment on " + name + " cannot be null. Skipping for that quest");
}
}
}
if (display != null) {
@ -304,7 +320,7 @@ public class ItemUtil {
}
if (is.getEnchantments().isEmpty() == false) {
for (Entry<Enchantment, Integer> e : is.getEnchantments().entrySet()) {
serial += ":enchantment-" + Quester.enchantmentString(e.getKey()) + " " + e.getValue();
serial += ":enchantment-" + e.getKey().getName() + " " + e.getValue();
}
}
if (is.hasItemMeta()) {

View File

@ -63,6 +63,11 @@ public class Lang {
return langMap.containsKey(key) ? tokens.convertString(langMap.get(key)) : "NULL";
}
/**
* Get key for lang string
* @param val
* @return key or "NULL" as String
*/
public static String getKey(String val) {
for (Entry<String, String> entry : langMap.entrySet()) {
if (entry.getValue().equals(val)) {
@ -72,6 +77,11 @@ public class Lang {
return "NULL";
}
/**
* Get key starting with "COMMAND_" for lang string
* @param val
* @return key or "NULL" as String
*/
public static String getCommandKey(String val) {
for (Entry<String, String> entry : langMap.entrySet()) {
if (entry.getValue().equalsIgnoreCase(val) && entry.getKey().toUpperCase().startsWith("COMMAND_")) {