diff --git a/common/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java b/common/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java index 783a40345..6e905698c 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java @@ -26,6 +26,7 @@ import dev.latvian.mods.kubejs.block.MapColorHelper; import dev.latvian.mods.kubejs.block.SoundTypeWrapper; import dev.latvian.mods.kubejs.block.custom.BasicBlockJS; +import dev.latvian.mods.kubejs.block.custom.CarpetBlockBuilder; import dev.latvian.mods.kubejs.block.custom.ButtonBlockBuilder; import dev.latvian.mods.kubejs.block.custom.CropBlockBuilder; import dev.latvian.mods.kubejs.block.custom.FallingBlockBuilder; @@ -203,6 +204,7 @@ public void init() { RegistryInfo.BLOCK.addType("falling", FallingBlockBuilder.class, FallingBlockBuilder::new); RegistryInfo.BLOCK.addType("crop", CropBlockBuilder.class, CropBlockBuilder::new); RegistryInfo.BLOCK.addType("cardinal", HorizontalDirectionalBlockBuilder.class, HorizontalDirectionalBlockBuilder::new); + RegistryInfo.BLOCK.addType("carpet", CarpetBlockBuilder.class, CarpetBlockBuilder::new); RegistryInfo.ITEM.addType("basic", BasicItemJS.Builder.class, BasicItemJS.Builder::new); RegistryInfo.ITEM.addType("sword", SwordItemBuilder.class, SwordItemBuilder::new); diff --git a/common/src/main/java/dev/latvian/mods/kubejs/block/custom/CarpetBlockBuilder.java b/common/src/main/java/dev/latvian/mods/kubejs/block/custom/CarpetBlockBuilder.java new file mode 100644 index 000000000..5743391a6 --- /dev/null +++ b/common/src/main/java/dev/latvian/mods/kubejs/block/custom/CarpetBlockBuilder.java @@ -0,0 +1,40 @@ +package dev.latvian.mods.kubejs.block.custom; + +import dev.latvian.mods.kubejs.client.VariantBlockStateGenerator; +import dev.latvian.mods.kubejs.generator.AssetJsonGenerator; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.BlockTags; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.CarpetBlock; + +public class CarpetBlockBuilder extends ShapedBlockBuilder { + public CarpetBlockBuilder(ResourceLocation i) { + super(i, "_carpet"); + tagBoth(BlockTags.WOOL_CARPETS.location()); + } + + @Override + public Block createObject() { + return new CarpetBlock(createProperties()); + } + + @Override + protected void generateBlockStateJson(VariantBlockStateGenerator bs) { + var mod = newID("block/", "").toString(); + bs.variant("", (v) -> v.model(mod)); + } + + @Override + protected void generateBlockModelJsons(AssetJsonGenerator generator) { + var texture = textures.get("texture").getAsString(); + + generator.blockModel(id, m -> { + m.parent("minecraft:block/carpet"); + m.texture("wool", texture); + }); + } + + public CarpetBlockBuilder texture(String texture) { + return (CarpetBlockBuilder) textureAll(texture); + } +} diff --git a/fabric/src/main/java/dev/latvian/mods/kubejs/core/mixin/fabric/ShapedRecipeMixin.java b/fabric/src/main/java/dev/latvian/mods/kubejs/core/mixin/fabric/ShapedRecipeMixin.java new file mode 100644 index 000000000..0f040c599 --- /dev/null +++ b/fabric/src/main/java/dev/latvian/mods/kubejs/core/mixin/fabric/ShapedRecipeMixin.java @@ -0,0 +1,54 @@ +package dev.latvian.mods.kubejs.core.mixin.fabric; + +import com.google.gson.JsonObject; +import com.google.gson.JsonSyntaxException; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import dev.latvian.mods.rhino.mod.util.NBTUtils; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.TagParser; +import net.minecraft.util.GsonHelper; +import net.minecraft.world.item.ItemStack; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(net.minecraft.world.item.crafting.ShapedRecipe.class) +public class ShapedRecipeMixin { + @Inject( + method = "itemStackFromJson", + at = @At( + value = "RETURN", + remap = false + ), + cancellable = false + ) + private static void itemStackFromJson(JsonObject jsonObject, CallbackInfoReturnable cir) { + if(cir.getReturnValue() == null || cir.getReturnValue() == ItemStack.EMPTY) { + return; + } + + if(jsonObject.has("nbt")) { + var json = jsonObject.get("nbt"); + // Process null + if (json == null || json.isJsonNull()) { + return; + } + + try { + CompoundTag tag = null; + if (json.isJsonObject()) { + // We use a normal .toString() to convert the json to string, and read it as SNBT. + // Using DynamicOps would mess with the type of integers and cause things like damage comparisons to fail... + tag = TagParser.parseTag(json.toString()); + } else { + // Assume it's a string representation of the NBT + tag = TagParser.parseTag(GsonHelper.convertToString(json, "nbt")); + } + cir.getReturnValue().getOrCreateTag().merge(tag); + } catch (CommandSyntaxException commandSyntaxException) { + throw new JsonSyntaxException("Invalid nbt tag: " + commandSyntaxException.getMessage()); + } + } + } +} diff --git a/fabric/src/main/resources/kubejs-fabric.mixins.json b/fabric/src/main/resources/kubejs-fabric.mixins.json index a961b44e0..974621e10 100644 --- a/fabric/src/main/resources/kubejs-fabric.mixins.json +++ b/fabric/src/main/resources/kubejs-fabric.mixins.json @@ -8,6 +8,7 @@ "DifferenceIngredientMixin", "IngredientMatchingMixin", "NbtIngredientMixin", + "ShapedRecipeMixin", "tools.shears.BlockInteractShearsMixin", "tools.shears.EntityInteractShearsMixin", "tools.shears.MatchToolMixin",