Add stair, slab and wall blocks for all metal storage blocks
This commit is contained in:
parent
4c400dd9a4
commit
d92343f5a7
443 changed files with 10811 additions and 33 deletions
|
@ -0,0 +1,73 @@
|
|||
package techreborn.init.template;
|
||||
|
||||
import com.mojang.brigadier.Command;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.minecraft.text.LiteralText;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import techreborn.init.TRContent;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.mojang.brigadier.arguments.StringArgumentType.greedyString;
|
||||
import static net.minecraft.server.command.CommandManager.argument;
|
||||
import static net.minecraft.server.command.CommandManager.literal;
|
||||
|
||||
public class TechRebornTemplates {
|
||||
|
||||
public static void init() {
|
||||
CommandRegistrationCallback.EVENT.register((dispatcher, b) -> dispatcher.register(
|
||||
literal("techreborn")
|
||||
.then(literal("template")
|
||||
.requires(source -> source.hasPermissionLevel(3))
|
||||
.requires(source -> FabricLoader.getInstance().isDevelopmentEnvironment())
|
||||
.then(literal("generate")
|
||||
.then(
|
||||
argument("path", greedyString())
|
||||
.executes(TechRebornTemplates::process)
|
||||
)
|
||||
)
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
private static int process(CommandContext<ServerCommandSource> ctx) {
|
||||
Path path = Paths.get(StringArgumentType.getString(ctx, "path"));
|
||||
TemplateProcessor processor = new TemplateProcessor(path);
|
||||
|
||||
for (TRContent.StorageBlocks value : TRContent.StorageBlocks.values()) {
|
||||
System.out.println(Registry.BLOCK.getId(value.block));
|
||||
}
|
||||
|
||||
if (true) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
try {
|
||||
process(processor);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
ctx.getSource().sendError(new LiteralText(e.getMessage()));
|
||||
return 0;
|
||||
}
|
||||
|
||||
ctx.getSource().sendFeedback(new LiteralText("done"), true);
|
||||
|
||||
return Command.SINGLE_SUCCESS;
|
||||
}
|
||||
|
||||
private static void process(TemplateProcessor processor) throws IOException {
|
||||
processor.processSimpleBlocks("storage_blocks", Arrays.stream(TRContent.StorageBlocks.values())
|
||||
.map(storageBlocks -> storageBlocks.block)
|
||||
.collect(Collectors.toList())
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
package techreborn.init.template;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class TemplateProcessor {
|
||||
|
||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
||||
|
||||
private final Path resources;
|
||||
|
||||
public TemplateProcessor(Path resources) {
|
||||
this.resources = resources;
|
||||
}
|
||||
|
||||
public void processSimpleBlocks(String template, List<Block> blocks) throws IOException {
|
||||
for (Block block : blocks) {
|
||||
Map<String, String> values = new HashMap<>();
|
||||
values.put("name", Registry.BLOCK.getId(block).getPath());
|
||||
|
||||
process(template, values);
|
||||
}
|
||||
}
|
||||
|
||||
public void process(String template, Map<String, String> values) throws IOException {
|
||||
Path directory = resources.resolve("templates").resolve(template);
|
||||
JsonObject info = getJson(directory.resolve("info.json"));
|
||||
|
||||
JsonArray files = info.getAsJsonArray("files");
|
||||
for (JsonElement fileElement : files) {
|
||||
JsonObject file = fileElement.getAsJsonObject();
|
||||
Path inputFile = directory.resolve(file.get("from").getAsString());
|
||||
Path outputFile = resources.resolve(replaceValues(file.get("to").getAsString(), values));
|
||||
|
||||
processFile(inputFile, outputFile, values);
|
||||
}
|
||||
}
|
||||
|
||||
private void processFile(Path inputFile, Path outputFile, Map<String, String> values) throws IOException {
|
||||
String input = new String(Files.readAllBytes(inputFile), StandardCharsets.UTF_8);
|
||||
String output = replaceValues(input, values);
|
||||
Files.write(outputFile, output.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private static String replaceValues(String input, Map<String, String> values) {
|
||||
for (Map.Entry<String, String> entry : values.entrySet()) {
|
||||
input = input.replaceAll("%" + entry.getKey() + "%", entry.getValue());
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
private JsonObject getJson(Path path) throws IOException {
|
||||
if (!Files.exists(path)) {
|
||||
throw new FileNotFoundException("Failed to find " + path.toString());
|
||||
}
|
||||
|
||||
try (InputStream stream = Files.newInputStream(path)) {
|
||||
return GSON.fromJson(new InputStreamReader(stream), JsonObject.class);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue