Skip to content

Commit

Permalink
Improve structure of generations
Browse files Browse the repository at this point in the history
  • Loading branch information
Griefed committed Nov 8, 2023
1 parent e8368c6 commit 332ebc7
Show file tree
Hide file tree
Showing 43 changed files with 142 additions and 121 deletions.
5 changes: 1 addition & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import de.griefed.generation.BlockAndItemCodeGenerator

plugins {
id 'fabric-loom' version '1.3-SNAPSHOT' apply(false)
id 'net.minecraftforge.gradle' version '[6.0,6.2)' apply(false)
id 'org.spongepowered.gradle.vanilla' version '0.2.1-SNAPSHOT' apply(false)
id("org.spongepowered.mixin") version "0.7-SNAPSHOT" apply(false)
id 'de.griefed.generation.block-and-item-code-generator'
}

apply plugin: BlockAndItemCodeGenerator

subprojects {
apply plugin: 'java'

Expand Down
9 changes: 9 additions & 0 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'java'
id 'java-gradle-plugin'
}

group = 'de.griefed'
Expand All @@ -19,3 +20,11 @@ test {
useJUnitPlatform()
}

gradlePlugin {
plugins {
create("block-and-item-code-generator") {
id = "de.griefed.generation.block-and-item-code-generator"
implementationClass = "de.griefed.generation.BlockAndItemCodeGenerator"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public abstract class CodeGenerator {
private static final String BLOCKSTATE_TEMPLATE = """
{
"variants": {
"": { "model": "MODID:block/BLOCKID_block" }
"": { "model": "%s:block/generated/%s/%s_block" }
}
}
""";
Expand All @@ -32,14 +32,14 @@ public abstract class CodeGenerator {
{
"parent": "block/cube_all",
"textures": {
"all": "MODID:block/BLOCKID_block"
"all": "%s:block/generated/%s/%s_block"
}
}
""";

private static final String BLOCK_MODELS_ITEM_TEMPLATE = """
{
"parent": "MODID:block/BLOCKID_block"
"parent": "%s:block/generated/%s/%s_block"
}
""";

Expand Down Expand Up @@ -157,12 +157,16 @@ protected void updateTranslations() throws IOException {
} else {
//translations not from our generation, add our block
ObjectNode translationNode = (ObjectNode) objectMapper.readTree(this.translationsFile);
String itemPrefix = "\"item." + id + ".";
String blockPrefix = "\"block." + id + ".";
String itemPrefix = "\"item.%s.generated.%s.%s_block";
String blockPrefix = "\"block.%s.generated.%s.%s_block\"";
translationNode.put("DO.NOT.EDIT.MANUALLY.BEGIN", "BEGIN");
for (BlockDefinition block : blockDefinitionParser.getBlocks()) {
translationNode.put(itemPrefix + block.getId() + "_block", block.getTranslation());
translationNode.put(blockPrefix + block.getId() + "_block", block.getTranslation());
translationNode.put(
String.format(itemPrefix, id, block.getMaterial().toLowerCase(), block.getId()),
block.getTranslation());
translationNode.put(
String.format(blockPrefix, id, block.getMaterial().toLowerCase(), block.getId()),
block.getTranslation());
}
translationNode.put("DO.NOT.EDIT.MANUALLY.END", "END");
objectMapper.writeValue(this.translationsFile, translationNode);
Expand All @@ -172,17 +176,17 @@ protected void updateTranslations() throws IOException {

private StringBuilder buildTranslationText() {
StringBuilder translations = new StringBuilder();
String itemPrefix = "\"item." + id + ".";
String blockPrefix = "\"block." + id + ".";
String itemPrefix = "\"item.%s.generated.%s.%s_block";
String blockPrefix = "\"block.%s.generated.%s.%s_block";
for (BlockDefinition block : blockDefinitionParser.getBlocks()) {
//add item
//key
translations.append("\n ").append(itemPrefix).append(block.getId()).append("_block").append("\":");
translations.append("\n ").append(String.format(itemPrefix, id, block.getMaterial().toLowerCase(), block.getId())).append("\":");
//value
translations.append(" \"").append(block.getTranslation()).append("\",");
//add block
//key
translations.append("\n ").append(blockPrefix).append(block.getId()).append("_block").append("\":");
translations.append("\n ").append(String.format(blockPrefix, id, block.getMaterial().toLowerCase(), block.getId())).append("\":");
//value
translations.append(" \"").append(block.getTranslation()).append("\",\n");
}
Expand All @@ -206,10 +210,10 @@ public void createBlockFiles() throws IOException {
itemModelsDir.mkdirs();
itemTexturesDir.mkdirs();
blockTexturesDir.mkdirs();
String blockstatesTemp = "BLOCKID_block.json";
String blockModelTemp = "BLOCKID_block.json";
String itemBlockModelTemp = "BLOCKID.json";
String blockTextureTemp = "BLOCKID_block.png";
String blockstatesTemp = "generated/%s/%s_block.json";
String blockModelTemp = "generated/%s/%s_block.json";
String itemBlockModelTemp = "generated/%s/%s.json";
String blockTextureTemp = "generated/%s/%s_block.png";
File blockstate;
File blockModel;
File itemBlockModel;
Expand All @@ -219,21 +223,28 @@ public void createBlockFiles() throws IOException {
BufferedImage texture;
for (BlockDefinition block : blockDefinitionParser.getBlocks()) {
//blockstate
blockstate = new File(blockstatesDir, blockstatesTemp.replace("BLOCKID", block.getId()));
writeToFile(blockstate, BLOCKSTATE_TEMPLATE.replace("MODID", id).replace("BLOCKID", block.getId()));
blockstate = new File(blockstatesDir, String.format(blockstatesTemp, block.getMaterial().toLowerCase(), block.getId()));
blockstate.getParentFile().mkdirs();
writeToFile(blockstate, String.format(BLOCKSTATE_TEMPLATE, id, block.getMaterial().toLowerCase(), block.getId()));

//block model
blockModel = new File(blockModelsDir, blockModelTemp.replace("BLOCKID", block.getId()));
writeToFile(blockModel, BLOCK_MODELS_TEMPLATE.replace("MODID", id).replace("BLOCKID", block.getId()));
blockModel = new File(blockModelsDir, String.format(blockModelTemp, block.getMaterial().toLowerCase(), block.getId()));
blockModel.getParentFile().mkdirs();
writeToFile(blockModel, String.format(BLOCK_MODELS_TEMPLATE, id, block.getMaterial().toLowerCase(), block.getId()));

//item block model
itemBlockModel = new File(itemModelsDir, itemBlockModelTemp.replace("BLOCKID", block.getId()));
writeToFile(itemBlockModel, BLOCK_MODELS_ITEM_TEMPLATE.replace("MODID", id).replace("BLOCKID", block.getId()));
itemBlockModel = new File(itemModelsDir, String.format(itemBlockModelTemp, block.getMaterial().toLowerCase(), block.getId()));
itemBlockModel.getParentFile().mkdirs();
writeToFile(itemBlockModel, String.format(BLOCK_MODELS_ITEM_TEMPLATE, id, block.getMaterial().toLowerCase(), block.getId()));

//block texture
blockTexture = new File(blockTexturesDir, blockTextureTemp.replace("BLOCKID", block.getId()));
blockTexture = new File(blockTexturesDir, String.format(blockTextureTemp, block.getMaterial().toLowerCase(), block.getId()));
blockTexture.getParentFile().mkdirs();
textureSource = new File(blockDefinitionParser.getAssetsDirectory(), block.getId() + ".png");
texture = ImageIO.read(textureSource);
if (texture.getWidth() > 16 && texture.getHeight() > 16) {
texture = TextureScaler.getScaledInstance(texture);
ImageIO.write(texture,"png",textureSource);
ImageIO.write(texture, "png", textureSource);
}
if (texture.getWidth() < texture.getHeight()) {
textureMCMeta = new File(blockTexture.getParent(), blockTexture.getName() + ".mcmeta");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ public static void onItemTooltip(ItemStack stack, TooltipFlag context, List<Comp
.replace("MODID", getId());

private static final String BLOCK_REGISTRATION_TEMPLATE = """
\tpublic static final RegistryObject<Block> %s = BLOCKS.register("%s_block",
\tpublic static final RegistryObject<Block> %s = BLOCKS.register("generated/%s/%s_block",
\t\t\t() -> new Block(BlockBehaviour.Properties.of(Material.%s).sound(SoundType.%s)
\t.strength(%df, %df).lightLevel(state -> %d).explosionResistance(%df)TOOLBREAK));
""";

private static final String BLOCK_ITEM_REGISTRATION_TEMPLATE = """
\tpublic static final RegistryObject<Item> %s_ITEM = ITEMS.register("%s",
\tpublic static final RegistryObject<Item> %s_ITEM = ITEMS.register("generated/%s/%s",
\t\t() -> new BlockItem(%s.get(), itemBuilder()));
""";

Expand Down Expand Up @@ -193,7 +193,7 @@ public void updateModBlocksClass() throws IOException {
//block
blockToRegister = String.format(
BLOCK_REGISTRATION_TEMPLATE,
block.getId().toUpperCase(), block.getId(),
block.getId().toUpperCase(), block.getMaterial().toLowerCase(), block.getId(),
block.getMaterial(), block.getSoundType(), block.getStrengthOne(), block.getStrengthTwo(),
block.getLightLevel(), block.getExplosionResistance());
if (block.isRequiresCorrectTool()) {
Expand All @@ -210,7 +210,7 @@ public void updateModBlocksClass() throws IOException {
//block item
blockItemToRegister = String.format(
BLOCK_ITEM_REGISTRATION_TEMPLATE,
block.getId().toUpperCase(), block.getId(), block.getId().toUpperCase()
block.getId().toUpperCase(), block.getMaterial().toLowerCase(), block.getId(), block.getId().toUpperCase()
);

//add block and block item
Expand Down
4 changes: 4 additions & 0 deletions common/src/main/java/de/griefed/addemall/CommonClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import de.griefed.addemall.platform.Services;
import net.minecraft.core.Registry;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.block.Block;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,39 @@ public class GeneratedModBlocks {

/*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/

public static final RegistryObject<Block> GREEN_ZEN = BLOCKS.register("green_zen_block",
public static final RegistryObject<Block> GREEN_ZEN = BLOCKS.register("generated/dirt/green_zen_block",
() -> new Block(BlockBehaviour.Properties.of(Material.DIRT).sound(SoundType.GRASS)
.strength(2f, 2f).lightLevel(state -> 4).explosionResistance(0f)));

public static final RegistryObject<Item> GREEN_ZEN_ITEM = ITEMS.register("green_zen",
public static final RegistryObject<Item> GREEN_ZEN_ITEM = ITEMS.register("generated/dirt/green_zen",
() -> new BlockItem(GREEN_ZEN.get(), itemBuilder()));

public static final RegistryObject<Block> YELLOW_ZEN = BLOCKS.register("yellow_zen_block",
public static final RegistryObject<Block> YELLOW_ZEN = BLOCKS.register("generated/dirt/yellow_zen_block",
() -> new Block(BlockBehaviour.Properties.of(Material.DIRT).sound(SoundType.GRASS)
.strength(2f, 2f).lightLevel(state -> 4).explosionResistance(0f)));

public static final RegistryObject<Item> YELLOW_ZEN_ITEM = ITEMS.register("yellow_zen",
public static final RegistryObject<Item> YELLOW_ZEN_ITEM = ITEMS.register("generated/dirt/yellow_zen",
() -> new BlockItem(YELLOW_ZEN.get(), itemBuilder()));

public static final RegistryObject<Block> BLUE_ZEN = BLOCKS.register("blue_zen_block",
public static final RegistryObject<Block> BLUE_ZEN = BLOCKS.register("generated/dirt/blue_zen_block",
() -> new Block(BlockBehaviour.Properties.of(Material.DIRT).sound(SoundType.GRASS)
.strength(2f, 2f).lightLevel(state -> 4).explosionResistance(0f)));

public static final RegistryObject<Item> BLUE_ZEN_ITEM = ITEMS.register("blue_zen",
public static final RegistryObject<Item> BLUE_ZEN_ITEM = ITEMS.register("generated/dirt/blue_zen",
() -> new BlockItem(BLUE_ZEN.get(), itemBuilder()));

public static final RegistryObject<Block> BROWN_ZEN = BLOCKS.register("brown_zen_block",
public static final RegistryObject<Block> BROWN_ZEN = BLOCKS.register("generated/dirt/brown_zen_block",
() -> new Block(BlockBehaviour.Properties.of(Material.DIRT).sound(SoundType.GRASS)
.strength(2f, 2f).lightLevel(state -> 4).explosionResistance(0f)));

public static final RegistryObject<Item> BROWN_ZEN_ITEM = ITEMS.register("brown_zen",
public static final RegistryObject<Item> BROWN_ZEN_ITEM = ITEMS.register("generated/dirt/brown_zen",
() -> new BlockItem(BROWN_ZEN.get(), itemBuilder()));

public static final RegistryObject<Block> RED_ZEN = BLOCKS.register("red_zen_block",
public static final RegistryObject<Block> RED_ZEN = BLOCKS.register("generated/dirt/red_zen_block",
() -> new Block(BlockBehaviour.Properties.of(Material.DIRT).sound(SoundType.GRASS)
.strength(2f, 2f).lightLevel(state -> 4).explosionResistance(0f)));

public static final RegistryObject<Item> RED_ZEN_ITEM = ITEMS.register("red_zen",
public static final RegistryObject<Item> RED_ZEN_ITEM = ITEMS.register("generated/dirt/red_zen",
() -> new BlockItem(RED_ZEN.get(), itemBuilder()));

/*###GENERATED CODE - DO NOT EDIT - MANUALLY EDITED CODE WILL BE LOST###*/
Expand Down
Binary file modified common/src/main/resources/addemall.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"variants": {
"": { "model": "addemall:block/generated/dirt/blue_zen_block" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"variants": {
"": { "model": "addemall:block/generated/dirt/brown_zen_block" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"variants": {
"": { "model": "addemall:block/generated/dirt/green_zen_block" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"variants": {
"": { "model": "addemall:block/generated/dirt/red_zen_block" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"variants": {
"": { "model": "addemall:block/generated/dirt/yellow_zen_block" }
}
}

This file was deleted.

This file was deleted.

This file was deleted.

20 changes: 10 additions & 10 deletions common/src/main/resources/assets/addemall/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"DO.NOT.EDIT.MANUALLY.BEGIN": "BEGIN",
"item.addemall.green_zen_block": "Green Zen",
"block.addemall.green_zen_block": "Green Zen",
"item.addemall.generated.dirt.green_zen_block": "Green Zen",
"block.addemall.generated.dirt.green_zen_block": "Green Zen",

"item.addemall.yellow_zen_block": "Yellow Zen",
"block.addemall.yellow_zen_block": "Yellow Zen",
"item.addemall.generated.dirt.yellow_zen_block": "Yellow Zen",
"block.addemall.generated.dirt.yellow_zen_block": "Yellow Zen",

"item.addemall.blue_zen_block": "Blue Zen",
"block.addemall.blue_zen_block": "Blue Zen",
"item.addemall.generated.dirt.blue_zen_block": "Blue Zen",
"block.addemall.generated.dirt.blue_zen_block": "Blue Zen",

"item.addemall.brown_zen_block": "Brown Zen",
"block.addemall.brown_zen_block": "Brown Zen",
"item.addemall.generated.dirt.brown_zen_block": "Brown Zen",
"block.addemall.generated.dirt.brown_zen_block": "Brown Zen",

"item.addemall.red_zen_block": "Red Zen",
"block.addemall.red_zen_block": "Red Zen",
"item.addemall.generated.dirt.red_zen_block": "Red Zen",
"block.addemall.generated.dirt.red_zen_block": "Red Zen",
"DO.NOT.EDIT.MANUALLY.END": "END",
"itemGroup.addemall.tab": "AddEmAll"
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "block/cube_all",
"textures": {
"all": "addemall:block/generated/dirt/blue_zen_block"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "block/cube_all",
"textures": {
"all": "addemall:block/generated/dirt/brown_zen_block"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "block/cube_all",
"textures": {
"all": "addemall:block/generated/dirt/green_zen_block"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "block/cube_all",
"textures": {
"all": "addemall:block/generated/dirt/red_zen_block"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "block/cube_all",
"textures": {
"all": "addemall:block/generated/dirt/yellow_zen_block"
}
}
Loading

0 comments on commit 332ebc7

Please sign in to comment.