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;
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<Item> 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<Item> 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<Item> 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<Item> asUnifyTag = UnifyTag.item(tag);
ResourceLocation item = AlmostUnified
.getRuntime()
.getReplacementMap()
.orElseThrow(UnifyWrapper::notLoadedException)
.getPreferredItemForTag(asUnifyTag, $ -> true);
return ItemStackJS.of(item);
}
public static Set<String> getTags() {
return AlmostUnified
.getRuntime()
.getFilteredTagMap()
.orElseThrow(AlmostKube::notLoadedException)
.getTags()
.stream()
.map(tag -> tag.location().toString())
.collect(Collectors.toSet());
}
public static Set<String> getTags() {
return AlmostUnified
.getRuntime()
.getFilteredTagMap()
.orElseThrow(UnifyWrapper::notLoadedException)
.getTags()
.stream()
.map(tag -> tag.location().toString())
.collect(Collectors.toSet());
}
public static Set<String> getItemIds(ResourceLocation tag) {
UnifyTag<Item> asUnifyTag = UnifyTag.item(tag);
return AlmostUnified
.getRuntime()
.getFilteredTagMap()
.orElseThrow(AlmostKube::notLoadedException)
.getEntriesByTag(asUnifyTag)
.stream()
.map(ResourceLocation::toString)
.collect(Collectors.toSet());
}
public static Set<String> getItemIds(ResourceLocation tag) {
UnifyTag<Item> 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"
);
}
}

View file

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

View file

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

View file

@ -38,10 +38,3 @@ mandatory = false
versionRange = "[${reiVersion},)"
ordering = "BEFORE"
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
# Forge Dependencies
forgeVersion = 43.2.10
forgeVersion = 43.2.0
forgeRecipeViewer = jei
# Github