diff --git a/src/main/java/me/blvckbytes/item_predicate_parser/translation/VersionDependentCodeFactory.java b/src/main/java/me/blvckbytes/item_predicate_parser/translation/VersionDependentCodeFactory.java index d09d713..4d84bf3 100644 --- a/src/main/java/me/blvckbytes/item_predicate_parser/translation/VersionDependentCodeFactory.java +++ b/src/main/java/me/blvckbytes/item_predicate_parser/translation/VersionDependentCodeFactory.java @@ -1,7 +1,6 @@ package me.blvckbytes.item_predicate_parser.translation; import me.blvckbytes.item_predicate_parser.translation.version.DetectedServerVersion; -import me.blvckbytes.item_predicate_parser.translation.version.ExpectedServerVersion; import me.blvckbytes.item_predicate_parser.translation.version.IVersionDependentCode; import java.lang.invoke.MethodHandles; @@ -14,7 +13,8 @@ public class VersionDependentCodeFactory { private static final MethodType constructorType = MethodType.methodType(void.class, DetectedServerVersion.class, Logger.class); // NOTE: Using path-notation to support relocation, if ever needed - private static final String GTE_1_20_PATH = "me/blvckbytes/item_predicate_parser/translation/version/VersionDependentCode_GTE_1_20"; + private static final String GT_1_20_PATH = "me/blvckbytes/item_predicate_parser/translation/version/VersionDependentCode_GT_1_20"; + private static final String E_1_20_PATH = "me/blvckbytes/item_predicate_parser/translation/version/VersionDependentCode_E_1_20"; private static final String LT_1_20_PATH = "me/blvckbytes/item_predicate_parser/translation/version/VersionDependentCode_LT_1_20"; private final IVersionDependentCode instance; @@ -22,17 +22,17 @@ public class VersionDependentCodeFactory { public VersionDependentCodeFactory(DetectedServerVersion serverVersion, Logger logger) throws Throwable { Class targetClass; - // 1.20 is greater than current => LT 1.20 - if (ExpectedServerVersion.V1_20.compare(serverVersion) > 0) { + if (serverVersion.major() != 1) + throw new IllegalStateException("Cannot handle a major-version greater than one"); + + if (serverVersion.minor() == 20) + targetClass = Class.forName(E_1_20_PATH.replace('/', '.')); + + else if (serverVersion.minor() > 20) + targetClass = Class.forName(GT_1_20_PATH.replace('/', '.')); + + else targetClass = Class.forName(LT_1_20_PATH.replace('/', '.')); - logger.info("Detected version < 1.20"); - } - - // 1.20 is less than or equal to current => GTE 1.20 - else { - targetClass = Class.forName(GTE_1_20_PATH.replace('/', '.')); - logger.info("Detected version >= 1.20"); - } var constructorHandle = publicLookup.findConstructor(targetClass, constructorType); this.instance = (IVersionDependentCode) constructorHandle.invoke(serverVersion, logger); diff --git a/src/main/java/me/blvckbytes/item_predicate_parser/translation/keyed/LangKeyedItemMaterial.java b/src/main/java/me/blvckbytes/item_predicate_parser/translation/keyed/LangKeyedItemMaterial.java index 9cd883f..86e5ade 100644 --- a/src/main/java/me/blvckbytes/item_predicate_parser/translation/keyed/LangKeyedItemMaterial.java +++ b/src/main/java/me/blvckbytes/item_predicate_parser/translation/keyed/LangKeyedItemMaterial.java @@ -76,11 +76,11 @@ public String toString() { /* NETHERITE_UPGRADE_SMITHING_TEMPLATE: - <= 1.19.4: item.minecraft.smithing_template + <= 1.20.X: item.minecraft.smithing_template > : item.minecraft.netherite_upgrade_smithing_template */ - if (version.major() == 1 && version.minor() <= 19) { + if (version.major() == 1 && version.minor() <= 20) { if (key.equals("netherite_upgrade_smithing_template")) return "item.minecraft.smithing_template"; } diff --git a/src/main/java/me/blvckbytes/item_predicate_parser/translation/version/ExpectedServerVersion.java b/src/main/java/me/blvckbytes/item_predicate_parser/translation/version/ExpectedServerVersion.java deleted file mode 100644 index 7336e2a..0000000 --- a/src/main/java/me/blvckbytes/item_predicate_parser/translation/version/ExpectedServerVersion.java +++ /dev/null @@ -1,26 +0,0 @@ -package me.blvckbytes.item_predicate_parser.translation.version; - -public enum ExpectedServerVersion { - V1_20(1, 20, 0) - ; - - public final int major; - public final int minor; - public final int patch; - - ExpectedServerVersion(int major, int minor, int patch) { - this.major = major; - this.minor = minor; - this.patch = patch; - } - - public int compare(DetectedServerVersion version) { - if (major > version.major()) return 1; - if (major < version.major()) return -1; - - if (minor > version.minor()) return 1; - if (minor < version.minor()) return -1; - - return Integer.compare(patch, version.patch()); - } -} diff --git a/src/main/java/me/blvckbytes/item_predicate_parser/translation/version/VersionDependentCode_E_1_20.java b/src/main/java/me/blvckbytes/item_predicate_parser/translation/version/VersionDependentCode_E_1_20.java new file mode 100644 index 0000000..1c5b958 --- /dev/null +++ b/src/main/java/me/blvckbytes/item_predicate_parser/translation/version/VersionDependentCode_E_1_20.java @@ -0,0 +1,57 @@ +package me.blvckbytes.item_predicate_parser.translation.version; + +import com.google.gson.JsonObject; +import me.blvckbytes.item_predicate_parser.predicate.ItemPredicate; +import me.blvckbytes.item_predicate_parser.predicate.MusicInstrumentPredicate; +import me.blvckbytes.item_predicate_parser.token.Token; +import me.blvckbytes.item_predicate_parser.translation.TranslatedLangKeyed; +import me.blvckbytes.item_predicate_parser.translation.keyed.*; +import org.bukkit.Material; +import org.bukkit.Registry; +import org.bukkit.potion.PotionEffectType; +import org.jetbrains.annotations.Nullable; + +import java.util.Arrays; +import java.util.logging.Logger; + +public class VersionDependentCode_E_1_20 implements IVersionDependentCode { + + private final Logger logger; + private final DetectedServerVersion serverVersion; + + public VersionDependentCode_E_1_20(DetectedServerVersion serverVersion, Logger logger) { + this.logger = logger; + this.serverVersion = serverVersion; + } + + @Override + public Iterable> getEnchantments() { + return Registry.ENCHANTMENT.stream().map(LangKeyedEnchantment::new).toList(); + } + + @Override + public Iterable> getEffects() { + return Arrays.stream(PotionEffectType.values()).map(LangKeyedPotionEffectType::new).toList(); + } + + @Override + public Iterable> getItemMaterials(JsonObject languageJson) { + return Registry.MATERIAL.stream().filter(Material::isItem).map(it -> new LangKeyedItemMaterial(it, serverVersion, languageJson)).toList(); + } + + @Override + public @Nullable Iterable> getInstruments() { + return Registry.INSTRUMENT.stream().map(LangKeyedMusicInstrument::new).toList(); + } + + @Override + @SuppressWarnings("unchecked") + public @Nullable ItemPredicate makeInstrumentPredicate(Token token, TranslatedLangKeyed translation) { + if (!(translation.langKeyed instanceof LangKeyedMusicInstrument)) { + logger.severe("Unexpected lang-keyed of non-MusicInstrument type; returning null!"); + return null; + } + + return new MusicInstrumentPredicate(token, (TranslatedLangKeyed) translation); + } +} diff --git a/src/main/java/me/blvckbytes/item_predicate_parser/translation/version/VersionDependentCode_GTE_1_20.java b/src/main/java/me/blvckbytes/item_predicate_parser/translation/version/VersionDependentCode_GT_1_20.java similarity index 91% rename from src/main/java/me/blvckbytes/item_predicate_parser/translation/version/VersionDependentCode_GTE_1_20.java rename to src/main/java/me/blvckbytes/item_predicate_parser/translation/version/VersionDependentCode_GT_1_20.java index 467a5f7..6fce90d 100644 --- a/src/main/java/me/blvckbytes/item_predicate_parser/translation/version/VersionDependentCode_GTE_1_20.java +++ b/src/main/java/me/blvckbytes/item_predicate_parser/translation/version/VersionDependentCode_GT_1_20.java @@ -12,12 +12,12 @@ import java.util.logging.Logger; -public class VersionDependentCode_GTE_1_20 implements IVersionDependentCode { +public class VersionDependentCode_GT_1_20 implements IVersionDependentCode { private final Logger logger; private final DetectedServerVersion serverVersion; - public VersionDependentCode_GTE_1_20(DetectedServerVersion serverVersion, Logger logger) { + public VersionDependentCode_GT_1_20(DetectedServerVersion serverVersion, Logger logger) { this.logger = logger; this.serverVersion = serverVersion; }