init material grabber

This commit is contained in:
ForestOfLight
2025-04-21 00:44:36 -07:00
Unverified
parent 9a40363c1f
commit 6d7db482c0
6 changed files with 204 additions and 35 deletions
@@ -1,6 +1,9 @@
import { BuilderOptions } from "./BuilderOptions";
export class Builder {
playerId;
materialInstanceName;
constructor(playerId) {
this.playerId = playerId;
}
@@ -5,12 +5,14 @@ import { Structure } from "../Structure/Structure";
import { InstanceOptions } from "./InstanceOptions";
import { TicksPerSecond } from "@minecraft/server";
import { InstanceNotPlacedError } from "../Errors/InstanceNotPlacedError";
import { StructureMaterials } from "../Materials/StructureMaterials";
export class StructureInstance {
options;
structure = void 0;
verifier = void 0;
outliner = void 0;
materials = void 0;
constructor(instanceName, structureId) {
this.structure = new Structure(structureId);
@@ -28,6 +30,9 @@ export class StructureInstance {
}
refreshBox() {
if (!this.materials)
this.materials = new StructureMaterials(this);
this.materials.refresh();
if (!this.hasLocation())
return;
if (!this.outliner)
@@ -134,6 +139,10 @@ export class StructureInstance {
return this.structure.getAllLocations();
}
getActiveMaterials() {
return this.materials;
}
isEnabled() {
return this.options.isEnabled;
}
@@ -0,0 +1,37 @@
import { MaterialsFormBuilder } from './MaterialsFormBuilder';
import { forceShow } from '../../utils';
import { Builders } from '../Builder/Builders';
export class MaterialsForm {
constructor(player) {
this.player = player;
this.show();
}
show() {
try {
return forceShow(this.player, MaterialsFormBuilder.buildInstanceSelector()).then((response) => {
if (response.canceled)
return;
let selection = response.selection;
const selectedInstanceName = structureCollection.getInstanceNames()[selection];
if (selectedInstanceName) {
this.setActiveInstance(selectedInstanceName);
this.player.sendMessage(`§8Selected instance for material grabber: §2${selectedInstanceName}`);
return;
}
});
} catch (e) {
if (e.message === 'Menu timed out.') {
this.player.sendMessage('§8Menu timed out.');
return;
}
throw e;
}
}
setActiveInstance(instanceName) {
const builder = Builders.get(this.player.id);
builder.materialInstanceName = instanceName;
}
}
@@ -0,0 +1,15 @@
import { MenuFormBuilder } from "../MenuFormBuilder";
export class InstanceFormBuilder {
menuTitle = MenuFormBuilder.menuTitle + ' Material Grabber';
static buildInstanceSelector() {
const allInstanceNameForm = new ActionFormData()
.title(this.menuTitle)
.body('Select an instance:');
structureCollection.getInstanceNames().forEach(instanceName => {
allInstanceNameForm.button(`§2${instanceName}`);
});
return allInstanceNameForm;
}
}
@@ -1,5 +1,5 @@
class MaterialCounter {
instance
class StructureMaterials {
instance;
materials;
constructor(instance) {
@@ -7,6 +7,11 @@ class MaterialCounter {
this.materials = {};
}
refresh() {
this.clear();
this.populateActive();
}
populateAll() {
for (const layer = 0; layer < this.instance.getMaxLayer(); layer++)
this.getLayer(layer)
@@ -32,47 +37,33 @@ class MaterialCounter {
}
clear() {
this.materials = {}; // does this leak memory??
for (const key in this.materials)
delete this.materials[key];
}
toString() {
const materials = {};
for (const block of this.instance.getAllBlocks()) {
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;
for (const blockType in this.materials) {
let count = this.materials[blockType].count;
let countStr = '';
const maxStack = materials[blockType].maxStack;
const fullShulker = 27 * maxStack;
if (count >= fullShulker) {
const stackSize = this.materials[blockType].stackSize;
const fullShulker = 27 * stackSize;
if (count >= fullShulker)
countStr = `${Math.floor(count / fullShulker)} sb`;
}
if (count > fullShulker) {
if (count > fullShulker)
countStr += ' + ';
}
count %= fullShulker;
if (count >= maxStack) {
countStr += `${Math.floor(count / maxStack)} stack`;
}
if (count > maxStack) {
if (count >= stackSize)
countStr += `${Math.floor(count / stackSize)} stack`;
if (count > stackSize)
countStr += ' + ';
}
count %= maxStack;
if (count > 0) {
count %= stackSize;
if (count > 0)
countStr += count;
}
message.push(` ${blockType}: ${countStr}`);
}
return message.sort().join('\n');
}
}
export { MaterialCounter };
export { StructureMaterials };