Material List form with missing items from inventory
This commit is contained in:
+3
-3
@@ -1,9 +1,9 @@
|
||||
import { MaterialsFormBuilder } from './MaterialsFormBuilder';
|
||||
import { MaterialGrabberFormBuilder } from './MaterialGrabberFormBuilder';
|
||||
import { forceShow } from '../../utils';
|
||||
import { Builders } from '../Builder/Builders';
|
||||
import { structureCollection } from '../Structure/StructureCollection';
|
||||
|
||||
export class MaterialsForm {
|
||||
export class MaterialGrabberForm {
|
||||
constructor(player) {
|
||||
this.player = player;
|
||||
this.show();
|
||||
@@ -11,7 +11,7 @@ export class MaterialsForm {
|
||||
|
||||
show() {
|
||||
try {
|
||||
return forceShow(this.player, MaterialsFormBuilder.buildInstanceSelector(this.player)).then((response) => {
|
||||
return forceShow(this.player, MaterialGrabberFormBuilder.buildInstanceSelector(this.player)).then((response) => {
|
||||
if (response.canceled)
|
||||
return;
|
||||
const selectedInstanceName = structureCollection.getInstanceNames()[response.selection];
|
||||
+1
-1
@@ -3,7 +3,7 @@ import { MenuFormBuilder } from "../MenuFormBuilder";
|
||||
import { structureCollection } from "../Structure/StructureCollection";
|
||||
import { Builders } from "../Builder/Builders";
|
||||
|
||||
export class MaterialsFormBuilder {
|
||||
export class MaterialGrabberFormBuilder {
|
||||
static menuTitle = MenuFormBuilder.menuTitle + ' Material Grabber';
|
||||
|
||||
static buildInstanceSelector(player) {
|
||||
@@ -1,3 +1,5 @@
|
||||
import { ItemStack } from "@minecraft/server";
|
||||
|
||||
class StructureMaterials {
|
||||
instance;
|
||||
materials;
|
||||
@@ -74,28 +76,54 @@ class StructureMaterials {
|
||||
delete this.materials[key];
|
||||
}
|
||||
|
||||
toString() {
|
||||
let message = [];
|
||||
for (const blockType in this.materials) {
|
||||
let count = this.materials[blockType].count;
|
||||
formatString(otherMaterials = void 0) {
|
||||
const materials = otherMaterials || this.materials;
|
||||
let message = { rawtext: [] };
|
||||
const sortedTypes = Object.keys(materials).sort(
|
||||
(a, b) => materials[b].count - materials[a].count
|
||||
);
|
||||
for (const blockType of sortedTypes) {
|
||||
let count = materials[blockType].count;
|
||||
let countStr = '';
|
||||
const stackSize = this.materials[blockType].stackSize;
|
||||
const stackSize = materials[blockType].stackSize;
|
||||
const fullShulker = 27 * stackSize;
|
||||
if (count >= fullShulker)
|
||||
countStr = `${Math.floor(count / fullShulker)} \uE200`;
|
||||
if (count > fullShulker)
|
||||
countStr = `${Math.floor(count / fullShulker)}\uE200`;
|
||||
if (count > fullShulker && count % fullShulker > 0)
|
||||
countStr += ' + ';
|
||||
count %= fullShulker;
|
||||
if (count >= stackSize)
|
||||
countStr += `${Math.floor(count / stackSize)} stack`;
|
||||
if (count > stackSize)
|
||||
if (count >= stackSize) {
|
||||
const numStacks = Math.floor(count / stackSize);
|
||||
countStr += `${numStacks} stack`;
|
||||
if (numStacks > 1)
|
||||
countStr += 's';
|
||||
}
|
||||
if (count > stackSize && count % stackSize > 0)
|
||||
countStr += ' + ';
|
||||
count %= stackSize;
|
||||
if (count > 0)
|
||||
countStr += count;
|
||||
message.push(` ${blockType}: ${countStr}`);
|
||||
message.rawtext.push({ text: '§3' });
|
||||
message.rawtext.push({ translate: new ItemStack(blockType).localizationKey })
|
||||
message.rawtext.push({ text: `§f: ${countStr}\n` });
|
||||
}
|
||||
return message.sort().join('\n');
|
||||
return message;
|
||||
}
|
||||
|
||||
getMaterialsDifference(container) {
|
||||
const missingMaterials = JSON.parse(JSON.stringify(this.materials));
|
||||
for (let slotIndex = 0; slotIndex < container.size; slotIndex++) {
|
||||
const slot = container.getSlot(slotIndex);
|
||||
if (slot.hasItem()) {
|
||||
const itemType = slot.getItem().typeId;
|
||||
if (!missingMaterials[itemType])
|
||||
continue;
|
||||
missingMaterials[itemType].count -= slot.amount;
|
||||
if (missingMaterials[itemType].count <= 0)
|
||||
delete missingMaterials[itemType];
|
||||
}
|
||||
}
|
||||
return missingMaterials;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user