Skip to content

Commit

Permalink
Merge pull request #78 from emoticone11/1.18.2
Browse files Browse the repository at this point in the history
Added class for dumping compatibility data for external mods
  • Loading branch information
jacobgranberry authored Oct 2, 2024
2 parents b2c78ee + 04b5ae2 commit 22f03f9
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -451,73 +451,4 @@ public static List<RandomTextureSet> getRandomTexturesForVariant(List<RandomText

return (!randomTextures.isEmpty()) ? randomTextures : null;
}

/*
* The following allows block set information to be dumped to a json for
* the purpose of supporting external tools
*/
private static class BlockSetFileDef {
public String id = "";
public String variant = "";
}
private static class BlockSetFileSetDef {
public String id = "";
public String altname = "";
public List<BlockSetFileDef> blocks = new ArrayList<BlockSetFileDef>();
}
private static class BlockSetFile {
public List<BlockSetFileSetDef> blocksets = new ArrayList<BlockSetFileSetDef>();
}

public static void dumpBlockSets(WesterosBlockSetDef[] blockSets, Path path) {
FileWriter fos = null;
try {
// Create output file format
BlockSetFile bsf = new BlockSetFile();
for (WesterosBlockSetDef blockSet : blockSets) {
BlockSetFileSetDef bsf_set = new BlockSetFileSetDef();
bsf_set.id = WesterosBlocks.MOD_ID + ":" + blockSet.baseBlockName;
if (blockSet.baseLabel != null) {
bsf_set.altname = blockSet.baseLabel.replaceAll(" ", "_").toLowerCase();
}
// The following is duplicated from generateBlockDefs and can perhaps be refactored
for (String variant : WesterosBlockSetDef.SUPPORTED_VARIANTS) {
if (blockSet.variants != null && !blockSet.variants.contains(variant))
continue;
else if (blockSet.variants == null && !WesterosBlockSetDef.DEFAULT_VARIANTS.contains(variant))
continue;
BlockSetFileDef bsf_def = new BlockSetFileDef();
String suffix = (variant.equals("solid")) ? "" : variant;
if (blockSet.altNames != null && blockSet.altNames.containsKey(variant)) {
bsf_def.id = WesterosBlocks.MOD_ID + ":" + blockSet.altNames.get(variant);
}
else {
bsf_def.id = WesterosBlocks.MOD_ID + ":" + blockSet.baseBlockName;
if (!suffix.isEmpty())
bsf_def.id += "_" + suffix;
}
bsf_def.variant = variant;
bsf_set.blocks.add(bsf_def);
}
bsf.blocksets.add(bsf_set);
}
// Write json
fos = new FileWriter(new File(path.toFile(), "blocksets.json"));
Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
gson.toJson(bsf, fos);

} catch (IOException e) {
WesterosBlocks.log.error("Could not write blocksets.json");
return;

} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
return;
}
}
}
}
}
14 changes: 12 additions & 2 deletions src/main/java/com/westeroscraft/westerosblocks/WesterosBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.mojang.brigadier.CommandDispatcher;

import com.westeroscraft.westerosblocks.blocks.WCHalfDoorBlock;
import com.westeroscraft.westerosblocks.commands.NVCommand;
import com.westeroscraft.westerosblocks.commands.PTimeCommand;
Expand Down Expand Up @@ -326,6 +327,7 @@ public static void registerBlockColors(ColorHandlerEvent.Block event) {
// Event bus for receiving Registry Events)
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
public static class RegistryEvents {

private static boolean didInit = false;
public static void initialize() {
if (didInit) return;
Expand Down Expand Up @@ -376,10 +378,10 @@ public static void initialize() {
}
// Register custom tags
ModelExport.declareCustomTags(customConfig);
// Dump block set information
WesterosBlockSetDef.dumpBlockSets(customConfig.blockSets, modConfigPath);

log.info("initialize done");
}

@SubscribeEvent
public static void onBlocksRegistry(final RegistryEvent.Register<Block> event) {
log.info("block register start");
Expand Down Expand Up @@ -413,16 +415,24 @@ public static void onBlocksRegistry(final RegistryEvent.Register<Block> event) {
}
customBlocks = blklist.toArray(new Block[blklist.size()]);
WesterosBlockDef.dumpBlockPerf();

// Dump information for external mods
WesterosBlocksCompatibility.dumpBlockSets(customConfig.blockSets, modConfigPath);
WesterosBlocksCompatibility.dumpWorldPainterConfig(customBlocks, modConfigPath);

// Brag on block type counts
log.info("Count of custom blocks by type:");
for (String type : countsByType.keySet()) {
log.info(type + ": " + countsByType.get(type) + " blocks");
}
log.info("TOTAL: " + blockcount + " blocks");

colorMaps = customConfig.colorMaps;
menuOverrides = customConfig.menuOverrides;

log.info("block register done");
}

@SubscribeEvent
public static void onItemsRegistry(final RegistryEvent.Register<Item> event) {
log.info("item register start");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package com.westeroscraft.westerosblocks;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.StateDefinition;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

// Contains methods for generating config files and other artifacts
// needed for compatibility with external mods
public class WesterosBlocksCompatibility {

public static final String BLOCKSET_PATH = "blocksets.json";
public static final String WORLDPAINTER_PATH = "worldpainter.csv";
public static final String[] WORLDPAINTER_COLS = {
"name", "discriminator", "properties", "opacity", "receivesLight", "insubstantial",
"resource", "tileEntity", "tileEntityId", "treeRelated", "vegetation", "blockLight",
"natural", "watery", "colour", "horizontal_orientation_schemes", "vertical_orientation_scheme"
};

/*
* The following allows block set information to be dumped to a json for
* the purpose of supporting external tools
*/
private static class BlockSetFileDef {
public String id = "";
public String variant = "";
}
private static class BlockSetFileSetDef {
public String id = "";
public String altname = "";
public List<BlockSetFileDef> blocks = new ArrayList<BlockSetFileDef>();
}
private static class BlockSetFile {
public List<BlockSetFileSetDef> blocksets = new ArrayList<BlockSetFileSetDef>();
}

/*
* Dump information about block sets for external tools
*/
public static void dumpBlockSets(WesterosBlockSetDef[] blockSets, Path path) {
FileWriter fos = null;
try {
// Create output file format
BlockSetFile bsf = new BlockSetFile();
for (WesterosBlockSetDef blockSet : blockSets) {
BlockSetFileSetDef bsf_set = new BlockSetFileSetDef();
bsf_set.id = WesterosBlocks.MOD_ID + ":" + blockSet.baseBlockName;
if (blockSet.baseLabel != null) {
bsf_set.altname = blockSet.baseLabel.replaceAll(" ", "_").toLowerCase();
}
// The following is duplicated from generateBlockDefs and can perhaps be refactored
for (String variant : WesterosBlockSetDef.SUPPORTED_VARIANTS) {
if (blockSet.variants != null && !blockSet.variants.contains(variant))
continue;
else if (blockSet.variants == null && !WesterosBlockSetDef.DEFAULT_VARIANTS.contains(variant))
continue;
BlockSetFileDef bsf_def = new BlockSetFileDef();
String suffix = (variant.equals("solid")) ? "" : variant;
if (blockSet.altNames != null && blockSet.altNames.containsKey(variant)) {
bsf_def.id = WesterosBlocks.MOD_ID + ":" + blockSet.altNames.get(variant);
}
else {
bsf_def.id = WesterosBlocks.MOD_ID + ":" + blockSet.baseBlockName;
if (!suffix.isEmpty())
bsf_def.id += "_" + suffix;
}
bsf_def.variant = variant;
bsf_set.blocks.add(bsf_def);
}
bsf.blocksets.add(bsf_set);
}
// Write json
fos = new FileWriter(new File(path.toFile(), BLOCKSET_PATH));
Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
gson.toJson(bsf, fos);

} catch (IOException e) {
WesterosBlocks.log.error("Could not write "+BLOCKSET_PATH);
return;

} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
return;
}
}
}
}

/*
* Dump a CustomBlocks config file for WorldPainter
* https://www.worldpainter.net/trac/wiki/CustomBlocks
*/
public static void dumpWorldPainterConfig(Block[] blocks, Path path) {
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

for (Block blk : blocks) {
if (!(blk instanceof WesterosBlockLifecycle))
continue;

WesterosBlockDef def = ((WesterosBlockLifecycle)blk).getWBDefinition();
StateDefinition sd = blk.getStateDefinition();

Map<String, Object> row = new HashMap<String, Object>();
row.put("name", WesterosBlocks.MOD_ID + ":" + def.blockName);
// TODO
row.put("discriminator", null);
row.put("properties", null);
row.put("opacity", null);
row.put("receivesLight", null);
row.put("insubstantial", null);
row.put("resource", null);
row.put("tileEntity", null);
row.put("tileEntityId", null);
row.put("treeRelated", null);
row.put("vegetation", null);
row.put("blockLight", null);
row.put("natural", null);
row.put("watery", null);
row.put("colour", null);
row.put("horizontal_orientation_schemes", null);
row.put("vertical_orientation_scheme", null);
}

try {
File file = new File(path.toFile(), WORLDPAINTER_PATH);
writeCSV(data, WORLDPAINTER_COLS, file);
} catch (IOException e) {
WesterosBlocks.log.error("Could not write "+WORLDPAINTER_PATH);
return;
}
}

public static void writeCSV(List<Map<String, Object>> data, String[] columns, File file)
throws IOException {
String out = String.join(",", columns) + "\n";

for (Map<String, Object> d : data) {
ArrayList<String> row = new ArrayList<String>();
for (String c : columns) {
if (d.containsKey(c) && d.get(c) != null) {
Object val = d.get(c);
if (val instanceof Integer)
row.add(String.valueOf(val));
else if (val instanceof Boolean)
row.add(((Boolean)val) ? "true" : "false");
else
row.add("\"" + val + "\"");
}
else {
row.add("");
}
}
out += String.join(",", row) + "\n";
}

BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(out);
writer.close();
}
}

0 comments on commit 22f03f9

Please sign in to comment.