switch to resource based KubeJS binding

This commit is contained in:
Relentless 2023-08-10 22:00:03 +02:00
parent ae6c979827
commit 050ce63593
No known key found for this signature in database
GPG key ID: 50C5FD225130D790
5 changed files with 66 additions and 82 deletions

View file

@ -1,12 +1,8 @@
package com.almostreliable.unified.compat; package com.almostreliable.unified.compat;
import com.almostreliable.unified.AlmostUnified; import com.almostreliable.unified.AlmostUnified;
import com.almostreliable.unified.BuildConfig;
import com.almostreliable.unified.config.UnifyConfig; import com.almostreliable.unified.config.UnifyConfig;
import com.almostreliable.unified.utils.UnifyTag; 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.core.Registry;
import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
@ -17,82 +13,77 @@ import javax.annotation.Nullable;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class AlmostKube extends KubeJSPlugin { @SuppressWarnings("unused")
public final class AlmostKube {
@Override private AlmostKube() {}
public void registerBindings(BindingsEvent event) {
if (event.getType().isServer()) { @Nullable
event.add(BuildConfig.MOD_NAME, UnifyWrapper.class); public static String getPreferredTagForItem(ItemStack stack) {
} UnifyTag<Item> tag = AlmostUnified
.getRuntime()
.getReplacementMap()
.orElseThrow(AlmostKube::notLoadedException)
.getPreferredTagForItem(getId(stack));
return tag == null ? null : tag.location().toString();
} }
public static class UnifyWrapper { public static ItemStack getReplacementForItem(ItemStack stack) {
@Nullable ResourceLocation replacement = AlmostUnified
public static String getPreferredTagForItem(ItemStack stack) { .getRuntime()
UnifyTag<Item> tag = AlmostUnified .getReplacementMap()
.getRuntime() .orElseThrow(AlmostKube::notLoadedException)
.getReplacementMap() .getReplacementForItem(getId(stack));
.orElseThrow(UnifyWrapper::notLoadedException) return Registry.ITEM.get(replacement).getDefaultInstance();
.getPreferredTagForItem(getId(stack)); }
return tag == null ? null : tag.location().toString();
}
public static ItemStack getReplacementForItem(ItemStack stack) { public static ItemStack getPreferredItemForTag(ResourceLocation tag) {
ResourceLocation replacement = AlmostUnified UnifyTag<Item> asUnifyTag = UnifyTag.item(tag);
.getRuntime() ResourceLocation item = AlmostUnified
.getReplacementMap() .getRuntime()
.orElseThrow(UnifyWrapper::notLoadedException) .getReplacementMap()
.getReplacementForItem(getId(stack)); .orElseThrow(AlmostKube::notLoadedException)
return ItemStackJS.of(replacement); .getPreferredItemForTag(asUnifyTag, $ -> true);
} return Registry.ITEM.get(item).getDefaultInstance();
}
public static ItemStack getPreferredItemForTag(ResourceLocation tag) { public static Set<String> getTags() {
UnifyTag<Item> asUnifyTag = UnifyTag.item(tag); return AlmostUnified
ResourceLocation item = AlmostUnified .getRuntime()
.getRuntime() .getFilteredTagMap()
.getReplacementMap() .orElseThrow(AlmostKube::notLoadedException)
.orElseThrow(UnifyWrapper::notLoadedException) .getTags()
.getPreferredItemForTag(asUnifyTag, $ -> true); .stream()
return ItemStackJS.of(item); .map(tag -> tag.location().toString())
} .collect(Collectors.toSet());
}
public static Set<String> getTags() { public static Set<String> getItemIds(ResourceLocation tag) {
return AlmostUnified UnifyTag<Item> asUnifyTag = UnifyTag.item(tag);
.getRuntime() return AlmostUnified
.getFilteredTagMap() .getRuntime()
.orElseThrow(UnifyWrapper::notLoadedException) .getFilteredTagMap()
.getTags() .orElseThrow(AlmostKube::notLoadedException)
.stream() .getEntriesByTag(asUnifyTag)
.map(tag -> tag.location().toString()) .stream()
.collect(Collectors.toSet()); .map(ResourceLocation::toString)
} .collect(Collectors.toSet());
}
public static Set<String> getItemIds(ResourceLocation tag) { public static UnifyConfig getUnifyConfig() {
UnifyTag<Item> asUnifyTag = UnifyTag.item(tag); return AlmostUnified.getRuntime().getUnifyConfig().orElseThrow(AlmostKube::notLoadedException);
return AlmostUnified }
.getRuntime()
.getFilteredTagMap()
.orElseThrow(UnifyWrapper::notLoadedException)
.getItemsByTag(asUnifyTag)
.stream()
.map(ResourceLocation::toString)
.collect(Collectors.toSet());
}
public static UnifyConfig getUnifyConfig() { private static ResourceLocation getId(ItemStack stack) {
return AlmostUnified.getRuntime().getUnifyConfig().orElseThrow(UnifyWrapper::notLoadedException); return Registry.ITEM
} .getResourceKey(stack.getItem())
.map(ResourceKey::location)
.orElseThrow(() -> new IllegalArgumentException("Item not found in registry"));
}
private static ResourceLocation getId(ItemStack stack) { private static IllegalStateException notLoadedException() {
return Registry.ITEM return new IllegalStateException(
.getResourceKey(stack.getItem()) "AlmostUnifiedRuntime is unavailable in KubeJS! Possible reasons: calling runtime too early, not in a server environment"
.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");
}
} }
} }

View file

@ -0,0 +1 @@
* AlmostUnified com.almostreliable.unified.compat.AlmostKube

View file

@ -35,7 +35,6 @@
}, },
"suggests": { "suggests": {
"jei": ">=${jeiVersion}", "jei": ">=${jeiVersion}",
"roughlyenoughitems": ">=${reiVersion}", "roughlyenoughitems": ">=${reiVersion}"
"kubejs": ">=${kubejsVersion}"
} }
} }

View file

@ -38,10 +38,3 @@ mandatory = false
versionRange = "[${reiVersion},)" versionRange = "[${reiVersion},)"
ordering = "BEFORE" ordering = "BEFORE"
side = "BOTH" side = "BOTH"
[[dependencies."${modId}"]]
modId = "kubejs"
mandatory = false
versionRange = "[${kubejsVersion},)"
ordering = "BEFORE"
side = "BOTH"

View file

@ -30,7 +30,7 @@ fabricApiVersion = 0.76.0
fabricRecipeViewer = rei fabricRecipeViewer = rei
# Forge Dependencies # Forge Dependencies
forgeVersion = 43.2.10 forgeVersion = 43.2.0
forgeRecipeViewer = jei forgeRecipeViewer = jei
# Github # Github