fix: new generator script and refactor

This commit is contained in:
trafficlunar 2024-12-22 15:58:02 +00:00
parent 3e00029466
commit 7f52cabfbd
13 changed files with 219 additions and 42 deletions

2
.gitignore vendored
View file

@ -25,4 +25,4 @@ dist-ssr
# Generator
generator/blocks/
generator/data/
generator/output/

View file

@ -4,7 +4,7 @@ Collection of Node.JS scripts to generate data for blockmatic. See below to see
Before running any of these scripts you need to have a `blocks` directory with Minecraft textures in them. See below on how to do that.
1. Create a `blocks` directory
1. Create a `blocks` directory in the same folder where this README file is
2. Open a Minecraft client jar in a zip extraction program (such as 7zip, WinRAR, Xarchiver, etc.)
3. Go to `assets/minecraft/textures/blocks`
4. Copy all textures into the `blocks` directory
@ -13,21 +13,21 @@ Before running any of these scripts you need to have a `blocks` directory with M
> Deletes every unneeded texture in the `blocks` directory
1. Run `node filter.js`
1. Run `node scripts/filter.js`
2. Every unneeded texture has been deleted
## resize.js
> Resizes image files bigger than 16x16 (these files are usually for animations such as fire)
1. Run `node resize.js`
1. Run `node scripts/resize.js`
2. Every file has been resized accordingly
## averageColors.js
## data.js
> Calculates the average color for each texture and outputs it into a JSON file
> Generates average colors, versions, names, ids, properties for blocks (NEEDS MANUAL EDITING AFTER!)
1. Run `node averageColors.js`
1. Run `node scripts/data.js`
2. In the `data` directory there should be a JSON file generated
3. Put that JSON file into `src/data`
@ -35,7 +35,7 @@ Before running any of these scripts you need to have a `blocks` directory with M
> Generates a spritesheet for Pixi.JS to use
1. Run `node spritesheet.js`
1. Run `node scripts/spritesheet.js`
2. In the `data` directory there should be two files generated - `spritesheet.json` and `spritesheet.png`
3. Put `spritesheet.png` into the `public` folder
4. Put `spritesheet.json` into `src/data`
@ -47,6 +47,6 @@ Before running any of these scripts you need to have a `blocks` directory with M
You probably don't need to run this script.
```bash
$ chmod +x ./deleteBlocks.sh
$ ./deleteBlocks.sh
$ chmod +x ./scripts/deleteBlocks.sh
$ ./scripts/deleteBlocks.sh
```

View file

@ -1,25 +0,0 @@
const fs = require("fs");
const path = require("path");
const { getAverageColor } = require("fast-average-color-node");
const INPUT = path.join(__dirname, "blocks/");
const OUTPUT = path.join(__dirname, "data/average_colors.json");
const data = {};
(async () => {
const files = fs.readdirSync(INPUT);
for (const file of files) {
const filePath = path.join(INPUT, file);
const color = await getAverageColor(filePath);
const fileName = file.slice(0, -4);
data[fileName] = [color.value[0], color.value[1], color.value[2], color.value[3]];
}
fs.writeFileSync(OUTPUT, JSON.stringify(data));
console.log("Done!");
})();

View file

@ -0,0 +1,136 @@
{
"concrete": "1.12",
"concrete_powder": "1.12",
"glazed_terracotta": "1.12",
"shulker_box": "1.11",
"coral": "1.13",
"stripped": "1.13",
"stained_glass": "1.7.2",
"_wool": "1.2",
"_terracotta": "1.6.1",
"prismarine": "1.8",
"command_block": "1.4.2",
"hay_block": "1.6.1",
"observer": "1.11",
"bone_block": "1.10",
"piston": "1.7",
"acacia_log": "1.7.2",
"acacia_log_top": "1.7.2",
"acacia_planks": "1.7.2",
"acacia_trapdoor": "1.13",
"andesite": "1.8",
"bedrock": "1.0",
"birch_log": "1.2",
"birch_log_top": "1.2",
"birch_planks": "1.2",
"birch_trapdoor": "1.13",
"blue_ice": "1.13",
"bookshelf": "1.0",
"bricks": "1.0",
"brown_mushroom_block": "1.8",
"carved_pumpkin": "1.0",
"cauldron": "1.0",
"chiseled_quartz_block": "1.5",
"chiseled_red_sandstone": "1.8",
"chiseled_sandstone": "1.2.4",
"chiseled_stone_bricks": "1.2.1",
"clay": "1.0",
"coal_block": "1.6.1",
"coal_ore": "1.0",
"coarse_dirt": "1.7.2",
"cobblestone": "1.0",
"cobweb": "1.5",
"cracked_stone_bricks": "1.0",
"crafting_table": "1.0",
"cut_red_sandstone": "1.8",
"cut_sandstone": "1.2.4",
"dark_oak_log": "1.7.2",
"dark_oak_log_top": "1.7.2",
"dark_oak_planks": "1.7.2",
"dark_oak_trapdoor": "1.13",
"diamond_block": "1.0",
"diamond_ore": "1.0",
"diorite": "1.8",
"dirt": "1.0",
"dispenser": "1.2",
"dried_kelp": "1.13",
"dropper": "1.5",
"emerald_block": "1.3.1",
"emerald_ore": "1.3.1",
"end_stone": "1.0.0",
"end_stone_bricks": "1.9",
"furnace": "1.0",
"glass": "1.0",
"glowstone": "1.0",
"gold_block": "1.0",
"gold_ore": "1.0",
"granite": "1.8",
"grass_block": "1.0",
"grass_block_snow": "1.0",
"gravel": "1.0",
"ice": "1.0",
"iron_block": "1.0",
"iron_ore": "1.0",
"jack_o_lantern": "1.0",
"jungle_log": "1.2.1",
"jungle_log_top": "1.2.1",
"jungle_planks": "1.2.1",
"jungle_trapdoor": "1.13",
"lapis_block": "1.2",
"lapis_ore": "1.2",
"magma": "1.10",
"melon": "1.8",
"mossy_cobblestone": "1.0",
"mossy_stone_bricks": "1.8",
"mushroom_block_inside": "1.8",
"mushroom_stem": "1.8",
"mycelium": "1.0",
"nether_bricks": "1.0",
"nether_quartz_ore": "1.5",
"nether_wart_block": "1.10",
"netherrack": "1.0",
"note_block": "1.0",
"oak_log": "1.0",
"oak_log_top": "1.0",
"oak_planks": "1.0",
"oak_trapdoor": "1.8",
"obsidian": "1.0",
"packed_ice": "1.7.2",
"podzol": "1.7.2",
"polished_andesite": "1.8",
"polished_diorite": "1.8",
"polished_granite": "1.8",
"pumpkin": "1.13",
"purpur_block": "1.9",
"purpur_pillar": "1.9",
"quartz_block": "1.5",
"quartz_pillar": "1.5",
"red_mushroom_block": "1.8",
"red_nether_bricks": "1.10",
"red_sand": "1.7.2",
"red_sandstone": "1.8",
"redstone_block": "1.5",
"redstone_lamp": "1.2.1",
"redstone_ore": "1.0",
"sand": "1.0",
"sandstone": "1.2",
"sea_lantern": "1.8",
"slime_block": "1.8",
"snow": "1.0",
"soul_sand": "1.0",
"spawner": "1.0",
"sponge": "1.0",
"spruce_log": "1.0",
"spruce_log_top": "1.0",
"spruce_planks": "1.0",
"spruce_trapdoor": "1.13",
"stone": "1.0",
"stone_bricks": "1.0",
"stone_slab": "1.0",
"structure_block": "1.9",
"terracotta": "1.6.1",
"tnt": "1.0",
"wet_sponge": "1.0",
"white_wool": "1.0"
}

View file

@ -1,2 +0,0 @@
#!/bin/bash
rm blocks/*

View file

@ -4,6 +4,7 @@
"version": "1.0.0",
"dependencies": {
"fast-average-color-node": "^3.1.0",
"minecraft-data": "^3.80.0",
"sharp": "^0.33.5",
"spritesheet-js": "^1.2.8"
}

View file

@ -11,6 +11,9 @@ importers:
fast-average-color-node:
specifier: ^3.1.0
version: 3.1.0
minecraft-data:
specifier: ^3.80.0
version: 3.80.0
sharp:
specifier: ^0.33.5
version: 0.33.5
@ -173,6 +176,9 @@ packages:
lru-cache@2.7.3:
resolution: {integrity: sha512-WpibWJ60c3AgAz8a2iYErDrcT2C7OmKnsWhIcHOjkUHFjkXncJhtLxNSqUmxRxRunpb5I8Vprd7aNSd2NtksJQ==}
minecraft-data@3.80.0:
resolution: {integrity: sha512-UYq+ADpS9K1+cqiJiz6tqkht4y4cRYF3qOYanG9eIiHY+VC+qIAC7/UcW6G3adayvj5YBOCurlqaw3E0TMAtHg==}
minimatch@0.3.0:
resolution: {integrity: sha512-WFX1jI1AaxNTZVOHLBVazwTWKaQjoykSzCBNXB72vDTCzopQGtyP91tKdFK5cv1+qMwPyiTu1HqUriqplI8pcA==}
deprecated: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
@ -341,6 +347,8 @@ snapshots:
lru-cache@2.7.3: {}
minecraft-data@3.80.0: {}
minimatch@0.3.0:
dependencies:
lru-cache: 2.7.3

54
generator/scripts/data.js Normal file
View file

@ -0,0 +1,54 @@
const fs = require("fs");
const path = require("path");
const { getAverageColor } = require("fast-average-color-node");
const minecraftData = require("minecraft-data");
const versionRegex = require("../data/versions.json");
const INPUT = path.join(__dirname, "../blocks/");
const OUTPUT_DIR = path.join(__dirname, "../output/");
const OUTPUT = path.join(OUTPUT_DIR, "data.json");
const VERSION_DATA = minecraftData("1.13.2");
if (!fs.existsSync(OUTPUT_DIR)) fs.mkdirSync(OUTPUT_DIR);
const data = {};
(async () => {
const files = fs.readdirSync(INPUT);
for (const file of files) {
const filePath = path.join(INPUT, file);
const fileName = file.slice(0, -4);
const color = await getAverageColor(filePath);
const nameRegex = ["_top", "_side", "_front", "_back"];
const pattern = new RegExp(nameRegex.join("|"), "g");
const blockName = fileName.replace(pattern, "");
function getDataBlockProperty(property) {
return VERSION_DATA.blocksByName[blockName] ? VERSION_DATA.blocksByName[blockName][property] : "REPLACE_ME_REPLACE_ME_REPLACE_ME_REPLACE_ME";
}
function getVersion() {
for (const key of Object.keys(versionRegex)) {
if (blockName.includes(key)) {
return versionRegex[key];
}
}
return "REPLACE_ME_REPLACE_ME_REPLACE_ME_REPLACE_ME";
}
data[fileName] = {
name: VERSION_DATA.blocksByName[fileName] ? VERSION_DATA.blocksByName[fileName].displayName : "REPLACE_ME_REPLACE_ME_REPLACE_ME_REPLACE_ME",
version: getVersion(),
id: [getDataBlockProperty("name"), getDataBlockProperty("id")],
color: [color.value[0], color.value[1], color.value[2], color.value[3]],
};
}
fs.writeFileSync(OUTPUT, JSON.stringify(data, null, 4));
console.log("Done!");
})();

View file

@ -0,0 +1,2 @@
#!/bin/bash
rm ../blocks/*

View file

@ -1,9 +1,9 @@
const fs = require("fs");
const path = require("path");
const regex = require("./regex.json");
const regex = require("../data/regex.json");
const INPUT = path.join(__dirname, "blocks/");
const INPUT = path.join(__dirname, "../blocks/");
function isBlacklisted(fileName) {
if (!fileName.endsWith(".png")) return true;

View file

@ -2,7 +2,7 @@ const fs = require("fs");
const path = require("path");
const sharp = require("sharp");
const INPUT = path.join(__dirname, "blocks/");
const INPUT = path.join(__dirname, "../blocks/");
(async () => {
const files = fs.readdirSync(INPUT);

View file

@ -1,9 +1,12 @@
const fs = require("fs");
const path = require("path");
const spritesheet = require("spritesheet-js");
const INPUT = path.join(__dirname, "blocks/");
const OUTPUT = path.join(__dirname, "data/");
const INPUT = path.join(__dirname, "../blocks/");
const OUTPUT = path.join(__dirname, "../output/");
if (!fs.existsSync(OUTPUT)) fs.mkdirSync(OUTPUT);
spritesheet(path.join(INPUT, "*.png"), { format: "pixi.js", path: OUTPUT }, function (err) {
if (err) throw err;