From e7c935f0a01a5435f3b983ef43f2f96d979ffce8 Mon Sep 17 00:00:00 2001 From: James Mitchell Date: Sat, 7 Sep 2024 06:16:00 -1000 Subject: [PATCH] Tidy up JEI integration, add RegistryAccess to ZGatherHints interface (#49) --- build.gradle | 13 +- dependencies.properties | 2 +- .../zeta/event/load/ZGatherHints.java | 2 + .../zeta/integration/JeiGatherHints.java | 177 ++++++++++++++++++ .../zeta/integration/ZetaJeiPlugin.java | 160 +--------------- .../event/load/ForgeZGatherHints.java | 6 +- 6 files changed, 198 insertions(+), 162 deletions(-) create mode 100644 src/main/java/org/violetmoon/zeta/integration/JeiGatherHints.java diff --git a/build.gradle b/build.gradle index 9e96765..6005ffc 100644 --- a/build.gradle +++ b/build.gradle @@ -83,6 +83,13 @@ repositories { includeGroup "curse.maven" } } + maven { + name "blamejared" + url "https://maven.blamejared.com" + content { + includeGroup "mezz.jei" + } + } maven { name "jitpack" @@ -100,8 +107,12 @@ dependencies { } // deps - implementation fg.deobf("curse.maven:jei-238222:${deps.jei}") + // compile against the JEI API but do not include it at runtime + compileOnly fg.deobf("mezz.jei:jei-${config.mc_version}-common-api:${deps.jei}") + compileOnly fg.deobf("mezz.jei:jei-${config.mc_version}-forge-api:${deps.jei}") + // at runtime, use the full JEI jar for Forge + runtimeOnly fg.deobf("mezz.jei:jei-${config.mc_version}-forge:${deps.jei}") annotationProcessor 'org.spongepowered:mixin:0.8.5:processor' } diff --git a/dependencies.properties b/dependencies.properties index 78e99ba..025d321 100644 --- a/dependencies.properties +++ b/dependencies.properties @@ -1,2 +1,2 @@ forge=47.1.3 -jei=4712868 +jei=15.17.0.76 diff --git a/src/main/java/org/violetmoon/zeta/event/load/ZGatherHints.java b/src/main/java/org/violetmoon/zeta/event/load/ZGatherHints.java index c869d80..aba4337 100644 --- a/src/main/java/org/violetmoon/zeta/event/load/ZGatherHints.java +++ b/src/main/java/org/violetmoon/zeta/event/load/ZGatherHints.java @@ -1,5 +1,6 @@ package org.violetmoon.zeta.event.load; +import net.minecraft.core.RegistryAccess; import net.minecraft.network.chat.Component; import net.minecraft.world.level.ItemLike; import org.violetmoon.zeta.config.ConfigFlagManager; @@ -17,4 +18,5 @@ public interface ZGatherHints extends IZetaLoadEvent { void gatherHintsFromModule(ZetaModule module, ConfigFlagManager cfm); + RegistryAccess getRegistryAccess(); } diff --git a/src/main/java/org/violetmoon/zeta/integration/JeiGatherHints.java b/src/main/java/org/violetmoon/zeta/integration/JeiGatherHints.java new file mode 100644 index 0000000..cc64799 --- /dev/null +++ b/src/main/java/org/violetmoon/zeta/integration/JeiGatherHints.java @@ -0,0 +1,177 @@ +package org.violetmoon.zeta.integration; + +import mezz.jei.api.registration.IRecipeRegistration; +import net.minecraft.core.RegistryAccess; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.TagKey; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.ItemLike; +import org.violetmoon.zeta.config.ConfigFlagManager; +import org.violetmoon.zeta.config.ConfigObjectMapper; +import org.violetmoon.zeta.event.load.ZGatherHints; +import org.violetmoon.zeta.module.ZetaModule; +import org.violetmoon.zeta.util.Hint; +import org.violetmoon.zeta.util.RegistryUtil; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +class JeiGatherHints implements ZGatherHints { + + private final IRecipeRegistration registration; + private final String modId; + private final RegistryAccess registryAccess; + private final List blacklist; + private final MutableComponent externalPreamble; + + public JeiGatherHints(IRecipeRegistration registration, String modId, RegistryAccess registryAccess, List blacklist) { + this.registration = registration; + this.modId = modId; + this.registryAccess = registryAccess; + this.blacklist = blacklist; + + this.externalPreamble = Component.translatable(modId + ".jei.hint.preamble"); + externalPreamble.setStyle(externalPreamble.getStyle().withColor(0x0b5d4b)); + } + + @Override + public void accept(ItemLike itemLike, Component extra) { + Item item = itemLike.asItem(); + + if (blacklist.contains(item)) + return; + + MutableComponent compound = Component.literal(""); + if (!BuiltInRegistries.ITEM.getKey(item).getNamespace().equals(modId)) + compound = compound.append(externalPreamble); + compound = compound.append(extra); + + registration.addItemStackInfo(new ItemStack(item), compound); + } + + private void applyTag(TagKey tkey, String key, Object... extra) { + if (key.isEmpty()) + key = tkey.location().getPath(); + + try { + List tagItems = RegistryUtil.getTagValues(registryAccess, tkey); + applyIterable(tagItems, key, extra); + } catch (IllegalStateException e) { + throw new RuntimeException("TagKey " + tkey + " failed to load.", e); + } + } + + private void applyIterable(Iterable iter, String key, Object... extra) { + if (key.isEmpty()) + throw new RuntimeException("Multi-item @Hints need a defined key."); + + for (Object obj : iter) + applyObject(obj, key, extra); + } + + private void applyObject(Object obj, String key, Object... extra) { + if (obj instanceof ItemLike ilike) + applyItemLike(ilike, key, extra); + else + throw new RuntimeException("Not an ItemLike."); + } + + private void applyItemLike(ItemLike itemLike, String key, Object... extra) { + if (key.isEmpty()) + hintItem(itemLike, extra); + else + hintItem(itemLike, key, extra); + } + + @Override + public void hintItem(ItemLike itemLike, Object... extra) { + Item item = itemLike.asItem(); + ResourceLocation res = BuiltInRegistries.ITEM.getKey(item); + String ns = res.getNamespace(); + String path = res.getPath(); + if (ns.equals(modId)) + ns = ""; + else ns += "."; + + hintItem(itemLike, ns + path, extra); + } + + @Override + public void hintItem(ItemLike itemLike, String key, Object... extra) { + Item item = itemLike.asItem(); + String hint = modId + ".jei.hint." + key; + accept(item, Component.translatable(hint, extra)); + } + + @Override + public void gatherHintsFromModule(ZetaModule module, ConfigFlagManager cfm) { + if (!module.isEnabled()) + return; + + List fields = ConfigObjectMapper.walkModuleFields(module.getClass()); + + Map fieldsByName = new HashMap<>(); + for (Field f : fields) + fieldsByName.put(f.getName(), f); + + for (Field f : fields) { + try { + Hint hint = f.getAnnotation(Hint.class); + if (hint == null) + continue; + f.setAccessible(true); + + //Target + Object target = ConfigObjectMapper.getField(module, f); + if (target == null) + continue; + + //Flag + String flag = hint.value(); + if (!flag.isEmpty() && cfm.getFlag(flag) == !hint.negate()) + continue; + + //Translation, & gathering extra content + String key = hint.key(); + List extraList = new ArrayList<>(hint.content().length); + for (String c : hint.content()) { + if (c.isEmpty()) + continue; + + Field extraField = fieldsByName.get(c); + if (extraField == null) + throw new RuntimeException("No field " + c + " referenced in @Hint " + f); + + Object yes = ConfigObjectMapper.getField(module, extraField); + extraList.add(yes); + } + Object[] extra = extraList.toArray(new Object[0]); + + //Application + if (target instanceof TagKey tkey) + applyTag(tkey, key, extra); + else if (target instanceof Iterable iter) + applyIterable(iter, key, extra); + else + applyObject(target, key, extra); + + } catch (Exception e) { + throw new RuntimeException("Problem applying annotation hint " + f.getName() + + " from module " + module.getClass().getName() + + ": " + e.getMessage(), e); + } + } + } + + @Override + public RegistryAccess getRegistryAccess() { + return registryAccess; + } +} diff --git a/src/main/java/org/violetmoon/zeta/integration/ZetaJeiPlugin.java b/src/main/java/org/violetmoon/zeta/integration/ZetaJeiPlugin.java index 1eba801..cfde4f4 100644 --- a/src/main/java/org/violetmoon/zeta/integration/ZetaJeiPlugin.java +++ b/src/main/java/org/violetmoon/zeta/integration/ZetaJeiPlugin.java @@ -11,30 +11,21 @@ import net.minecraft.core.RegistryAccess; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.Registries; -import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.MutableComponent; import net.minecraft.resources.ResourceLocation; -import net.minecraft.tags.TagKey; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.PotionItem; import net.minecraft.world.item.TippedArrowItem; import net.minecraft.world.item.alchemy.Potion; import net.minecraft.world.item.alchemy.PotionUtils; -import net.minecraft.world.level.ItemLike; import org.jetbrains.annotations.NotNull; import org.violetmoon.zeta.Zeta; -import org.violetmoon.zeta.config.ConfigFlagManager; -import org.violetmoon.zeta.config.ConfigObjectMapper; import org.violetmoon.zeta.config.ZetaGeneralConfig; import org.violetmoon.zeta.event.load.ZGatherHints; import org.violetmoon.zeta.mod.ZetaMod; -import org.violetmoon.zeta.module.ZetaModule; -import org.violetmoon.zeta.util.Hint; import org.violetmoon.zeta.util.RegistryUtil; import org.violetmoon.zeta.util.zetalist.ZetaList; -import java.lang.reflect.Field; import java.util.*; @JeiPlugin @@ -119,156 +110,7 @@ public void registerRecipes(@NotNull IRecipeRegistration registration) { List blacklist = RegistryUtil.massRegistryGet(ZetaGeneralConfig.suppressedInfo, BuiltInRegistries.ITEM); for (Zeta z : ZetaList.INSTANCE.getZetas()) { - z.loadBus.fire(new GatherHintImpl(registration, z, registryAccess, blacklist), ZGatherHints.class); - } - } - - // feel free to move it as public outer class - private static class GatherHintImpl implements ZGatherHints { - - private final Zeta zeta; - private final RegistryAccess registryAccess; - private final IRecipeRegistration registration; - private final List blacklist; - private final MutableComponent externalPreamble; - - public GatherHintImpl(IRecipeRegistration registration, Zeta z, RegistryAccess registryAccess, List blacklist) { - this.zeta = z; - this.registryAccess = registryAccess; - this.registration = registration; - this.blacklist = blacklist; - - this.externalPreamble = Component.translatable(z.modid + ".jei.hint.preamble"); - externalPreamble.setStyle(externalPreamble.getStyle().withColor(0x0b5d4b)); - } - - @Override - public void accept(ItemLike itemLike, Component extra) { - Item item = itemLike.asItem(); - - if (blacklist.contains(item)) - return; - - MutableComponent compound = Component.literal(""); - if (!BuiltInRegistries.ITEM.getKey(item).getNamespace().equals(zeta.modid)) - compound = compound.append(externalPreamble); - compound = compound.append(extra); - - registration.addItemStackInfo(new ItemStack(item), compound); - } - - private void applyTag(TagKey tkey, String key, Object... extra) { - if (key.isEmpty()) - key = tkey.location().getPath(); - - try { - List tagItems = RegistryUtil.getTagValues(registryAccess, tkey); - applyIterable(tagItems, key, extra); - } catch (IllegalStateException e) { - throw new RuntimeException("TagKey " + tkey + " failed to load.", e); - } - } - - private void applyIterable(Iterable iter, String key, Object... extra) { - if (key.isEmpty()) - throw new RuntimeException("Multi-item @Hints need a defined key."); - - for (Object obj : iter) - applyObject(obj, key, extra); - } - - private void applyObject(Object obj, String key, Object... extra) { - if (obj instanceof ItemLike ilike) - applyItemLike(ilike, key, extra); - else - throw new RuntimeException("Not an ItemLike."); - } - - private void applyItemLike(ItemLike itemLike, String key, Object... extra) { - if (key.isEmpty()) - hintItem(itemLike, extra); - else - hintItem(itemLike, key, extra); - } - - @Override - public void hintItem(ItemLike itemLike, Object... extra) { - Item item = itemLike.asItem(); - ResourceLocation res = BuiltInRegistries.ITEM.getKey(item); - String ns = res.getNamespace(); - String path = res.getPath(); - if (ns.equals(zeta.modid)) - ns = ""; - else ns += "."; - - hintItem(itemLike, ns + path, extra); - } - - @Override - public void hintItem(ItemLike itemLike, String key, Object... extra) { - Item item = itemLike.asItem(); - String hint = zeta.modid + ".jei.hint." + key; - accept(item, Component.translatable(hint, extra)); - } - - @Override - public void gatherHintsFromModule(ZetaModule module, ConfigFlagManager cfm) { - if (!module.isEnabled()) - return; - - List fields = ConfigObjectMapper.walkModuleFields(module.getClass()); - - Map fieldsByName = new HashMap<>(); - for (Field f : fields) - fieldsByName.put(f.getName(), f); - - for (Field f : fields) { - try { - Hint hint = f.getAnnotation(Hint.class); - if (hint == null) - continue; - f.setAccessible(true); - - //Target - Object target = ConfigObjectMapper.getField(module, f); - if (target == null) - continue; - - //Flag - String flag = hint.value(); - if (!flag.isEmpty() && cfm.getFlag(flag) == !hint.negate()) - continue; - - //Translation, & gathering extra content - String key = hint.key(); - List extraList = new ArrayList<>(hint.content().length); - for (String c : hint.content()) { - if (c.isEmpty()) - continue; - - Field extraField = fieldsByName.get(c); - if (extraField == null) - throw new RuntimeException("No field " + c + " referenced in @Hint " + f); - - Object yes = ConfigObjectMapper.getField(module, extraField); - extraList.add(yes); - } - Object[] extra = extraList.toArray(new Object[0]); - - //Application - if (target instanceof TagKey tkey) - applyTag(tkey, key, extra); - else if (target instanceof Iterable iter) - applyIterable(iter, key, extra); - else - applyObject(target, key, extra); - - } catch (Exception e) { - throw new RuntimeException("Problem applying annotation hint " + f.getName() + - " from module " + module.getClass().getName() + - ": " + e.getMessage(), e); - } - } + z.loadBus.fire(new JeiGatherHints(registration, z.modid, registryAccess, blacklist), ZGatherHints.class); } } diff --git a/src/main/java/org/violetmoon/zetaimplforge/event/load/ForgeZGatherHints.java b/src/main/java/org/violetmoon/zetaimplforge/event/load/ForgeZGatherHints.java index e589ece..ae6c311 100644 --- a/src/main/java/org/violetmoon/zetaimplforge/event/load/ForgeZGatherHints.java +++ b/src/main/java/org/violetmoon/zetaimplforge/event/load/ForgeZGatherHints.java @@ -4,7 +4,6 @@ import net.minecraft.network.chat.Component; import net.minecraft.world.level.ItemLike; import net.minecraftforge.eventbus.api.Event; -import org.violetmoon.zeta.Zeta; import org.violetmoon.zeta.config.ConfigFlagManager; import org.violetmoon.zeta.event.load.ZGatherHints; import org.violetmoon.zeta.module.ZetaModule; @@ -35,4 +34,9 @@ public void hintItem(ItemLike itemLike, String key, Object... extra) { public void gatherHintsFromModule(ZetaModule module, ConfigFlagManager cfm) { wrapped.gatherHintsFromModule(module, cfm); } + + @Override + public RegistryAccess getRegistryAccess() { + return wrapped.getRegistryAccess(); + } }