Material counter and cleanup

This commit is contained in:
ForestOfLight
2025-03-11 02:13:16 -07:00
Unverified
parent 67e3f6fecc
commit 830b54e55a
5 changed files with 211 additions and 30 deletions
+73
View File
@@ -0,0 +1,73 @@
import { structureCollection } from '../classes/StructureCollection';
class MaterialCounter {
static getAll(name) {
const structure = structureCollection.get(name);
const materials = {};
for (const block of structure.getBlocks()) {
const typeId = block?.getItemStack()?.typeId.replace('minecraft:', '');
if (!typeId) continue;
if (!materials[typeId]) {
materials[typeId] = 0;
}
materials[typeId]++;
}
return materials;
}
static getLayer(name, layer) {
const structure = structureCollection.get(name);
const materials = {};
for (const block of structure.getLayerBlocks(layer)) {
const typeId = block?.getItemStack()?.typeId.replace('minecraft:', '');
if (!typeId) continue;
if (!materials[typeId]) {
materials[typeId] = 0;
}
materials[typeId]++;
}
return materials;
}
static getPrintable(name) {
const structure = structureCollection.get(name);
const materials = {};
for (const block of structure.getBlocks()) {
const itemStack = block?.getItemStack();
const typeId = itemStack?.typeId.replace('minecraft:', '');
if (!typeId) continue;
if (!materials[typeId]) {
materials[typeId] = { count: 0, maxStack: itemStack.maxAmount };
}
materials[typeId].count++;
}
let message = [];
for (const blockType in materials) {
let count = materials[blockType].count;
let countStr = '';
const maxStack = materials[blockType].maxStack;
const fullShulker = 27 * maxStack;
if (count >= fullShulker) {
countStr = `${Math.floor(count / fullShulker)} sb`;
}
if (count > fullShulker) {
countStr += ' + ';
}
count %= fullShulker;
if (count >= maxStack) {
countStr += `${Math.floor(count / maxStack)} stack`;
}
if (count > maxStack) {
countStr += ' + ';
}
count %= maxStack;
if (count > 0) {
countStr += count;
}
message.push(` ${blockType}: ${countStr}`);
}
return message.sort().join('\n');
}
}
export { MaterialCounter };