-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f62e5d
commit 4a5391b
Showing
5 changed files
with
118 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/dev/latvian/mods/kubejs/recipe/component/ResourceKeyComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package dev.latvian.mods.kubejs.recipe.component; | ||
|
||
import com.mojang.serialization.Codec; | ||
import dev.latvian.mods.kubejs.recipe.KubeRecipe; | ||
import dev.latvian.mods.kubejs.recipe.schema.RecipeComponentFactory; | ||
import dev.latvian.mods.kubejs.registry.RegistryType; | ||
import dev.latvian.mods.kubejs.util.ID; | ||
import dev.latvian.mods.rhino.Context; | ||
import dev.latvian.mods.rhino.type.TypeInfo; | ||
import net.minecraft.core.Registry; | ||
import net.minecraft.core.registries.Registries; | ||
import net.minecraft.resources.ResourceKey; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.storage.loot.LootTable; | ||
|
||
public record ResourceKeyComponent<T>(ResourceKey<? extends Registry<T>> registryKey) implements RecipeComponent<ResourceKey<T>> { | ||
public static final RecipeComponent<ResourceKey<Level>> DIMENSION = new ResourceKeyComponent<>(Registries.DIMENSION); | ||
public static final RecipeComponent<ResourceKey<LootTable>> LOOT_TABLE = new ResourceKeyComponent<>(Registries.LOOT_TABLE); | ||
|
||
@SuppressWarnings({"rawtypes"}) | ||
public static final RecipeComponentFactory FACTORY = (registries, storage, reader) -> { | ||
reader.skipWhitespace(); | ||
reader.expect('<'); | ||
reader.skipWhitespace(); | ||
var regId = ResourceLocation.read(reader); | ||
reader.expect('>'); | ||
var key = ResourceKey.createRegistryKey(regId); | ||
return new ResourceKeyComponent(key); | ||
}; | ||
|
||
@Override | ||
public Codec<ResourceKey<T>> codec() { | ||
return ResourceKey.codec(registryKey); | ||
} | ||
|
||
@Override | ||
public TypeInfo typeInfo() { | ||
var reg = RegistryType.ofKey(registryKey); | ||
return reg == null ? TypeInfo.of(ResourceKey.class) : TypeInfo.of(ResourceKey.class).withParams(reg.type()); | ||
} | ||
|
||
@Override | ||
public ResourceKey<T> wrap(Context cx, KubeRecipe recipe, Object from) { | ||
return ResourceKey.create(registryKey, ID.mc(from)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
var key = (ResourceKey) registryKey; | ||
|
||
if (key == Registries.DIMENSION) { | ||
return "dimension_resource_key"; | ||
} else if (key == Registries.LOOT_TABLE) { | ||
return "loot_table_resource_key"; | ||
} else { | ||
return "resource_key<" + registryKey.location() + ">"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters