From 050ce6359366defe6e7b4da21a3f9f845df567e7 Mon Sep 17 00:00:00 2001 From: Relentless Date: Thu, 10 Aug 2023 22:00:03 +0200 Subject: [PATCH] switch to resource based KubeJS binding --- .../unified/compat/AlmostKube.java | 135 ++++++++---------- Common/src/main/resources/kubejs.bindings.txt | 1 + Fabric/src/main/resources/fabric.mod.json | 3 +- Forge/src/main/resources/META-INF/mods.toml | 7 - gradle.properties | 2 +- 5 files changed, 66 insertions(+), 82 deletions(-) create mode 100644 Common/src/main/resources/kubejs.bindings.txt diff --git a/Common/src/main/java/com/almostreliable/unified/compat/AlmostKube.java b/Common/src/main/java/com/almostreliable/unified/compat/AlmostKube.java index 38f6a56..a86459c 100644 --- a/Common/src/main/java/com/almostreliable/unified/compat/AlmostKube.java +++ b/Common/src/main/java/com/almostreliable/unified/compat/AlmostKube.java @@ -1,12 +1,8 @@ package com.almostreliable.unified.compat; import com.almostreliable.unified.AlmostUnified; -import com.almostreliable.unified.BuildConfig; import com.almostreliable.unified.config.UnifyConfig; import com.almostreliable.unified.utils.UnifyTag; -import dev.latvian.mods.kubejs.KubeJSPlugin; -import dev.latvian.mods.kubejs.item.ItemStackJS; -import dev.latvian.mods.kubejs.script.BindingsEvent; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; @@ -17,82 +13,77 @@ import javax.annotation.Nullable; import java.util.Set; import java.util.stream.Collectors; -public class AlmostKube extends KubeJSPlugin { +@SuppressWarnings("unused") +public final class AlmostKube { - @Override - public void registerBindings(BindingsEvent event) { - if (event.getType().isServer()) { - event.add(BuildConfig.MOD_NAME, UnifyWrapper.class); - } + private AlmostKube() {} + + @Nullable + public static String getPreferredTagForItem(ItemStack stack) { + UnifyTag tag = AlmostUnified + .getRuntime() + .getReplacementMap() + .orElseThrow(AlmostKube::notLoadedException) + .getPreferredTagForItem(getId(stack)); + return tag == null ? null : tag.location().toString(); } - public static class UnifyWrapper { - @Nullable - public static String getPreferredTagForItem(ItemStack stack) { - UnifyTag tag = AlmostUnified - .getRuntime() - .getReplacementMap() - .orElseThrow(UnifyWrapper::notLoadedException) - .getPreferredTagForItem(getId(stack)); - return tag == null ? null : tag.location().toString(); - } + public static ItemStack getReplacementForItem(ItemStack stack) { + ResourceLocation replacement = AlmostUnified + .getRuntime() + .getReplacementMap() + .orElseThrow(AlmostKube::notLoadedException) + .getReplacementForItem(getId(stack)); + return Registry.ITEM.get(replacement).getDefaultInstance(); + } - public static ItemStack getReplacementForItem(ItemStack stack) { - ResourceLocation replacement = AlmostUnified - .getRuntime() - .getReplacementMap() - .orElseThrow(UnifyWrapper::notLoadedException) - .getReplacementForItem(getId(stack)); - return ItemStackJS.of(replacement); - } + public static ItemStack getPreferredItemForTag(ResourceLocation tag) { + UnifyTag asUnifyTag = UnifyTag.item(tag); + ResourceLocation item = AlmostUnified + .getRuntime() + .getReplacementMap() + .orElseThrow(AlmostKube::notLoadedException) + .getPreferredItemForTag(asUnifyTag, $ -> true); + return Registry.ITEM.get(item).getDefaultInstance(); + } - public static ItemStack getPreferredItemForTag(ResourceLocation tag) { - UnifyTag asUnifyTag = UnifyTag.item(tag); - ResourceLocation item = AlmostUnified - .getRuntime() - .getReplacementMap() - .orElseThrow(UnifyWrapper::notLoadedException) - .getPreferredItemForTag(asUnifyTag, $ -> true); - return ItemStackJS.of(item); - } + public static Set getTags() { + return AlmostUnified + .getRuntime() + .getFilteredTagMap() + .orElseThrow(AlmostKube::notLoadedException) + .getTags() + .stream() + .map(tag -> tag.location().toString()) + .collect(Collectors.toSet()); + } - public static Set getTags() { - return AlmostUnified - .getRuntime() - .getFilteredTagMap() - .orElseThrow(UnifyWrapper::notLoadedException) - .getTags() - .stream() - .map(tag -> tag.location().toString()) - .collect(Collectors.toSet()); - } + public static Set getItemIds(ResourceLocation tag) { + UnifyTag asUnifyTag = UnifyTag.item(tag); + return AlmostUnified + .getRuntime() + .getFilteredTagMap() + .orElseThrow(AlmostKube::notLoadedException) + .getEntriesByTag(asUnifyTag) + .stream() + .map(ResourceLocation::toString) + .collect(Collectors.toSet()); + } - public static Set getItemIds(ResourceLocation tag) { - UnifyTag asUnifyTag = UnifyTag.item(tag); - return AlmostUnified - .getRuntime() - .getFilteredTagMap() - .orElseThrow(UnifyWrapper::notLoadedException) - .getItemsByTag(asUnifyTag) - .stream() - .map(ResourceLocation::toString) - .collect(Collectors.toSet()); - } + public static UnifyConfig getUnifyConfig() { + return AlmostUnified.getRuntime().getUnifyConfig().orElseThrow(AlmostKube::notLoadedException); + } - public static UnifyConfig getUnifyConfig() { - return AlmostUnified.getRuntime().getUnifyConfig().orElseThrow(UnifyWrapper::notLoadedException); - } + private static ResourceLocation getId(ItemStack stack) { + return Registry.ITEM + .getResourceKey(stack.getItem()) + .map(ResourceKey::location) + .orElseThrow(() -> new IllegalArgumentException("Item not found in registry")); + } - private static ResourceLocation getId(ItemStack stack) { - return Registry.ITEM - .getResourceKey(stack.getItem()) - .map(ResourceKey::location) - .orElseThrow(() -> new IllegalArgumentException("Item not found in registry")); - } - - private static IllegalStateException notLoadedException() { - return new IllegalStateException( - "AlmostUnified runtime is not available in KubeJS! Possible reasons: calling runtime too early, not in a server environment"); - } + private static IllegalStateException notLoadedException() { + return new IllegalStateException( + "AlmostUnifiedRuntime is unavailable in KubeJS! Possible reasons: calling runtime too early, not in a server environment" + ); } } diff --git a/Common/src/main/resources/kubejs.bindings.txt b/Common/src/main/resources/kubejs.bindings.txt new file mode 100644 index 0000000..fe3a346 --- /dev/null +++ b/Common/src/main/resources/kubejs.bindings.txt @@ -0,0 +1 @@ +* AlmostUnified com.almostreliable.unified.compat.AlmostKube diff --git a/Fabric/src/main/resources/fabric.mod.json b/Fabric/src/main/resources/fabric.mod.json index 30c90ef..a7cde67 100644 --- a/Fabric/src/main/resources/fabric.mod.json +++ b/Fabric/src/main/resources/fabric.mod.json @@ -35,7 +35,6 @@ }, "suggests": { "jei": ">=${jeiVersion}", - "roughlyenoughitems": ">=${reiVersion}", - "kubejs": ">=${kubejsVersion}" + "roughlyenoughitems": ">=${reiVersion}" } } diff --git a/Forge/src/main/resources/META-INF/mods.toml b/Forge/src/main/resources/META-INF/mods.toml index b2709af..3f37e90 100644 --- a/Forge/src/main/resources/META-INF/mods.toml +++ b/Forge/src/main/resources/META-INF/mods.toml @@ -38,10 +38,3 @@ mandatory = false versionRange = "[${reiVersion},)" ordering = "BEFORE" side = "BOTH" - -[[dependencies."${modId}"]] -modId = "kubejs" -mandatory = false -versionRange = "[${kubejsVersion},)" -ordering = "BEFORE" -side = "BOTH" diff --git a/gradle.properties b/gradle.properties index 12922ad..0cd194d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -30,7 +30,7 @@ fabricApiVersion = 0.76.0 fabricRecipeViewer = rei # Forge Dependencies -forgeVersion = 43.2.10 +forgeVersion = 43.2.0 forgeRecipeViewer = jei # Github