From 46122977c31bfc7f6d7d2833173a4f3bf11b32ac Mon Sep 17 00:00:00 2001 From: LatvianModder Date: Tue, 3 Sep 2024 02:20:01 +0300 Subject: [PATCH] Block entity attachment rewrite, more model generator tweaks. BE inventories fixed, made attachments directional, added custom Capability attachments --- .../mods/kubejs/BuiltinKubeJSPlugin.java | 8 +- .../mods/kubejs/KubeJSModEventHandler.java | 38 +++-- .../kubejs/bindings/DirectionWrapper.java | 2 + .../mods/kubejs/block/BlockBuilder.java | 46 +++--- .../mods/kubejs/block/DetectorBlock.java | 16 +- .../mods/kubejs/block/SeedItemBuilder.java | 5 - .../kubejs/block/custom/BasicBlockJS.java | 6 +- .../block/custom/ButtonBlockBuilder.java | 7 +- .../block/custom/CarpetBlockBuilder.java | 4 +- .../kubejs/block/custom/CropBlockBuilder.java | 4 +- .../kubejs/block/custom/DoorBlockBuilder.java | 6 +- .../block/custom/FenceBlockBuilder.java | 13 +- .../block/custom/FenceGateBlockBuilder.java | 6 +- .../HorizontalDirectionalBlockBuilder.java | 6 +- .../custom/MultipartShapedBlockBuilder.java | 37 ----- .../custom/PressurePlateBlockBuilder.java | 6 +- .../kubejs/block/custom/SlabBlockBuilder.java | 6 +- .../block/custom/StairBlockBuilder.java | 4 +- .../block/custom/TrapdoorBlockBuilder.java | 6 +- .../kubejs/block/custom/WallBlockBuilder.java | 13 +- .../block/entity/BlockEntityAttachment.java | 29 ++-- .../entity/BlockEntityAttachmentFactory.java | 17 ++ .../entity/BlockEntityAttachmentHandler.java | 44 ++++++ .../entity/BlockEntityAttachmentHolder.java | 6 +- .../entity/BlockEntityAttachmentInfo.java | 12 ++ .../entity/BlockEntityAttachmentRegistry.java | 12 ++ .../entity/BlockEntityAttachmentType.java | 19 +-- .../kubejs/block/entity/BlockEntityInfo.java | 50 +++--- .../entity/CustomCapabilityAttachment.java | 53 +++++++ .../block/entity/InventoryAttachment.java | 148 +++++++++--------- .../kubejs/block/entity/KubeBlockEntity.java | 127 ++++++++++----- .../latvian/mods/kubejs/core/LevelKJS.java | 12 ++ .../kubejs/core/mixin/ClientLevelMixin.java | 8 + .../kubejs/core/mixin/IItemHandlerMixin.java | 33 ++-- .../kubejs/core/mixin/ServerLevelMixin.java | 7 + .../mods/kubejs/fluid/FluidBlockBuilder.java | 5 +- .../mods/kubejs/fluid/FluidBuilder.java | 6 - .../latvian/mods/kubejs/item/ItemBuilder.java | 14 -- .../mods/kubejs/plugin/KubeJSPlugin.java | 5 +- .../registry/RegistryObjectStorage.java | 2 + 40 files changed, 502 insertions(+), 346 deletions(-) delete mode 100644 src/main/java/dev/latvian/mods/kubejs/block/custom/MultipartShapedBlockBuilder.java create mode 100644 src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentFactory.java create mode 100644 src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentHandler.java create mode 100644 src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentInfo.java create mode 100644 src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentRegistry.java create mode 100644 src/main/java/dev/latvian/mods/kubejs/block/entity/CustomCapabilityAttachment.java diff --git a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java index f077714a5..ea9cad0e7 100644 --- a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java +++ b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java @@ -47,7 +47,8 @@ import dev.latvian.mods.kubejs.block.custom.StairBlockBuilder; import dev.latvian.mods.kubejs.block.custom.TrapdoorBlockBuilder; import dev.latvian.mods.kubejs.block.custom.WallBlockBuilder; -import dev.latvian.mods.kubejs.block.entity.BlockEntityAttachmentType; +import dev.latvian.mods.kubejs.block.entity.BlockEntityAttachmentRegistry; +import dev.latvian.mods.kubejs.block.entity.CustomCapabilityAttachment; import dev.latvian.mods.kubejs.block.entity.InventoryAttachment; import dev.latvian.mods.kubejs.block.state.BlockStatePredicate; import dev.latvian.mods.kubejs.color.KubeColor; @@ -703,8 +704,9 @@ public void registerRecipeComponents(RecipeComponentFactoryRegistry registry) { } @Override - public void registerBlockEntityAttachments(List types) { - types.add(InventoryAttachment.TYPE); + public void registerBlockEntityAttachments(BlockEntityAttachmentRegistry registry) { + registry.register(CustomCapabilityAttachment.TYPE); + registry.register(InventoryAttachment.TYPE); } @Override diff --git a/src/main/java/dev/latvian/mods/kubejs/KubeJSModEventHandler.java b/src/main/java/dev/latvian/mods/kubejs/KubeJSModEventHandler.java index 154796a98..49bb65aa0 100644 --- a/src/main/java/dev/latvian/mods/kubejs/KubeJSModEventHandler.java +++ b/src/main/java/dev/latvian/mods/kubejs/KubeJSModEventHandler.java @@ -1,25 +1,35 @@ package dev.latvian.mods.kubejs; import dev.latvian.mods.kubejs.bindings.event.StartupEvents; +import dev.latvian.mods.kubejs.block.entity.BlockEntityAttachmentInfo; +import dev.latvian.mods.kubejs.block.entity.BlockEntityBuilder; +import dev.latvian.mods.kubejs.block.entity.KubeBlockEntity; import dev.latvian.mods.kubejs.event.KubeStartupEvent; import dev.latvian.mods.kubejs.item.creativetab.CreativeTabCallbackForge; import dev.latvian.mods.kubejs.item.creativetab.CreativeTabKubeEvent; import dev.latvian.mods.kubejs.plugin.KubeJSPlugin; import dev.latvian.mods.kubejs.plugin.KubeJSPlugins; +import dev.latvian.mods.kubejs.registry.RegistryObjectStorage; import dev.latvian.mods.kubejs.script.ConsoleJS; import dev.latvian.mods.kubejs.script.ConsoleLine; import dev.latvian.mods.kubejs.script.ScriptType; import dev.latvian.mods.kubejs.script.ScriptsLoadedEvent; import dev.latvian.mods.kubejs.util.UtilsJS; import net.minecraft.Util; +import net.minecraft.core.Direction; +import net.minecraft.world.level.block.entity.BlockEntityType; import net.neoforged.api.distmarker.Dist; import net.neoforged.bus.api.EventPriority; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.fml.event.lifecycle.FMLLoadCompleteEvent; import net.neoforged.fml.loading.FMLLoader; +import net.neoforged.neoforge.capabilities.BlockCapability; +import net.neoforged.neoforge.capabilities.ICapabilityProvider; +import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent; +import org.jetbrains.annotations.Nullable; import java.net.URI; import java.net.http.HttpClient; @@ -98,20 +108,26 @@ private static void loadComplete0() { }); } - /* - @SubscribeEvent(priority = EventPriority.HIGH) - public static void addPacksFirst(AddPackFindersEvent event) { - if (event.getPackType() == PackType.SERVER_DATA) { - // ServerScriptManager.addPacksFirst(event); + private record KubeEntityCapabilityProvider(BlockCapability capability, BlockEntityAttachmentInfo attachment) implements ICapabilityProvider { + @Override + @Nullable + public CAP getCapability(KubeBlockEntity entity, SRC from) { + if (attachment.directions().isEmpty() || (from instanceof Direction d && attachment.directions().contains(d))) { + return entity.attachmentArray[attachment.index()].attachment().getCapability(capability); + } + + return null; } } - @SubscribeEvent(priority = EventPriority.LOW) - public static void addPacksLast(AddPackFindersEvent event) { - if (event.getPackType() == PackType.SERVER_DATA) { - // ServerScriptManager.addPacksLast(event); + @SubscribeEvent + public static void registerCapabilities(RegisterCapabilitiesEvent event) { + for (var info : RegistryObjectStorage.BLOCK_ENTITY.objects.values().stream().map(b -> ((BlockEntityBuilder) b).info).toList()) { + for (var attachment : info.attachments.values()) { + for (var capability : attachment.factory().getCapabilities()) { + event.registerBlockEntity(capability, (BlockEntityType) info.entityType, new KubeEntityCapabilityProvider(capability, attachment)); + } + } } } - - */ } diff --git a/src/main/java/dev/latvian/mods/kubejs/bindings/DirectionWrapper.java b/src/main/java/dev/latvian/mods/kubejs/bindings/DirectionWrapper.java index 759f5fd19..c662bec22 100644 --- a/src/main/java/dev/latvian/mods/kubejs/bindings/DirectionWrapper.java +++ b/src/main/java/dev/latvian/mods/kubejs/bindings/DirectionWrapper.java @@ -3,6 +3,7 @@ import net.minecraft.core.Direction; import java.util.Arrays; +import java.util.EnumSet; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; @@ -22,4 +23,5 @@ public interface DirectionWrapper { Direction EAST = Direction.EAST; Direction[] VALUES = Direction.values(); Map ALL = Map.copyOf(Arrays.stream(VALUES).collect(Collectors.toMap(Direction::getSerializedName, Function.identity()))); + EnumSet EMPTY_SET = EnumSet.noneOf(Direction.class); } \ No newline at end of file diff --git a/src/main/java/dev/latvian/mods/kubejs/block/BlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/BlockBuilder.java index c3fe63d6b..da3b31dc5 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/BlockBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/BlockBuilder.java @@ -1,6 +1,5 @@ package dev.latvian.mods.kubejs.block; -import com.google.gson.JsonObject; import com.mojang.serialization.JsonOps; import dev.latvian.mods.kubejs.bindings.AABBWrapper; import dev.latvian.mods.kubejs.bindings.DirectionWrapper; @@ -18,6 +17,7 @@ import dev.latvian.mods.kubejs.block.entity.BlockEntityBuilder; import dev.latvian.mods.kubejs.block.entity.BlockEntityInfo; import dev.latvian.mods.kubejs.client.ModelGenerator; +import dev.latvian.mods.kubejs.client.MultipartBlockStateGenerator; import dev.latvian.mods.kubejs.client.VariantBlockStateGenerator; import dev.latvian.mods.kubejs.generator.KubeAssetGenerator; import dev.latvian.mods.kubejs.generator.KubeDataGenerator; @@ -95,8 +95,6 @@ public abstract class BlockBuilder extends BuilderBase { public transient float jumpFactor = Float.NaN; public Consumer randomTickCallback; public BlockDropSupplier drops; - public JsonObject blockstateJson; - public JsonObject modelJson; public transient boolean noValidSpawns; public transient boolean suffocating; public transient boolean viewBlocking; @@ -141,8 +139,6 @@ public BlockBuilder(ResourceLocation i) { notSolid = false; randomTickCallback = null; drops = null; - blockstateJson = null; - modelJson = null; noValidSpawns = false; suffocating = true; viewBlocking = true; @@ -233,38 +229,25 @@ public LootTable generateLootTable() { @Override public void generateAssets(KubeAssetGenerator generator) { - if (blockstateJson != null) { - generator.json(id.withPath(ID.BLOCKSTATE), blockstateJson); + if (useMultipartBlockState()) { + generator.multipartState(id, this::generateMultipartBlockState); } else { - generator.blockState(id, this::generateBlockStateJson); + generator.blockState(id, this::generateBlockState); } - if (modelJson != null) { - generator.json(id.withPath(ID.BLOCK_MODEL), modelJson); - } else { - // This is different because there can be multiple models, so we should let the block handle those - generateBlockModelJsons(generator); - } + generateBlockModel(generator); if (itemBuilder != null) { - if (itemBuilder.modelJson != null) { - generator.json(id.withPath(ID.ITEM_MODEL), itemBuilder.modelJson); - } else { - generator.itemModel(itemBuilder.id, this::generateItemModelJson); - } + generator.itemModel(itemBuilder.id, this::generateItemModel); } } - protected void generateItemModelJson(ModelGenerator m) { - if (model != null) { - m.parent(model); - } else { - m.parent(id.withPath(ID.BLOCK)); - } + protected void generateItemModel(ModelGenerator m) { + m.parent(model != null ? model : id.withPath(ID.BLOCK)); } - protected void generateBlockModelJsons(KubeAssetGenerator generator) { + protected void generateBlockModel(KubeAssetGenerator generator) { generator.blockModel(id, mg -> { var particle = textures.get("particle"); @@ -303,8 +286,15 @@ protected void generateBlockModelJsons(KubeAssetGenerator generator) { }); } - protected void generateBlockStateJson(VariantBlockStateGenerator bs) { - bs.simpleVariant("", model == null ? id.withPath(ID.BLOCK) : model); + protected boolean useMultipartBlockState() { + return false; + } + + protected void generateBlockState(VariantBlockStateGenerator bs) { + bs.simpleVariant("", model != null ? model : id.withPath(ID.BLOCK)); + } + + protected void generateMultipartBlockState(MultipartBlockStateGenerator bs) { } protected boolean areAllTexturesEqual(String t) { diff --git a/src/main/java/dev/latvian/mods/kubejs/block/DetectorBlock.java b/src/main/java/dev/latvian/mods/kubejs/block/DetectorBlock.java index 8e4a850c6..84de93ab2 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/DetectorBlock.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/DetectorBlock.java @@ -2,7 +2,8 @@ import dev.latvian.mods.kubejs.KubeJS; import dev.latvian.mods.kubejs.bindings.event.BlockEvents; -import dev.latvian.mods.kubejs.generator.KubeAssetGenerator; +import dev.latvian.mods.kubejs.client.ModelGenerator; +import dev.latvian.mods.kubejs.client.VariantBlockStateGenerator; import dev.latvian.mods.rhino.util.ReturnsSelf; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; @@ -49,13 +50,14 @@ public Block createObject() { } @Override - public void generateAssets(KubeAssetGenerator generator) { - generator.blockState(id, bs -> { - bs.simpleVariant("powered=false", OFF_MODEL); - bs.simpleVariant("powered=true", ON_MODEL); - }); + protected void generateBlockState(VariantBlockStateGenerator bs) { + bs.simpleVariant("powered=false", OFF_MODEL); + bs.simpleVariant("powered=true", ON_MODEL); + } - generator.itemModel(id, m -> m.parent(KubeJS.MOD_ID + ":block/detector")); + @Override + protected void generateItemModel(ModelGenerator m) { + m.parent(OFF_MODEL); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/SeedItemBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/SeedItemBuilder.java index e8be848dc..adeec6c22 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/SeedItemBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/SeedItemBuilder.java @@ -55,11 +55,6 @@ public Item createObject() { @Override public void generateAssets(KubeAssetGenerator generator) { - if (modelJson != null) { - generator.json(id.withPath(ID.ITEM_MODEL), modelJson); - return; - } - generator.itemModel(id, m -> { m.parent(parentModel != null ? parentModel : KubeAssetGenerator.GENERATED_ITEM_MODEL); diff --git a/src/main/java/dev/latvian/mods/kubejs/block/custom/BasicBlockJS.java b/src/main/java/dev/latvian/mods/kubejs/block/custom/BasicBlockJS.java index 2741b4f80..ab5b7d6c4 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/custom/BasicBlockJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/custom/BasicBlockJS.java @@ -339,9 +339,9 @@ public ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Level public void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean bl) { if (!state.is(newState.getBlock())) { if (level.getBlockEntity(pos) instanceof KubeBlockEntity entity) { - if (level instanceof ServerLevel) { - for (var attachment : entity.attachments) { - attachment.onRemove(newState); + if (level instanceof ServerLevel s) { + for (var entry : entity.attachmentArray) { + entry.attachment().onRemove(s, entity, newState); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/custom/ButtonBlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/custom/ButtonBlockBuilder.java index d3255f1db..c26c54237 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/custom/ButtonBlockBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/custom/ButtonBlockBuilder.java @@ -23,7 +23,6 @@ public ButtonBlockBuilder(ResourceLocation i) { super(i, "_button"); noCollision(); tagBoth(BUTTON_TAGS); - // tagBoth(BlockTags.WOODEN_BUTTONS.location()); behaviour = BlockSetType.OAK; ticksToStayPressed = 30; } @@ -44,7 +43,7 @@ public Block createObject() { } @Override - protected void generateBlockStateJson(VariantBlockStateGenerator bs) { + protected void generateBlockState(VariantBlockStateGenerator bs) { var mod0 = newID("block/", ""); var mod1 = newID("block/", "_pressed"); @@ -75,7 +74,7 @@ protected void generateBlockStateJson(VariantBlockStateGenerator bs) { } @Override - protected void generateBlockModelJsons(KubeAssetGenerator generator) { + protected void generateBlockModel(KubeAssetGenerator generator) { var texture = textures.get("texture"); generator.blockModel(id, m -> { @@ -90,7 +89,7 @@ protected void generateBlockModelJsons(KubeAssetGenerator generator) { } @Override - protected void generateItemModelJson(ModelGenerator m) { + protected void generateItemModel(ModelGenerator m) { m.parent("minecraft:block/button_inventory"); m.texture("texture", textures.get("texture")); } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/custom/CarpetBlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/custom/CarpetBlockBuilder.java index a64a6f192..ed22e681c 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/custom/CarpetBlockBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/custom/CarpetBlockBuilder.java @@ -26,13 +26,13 @@ public Block createObject() { } @Override - protected void generateBlockStateJson(VariantBlockStateGenerator bs) { + protected void generateBlockState(VariantBlockStateGenerator bs) { var mod = id.withPath(ID.BLOCK); bs.variant("", (v) -> v.model(mod)); } @Override - protected void generateBlockModelJsons(KubeAssetGenerator generator) { + protected void generateBlockModel(KubeAssetGenerator generator) { var texture = textures.get("texture"); generator.blockModel(id, m -> { diff --git a/src/main/java/dev/latvian/mods/kubejs/block/custom/CropBlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/custom/CropBlockBuilder.java index 1807556c1..0da41b781 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/custom/CropBlockBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/custom/CropBlockBuilder.java @@ -253,14 +253,14 @@ public LootTable generateLootTable() { @Override - protected void generateBlockStateJson(VariantBlockStateGenerator bs) { + protected void generateBlockState(VariantBlockStateGenerator bs) { for (int i = 0; i <= age; i++) { bs.simpleVariant("age=" + i, model == null ? id.withPath("block/" + id.getPath() + "/" + i) : model); } } @Override - protected void generateBlockModelJsons(KubeAssetGenerator generator) { + protected void generateBlockModel(KubeAssetGenerator generator) { for (int i = 0; i <= age; i++) { final int fi = i; generator.blockModel(newID("", "/" + i), m -> { diff --git a/src/main/java/dev/latvian/mods/kubejs/block/custom/DoorBlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/custom/DoorBlockBuilder.java index abcf64ab8..868fcf604 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/custom/DoorBlockBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/custom/DoorBlockBuilder.java @@ -61,7 +61,7 @@ public Block createObject() { } @Override - protected void generateBlockStateJson(VariantBlockStateGenerator bs) { + protected void generateBlockState(VariantBlockStateGenerator bs) { var modelMap = Map.of( DoubleBlockHalf.UPPER, Map.of( DoorHingeSide.RIGHT, Map.of( @@ -147,7 +147,7 @@ Boolean.TRUE, newID("block/", "_bottom_left_open") } @Override - protected void generateBlockModelJsons(KubeAssetGenerator generator) { + protected void generateBlockModel(KubeAssetGenerator generator) { var topTexture = textures.get("top"); var bottomTexture = textures.get("bottom"); @@ -205,7 +205,7 @@ public LootTable generateLootTable() { } @Override - protected void generateItemModelJson(ModelGenerator m) { + protected void generateItemModel(ModelGenerator m) { m.parent(KubeAssetGenerator.GENERATED_ITEM_MODEL); m.texture("layer0", id.withPath(ID.ITEM).toString()); } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/custom/FenceBlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/custom/FenceBlockBuilder.java index 6d1e7e2dc..53753f321 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/custom/FenceBlockBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/custom/FenceBlockBuilder.java @@ -9,7 +9,7 @@ import net.minecraft.world.level.block.FenceBlock; import net.neoforged.neoforge.common.Tags; -public class FenceBlockBuilder extends MultipartShapedBlockBuilder { +public class FenceBlockBuilder extends ShapedBlockBuilder { public static final ResourceLocation[] FENCE_TAGS = { BlockTags.FENCES.location(), Tags.Blocks.FENCES.location(), @@ -26,7 +26,12 @@ public Block createObject() { } @Override - protected void generateMultipartBlockStateJson(MultipartBlockStateGenerator bs) { + protected boolean useMultipartBlockState() { + return true; + } + + @Override + protected void generateMultipartBlockState(MultipartBlockStateGenerator bs) { var modPost = newID("block/", "_post"); var modSide = newID("block/", "_side"); @@ -38,13 +43,13 @@ protected void generateMultipartBlockStateJson(MultipartBlockStateGenerator bs) } @Override - protected void generateItemModelJson(ModelGenerator m) { + protected void generateItemModel(ModelGenerator m) { m.parent("minecraft:block/fence_inventory"); m.texture("texture", textures.get("texture")); } @Override - protected void generateBlockModelJsons(KubeAssetGenerator generator) { + protected void generateBlockModel(KubeAssetGenerator generator) { var texture = textures.get("texture"); generator.blockModel(newID("", "_post"), m -> { diff --git a/src/main/java/dev/latvian/mods/kubejs/block/custom/FenceGateBlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/custom/FenceGateBlockBuilder.java index 41aa91177..e71a39894 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/custom/FenceGateBlockBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/custom/FenceGateBlockBuilder.java @@ -50,7 +50,7 @@ public Block createObject() { } @Override - protected void generateBlockStateJson(VariantBlockStateGenerator bs) { + protected void generateBlockState(VariantBlockStateGenerator bs) { var mod = newID("block/", ""); var modOpen = newID("block/", "_open"); var modWall = newID("block/", "_wall"); @@ -75,7 +75,7 @@ protected void generateBlockStateJson(VariantBlockStateGenerator bs) { } @Override - protected void generateBlockModelJsons(KubeAssetGenerator generator) { + protected void generateBlockModel(KubeAssetGenerator generator) { var texture = textures.get("texture"); generator.blockModel(id, m -> { @@ -100,7 +100,7 @@ protected void generateBlockModelJsons(KubeAssetGenerator generator) { } @Override - protected void generateItemModelJson(ModelGenerator m) { + protected void generateItemModel(ModelGenerator m) { m.parent("minecraft:block/template_fence_gate"); m.texture("texture", textures.get("texture")); } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/custom/HorizontalDirectionalBlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/custom/HorizontalDirectionalBlockBuilder.java index 1ea8ae7d7..c0f7899c9 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/custom/HorizontalDirectionalBlockBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/custom/HorizontalDirectionalBlockBuilder.java @@ -44,7 +44,7 @@ public HorizontalDirectionalBlockBuilder(ResourceLocation i) { } @Override - protected void generateBlockStateJson(VariantBlockStateGenerator bs) { + protected void generateBlockState(VariantBlockStateGenerator bs) { var modelLocation = model == null ? id.withPath(ID.BLOCK) : model; bs.variant("facing=north", v -> v.model(modelLocation)); bs.variant("facing=east", v -> v.model(modelLocation).y(90)); @@ -53,7 +53,7 @@ protected void generateBlockStateJson(VariantBlockStateGenerator bs) { } @Override - protected void generateBlockModelJsons(KubeAssetGenerator gen) { + protected void generateBlockModel(KubeAssetGenerator gen) { gen.blockModel(id, mg -> { var side = getTextureOrDefault("side", id.withPath(ID.BLOCK).toString()); @@ -72,7 +72,7 @@ protected void generateBlockModelJsons(KubeAssetGenerator gen) { } @Override - protected void generateItemModelJson(ModelGenerator m) { + protected void generateItemModel(ModelGenerator m) { m.parent(model == null ? newID("block/", "") : model); } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/custom/MultipartShapedBlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/custom/MultipartShapedBlockBuilder.java deleted file mode 100644 index 9d09f3dea..000000000 --- a/src/main/java/dev/latvian/mods/kubejs/block/custom/MultipartShapedBlockBuilder.java +++ /dev/null @@ -1,37 +0,0 @@ -package dev.latvian.mods.kubejs.block.custom; - -import dev.latvian.mods.kubejs.client.MultipartBlockStateGenerator; -import dev.latvian.mods.kubejs.generator.KubeAssetGenerator; -import dev.latvian.mods.kubejs.util.ID; -import net.minecraft.resources.ResourceLocation; - -public abstract class MultipartShapedBlockBuilder extends ShapedBlockBuilder { - public MultipartShapedBlockBuilder(ResourceLocation i, String... suffixes) { - super(i, suffixes); - } - - @Override - public void generateAssets(KubeAssetGenerator generator) { - if (blockstateJson != null) { - generator.json(id.withPath(ID.BLOCKSTATE), blockstateJson); - } else { - generator.multipartState(id, this::generateMultipartBlockStateJson); - } - - if (modelJson != null) { - generator.json(id.withPath(ID.MODEL), modelJson); - } else { - generateBlockModelJsons(generator); - } - - if (itemBuilder != null) { - if (itemBuilder.modelJson != null) { - generator.json(id.withPath(ID.ITEM_MODEL), itemBuilder.modelJson); - } else { - generator.itemModel(itemBuilder.id, this::generateItemModelJson); - } - } - } - - protected abstract void generateMultipartBlockStateJson(MultipartBlockStateGenerator bs); -} diff --git a/src/main/java/dev/latvian/mods/kubejs/block/custom/PressurePlateBlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/custom/PressurePlateBlockBuilder.java index df5b12ce3..2a53e5bba 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/custom/PressurePlateBlockBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/custom/PressurePlateBlockBuilder.java @@ -49,13 +49,13 @@ public Block createObject() { } @Override - protected void generateBlockStateJson(VariantBlockStateGenerator bs) { + protected void generateBlockState(VariantBlockStateGenerator bs) { bs.variant("powered=true", v -> v.model(newID("block/", "_down"))); bs.variant("powered=false", v -> v.model(newID("block/", "_up"))); } @Override - protected void generateBlockModelJsons(KubeAssetGenerator generator) { + protected void generateBlockModel(KubeAssetGenerator generator) { var texture = textures.get("texture"); generator.blockModel(newID("", "_down"), m -> { @@ -70,7 +70,7 @@ protected void generateBlockModelJsons(KubeAssetGenerator generator) { } @Override - protected void generateItemModelJson(ModelGenerator m) { + protected void generateItemModel(ModelGenerator m) { m.parent(newID("block/", "_up")); } } \ No newline at end of file diff --git a/src/main/java/dev/latvian/mods/kubejs/block/custom/SlabBlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/custom/SlabBlockBuilder.java index a52404d0c..4e30a257d 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/custom/SlabBlockBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/custom/SlabBlockBuilder.java @@ -24,14 +24,14 @@ public Block createObject() { } @Override - protected void generateBlockStateJson(VariantBlockStateGenerator bs) { + protected void generateBlockState(VariantBlockStateGenerator bs) { bs.variant("type=double", v -> v.model(newID("block/", "_double"))); bs.variant("type=bottom", v -> v.model(newID("block/", "_bottom"))); bs.variant("type=top", v -> v.model(newID("block/", "_top"))); } @Override - protected void generateBlockModelJsons(KubeAssetGenerator generator) { + protected void generateBlockModel(KubeAssetGenerator generator) { var texture = textures.get("texture"); generator.blockModel(newID("", "_double"), m -> { @@ -55,7 +55,7 @@ protected void generateBlockModelJsons(KubeAssetGenerator generator) { } @Override - protected void generateItemModelJson(ModelGenerator m) { + protected void generateItemModel(ModelGenerator m) { m.parent(newID("block/", "_bottom")); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/custom/StairBlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/custom/StairBlockBuilder.java index 5dfc1d765..b3decb9c1 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/custom/StairBlockBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/custom/StairBlockBuilder.java @@ -24,7 +24,7 @@ public Block createObject() { } @Override - protected void generateBlockStateJson(VariantBlockStateGenerator bs) { + protected void generateBlockState(VariantBlockStateGenerator bs) { var mod = newID("block/", ""); var modInner = newID("block/", "_inner"); var modOuter = newID("block/", "_outer"); @@ -72,7 +72,7 @@ protected void generateBlockStateJson(VariantBlockStateGenerator bs) { } @Override - protected void generateBlockModelJsons(KubeAssetGenerator generator) { + protected void generateBlockModel(KubeAssetGenerator generator) { var texture = textures.get("texture"); generator.blockModel(id, m -> { diff --git a/src/main/java/dev/latvian/mods/kubejs/block/custom/TrapdoorBlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/custom/TrapdoorBlockBuilder.java index 63b6b62aa..c391c3e78 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/custom/TrapdoorBlockBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/custom/TrapdoorBlockBuilder.java @@ -45,7 +45,7 @@ public Block createObject() { } @Override - protected void generateBlockStateJson(VariantBlockStateGenerator bs) { + protected void generateBlockState(VariantBlockStateGenerator bs) { var modelOpen = newID("block/", "_open"); var modelBottom = newID("block/", "_bottom"); var modelTop = newID("block/", "_top"); @@ -75,7 +75,7 @@ protected void generateBlockStateJson(VariantBlockStateGenerator bs) { } @Override - protected void generateBlockModelJsons(KubeAssetGenerator generator) { + protected void generateBlockModel(KubeAssetGenerator generator) { var texture = textures.get("texture"); generator.blockModel(newID("", "_bottom"), m -> { @@ -95,7 +95,7 @@ protected void generateBlockModelJsons(KubeAssetGenerator generator) { } @Override - protected void generateItemModelJson(ModelGenerator m) { + protected void generateItemModel(ModelGenerator m) { m.parent(newID("block/", "_bottom")); } } \ No newline at end of file diff --git a/src/main/java/dev/latvian/mods/kubejs/block/custom/WallBlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/block/custom/WallBlockBuilder.java index bf8487814..db0c17268 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/custom/WallBlockBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/custom/WallBlockBuilder.java @@ -8,7 +8,7 @@ import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.WallBlock; -public class WallBlockBuilder extends MultipartShapedBlockBuilder { +public class WallBlockBuilder extends ShapedBlockBuilder { public static final ResourceLocation[] WALL_TAGS = { BlockTags.WALLS.location(), }; @@ -24,7 +24,12 @@ public Block createObject() { } @Override - protected void generateMultipartBlockStateJson(MultipartBlockStateGenerator bs) { + protected boolean useMultipartBlockState() { + return true; + } + + @Override + protected void generateMultipartBlockState(MultipartBlockStateGenerator bs) { var modPost = newID("block/", "_post"); var modSide = newID("block/", "_side"); var modSideTall = newID("block/", "_side_tall"); @@ -41,13 +46,13 @@ protected void generateMultipartBlockStateJson(MultipartBlockStateGenerator bs) } @Override - protected void generateItemModelJson(ModelGenerator m) { + protected void generateItemModel(ModelGenerator m) { m.parent("minecraft:block/wall_inventory"); m.texture("wall", textures.get("texture")); } @Override - protected void generateBlockModelJsons(KubeAssetGenerator generator) { + protected void generateBlockModel(KubeAssetGenerator generator) { var texture = textures.get("texture"); generator.blockModel(newID("", "_post"), m -> { diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachment.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachment.java index c37451daf..7ba7c524e 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachment.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachment.java @@ -1,32 +1,23 @@ package dev.latvian.mods.kubejs.block.entity; -import dev.latvian.mods.rhino.Context; -import net.minecraft.core.HolderLookup; -import net.minecraft.nbt.CompoundTag; +import net.minecraft.server.level.ServerLevel; import net.minecraft.world.level.block.state.BlockState; - -import java.util.Map; +import net.neoforged.neoforge.capabilities.BlockCapability; +import org.jetbrains.annotations.Nullable; public interface BlockEntityAttachment { - BlockEntityAttachment[] EMPTY_ARRAY = new BlockEntityAttachment[0]; - - interface Factory { - BlockEntityAttachment create(KubeBlockEntity entity); + default Object getExposedObject() { + return this; } - interface FactoryProvider { - Factory createFactory(Context cx, Map map); - } - - // TODO: Replace with Codec - - default CompoundTag writeAttachment(HolderLookup.Provider registries) { - return new CompoundTag(); + @Nullable + default CAP getCapability(BlockCapability capability) { + return null; } - default void readAttachment(HolderLookup.Provider registries, CompoundTag tag) { + default void onRemove(ServerLevel level, KubeBlockEntity blockEntity, BlockState newState) { } - default void onRemove(BlockState newState) { + default void tick() { } } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentFactory.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentFactory.java new file mode 100644 index 000000000..2966be790 --- /dev/null +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentFactory.java @@ -0,0 +1,17 @@ +package dev.latvian.mods.kubejs.block.entity; + +import net.neoforged.neoforge.capabilities.BlockCapability; + +import java.util.List; + +public interface BlockEntityAttachmentFactory { + BlockEntityAttachment create(KubeBlockEntity entity); + + default List> getCapabilities() { + return List.of(); + } + + default boolean isTicking() { + return false; + } +} diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentHandler.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentHandler.java new file mode 100644 index 000000000..0167cd41d --- /dev/null +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentHandler.java @@ -0,0 +1,44 @@ +package dev.latvian.mods.kubejs.block.entity; + +import dev.latvian.mods.kubejs.item.ItemPredicate; +import dev.latvian.mods.kubejs.script.ConsoleJS; +import dev.latvian.mods.rhino.Context; +import dev.latvian.mods.rhino.util.HideFromJS; +import net.minecraft.core.Direction; +import net.neoforged.neoforge.capabilities.BlockCapability; +import org.jetbrains.annotations.Nullable; + +import java.util.Optional; +import java.util.Set; +import java.util.function.Supplier; + +public interface BlockEntityAttachmentHandler { + @HideFromJS + void attach(String id, BlockEntityAttachmentType type, Set directions, BlockEntityAttachmentFactory factory); + + default void attach(Context cx, String id, String type, Set directions, Object args) { + var att = BlockEntityAttachmentType.ALL.get().get(type); + + if (att != null) { + try { + attach(id, att, directions, (BlockEntityAttachmentFactory) cx.jsToJava(args, att.typeInfo())); + } catch (Exception ex) { + ConsoleJS.STARTUP.error("Error while creating BlockEntity attachment '" + type + "'", ex); + } + } else { + ConsoleJS.STARTUP.error("BlockEntity attachment '" + type + "' not found!"); + } + } + + default void attachCustomCapability(String id, Set directions, BlockCapability capability, Supplier dataFactory) { + attach(id, CustomCapabilityAttachment.TYPE, directions, new CustomCapabilityAttachment.Factory(capability, dataFactory)); + } + + default void inventory(String id, Set directions, int width, int height, @Nullable ItemPredicate inputFilter) { + attach(id, InventoryAttachment.TYPE, directions, new InventoryAttachment.Factory(width, height, Optional.ofNullable(inputFilter))); + } + + default void inventory(String id, Set directions, int width, int height) { + inventory(id, directions, width, height, null); + } +} diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentHolder.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentHolder.java index 764123a3e..ca348d864 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentHolder.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentHolder.java @@ -1,8 +1,10 @@ package dev.latvian.mods.kubejs.block.entity; -public record BlockEntityAttachmentHolder(int index, BlockEntityAttachment.Factory factory) { +public record BlockEntityAttachmentHolder(BlockEntityAttachmentInfo info, BlockEntityAttachment attachment) { + public static final BlockEntityAttachmentHolder[] EMPTY_ARRAY = new BlockEntityAttachmentHolder[0]; + @Override public String toString() { - return "attachment_" + index; + return info.id(); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentInfo.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentInfo.java new file mode 100644 index 000000000..d02636751 --- /dev/null +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentInfo.java @@ -0,0 +1,12 @@ +package dev.latvian.mods.kubejs.block.entity; + +import net.minecraft.core.Direction; + +import java.util.EnumSet; + +public record BlockEntityAttachmentInfo(String id, BlockEntityAttachmentType type, int index, EnumSet directions, BlockEntityAttachmentFactory factory) { + @Override + public String toString() { + return id; + } +} diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentRegistry.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentRegistry.java new file mode 100644 index 000000000..ea88606cc --- /dev/null +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentRegistry.java @@ -0,0 +1,12 @@ +package dev.latvian.mods.kubejs.block.entity; + +import dev.latvian.mods.rhino.type.TypeInfo; + +@FunctionalInterface +public interface BlockEntityAttachmentRegistry { + default void register(String name, Class factory) { + register(new BlockEntityAttachmentType(name, TypeInfo.of(factory))); + } + + void register(BlockEntityAttachmentType type); +} diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentType.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentType.java index 5994f8afd..10f8d91ef 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentType.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityAttachmentType.java @@ -5,25 +5,22 @@ import dev.latvian.mods.kubejs.util.Lazy; import dev.latvian.mods.rhino.type.TypeInfo; -import java.util.ArrayList; import java.util.HashMap; import java.util.Map; -public record BlockEntityAttachmentType(String type, TypeInfo input, BlockEntityAttachment.FactoryProvider factory) { +public record BlockEntityAttachmentType(String name, TypeInfo typeInfo) { public static final Lazy> ALL = Lazy.of(() -> { var map = new HashMap(); - var list = new ArrayList(); - KubeJSPlugins.forEachPlugin(list, KubeJSPlugin::registerBlockEntityAttachments); - - for (var type : list) { - map.put(type.type, type); - } - - return map; + KubeJSPlugins.forEachPlugin(type -> map.put(type.name, type), KubeJSPlugin::registerBlockEntityAttachments); + return Map.copyOf(map); }); + public BlockEntityAttachmentType(String name, Class type) { + this(name, TypeInfo.of(type)); + } + @Override public String toString() { - return type; + return name; } } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityInfo.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityInfo.java index 0dd7bba11..208ee8cca 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityInfo.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/BlockEntityInfo.java @@ -1,14 +1,14 @@ package dev.latvian.mods.kubejs.block.entity; +import dev.latvian.mods.kubejs.bindings.DirectionWrapper; import dev.latvian.mods.kubejs.block.BlockBuilder; +import dev.latvian.mods.kubejs.core.InventoryKJS; import dev.latvian.mods.kubejs.core.ServerPlayerKJS; -import dev.latvian.mods.kubejs.item.ItemPredicate; -import dev.latvian.mods.kubejs.script.ConsoleJS; -import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.util.HideFromJS; import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; @@ -16,20 +16,22 @@ import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; -import java.util.ArrayList; -import java.util.List; +import java.util.EnumSet; +import java.util.HashMap; import java.util.Map; +import java.util.Set; -public class BlockEntityInfo { +public class BlockEntityInfo implements BlockEntityAttachmentHandler { public transient final BlockBuilder blockBuilder; public transient BlockEntityType entityType; public transient CompoundTag initialData; public transient boolean serverTicking; public transient boolean clientTicking; + public transient boolean attachmentsTicking; public transient int tickFrequency; public transient int tickOffset; public transient boolean sync; - public transient List attachments; + public transient Map attachments; public transient Int2ObjectMap eventHandlers; public BlockEntityInfo(BlockBuilder blockBuilder) { @@ -37,10 +39,11 @@ public BlockEntityInfo(BlockBuilder blockBuilder) { this.initialData = new CompoundTag(); this.serverTicking = false; this.clientTicking = false; + this.attachmentsTicking = false; this.tickFrequency = 1; this.tickOffset = 0; this.sync = false; - this.attachments = new ArrayList<>(1); + this.attachments = new HashMap<>(1); this.eventHandlers = new Int2ObjectArrayMap<>(0); } @@ -73,36 +76,23 @@ public void enableSync() { sync = true; } - public void attach(Context cx, String type, Map args) { - var att = BlockEntityAttachmentType.ALL.get().get(type); + @Override + public void attach(String id, BlockEntityAttachmentType type, Set directions, BlockEntityAttachmentFactory factory) { + attachments.put(id, new BlockEntityAttachmentInfo(id, type, attachments.size(), directions == null || directions.isEmpty() ? DirectionWrapper.EMPTY_SET : EnumSet.copyOf(directions), factory)); - if (att != null) { - try { - attachments.add(new BlockEntityAttachmentHolder(attachments.size(), att.factory().createFactory(cx, args))); - } catch (Exception ex) { - ConsoleJS.STARTUP.error("Error while creating BlockEntity attachment '" + type + "'", ex); - } - } else { - ConsoleJS.STARTUP.error("BlockEntity attachment '" + type + "' not found!"); + if (!attachmentsTicking && factory.isTicking()) { + attachmentsTicking = true; } } - public void inventory(Context cx, int width, int height) { - attach(cx, "inventory", Map.of("width", width, "height", height)); - } - - public void inventory(Context cx, int width, int height, ItemPredicate inputFilter) { - attach(cx, "inventory", Map.of("width", width, "height", height, "inputFilter", inputFilter)); - } - public void eventHandler(int eventId, BlockEntityEventCallback callback) { eventHandlers.put(eventId, callback); } - public void rightClickOpensInventory() { + public void rightClickOpensInventory(String id) { blockBuilder.rightClick = e -> { - if (e.getBlock().getEntity() instanceof KubeBlockEntity entity && entity.inventory != null) { - ((ServerPlayerKJS) e.getPlayer()).kjs$openInventoryGUI(entity.inventory, blockBuilder.get().getName()); + if (e.getPlayer() instanceof ServerPlayerKJS p && e.getBlock().getEntity() instanceof KubeBlockEntity entity && entity.attachments.get(id) instanceof InventoryKJS inv) { + p.kjs$openInventoryGUI(inv, blockBuilder.get().getName()); } }; } @@ -117,7 +107,7 @@ public BlockEntityTicker getTicker(Level level) { if (level.isClientSide()) { return clientTicking ? (BlockEntityTicker) KubeBlockEntity.TICKER : null; } else { - return serverTicking ? (BlockEntityTicker) KubeBlockEntity.TICKER : null; + return serverTicking || attachmentsTicking ? (BlockEntityTicker) KubeBlockEntity.TICKER : null; } } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/CustomCapabilityAttachment.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/CustomCapabilityAttachment.java new file mode 100644 index 000000000..e945a0c4d --- /dev/null +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/CustomCapabilityAttachment.java @@ -0,0 +1,53 @@ +package dev.latvian.mods.kubejs.block.entity; + +import net.minecraft.core.HolderLookup; +import net.minecraft.nbt.Tag; +import net.neoforged.neoforge.capabilities.BlockCapability; +import net.neoforged.neoforge.common.util.INBTSerializable; +import org.jetbrains.annotations.Nullable; + +import java.util.List; +import java.util.function.Supplier; + +public record CustomCapabilityAttachment(BlockCapability capability, Object data) implements BlockEntityAttachment, INBTSerializable { + public static final BlockEntityAttachmentType TYPE = new BlockEntityAttachmentType("custom_capability", Factory.class); + + public record Factory(BlockCapability type, Supplier dataFactory) implements BlockEntityAttachmentFactory { + @Override + public BlockEntityAttachment create(KubeBlockEntity entity) { + return new CustomCapabilityAttachment(type, dataFactory.get()); + } + + @Override + public List> getCapabilities() { + return List.of(type); + } + } + + @Override + public Object getExposedObject() { + return data; + } + + @Override + @Nullable + public CAP getCapability(BlockCapability c) { + if (c == capability) { + return (CAP) data; + } + + return null; + } + + @Override + public Tag serializeNBT(HolderLookup.Provider registries) { + return data instanceof INBTSerializable s ? s.serializeNBT(registries) : null; + } + + @Override + public void deserializeNBT(HolderLookup.Provider registries, Tag tag) { + if (data instanceof INBTSerializable s) { + s.deserializeNBT(registries, tag); + } + } +} diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/InventoryAttachment.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/InventoryAttachment.java index 66ba908cf..31036ce28 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/entity/InventoryAttachment.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/InventoryAttachment.java @@ -1,70 +1,106 @@ package dev.latvian.mods.kubejs.block.entity; +import dev.latvian.mods.kubejs.core.InventoryKJS; import dev.latvian.mods.kubejs.item.ItemPredicate; -import dev.latvian.mods.kubejs.item.ingredient.IngredientJS; -import dev.latvian.mods.rhino.ScriptRuntime; -import dev.latvian.mods.rhino.type.JSObjectTypeInfo; -import dev.latvian.mods.rhino.type.JSOptionalParam; -import dev.latvian.mods.rhino.type.TypeInfo; import net.minecraft.core.HolderLookup; +import net.minecraft.core.NonNullList; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; -import net.minecraft.nbt.Tag; +import net.minecraft.server.level.ServerLevel; import net.minecraft.world.Containers; -import net.minecraft.world.SimpleContainer; -import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.state.BlockState; +import net.neoforged.neoforge.capabilities.BlockCapability; +import net.neoforged.neoforge.capabilities.Capabilities; +import net.neoforged.neoforge.common.util.INBTSerializable; +import net.neoforged.neoforge.items.ItemStackHandler; import org.jetbrains.annotations.Nullable; -public class InventoryAttachment extends SimpleContainer implements BlockEntityAttachment { - public static final BlockEntityAttachmentType TYPE = new BlockEntityAttachmentType( - "inventory", - JSObjectTypeInfo.of( - new JSOptionalParam("xsize", TypeInfo.INT), - new JSOptionalParam("ysize", TypeInfo.INT), - new JSOptionalParam("inputFilter", IngredientJS.TYPE_INFO) - ), - (cx, map) -> { - var width = ScriptRuntime.toInt32(cx, map.get("width")); - var height = ScriptRuntime.toInt32(cx, map.get("height")); - var inputFilter = map.containsKey("inputFilter") ? ItemPredicate.wrap(cx, map.get("inputFilter")) : null; - return new InventoryAttachmentFactory(width, height, inputFilter); - } - ); +import java.util.List; +import java.util.Optional; + +public class InventoryAttachment implements BlockEntityAttachment, INBTSerializable { + public static final BlockEntityAttachmentType TYPE = new BlockEntityAttachmentType("inventory", Factory.class); - private record InventoryAttachmentFactory(int width, int height, @Nullable ItemPredicate inputFilter) implements Factory { + public record Factory(int width, int height, Optional inputFilter) implements BlockEntityAttachmentFactory { @Override public BlockEntityAttachment create(KubeBlockEntity entity) { - return new InventoryAttachment(entity, width, height, inputFilter); + return new InventoryAttachment(entity, width, height, inputFilter.orElse(null)); + } + + @Override + public List> getCapabilities() { + return List.of(Capabilities.ItemHandler.BLOCK); + } + } + + public static class ItemHandler extends ItemStackHandler implements InventoryKJS { + private final InventoryAttachment attachment; + + public ItemHandler(InventoryAttachment attachment) { + super(attachment.width * attachment.height); + this.attachment = attachment; + } + + public NonNullList stacks() { + return stacks; + } + + @Override + protected void onContentsChanged(int slot) { + attachment.blockEntity.save(); + } + + @Override + public boolean isItemValid(int slot, ItemStack stack) { + return (attachment.inputFilter == null || attachment.inputFilter.test(stack)) && super.isItemValid(slot, stack); + } + + @Override + public int kjs$getWidth() { + return attachment.width; + } + + @Override + public int kjs$getHeight() { + return attachment.height; } } public final int width, height; public final KubeBlockEntity blockEntity; public final ItemPredicate inputFilter; + public final ItemHandler itemStackHandler; public InventoryAttachment(KubeBlockEntity blockEntity, int width, int height, @Nullable ItemPredicate inputFilter) { - super(width * height); this.width = width; this.height = height; this.blockEntity = blockEntity; this.inputFilter = inputFilter; + this.itemStackHandler = new ItemHandler(this); } @Override - public void setChanged() { - super.setChanged(); - blockEntity.save(); + public Object getExposedObject() { + return itemStackHandler; } @Override - public CompoundTag writeAttachment(HolderLookup.Provider registries) { - var tag = new CompoundTag(); + @Nullable + public CAP getCapability(BlockCapability capability) { + if (capability == Capabilities.ItemHandler.BLOCK) { + return (CAP) itemStackHandler; + } + + return null; + } + + @Override + public ListTag serializeNBT(HolderLookup.Provider registries) { var list = new ListTag(); - for (int i = 0; i < getContainerSize(); i++) { - var stack = getItem(i); + for (int i = 0; i < width * height; i++) { + var stack = itemStackHandler.stacks().get(i); if (!stack.isEmpty()) { var itemTag = (CompoundTag) stack.save(registries, new CompoundTag()); @@ -73,55 +109,25 @@ public CompoundTag writeAttachment(HolderLookup.Provider registries) { } } - tag.put("items", list); - return tag; + return list; } @Override - public void readAttachment(HolderLookup.Provider registries, CompoundTag tag) { - for (int i = 0; i < getContainerSize(); ++i) { - removeItemNoUpdate(i); - } + public void deserializeNBT(HolderLookup.Provider registries, ListTag list) { + itemStackHandler.setSize(width * height); - var list = tag.getList("items", Tag.TAG_COMPOUND); - - for (int i = 0; i < list.size(); ++i) { + for (int i = 0; i < list.size(); i++) { var itemTag = list.getCompound(i); var slot = itemTag.getByte("slot"); - if (slot >= 0 && slot < getContainerSize()) { - setItem(slot, ItemStack.parse(registries, itemTag).orElse(ItemStack.EMPTY)); + if (slot >= 0 && slot < width * height) { + itemStackHandler.stacks().set(slot, ItemStack.parse(registries, itemTag).orElse(ItemStack.EMPTY)); } } } @Override - public void onRemove(BlockState newState) { - Containers.dropContents(blockEntity.getLevel(), blockEntity.getBlockPos(), this); - } - - @Override - public boolean canAddItem(ItemStack itemStack) { - return (inputFilter == null || inputFilter.test(itemStack)) && super.canAddItem(itemStack); - } - - @Override - public boolean canPlaceItem(int i, ItemStack itemStack) { - return (inputFilter == null || inputFilter.test(itemStack)) && super.canPlaceItem(i, itemStack); - } - - @Override - public boolean stillValid(Player player) { - return !blockEntity.isRemoved(); - } - - @Override - public int kjs$getWidth() { - return width; - } - - @Override - public int kjs$getHeight() { - return height; + public void onRemove(ServerLevel level, KubeBlockEntity blockEntity, BlockState newState) { + Containers.dropContents(blockEntity.getLevel(), blockEntity.getBlockPos(), itemStackHandler.stacks()); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/block/entity/KubeBlockEntity.java b/src/main/java/dev/latvian/mods/kubejs/block/entity/KubeBlockEntity.java index c983f0190..6b4693670 100644 --- a/src/main/java/dev/latvian/mods/kubejs/block/entity/KubeBlockEntity.java +++ b/src/main/java/dev/latvian/mods/kubejs/block/entity/KubeBlockEntity.java @@ -1,24 +1,25 @@ package dev.latvian.mods.kubejs.block.entity; import dev.latvian.mods.kubejs.bindings.event.BlockEvents; -import dev.latvian.mods.kubejs.core.InventoryKJS; import dev.latvian.mods.kubejs.level.BlockContainerJS; import net.minecraft.core.BlockPos; import net.minecraft.core.HolderLookup; import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.ListTag; -import net.minecraft.nbt.Tag; import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.game.ClientGamePacketListener; import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket; import net.minecraft.resources.ResourceKey; +import net.minecraft.world.entity.Entity; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntityTicker; import net.minecraft.world.level.block.state.BlockState; +import net.neoforged.neoforge.common.util.INBTSerializable; import org.jetbrains.annotations.Nullable; +import java.util.HashMap; +import java.util.Map; import java.util.UUID; public class KubeBlockEntity extends BlockEntity { @@ -30,10 +31,12 @@ public class KubeBlockEntity extends BlockEntity { public final int x, y, z; public int tick, cycle; public CompoundTag data; - public final BlockEntityAttachment[] attachments; - public InventoryKJS inventory; + public final Map attachments; + public final transient BlockEntityAttachmentHolder[] attachmentArray; public UUID placerId; private BlockEntityTickKubeEvent tickEvent; + private boolean save; + private boolean sync; public KubeBlockEntity(BlockPos blockPos, BlockState blockState, BlockEntityInfo entityInfo) { super(entityInfo.entityType, blockPos, blockState); @@ -45,17 +48,19 @@ public KubeBlockEntity(BlockPos blockPos, BlockState blockState, BlockEntityInfo this.data = info.initialData.copy(); if (entityInfo.attachments != null) { - this.attachments = new BlockEntityAttachment[entityInfo.attachments.size()]; + var map = new HashMap(entityInfo.attachments.size()); + this.attachmentArray = new BlockEntityAttachmentHolder[entityInfo.attachments.size()]; - for (int i = 0; i < this.attachments.length; i++) { - this.attachments[i] = entityInfo.attachments.get(i).factory().create(this); - - if (this.inventory == null && this.attachments[i] instanceof InventoryKJS inv) { - this.inventory = inv; - } + for (var aInfo : entityInfo.attachments.values()) { + var f = aInfo.factory().create(this); + map.put(aInfo.id(), f.getExposedObject()); + this.attachmentArray[aInfo.index()] = new BlockEntityAttachmentHolder(aInfo, f); } + + this.attachments = Map.copyOf(map); } else { - this.attachments = BlockEntityAttachment.EMPTY_ARRAY; + this.attachments = Map.of(); + this.attachmentArray = BlockEntityAttachmentHolder.EMPTY_ARRAY; } } @@ -82,14 +87,20 @@ protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) tag.putUUID("placer", placerId); } - if (attachments.length > 0) { - var list = new ListTag(); + if (attachmentArray.length > 0) { + var data = new CompoundTag(); + + for (var entry : attachmentArray) { + if (entry.attachment() instanceof INBTSerializable s) { + var t = s.serializeNBT(registries); - for (var att : attachments) { - list.add(att.writeAttachment(registries)); + if (t != null) { + data.put(entry.info().id(), t); + } + } } - tag.put("attachments", list); + tag.put("attachments", data); } } @@ -101,16 +112,16 @@ public void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) { cycle = tag.getInt("cycle"); placerId = tag.contains("placer") ? tag.getUUID("placer") : null; - if (attachments.length > 0) { - var list = tag.getList("attachments", Tag.TAG_COMPOUND); + if (attachmentArray.length > 0) { + var data = tag.getCompound("attachments"); - if (attachments.length == list.size()) { - for (int i = 0; i < attachments.length; i++) { - attachments[i].readAttachment(registries, list.getCompound(i)); - } - } else { - for (var att : attachments) { - att.readAttachment(registries, new CompoundTag()); + for (var entry : attachmentArray) { + if (entry.attachment() instanceof INBTSerializable s) { + var t = data.get(entry.info().id()); + + if (t != null) { + s.deserializeNBT(registries, t); + } } } } @@ -143,14 +154,22 @@ public Packet getUpdatePacket() { public void save() { if (level != null) { - level.blockEntityChanged(worldPosition); + if (info.getTicker(level) != null) { + save = true; + } else { + level.blockEntityChanged(worldPosition); + } } } public void sync() { if (level != null) { - save(); - level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 11); + if (info.getTicker(level) != null) { + sync = true; + } else { + save(); + level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 11); + } } } @@ -183,22 +202,50 @@ public BlockContainerJS getBlock() { } private void tick() { - if (tick % info.tickFrequency == info.tickOffset) { - var side = level.kjs$getScriptType(); + if (level == null) { + return; + } + + if (level.isClientSide ? info.clientTicking : info.serverTicking) { + if (tick % info.tickFrequency == info.tickOffset) { + var side = level.kjs$getScriptType(); - try { - if (tickEvent == null) { - tickEvent = new BlockEntityTickKubeEvent(this); + try { + if (tickEvent == null) { + tickEvent = new BlockEntityTickKubeEvent(this); + } + + BlockEvents.BLOCK_ENTITY_TICK.post(side, blockKey, tickEvent); + } catch (Exception ex) { + side.console.error("Error while ticking KubeJS block entity '" + info.blockBuilder.id + "'", ex); } - BlockEvents.BLOCK_ENTITY_TICK.post(side, blockKey, tickEvent); - } catch (Exception ex) { - side.console.error("Error while ticking KubeJS block entity '" + info.blockBuilder.id + "'", ex); + cycle++; + } + + tick++; + } + + if (!level.isClientSide && info.attachmentsTicking) { + for (var entry : attachmentArray) { + entry.attachment().tick(); } + } - cycle++; + if (sync) { + level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 11); + save = true; + sync = false; } - tick++; + if (save) { + level.blockEntityChanged(worldPosition); + save = false; + } + } + + @Nullable + public Entity getPlacer() { + return level == null ? null : level.kjs$getEntityByUUID(placerId); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/LevelKJS.java b/src/main/java/dev/latvian/mods/kubejs/core/LevelKJS.java index e4d0e5aa8..c99b7550d 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/LevelKJS.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/LevelKJS.java @@ -24,6 +24,7 @@ import org.jetbrains.annotations.Nullable; import java.util.Collection; +import java.util.UUID; @RemapPrefixForJS("kjs$") public interface LevelKJS extends WithAttachedData, ScriptTypeHolder { @@ -138,4 +139,15 @@ public interface LevelKJS extends WithAttachedData, ScriptTypeHolder { default void kjs$spawnParticles(ParticleOptions options, boolean overrideLimiter, double x, double y, double z, double vx, double vy, double vz, int count, double speed) { } + + @Nullable + default Entity kjs$getEntityByUUID(UUID id) { + for (var entity : kjs$getEntities()) { + if (entity.getUUID().equals(id)) { + return entity; + } + } + + return null; + } } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/mixin/ClientLevelMixin.java b/src/main/java/dev/latvian/mods/kubejs/core/mixin/ClientLevelMixin.java index a8c944153..1a8b29607 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/mixin/ClientLevelMixin.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/mixin/ClientLevelMixin.java @@ -6,11 +6,13 @@ import net.minecraft.client.player.AbstractClientPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.level.entity.LevelEntityGetter; +import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import java.util.List; +import java.util.UUID; @Mixin(ClientLevel.class) public abstract class ClientLevelMixin implements ClientLevelKJS { @@ -26,4 +28,10 @@ public abstract class ClientLevelMixin implements ClientLevelKJS { @Shadow @HideFromJS protected abstract LevelEntityGetter getEntities(); + + @Override + @Nullable + public Entity kjs$getEntityByUUID(UUID id) { + return getEntities().get(id); + } } diff --git a/src/main/java/dev/latvian/mods/kubejs/core/mixin/IItemHandlerMixin.java b/src/main/java/dev/latvian/mods/kubejs/core/mixin/IItemHandlerMixin.java index 28d03b432..146600dbb 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/mixin/IItemHandlerMixin.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/mixin/IItemHandlerMixin.java @@ -10,8 +10,9 @@ import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.gen.Invoker; -@Mixin(value = IItemHandler.class, remap = false) +@Mixin(IItemHandler.class) public interface IItemHandlerMixin extends InventoryKJS { @Unique default IItemHandler kjs$self() { @@ -24,14 +25,12 @@ public interface IItemHandlerMixin extends InventoryKJS { } @Override - default int kjs$getSlots() { - return kjs$self().getSlots(); - } + @Invoker("getSlots") + int kjs$getSlots(); @Override - default ItemStack kjs$getStackInSlot(int i) { - return kjs$self().getStackInSlot(i); - } + @Invoker("getStackInSlot") + ItemStack kjs$getStackInSlot(int i); @Override default void kjs$setStackInSlot(int slot, ItemStack stack) { @@ -43,24 +42,20 @@ public interface IItemHandlerMixin extends InventoryKJS { } @Override - default ItemStack kjs$insertItem(int i, ItemStack itemStack, boolean b) { - return kjs$self().insertItem(i, itemStack, b); - } + @Invoker("insertItem") + ItemStack kjs$insertItem(int i, ItemStack itemStack, boolean b); @Override - default ItemStack kjs$extractItem(int i, int i1, boolean b) { - return kjs$self().extractItem(i, i1, b); - } + @Invoker("extractItem") + ItemStack kjs$extractItem(int i, int i1, boolean b); @Override - default int kjs$getSlotLimit(int i) { - return kjs$self().getSlotLimit(i); - } + @Invoker("getSlotLimit") + int kjs$getSlotLimit(int i); @Override - default boolean kjs$isItemValid(int i, ItemStack itemStack) { - return kjs$self().isItemValid(i, itemStack); - } + @Invoker("isItemValid") + boolean kjs$isItemValid(int i, ItemStack itemStack); @Override default @Nullable BlockContainerJS kjs$getBlock(Level level) { diff --git a/src/main/java/dev/latvian/mods/kubejs/core/mixin/ServerLevelMixin.java b/src/main/java/dev/latvian/mods/kubejs/core/mixin/ServerLevelMixin.java index f130a9f61..3c4062944 100644 --- a/src/main/java/dev/latvian/mods/kubejs/core/mixin/ServerLevelMixin.java +++ b/src/main/java/dev/latvian/mods/kubejs/core/mixin/ServerLevelMixin.java @@ -12,7 +12,9 @@ import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; +import javax.annotation.Nullable; import java.util.List; +import java.util.UUID; @Mixin(ServerLevel.class) public abstract class ServerLevelMixin implements ServerLevelKJS { @@ -42,4 +44,9 @@ public abstract class ServerLevelMixin implements ServerLevelKJS { @Shadow @HideFromJS public abstract LevelEntityGetter getEntities(); + + @Shadow + @Nullable + @HideFromJS + public abstract Entity getEntity(UUID uniqueId); } diff --git a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBlockBuilder.java b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBlockBuilder.java index fb76a1ce1..07a3c84da 100644 --- a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBlockBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBlockBuilder.java @@ -29,7 +29,10 @@ public Block createObject() { } @Override - public void generateAssets(KubeAssetGenerator generator) { + protected void generateBlockModel(KubeAssetGenerator generator) { + generator.blockModel(id, mg -> { + var particle = textures.get("particle"); + }); } @Override diff --git a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBuilder.java b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBuilder.java index 86fd1e84c..578fe423e 100644 --- a/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/fluid/FluidBuilder.java @@ -7,7 +7,6 @@ import dev.latvian.mods.kubejs.generator.KubeAssetGenerator; import dev.latvian.mods.kubejs.registry.AdditionalObjectRegistry; import dev.latvian.mods.kubejs.registry.BuilderBase; -import dev.latvian.mods.kubejs.util.ID; import dev.latvian.mods.rhino.util.ReturnsSelf; import net.minecraft.core.registries.Registries; import net.minecraft.network.chat.Component; @@ -174,11 +173,6 @@ public void generateAssets(KubeAssetGenerator generator) { }); if (bucketItem != null) { - if (bucketItem.modelJson != null) { - generator.json(id.withPath(ID.ITEM_MODEL), bucketItem.modelJson); - return; - } - var fluidPath = newID("item/generated/", "_bucket_fluid"); generator.mask(fluidPath, KubeJS.id("item/bucket_mask"), fluidType.actualStillTexture); diff --git a/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java b/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java index e6e20fcee..475255818 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java @@ -1,6 +1,5 @@ package dev.latvian.mods.kubejs.item; -import com.google.gson.JsonObject; import dev.latvian.mods.kubejs.bindings.ItemWrapper; import dev.latvian.mods.kubejs.color.KubeColor; import dev.latvian.mods.kubejs.component.DataComponentWrapper; @@ -77,7 +76,6 @@ public record HurtEnemyContext(ItemStack getItem, LivingEntity getTarget, Living public ResourceLocation texture; public ResourceLocation parentModel; public Map textures; - public JsonObject modelJson; public transient Tool tool; public transient ItemAttributeModifiers itemAttributeModifiers; @@ -96,7 +94,6 @@ public ItemBuilder(ResourceLocation i) { textures = new HashMap<>(); parentModel = null; foodBuilder = null; - modelJson = null; anim = null; useDuration = null; use = null; @@ -123,11 +120,6 @@ public Item transformObject(Item obj) { @Override public void generateAssets(KubeAssetGenerator generator) { - if (modelJson != null) { - generator.json(id.withPath(ID.ITEM_MODEL), modelJson); - return; - } - generator.itemModel(id, m -> { m.parent(parentModel != null ? parentModel : KubeAssetGenerator.GENERATED_ITEM_MODEL); @@ -245,12 +237,6 @@ public ItemBuilder textures(Map tex) { return this; } - @Info("Directly set the item's model json.") - public ItemBuilder modelJson(JsonObject json) { - modelJson = json; - return this; - } - @Info("Sets the item's model (parent).") public ItemBuilder parentModel(ResourceLocation m) { parentModel = m; diff --git a/src/main/java/dev/latvian/mods/kubejs/plugin/KubeJSPlugin.java b/src/main/java/dev/latvian/mods/kubejs/plugin/KubeJSPlugin.java index 21a6e8cae..df5e2916b 100644 --- a/src/main/java/dev/latvian/mods/kubejs/plugin/KubeJSPlugin.java +++ b/src/main/java/dev/latvian/mods/kubejs/plugin/KubeJSPlugin.java @@ -1,7 +1,7 @@ package dev.latvian.mods.kubejs.plugin; import com.google.gson.JsonElement; -import dev.latvian.mods.kubejs.block.entity.BlockEntityAttachmentType; +import dev.latvian.mods.kubejs.block.entity.BlockEntityAttachmentRegistry; import dev.latvian.mods.kubejs.client.LangKubeEvent; import dev.latvian.mods.kubejs.core.RecipeManagerKJS; import dev.latvian.mods.kubejs.event.EventGroup; @@ -32,7 +32,6 @@ import net.minecraft.world.item.crafting.RecipeHolder; import net.minecraft.world.level.Level; -import java.util.List; import java.util.Map; import java.util.function.Consumer; @@ -82,7 +81,7 @@ default void registerRecipeComponents(RecipeComponentFactoryRegistry registry) { default void registerRecipeSchemas(RecipeSchemaRegistry registry) { } - default void registerBlockEntityAttachments(List types) { + default void registerBlockEntityAttachments(BlockEntityAttachmentRegistry registry) { } default void registerIngredientActionTypes(IngredientActionTypeRegistry registry) { diff --git a/src/main/java/dev/latvian/mods/kubejs/registry/RegistryObjectStorage.java b/src/main/java/dev/latvian/mods/kubejs/registry/RegistryObjectStorage.java index 103e15dba..415fed71d 100644 --- a/src/main/java/dev/latvian/mods/kubejs/registry/RegistryObjectStorage.java +++ b/src/main/java/dev/latvian/mods/kubejs/registry/RegistryObjectStorage.java @@ -9,6 +9,7 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.material.Fluid; import net.neoforged.neoforge.fluids.FluidType; import net.neoforged.neoforge.registries.NeoForgeRegistries; @@ -36,6 +37,7 @@ public static RegistryObjectStorage of(ResourceKey> key) { public static final RegistryObjectStorage FLUID = of(Registries.FLUID); public static final RegistryObjectStorage BLOCK = of(Registries.BLOCK); public static final RegistryObjectStorage ITEM = of(Registries.ITEM); + public static final RegistryObjectStorage> BLOCK_ENTITY = of(Registries.BLOCK_ENTITY_TYPE); public static final RegistryObjectStorage FLUID_TYPE = of(NeoForgeRegistries.Keys.FLUID_TYPES); public final ResourceKey> key;