Skip to content

Commit

Permalink
added missing version-dependent code-branch; fixed version-comparison…
Browse files Browse the repository at this point in the history
… in LangKeyedItemMaterial
  • Loading branch information
BlvckBytes committed Oct 12, 2024
1 parent b49ea0a commit 70519bb
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,25 +13,26 @@ 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;

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<? extends LangKeyed<?>> getEnchantments() {
return Registry.ENCHANTMENT.stream().map(LangKeyedEnchantment::new).toList();
}

@Override
public Iterable<? extends LangKeyed<?>> getEffects() {
return Arrays.stream(PotionEffectType.values()).map(LangKeyedPotionEffectType::new).toList();
}

@Override
public Iterable<? extends LangKeyed<?>> getItemMaterials(JsonObject languageJson) {
return Registry.MATERIAL.stream().filter(Material::isItem).map(it -> new LangKeyedItemMaterial(it, serverVersion, languageJson)).toList();
}

@Override
public @Nullable Iterable<? extends LangKeyed<?>> 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<LangKeyedMusicInstrument>) translation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 70519bb

Please sign in to comment.