This repository has been archived on 2024-06-11. You can view files and clone it, but cannot push or open issues or pull requests.
PackConverterJS/index.js

112 lines
3.2 KiB
JavaScript

const decompress = require('decompress');
const fs = require('fs');
const jsonfile = require('jsonfile');
// const file = 'blocks.json';
const blockRemaps = 'blocks.json';
let blocks;
jsonfile.readFile(blockRemaps, function (err, obj) {
if (err) console.error(err);
blocks = obj;
});
const itemRemaps = 'items.json';
let items;
jsonfile.readFile(itemRemaps, function (err, obj) {
if (err) console.error(err);
items = obj;
});
/*
Textures to check in the future:
- Clock
- Compass
- Filled Map
- Potion
*/
extract('1.8');
// extract("1.20.1")
function extract(string) {
const extractedPath = `out/${string}/assets/minecraft/textures/`;
decompress(`test/${string}.zip`, `out/${string}`).then(files => {
files.forEach(element => {
if (!element.path.includes('assets/minecraft/textures/')) {
return;
}
// const texture = element.path.match(/(?:[A-Za-z]*\/)*([A-Za-z]*)\/([A-Za-z_0-9]*)\.(png\.mcmeta|png)/);
const texture = element.path.match(/([A-Za-z]*)\/([A-Za-z]*)\/([A-Za-z_0-9]*)\.(png\.mcmeta|png)/);
if (!texture) return;
let type = texture[1];
const subtype = texture[2];
const name = texture[3];
const extension = texture[4];
let rename;
if (type == 'textures') {
type = subtype;
}
if (type == 'items') {
rename = items[name];
}
if (type == 'blocks') {
rename = blocks[name];
}
const path1 = `${extractedPath}${type}/`;
// if (type == 'items') {
// rename == items;
// }
// if (type == 'blocks') {
// rename == blocks;
// }
if (rename) {
try {
fs.renameSync(`${path1}${name}.${extension}`, `${path1}${rename}.${extension}`);
console.log(`Renamed ${path1}${name}.${extension} to ${path1}${rename}.${extension}`);
} catch (error) {
console.log(error);
// console.log(`(type ${type}\nsubtype ${subtype}\nname ${name}\nextension ${extension})`);
}
}
// const path = texture[0];
// const type = texture[1];
// const name = texture[2];
// const extension = texture[3];
// const rename = blocks[name];
// const poggers = `${extractedPath}${type}/`;
// if (rename) {
// try {
// // fs.renameSync(`${poggers}${name}.${extension}`, `${poggers}${rename}.${extension}`);
// // console.log(`Renamed ${poggers}${name}.${extension} to ${poggers}${rename}.${extension}`);
// }
// catch (err) {
// console.log(err);
// }
// }
// console.log(texture);
});
}).then(() => {
try {
fs.renameSync(`${extractedPath}blocks`, `${extractedPath}block`);
console.log(`Renamed ${extractedPath}blocks to ${extractedPath}block`);
} catch (error) {
console.log(error);
}
try {
fs.renameSync(`${extractedPath}items`, `${extractedPath}item`);
console.log(`Renamed ${extractedPath}items to ${extractedPath}item`);
} catch (error) {
console.log(error);
}
jsonfile.readFile(`out/${string}/pack.mcmeta`, function (err, obj) {
if (err) console.error(err);
const oldFormat = obj.pack.pack_format;
const newFormat = 15;
obj.pack.pack_format = newFormat;
jsonfile.writeFile(`out/${string}/pack.mcmeta`, obj, { spaces: 2 }, function (err) {
if (err) console.error(err);
console.log(`Pack format changed from ${oldFormat} to ${newFormat}`);
});
});
});
}