Skip to content

Commit

Permalink
Merge pull request #36 from maximumpower55/staticdata-named
Browse files Browse the repository at this point in the history
Add a name field to staticdata and use reflection
  • Loading branch information
maximumpower55 authored Jun 24, 2023
2 parents 815b27e + d4fe35b commit 65df10d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void onInitialize(ModContainer mod) {
throw new RuntimeException("There was a problem getting staticdata for mod container '"+item.getModId()+"' with resource id '"+item.getResourceId()+"'.", ex);
}
}

// Start resolving deferred blocks when their base-blocks appear
DeferredRegistration.init();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static void register(String modId, BlockGroup group, BlockGroup.GroupedBl

if (!deferral.register()) {
//ArchitectureExtensions.LOGGER.info("Deferred generation: "+deferral.modId()+" requested "+deferral.getIds()+" and registration was deferred.");
deferrals.put(groupedBlock.id(), deferral);
deferrals.put(groupedBlock.baseBlockId(), deferral);
} else {
//ArchitectureExtensions.LOGGER.info("Deferred generation: "+deferral.modId()+" requested "+deferral.getIds()+" and registration was completed immediately.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import net.minecraft.block.Block;
import net.minecraft.block.MapColor;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;

public final class BlockGroup implements Iterable<BlockGroup.GroupedBlock> {
Expand All @@ -32,29 +33,29 @@ public Iterator<BlockGroup.GroupedBlock> iterator() {
return groupedBlocks.iterator();
}

public record GroupedBlock(Identifier id, Supplier<Block> baseBlock, TextureConfiguration textureConfiguration, RecipeConfigurator recipeConfigurator, Optional<MapColor> mapColor) {
public GroupedBlock(String id, Supplier<Block> baseBlock, TextureConfiguration textureConfiguration, RecipeConfigurator recipeConfigurator, MapColor mapColor) {
this(new Identifier(id), baseBlock, textureConfiguration, recipeConfigurator, Optional.of(mapColor));
public record GroupedBlock(Identifier id, Identifier baseBlockId, Supplier<Block> baseBlock, TextureConfiguration textureConfiguration, RecipeConfigurator recipeConfigurator, Optional<MapColor> mapColor) {
public GroupedBlock(String id, Identifier baseBlockId, Supplier<Block> baseBlock, TextureConfiguration textureConfiguration, RecipeConfigurator recipeConfigurator, MapColor mapColor) {
this(new Identifier(id), baseBlockId, baseBlock, textureConfiguration, recipeConfigurator, Optional.of(mapColor));
}

public GroupedBlock(String id, Block baseBlock, TextureConfiguration textureConfiguration, RecipeConfigurator recipeConfigurator, MapColor mapColor) {
this(new Identifier(id), ()->baseBlock, textureConfiguration, recipeConfigurator, Optional.of(mapColor));
this(new Identifier(id), Registries.BLOCK.getId(baseBlock), ()->baseBlock, textureConfiguration, recipeConfigurator, Optional.of(mapColor));
}

public GroupedBlock(Identifier id, Supplier<Block> baseBlock, TextureConfiguration textureConfiguration, RecipeConfigurator recipeConfigurator, MapColor mapColor) {
this(id, baseBlock, textureConfiguration, recipeConfigurator, Optional.of(mapColor));
public GroupedBlock(Identifier id, Identifier baseBlockId, Supplier<Block> baseBlock, TextureConfiguration textureConfiguration, RecipeConfigurator recipeConfigurator, MapColor mapColor) {
this(id, baseBlockId, baseBlock, textureConfiguration, recipeConfigurator, Optional.of(mapColor));
}

public GroupedBlock(String id, Supplier<Block> baseBlock, TextureConfiguration textureConfiguration, RecipeConfigurator recipeConfigurator) {
this(new Identifier(id), baseBlock, textureConfiguration, recipeConfigurator, Optional.empty());
public GroupedBlock(String id, Identifier baseBlockId, Supplier<Block> baseBlock, TextureConfiguration textureConfiguration, RecipeConfigurator recipeConfigurator) {
this(new Identifier(id), baseBlockId, baseBlock, textureConfiguration, recipeConfigurator, Optional.empty());
}

public GroupedBlock(String id, Block baseBlock, TextureConfiguration textureConfiguration, RecipeConfigurator recipeConfigurator) {
this(new Identifier(id), ()->baseBlock, textureConfiguration, recipeConfigurator, Optional.empty());
this(new Identifier(id), Registries.BLOCK.getId(baseBlock), ()->baseBlock, textureConfiguration, recipeConfigurator, Optional.empty());
}

public GroupedBlock(Identifier id, Supplier<Block> baseBlock, TextureConfiguration textureConfiguration, RecipeConfigurator recipeConfigurator) {
this(id, baseBlock, textureConfiguration, recipeConfigurator, Optional.empty());
public GroupedBlock(Identifier id, Identifier baseBlockId, Supplier<Block> baseBlock, TextureConfiguration textureConfiguration, RecipeConfigurator recipeConfigurator) {
this(id, baseBlockId, baseBlock, textureConfiguration, recipeConfigurator, Optional.empty());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.github.debuggyteam.architecture_extensions.staticdata;

import java.util.HashSet;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;

import com.google.gson.GsonBuilder;
Expand All @@ -20,6 +22,7 @@
import net.minecraft.util.Identifier;

public class BlockGroupSchema {
public String name;
public String base_block;
public String textures;
public String recipes;
Expand All @@ -28,60 +31,46 @@ public class BlockGroupSchema {
public String only_if_present;

public BlockGroupSchema() {
name = null;
base_block = "minecraft:air";
types_to_generate = new String[0];
}

@SuppressWarnings("unchecked")
private static <T> Optional<T> reflectField(Class<?> originClass, String name) {
try {
return Optional.of((T) originClass.getDeclaredField(name).get(null));
} catch (Exception e) {
return Optional.empty();
}
}

public BlockGroup createBlockGroup() {
Identifier baseId = new Identifier(base_block);
Identifier baseBlockId = new Identifier(base_block);
Supplier<Block> getter = () -> {
Block b = Registries.BLOCK.get(baseId);
Block b = Registries.BLOCK.get(baseBlockId);
return (b == Blocks.AIR) ? null : b;
};

TextureConfiguration textureConfig = (textures.contains(":")) ?
TextureConfiguration.create(it->textures, it->textures, it->textures, it->textures) :
switch(textures) {
case "sided" -> TextureConfiguration.SIDED.apply(baseId);
case "wood_with_log" -> TextureConfiguration.WOOD_WITH_LOG.apply(baseId);
case "wood_with_stem" -> TextureConfiguration.WOOD_WITH_STEM.apply(baseId);
case "top" -> TextureConfiguration.TOP.apply(baseId);
case "top_bottom" -> TextureConfiguration.TOP_BOTTOM.apply(baseId);
default -> TextureConfiguration.TOP.apply(baseId);
};
RecipeConfigurator recipeConfig = switch(recipes) {
case "stonecutting" -> RecipeConfigurator.STONECUTTER;
case "sawing" -> RecipeConfigurator.SAWING;
case "crafting" -> RecipeConfigurator.CRAFTING;
default -> RecipeConfigurator.STONECUTTER;
};
Identifier baseId = baseBlockId;
if (name != null) baseId = new Identifier(baseBlockId.getNamespace(), name);

TextureConfiguration textureConfig = (textures.contains(":")) ? TextureConfiguration.create(it->textures, it->textures, it->textures, it->textures) :
BlockGroupSchema.<Function<Identifier, TextureConfiguration>>reflectField(TextureConfiguration.class, textures.toUpperCase(Locale.ROOT))
.orElse(TextureConfiguration.TOP).apply(baseId);
RecipeConfigurator recipeConfig = BlockGroupSchema.<RecipeConfigurator>reflectField(RecipeConfigurator.class, recipes.toUpperCase(Locale.ROOT))
.orElse(RecipeConfigurator.STONECUTTER);
Optional<MapColor> mapColor = MapColors.byName(map_color);

return BlockGroup.of(
new BlockGroup.GroupedBlock(baseId, getter, textureConfig, recipeConfig, mapColor)
new BlockGroup.GroupedBlock(baseId, baseBlockId, getter, textureConfig, recipeConfig, mapColor)
);
}

public Set<BlockType> getBlockTypes() {
Set<BlockType> result = new HashSet<>();

for(String s : types_to_generate) {
BlockType blockType = switch(s) {
case "arch" -> BlockType.ARCH;
case "beam" -> BlockType.BEAM;
case "h_beam" -> BlockType.H_BEAM;
case "wall_column" -> BlockType.WALL_COLUMN;
case "fence_post" -> BlockType.FENCE_POST;
case "joist" -> BlockType.JOIST;
case "crown_molding" -> BlockType.CROWN_MOLDING;
case "post_cap" -> BlockType.POST_CAP;
case "post_lantern" -> BlockType.POST_LANTERN;
case "lantern" -> BlockType.POST_LANTERN;
case "rod" -> BlockType.ROD;
case "roof" -> BlockType.ROOF;
case "wall_post" -> BlockType.WALL_POST;
default -> null;
};
BlockType blockType = BlockGroupSchema.<BlockType>reflectField(BlockType.class, s.toUpperCase(Locale.ROOT)).orElse(null);
if (blockType != null) {
result.add(blockType);
} else {
Expand Down

0 comments on commit 65df10d

Please sign in to comment.