Skip to content

Commit

Permalink
Merge pull request #459 from ChiefArug/orientableBlocks
Browse files Browse the repository at this point in the history
Add Cardinally orientable blocks and shuffle asset generation code
  • Loading branch information
LatvianModder authored Sep 27, 2022
2 parents 529bd61 + 97d3aa8 commit 43bfb90
Show file tree
Hide file tree
Showing 15 changed files with 511 additions and 284 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import dev.latvian.mods.kubejs.block.custom.FallingBlockBuilder;
import dev.latvian.mods.kubejs.block.custom.FenceBlockBuilder;
import dev.latvian.mods.kubejs.block.custom.FenceGateBlockBuilder;
import dev.latvian.mods.kubejs.block.custom.HorizontalDirectionalBlockBuilder;
import dev.latvian.mods.kubejs.block.custom.SlabBlockBuilder;
import dev.latvian.mods.kubejs.block.custom.StairBlockBuilder;
import dev.latvian.mods.kubejs.block.custom.StoneButtonBlockBuilder;
Expand Down Expand Up @@ -174,6 +175,7 @@ public void init() {
RegistryObjectBuilderTypes.BLOCK.addType("stone_button", StoneButtonBlockBuilder.class, StoneButtonBlockBuilder::new);
RegistryObjectBuilderTypes.BLOCK.addType("falling", FallingBlockBuilder.class, FallingBlockBuilder::new);
RegistryObjectBuilderTypes.BLOCK.addType("crop", CropBlockBuilder.class, CropBlockBuilder::new);
RegistryObjectBuilderTypes.BLOCK.addType("cardinal", HorizontalDirectionalBlockBuilder.class, HorizontalDirectionalBlockBuilder::new);

RegistryObjectBuilderTypes.ITEM.addType("basic", BasicItemJS.Builder.class, BasicItemJS.Builder::new);
RegistryObjectBuilderTypes.ITEM.addType("sword", SwordItemBuilder.class, SwordItemBuilder::new);
Expand Down
121 changes: 70 additions & 51 deletions common/src/main/java/dev/latvian/mods/kubejs/block/BlockBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import dev.architectury.registry.client.rendering.RenderTypeRegistry;
import dev.latvian.mods.kubejs.BuilderBase;
import dev.latvian.mods.kubejs.RegistryObjectBuilderTypes;
import dev.latvian.mods.kubejs.client.ModelGenerator;
import dev.latvian.mods.kubejs.client.VariantBlockStateGenerator;
import dev.latvian.mods.kubejs.generator.AssetJsonGenerator;
import dev.latvian.mods.kubejs.generator.DataJsonGenerator;
import dev.latvian.mods.kubejs.loot.LootBuilder;
Expand Down Expand Up @@ -153,62 +155,79 @@ public void generateDataJsons(DataJsonGenerator generator) {

@Override
public void generateAssetJsons(AssetJsonGenerator generator) {

if (blockstateJson != null) {
generator.json(newID("blockstates/", ""), blockstateJson);
} else {
generator.blockState(id, bs -> bs.variant("", model.isEmpty() ? (id.getNamespace() + ":block/" + id.getPath()) : model));
generator.blockState(id, this::generateBlockStateJson);
}

if (modelJson != null) {
generator.json(newID("models/block/", ""), modelJson);
generator.json(newID("models/", ""), modelJson);
} else {
generator.blockModel(id, m -> {
var particle = textures.get("particle").getAsString();

if (areAllTexturesEqual(textures, particle)) {
m.parent("minecraft:block/cube_all");
m.texture("all", particle);
} else {
m.parent("block/cube");
m.textures(textures);
}

if (!color.isEmpty() || !customShape.isEmpty()) {
List<AABB> boxes = new ArrayList<>(customShape);

if (boxes.isEmpty()) {
boxes.add(new AABB(0D, 0D, 0D, 1D, 1D, 1D));
}

for (var box : boxes) {
m.element(e -> {
e.box(box);

for (var direction : Direction.values()) {
e.face(direction, face -> {
face.tex("#" + direction.getSerializedName());
face.cull();

if (!color.isEmpty()) {
face.tintindex(0);
}
});
}
});
}
}
});
// This is different because there can be multiple models, so we should let the block handle those
generateBlockModelJsons(generator);
}

if (itemBuilder != null) {
generator.itemModel(itemBuilder.id, m -> {
if (!model.isEmpty()) {
m.parent(model);
} else {
m.parent(newID("block/", "").toString());
}
});
if (itemBuilder.modelJson != null) {
generator.json(newID("models/item/", ""), itemBuilder.modelJson);
} else {
generator.itemModel(itemBuilder.id, this::generateItemModelJson);
}
}

}

protected void generateItemModelJson(ModelGenerator m) {
if (!model.isEmpty()) {
m.parent(model);
} else {
m.parent(newID("block/", "").toString());
}
}

protected void generateBlockModelJsons(AssetJsonGenerator generator) {
generator.blockModel(newID("block/", ""), mg -> {
var particle = textures.get("particle").getAsString();

if (areAllTexturesEqual(textures, particle)) {
mg.parent("minecraft:block/cube_all");
mg.texture("all", particle);
} else {
mg.parent("block/cube");
mg.textures(textures);
}

if (!color.isEmpty() || !customShape.isEmpty()) {
List<AABB> boxes = new ArrayList<>(customShape);

if (boxes.isEmpty()) {
boxes.add(new AABB(0D, 0D, 0D, 1D, 1D, 1D));
}

for (var box : boxes) {
mg.element(e -> {
e.box(box);

for (var direction : Direction.values()) {
e.face(direction, face -> {
face.tex("#" + direction.getSerializedName());
face.cull();

if (!color.isEmpty()) {
face.tintindex(0);
}
});
}
});
}
}
});
}

protected void generateBlockStateJson(VariantBlockStateGenerator bs) {
bs.variant("", model.isEmpty() ? (id.getNamespace() + ":block/" + id.getPath()) : model);
}

public Map<ResourceLocation, JsonObject> generateBlockModels(BlockBuilder builder) {
Expand Down Expand Up @@ -266,7 +285,7 @@ public Map<ResourceLocation, JsonObject> generateBlockModels(BlockBuilder builde
return map;
}

private boolean areAllTexturesEqual(JsonObject tex, String t) {
protected boolean areAllTexturesEqual(JsonObject tex, String t) {
for (var direction : Direction.values()) {
if (!tex.get(direction.getSerializedName()).getAsString().equals(t)) {
return false;
Expand Down Expand Up @@ -408,15 +427,15 @@ public BlockBuilder box(double x0, double y0, double z0, double x1, double y1, d
return box(x0, y0, z0, x1, y1, z1, true);
}

public VoxelShape createShape() {
if (customShape.isEmpty()) {
public static VoxelShape createShape(List<AABB> boxes) {
if (boxes.isEmpty()) {
return Shapes.block();
}

var shape = Shapes.create(customShape.get(0));
var shape = Shapes.create(boxes.get(0));

for (var i = 1; i < customShape.size(); i++) {
shape = Shapes.or(shape, Shapes.create(customShape.get(i)));
for (var i = 1; i < boxes.size(); i++) {
shape = Shapes.or(shape, Shapes.create(boxes.get(i)));
}

return shape;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Block createObject() {
public BasicBlockJS(BlockBuilder p) {
super(p.createProperties());
blockBuilder = p;
shape = p.createShape();
shape = BlockBuilder.createShape(p.customShape);

var blockState = stateDefinition.any();
if (blockBuilder.defaultStateModification != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.latvian.mods.kubejs.block.MaterialListJS;
import dev.latvian.mods.kubejs.block.RandomTickCallbackJS;
import dev.latvian.mods.kubejs.block.SeedItemBuilder;
import dev.latvian.mods.kubejs.client.VariantBlockStateGenerator;
import dev.latvian.mods.kubejs.generator.AssetJsonGenerator;
import dev.latvian.mods.kubejs.item.ItemStackJS;
import dev.latvian.mods.kubejs.level.BlockContainerJS;
Expand Down Expand Up @@ -202,30 +203,22 @@ public BlockBuilder randomTick(@Nullable Consumer<RandomTickCallbackJS> randomTi
}

@Override
public void generateAssetJsons(AssetJsonGenerator generator) {
if (blockstateJson != null) {
generator.json(newID("blockstates/", ""), blockstateJson);
} else {
generator.blockState(id, bs -> {
for (int i = 0; i <= age; i++) {
bs.variant("age=%s".formatted(i), model.isEmpty() ? (id.getNamespace() + ":block/" + id.getPath() + i) : model);
}
});
}
if (modelJson != null) {
generator.json(newID("models/block/", ""), modelJson);
} else {
for (int i = 0; i <= age; i++) {
final int fi = i;
generator.blockModel(newID("", String.valueOf(i)), m -> {
m.parent("minecraft:block/crop");
m.texture("crop", textures.get(String.valueOf(fi)).getAsString());
});
}
protected void generateBlockStateJson(VariantBlockStateGenerator bs) {
for (int i = 0; i <= age; i++) {
bs.variant("age=%s".formatted(i), model.isEmpty() ? (id.getNamespace() + ":block/" + id.getPath() + i) : model);
}
if (itemBuilder != null) {
itemBuilder.generateAssetJsons(generator);
}

@Override
protected void generateBlockModelJsons(AssetJsonGenerator generator) {
for (int i = 0; i <= age; i++) {
final int fi = i;
generator.blockModel(newID("", String.valueOf(i)), m -> {
m.parent("minecraft:block/crop");
m.texture("crop", textures.get(String.valueOf(fi)).getAsString());
});
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package dev.latvian.mods.kubejs.block.custom;

import dev.architectury.platform.Platform;
import dev.latvian.mods.kubejs.client.ModelGenerator;
import dev.latvian.mods.kubejs.client.MultipartBlockStateGenerator;
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.FenceBlock;

public class FenceBlockBuilder extends ShapedBlockBuilder {
public class FenceBlockBuilder extends MultipartShapedBlockBuilder {
public FenceBlockBuilder(ResourceLocation i) {
super(i, "_fence");

Expand All @@ -24,33 +26,34 @@ public Block createObject() {
}

@Override
public void generateAssetJsons(AssetJsonGenerator generator) {
generator.multipartState(id, bs -> {
var modPost = newID("block/", "_post").toString();
var modSide = newID("block/", "_side").toString();

bs.part("", modPost);
bs.part("north=true", p -> p.model(modSide).uvlock());
bs.part("east=true", p -> p.model(modSide).uvlock().y(90));
bs.part("south=true", p -> p.model(modSide).uvlock().y(180));
bs.part("west=true", p -> p.model(modSide).uvlock().y(270));
});
protected void generateMultipartBlockStateJson(MultipartBlockStateGenerator bs) {
var modPost = newID("block/", "_post").toString();
var modSide = newID("block/", "_side").toString();

bs.part("", modPost);
bs.part("north=true", p -> p.model(modSide).uvlock());
bs.part("east=true", p -> p.model(modSide).uvlock().y(90));
bs.part("south=true", p -> p.model(modSide).uvlock().y(180));
bs.part("west=true", p -> p.model(modSide).uvlock().y(270));
}

@Override
protected void generateItemModelJson(ModelGenerator m) {
m.parent("minecraft:block/fence_inventory");
m.texture("texture", textures.get("texture").getAsString());
}

final var texture = textures.get("texture").getAsString();
@Override
protected void generateBlockModelJsons(AssetJsonGenerator generator) {
var texture = textures.get("texture").getAsString();

generator.blockModel(newID("", "_post"), m -> {
m.parent("minecraft:block/fence_post");
m.texture("texture", texture);
});

generator.blockModel(newID("", "_side"), m -> {
m.parent("minecraft:block/fence_side");
m.texture("texture", texture);
});

generator.itemModel(itemBuilder.id, m -> {
m.parent("minecraft:block/fence_inventory");
m.texture("texture", texture);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.latvian.mods.kubejs.block.custom;

import dev.architectury.platform.Platform;
import dev.latvian.mods.kubejs.client.ModelGenerator;
import dev.latvian.mods.kubejs.client.VariantBlockStateGenerator;
import dev.latvian.mods.kubejs.generator.AssetJsonGenerator;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BlockTags;
Expand All @@ -23,32 +25,33 @@ public Block createObject() {
}

@Override
public void generateAssetJsons(AssetJsonGenerator generator) {
generator.blockState(id, bs -> {
var mod = newID("block/", "").toString();
var modOpen = newID("block/", "_open").toString();
var modWall = newID("block/", "_wall").toString();
var modWallOpen = newID("block/", "_wall_open").toString();
protected void generateBlockStateJson(VariantBlockStateGenerator bs) {
var mod = newID("block/", "").toString();
var modOpen = newID("block/", "_open").toString();
var modWall = newID("block/", "_wall").toString();
var modWallOpen = newID("block/", "_wall_open").toString();

bs.variant("facing=east,in_wall=false,open=false", v -> v.model(mod).y(270).uvlock());
bs.variant("facing=east,in_wall=false,open=true", v -> v.model(modOpen).y(270).uvlock());
bs.variant("facing=east,in_wall=true,open=false", v -> v.model(modWall).y(270).uvlock());
bs.variant("facing=east,in_wall=true,open=true", v -> v.model(modWallOpen).y(270).uvlock());
bs.variant("facing=north,in_wall=false,open=false", v -> v.model(mod).y(180).uvlock());
bs.variant("facing=north,in_wall=false,open=true", v -> v.model(modOpen).y(180).uvlock());
bs.variant("facing=north,in_wall=true,open=false", v -> v.model(modWall).y(180).uvlock());
bs.variant("facing=north,in_wall=true,open=true", v -> v.model(modWallOpen).y(180).uvlock());
bs.variant("facing=south,in_wall=false,open=false", v -> v.model(mod).y(0).uvlock());
bs.variant("facing=south,in_wall=false,open=true", v -> v.model(modOpen).y(0).uvlock());
bs.variant("facing=south,in_wall=true,open=false", v -> v.model(modWall).y(0).uvlock());
bs.variant("facing=south,in_wall=true,open=true", v -> v.model(modWallOpen).y(0).uvlock());
bs.variant("facing=west,in_wall=false,open=false", v -> v.model(mod).y(90).uvlock());
bs.variant("facing=west,in_wall=false,open=true", v -> v.model(modOpen).y(90).uvlock());
bs.variant("facing=west,in_wall=true,open=false", v -> v.model(modWall).y(90).uvlock());
bs.variant("facing=west,in_wall=true,open=true", v -> v.model(modWallOpen).y(90).uvlock());
});
bs.variant("facing=east,in_wall=false,open=false", v -> v.model(mod).y(270).uvlock());
bs.variant("facing=east,in_wall=false,open=true", v -> v.model(modOpen).y(270).uvlock());
bs.variant("facing=east,in_wall=true,open=false", v -> v.model(modWall).y(270).uvlock());
bs.variant("facing=east,in_wall=true,open=true", v -> v.model(modWallOpen).y(270).uvlock());
bs.variant("facing=north,in_wall=false,open=false", v -> v.model(mod).y(180).uvlock());
bs.variant("facing=north,in_wall=false,open=true", v -> v.model(modOpen).y(180).uvlock());
bs.variant("facing=north,in_wall=true,open=false", v -> v.model(modWall).y(180).uvlock());
bs.variant("facing=north,in_wall=true,open=true", v -> v.model(modWallOpen).y(180).uvlock());
bs.variant("facing=south,in_wall=false,open=false", v -> v.model(mod).y(0).uvlock());
bs.variant("facing=south,in_wall=false,open=true", v -> v.model(modOpen).y(0).uvlock());
bs.variant("facing=south,in_wall=true,open=false", v -> v.model(modWall).y(0).uvlock());
bs.variant("facing=south,in_wall=true,open=true", v -> v.model(modWallOpen).y(0).uvlock());
bs.variant("facing=west,in_wall=false,open=false", v -> v.model(mod).y(90).uvlock());
bs.variant("facing=west,in_wall=false,open=true", v -> v.model(modOpen).y(90).uvlock());
bs.variant("facing=west,in_wall=true,open=false", v -> v.model(modWall).y(90).uvlock());
bs.variant("facing=west,in_wall=true,open=true", v -> v.model(modWallOpen).y(90).uvlock());
}

final var texture = textures.get("texture").getAsString();
@Override
protected void generateBlockModelJsons(AssetJsonGenerator generator) {
var texture = textures.get("texture").getAsString();

generator.blockModel(id, m -> {
m.parent("minecraft:block/template_fence_gate");
Expand All @@ -69,10 +72,11 @@ public void generateAssetJsons(AssetJsonGenerator generator) {
m.parent("minecraft:block/template_fence_gate_wall_open");
m.texture("texture", texture);
});
}

generator.itemModel(itemBuilder.id, m -> {
m.parent("minecraft:block/template_fence_gate");
m.texture("texture", texture);
});
@Override
protected void generateItemModelJson(ModelGenerator m) {
m.parent("minecraft:block/template_fence_gate");
m.texture("texture", textures.get("texture").getAsString());
}
}
Loading

0 comments on commit 43bfb90

Please sign in to comment.