Skip to content

Commit

Permalink
Block entity attachment rewrite, more model generator tweaks. BE inve…
Browse files Browse the repository at this point in the history
…ntories fixed, made attachments directional, added custom Capability attachments
  • Loading branch information
LatvianModder committed Sep 2, 2024
1 parent 439d707 commit 4612297
Show file tree
Hide file tree
Showing 40 changed files with 502 additions and 346 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -703,8 +704,9 @@ public void registerRecipeComponents(RecipeComponentFactoryRegistry registry) {
}

@Override
public void registerBlockEntityAttachments(List<BlockEntityAttachmentType> types) {
types.add(InventoryAttachment.TYPE);
public void registerBlockEntityAttachments(BlockEntityAttachmentRegistry registry) {
registry.register(CustomCapabilityAttachment.TYPE);
registry.register(InventoryAttachment.TYPE);
}

@Override
Expand Down
38 changes: 27 additions & 11 deletions src/main/java/dev/latvian/mods/kubejs/KubeJSModEventHandler.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<CAP, SRC>(BlockCapability<CAP, SRC> capability, BlockEntityAttachmentInfo attachment) implements ICapabilityProvider<KubeBlockEntity, SRC, CAP> {
@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<KubeBlockEntity>) info.entityType, new KubeEntityCapabilityProvider(capability, attachment));
}
}
}
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,4 +23,5 @@ public interface DirectionWrapper {
Direction EAST = Direction.EAST;
Direction[] VALUES = Direction.values();
Map<String, Direction> ALL = Map.copyOf(Arrays.stream(VALUES).collect(Collectors.toMap(Direction::getSerializedName, Function.identity())));
EnumSet<Direction> EMPTY_SET = EnumSet.noneOf(Direction.class);
}
46 changes: 18 additions & 28 deletions src/main/java/dev/latvian/mods/kubejs/block/BlockBuilder.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -95,8 +95,6 @@ public abstract class BlockBuilder extends BuilderBase<Block> {
public transient float jumpFactor = Float.NaN;
public Consumer<RandomTickCallbackJS> randomTickCallback;
public BlockDropSupplier drops;
public JsonObject blockstateJson;
public JsonObject modelJson;
public transient boolean noValidSpawns;
public transient boolean suffocating;
public transient boolean viewBlocking;
Expand Down Expand Up @@ -141,8 +139,6 @@ public BlockBuilder(ResourceLocation i) {
notSolid = false;
randomTickCallback = null;
drops = null;
blockstateJson = null;
modelJson = null;
noValidSpawns = false;
suffocating = true;
viewBlocking = true;
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/dev/latvian/mods/kubejs/block/DetectorBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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");

Expand Down Expand Up @@ -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 -> {
Expand All @@ -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"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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");

Expand All @@ -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 -> {
Expand Down
Loading

0 comments on commit 4612297

Please sign in to comment.