Skip to content

Commit

Permalink
EnchantmentType - fix toString showing namespacedkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneBeee committed Jul 5, 2024
1 parent 1ac7681 commit 81b01c3
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/main/java/ch/njol/skript/util/EnchantmentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package ch.njol.skript.util;

import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.bukkitutil.EnchantmentUtils;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.lang.ParseContext;
Expand Down Expand Up @@ -92,7 +91,7 @@ public boolean has(final ItemType item) {

@Override
public String toString() {
return EnchantmentUtils.toString(type) + (level == -1 ? "" : " " + level);
return getEnchantmentParser().toString(type, 0) + (level == -1 ? "" : " " + level);
}

@SuppressWarnings("null")
Expand All @@ -107,27 +106,18 @@ public String toString() {
*/
@Nullable
public static EnchantmentType parse(final String s) {
if (ENCHANTMENT_PARSER == null) {
ClassInfo<Enchantment> classInfo = Classes.getExactClassInfo(Enchantment.class);
if (classInfo == null) {
throw new IllegalStateException("Enchantment ClassInfo not found");
}
ENCHANTMENT_PARSER = (Parser<Enchantment>) classInfo.getParser();
if (ENCHANTMENT_PARSER == null) {
throw new IllegalStateException("Enchantment parser not found");
}
}
Parser<Enchantment> enchantmentParser = getEnchantmentParser();
if (pattern.matcher(s).matches()) {
String name = s.substring(0, s.lastIndexOf(' '));
assert name != null;
final Enchantment ench = ENCHANTMENT_PARSER.parse(name, ParseContext.DEFAULT);
final Enchantment ench = enchantmentParser.parse(name, ParseContext.DEFAULT);
if (ench == null)
return null;
String level = s.substring(s.lastIndexOf(' ') + 1);
assert level != null;
return new EnchantmentType(ench, Utils.parseInt(level));
}
final Enchantment ench = ENCHANTMENT_PARSER.parse(s, ParseContext.DEFAULT);
final Enchantment ench = enchantmentParser.parse(s, ParseContext.DEFAULT);
if (ench == null)
return null;
return new EnchantmentType(ench, -1);
Expand Down Expand Up @@ -156,4 +146,19 @@ public boolean equals(final @Nullable Object obj) {
return type.equals(other.type);
}

@SuppressWarnings("unchecked")
private static Parser<Enchantment> getEnchantmentParser() {
if (ENCHANTMENT_PARSER == null) {
ClassInfo<Enchantment> classInfo = Classes.getExactClassInfo(Enchantment.class);
if (classInfo == null) {
throw new IllegalStateException("Enchantment ClassInfo not found");
}
ENCHANTMENT_PARSER = (Parser<Enchantment>) classInfo.getParser();
if (ENCHANTMENT_PARSER == null) {
throw new IllegalStateException("Enchantment parser not found");
}
}
return ENCHANTMENT_PARSER;
}

}

0 comments on commit 81b01c3

Please sign in to comment.