Skip to content

Commit

Permalink
feat: implement block tag
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jun 10, 2024
1 parent e21cd8e commit edc9461
Show file tree
Hide file tree
Showing 9 changed files with 670 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.allaymc.api.block.tag;

/**
* Allay Project 2024/6/11
*
* @author daoge_cmd
*/
public record BlockTag(String name) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.allaymc.api.block.component.common.BlockBaseComponent;
import org.allaymc.api.block.component.common.CustomBlockComponent;
import org.allaymc.api.block.property.type.BlockPropertyType;
import org.allaymc.api.block.tag.BlockTag;
import org.allaymc.api.blockentity.type.BlockEntityType;
import org.allaymc.api.data.VanillaBlockId;
import org.allaymc.api.utils.Identifier;
Expand Down Expand Up @@ -73,6 +74,8 @@ private Map<Identifier, BlockComponent> listComponentToMap(List<BlockComponent>

BlockTypeBuilder<T> setBlockBaseComponentSupplier(Function<BlockType<T>, BlockBaseComponent> blockBaseComponentSupplier);

BlockTypeBuilder<T> setBlockTags(BlockTag... blockTags);

BlockType<T> build();

interface BlockTypeBuilderFactory {
Expand Down
85 changes: 85 additions & 0 deletions Allay-API/src/main/java/org/allaymc/api/data/VanillaBlockTags.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.allaymc.api.data;

import java.lang.String;
import java.util.HashMap;
import java.util.Map;
import org.allaymc.api.block.tag.BlockTag;

/**
* Automatically generated by {@code org.allaymc.codegen.VanillaBlockTagGen} <br>
* Allay Project <p>
* @author daoge_cmd
*/
public interface VanillaBlockTags {
Map<String, BlockTag> NAME_TO_TAG = new HashMap<>();

BlockTag CROP = create("minecraft:crop");

BlockTag LOG = create("log");

BlockTag STONE_PICK_DIGGABLE = create("stone_pick_diggable");

BlockTag GRASS = create("grass");

BlockTag OAK = create("oak");

BlockTag WOOD = create("wood");

BlockTag DARK_OAK = create("dark_oak");

BlockTag FERTILIZE_AREA = create("fertilize_area");

BlockTag SPRUCE = create("spruce");

BlockTag GOLD_PICK_DIGGABLE = create("gold_pick_diggable");

BlockTag TRAPDOORS = create("trapdoors");

BlockTag IRON_PICK_DIGGABLE = create("iron_pick_diggable");

BlockTag BIRCH = create("birch");

BlockTag RAIL = create("rail");

BlockTag DIRT = create("dirt");

BlockTag METAL = create("metal");

BlockTag TEXT_SIGN = create("text_sign");

BlockTag WATER = create("water");

BlockTag DIAMOND_PICK_DIGGABLE = create("diamond_pick_diggable");

BlockTag STONE = create("stone");

BlockTag GRAVEL = create("gravel");

BlockTag SAND = create("sand");

BlockTag SNOW = create("snow");

BlockTag PLANT = create("plant");

BlockTag WOOD_PICK_DIGGABLE = create("wood_pick_diggable");

BlockTag ACACIA = create("acacia");

BlockTag NOT_FEATURE_REPLACEABLE = create("not_feature_replaceable");

BlockTag JUNGLE = create("jungle");

BlockTag MOB_SPAWNER = create("mob_spawner");

BlockTag PUMPKIN = create("pumpkin");

static BlockTag create(String name) {
var tag = new BlockTag(name);
NAME_TO_TAG.put(name, tag);
return tag;
}

static BlockTag getTagByName(String name) {
return NAME_TO_TAG.get(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.allaymc.codegen;

import com.google.gson.JsonParser;
import com.squareup.javapoet.*;
import lombok.SneakyThrows;
import org.allaymc.dependence.StringUtils;

import javax.lang.model.element.Modifier;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* Allay Project 2023/11/18
*
* @author daoge_cmd
*/
public class VanillaBlockTagGen {
static final Path BLOCK_TAGS_FILE_PATH = Path.of(CodeGen.DATA_PATH + "block_tags.json");
static final ClassName BLOCK_TAG_CLASS = ClassName.get("org.allaymc.api.block.tag", "BlockTag");
static final Set<String> KEYS = new HashSet<>();
static final String JAVA_DOC = """
Automatically generated by {@code org.allaymc.codegen.VanillaBlockTagGen} <br>
Allay Project <p>
@author daoge_cmd
""";

static {
try {
JsonParser
.parseReader(Files.newBufferedReader(BLOCK_TAGS_FILE_PATH))
.getAsJsonObject()
.entrySet()
.forEach(entry -> KEYS.add(entry.getKey()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static void main(String[] args) {
generate();
}

@SneakyThrows
public static void generate() {
TypeSpec.Builder codeBuilder = TypeSpec.interfaceBuilder("VanillaBlockTags")
.addJavadoc(JAVA_DOC)
.addModifiers(Modifier.PUBLIC)
.addField(
FieldSpec
.builder(ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(String.class), BLOCK_TAG_CLASS), "NAME_TO_TAG", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer("new $T<>()", HashMap.class)
.build()
)
.addMethod(
MethodSpec
.methodBuilder("create")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(BLOCK_TAG_CLASS)
.addParameter(String.class, "name")
.addStatement("var tag = new $T(name)", BLOCK_TAG_CLASS)
.addStatement("NAME_TO_TAG.put(name, tag)")
.addStatement("return tag")
.build()
)
.addMethod(
MethodSpec
.methodBuilder("getTagByName")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(BLOCK_TAG_CLASS)
.addParameter(String.class, "name")
.addStatement("return NAME_TO_TAG.get(name)")
.build()
);
var fieldNames = new HashSet<String>();
for (var key : KEYS) {
var fieldName = StringUtils.fastTwoPartSplit(key, ":", "")[1].toUpperCase();
fieldNames.add(fieldName);
codeBuilder.addField(
FieldSpec
.builder(BLOCK_TAG_CLASS, fieldName)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer("create($S)", key)
.build()
);
}

var javaFile = JavaFile.builder("org.allaymc.api.data", codeBuilder.build()).build();
Files.writeString(Path.of("Allay-API/src/main/java/org/allaymc/api/data/VanillaBlockTags.java"), javaFile.toString());
}
}
Loading

0 comments on commit edc9461

Please sign in to comment.