decoupled verification rendering from the verifier with much better animations

This commit is contained in:
ForestOfLight
2025-06-06 01:31:49 -07:00
Unverified
parent 709eccef8c
commit f8bbc659fb
11 changed files with 171 additions and 51 deletions
@@ -8,7 +8,7 @@ export class BuilderFormBuilder {
.title(MenuFormBuilder.menuTitle); .title(MenuFormBuilder.menuTitle);
for (const optionId of BuilderOptions.getOptionIds()) { for (const optionId of BuilderOptions.getOptionIds()) {
const option = BuilderOptions.get(optionId); const option = BuilderOptions.get(optionId);
form.toggle(`${option.displayName} - ${option.description}`, { defaultValue: option.isEnabled(player.id) }); form.toggle(`${option.displayName}`, { defaultValue: option.isEnabled(player.id), tooltip: option.description });
} }
form.submitButton('§2Apply'); form.submitButton('§2Apply');
return form; return form;
@@ -148,6 +148,11 @@ export class InstanceForm {
this.player.sendMessage('§cA verification is already in progress. Please wait until it finishes.'); this.player.sendMessage('§cA verification is already in progress. Please wait until it finishes.');
return; return;
} }
throw e;
}
if (!statsForm) {
this.player.sendMessage('§cFailed to build statistics form.');
return;
} }
statsForm.form.show(this.player).then((response) => { statsForm.form.show(this.player).then((response) => {
if (response.canceled && response.cancelationReason === FormCancelationReason.UserBusy) if (response.canceled && response.cancelationReason === FormCancelationReason.UserBusy)
@@ -160,11 +165,7 @@ export class InstanceForm {
if (response.canceled) if (response.canceled)
return; return;
this.instance.setVerifierEnabled(response.formValues[0]); this.instance.setVerifierEnabled(response.formValues[0]);
if (response.formValues[1]) this.instance.setLayer(parseInt(response.formValues[1]));
this.instance.setVerifierDistance(5);
else
this.instance.setVerifierDistance(0);
this.instance.setLayer(parseInt(response.formValues[2]));
}); });
} }
@@ -3,10 +3,9 @@ import { MenuFormBuilder } from '../MenuFormBuilder';
import { StructureVerifier } from '../Verifier/StructureVerifier'; import { StructureVerifier } from '../Verifier/StructureVerifier';
import { StructureStatistics } from '../Structure/StructureStatistics'; import { StructureStatistics } from '../Structure/StructureStatistics';
import { EntityComponentTypes, TicksPerSecond } from '@minecraft/server'; import { EntityComponentTypes, TicksPerSecond } from '@minecraft/server';
import { BlockVerificationLevel } from '../Enums/BlockVerificationLevel';
export class InstanceFormBuilder { export class InstanceFormBuilder {
static structureVerifier;
static buildInstance(instance, options) { static buildInstance(instance, options) {
const location = instance.getLocation(); const location = instance.getLocation();
const form = new ActionFormData() const form = new ActionFormData()
@@ -33,10 +32,9 @@ export class InstanceFormBuilder {
.title(MenuFormBuilder.menuTitle) .title(MenuFormBuilder.menuTitle)
if (this.structureVerifier) if (this.structureVerifier)
throw new Error('StructureVerifier is already running.'); throw new Error('StructureVerifier is already running.');
this.structureVerifier = new StructureVerifier(instance, { isEnabled: true, trackPlayerDistance: 0, intervalOrLifetime: 30 * TicksPerSecond, isStandalone: true }); const structureVerifier = new StructureVerifier(instance, { isEnabled: true, particleLifetime: 1*TicksPerSecond, isStandalone: true });
const verification = await this.structureVerifier.verifyStructure(); const verification = await structureVerifier.verifyStructure(true);
const statistics = new StructureStatistics(instance, verification); const statistics = new StructureStatistics(instance, verification);
this.structureVerifier = void 0;
const statsMessage = statistics.getMessage(); const statsMessage = statistics.getMessage();
buildStatisticsForm.body(statsMessage); buildStatisticsForm.body(statsMessage);
return { form: buildStatisticsForm, stats: statsMessage }; return { form: buildStatisticsForm, stats: statsMessage };
@@ -46,7 +44,6 @@ export class InstanceFormBuilder {
return new ModalFormData() return new ModalFormData()
.title(MenuFormBuilder.menuTitle) .title(MenuFormBuilder.menuTitle)
.toggle('Block Validation', { defaultValue: instance.options.verifier.isEnabled, tooltip: 'Shows missing and incorrect block overlay.' }) .toggle('Block Validation', { defaultValue: instance.options.verifier.isEnabled, tooltip: 'Shows missing and incorrect block overlay.' })
.toggle('Distance-Based Block Validation', { defaultValue: instance.verifier.getTrackPlayerDistance() !== 0, tooltip: `If enabled, the verifier will only check within ${instance.verifier.getTrackPlayerDistance()} blocks of the player. This should stay enabled unless your structure is very small or in layer mode.` })
.slider("Layer", 0, instance.getMaxLayer(), { defaultValue: instance.getLayer(), valueStep: 1, tooltip: 'Changes the active layer. Use 0 for all layers.' }) .slider("Layer", 0, instance.getMaxLayer(), { defaultValue: instance.getLayer(), valueStep: 1, tooltip: 'Changes the active layer. Use 0 for all layers.' })
.submitButton('§2Apply'); .submitButton('§2Apply');
} }
@@ -13,7 +13,7 @@ export class InstanceOptions extends Option {
verifier = { verifier = {
isEnabled: true, isEnabled: true,
trackPlayerDistance: 5, trackPlayerDistance: 5,
intervalOrLifetime: 10 particleLifetime: 10
}; };
static getInstanceStrucetureId(instanceName) { static getInstanceStrucetureId(instanceName) {
@@ -1,17 +1,19 @@
import { Vector } from "../../lib/Vector"; import { Vector } from "../../lib/Vector";
import { StructureOutliner } from "../Structure/StructureOutliner"; import { StructureOutliner } from "../Render/StructureOutliner";
import { StructureVerifier } from "../Verifier/StructureVerifier"; import { StructureVerifier } from "../Verifier/StructureVerifier";
import { Structure } from "../Structure/Structure"; import { Structure } from "../Structure/Structure";
import { InstanceOptions } from "./InstanceOptions"; import { InstanceOptions } from "./InstanceOptions";
import { TicksPerSecond } from "@minecraft/server"; import { TicksPerSecond } from "@minecraft/server";
import { InstanceNotPlacedError } from "../Errors/InstanceNotPlacedError"; import { InstanceNotPlacedError } from "../Errors/InstanceNotPlacedError";
import { StructureMaterials } from "../Materials/StructureMaterials"; import { StructureMaterials } from "../Materials/StructureMaterials";
import { VerificationRenderer } from "../Render/VerificationRenderer";
export class StructureInstance { export class StructureInstance {
options; options;
structure = void 0; structure = void 0;
verifier = void 0;
outliner = void 0; outliner = void 0;
verifier = void 0;
verificationRenderer = void 0;
materials = void 0; materials = void 0;
constructor(instanceName, structureId) { constructor(instanceName, structureId) {
@@ -27,6 +29,7 @@ export class StructureInstance {
delete this.structure; delete this.structure;
delete this.outliner; delete this.outliner;
delete this.verifier; delete this.verifier;
delete this.verificationRenderer;
delete this.materials; delete this.materials;
} }
@@ -36,11 +39,14 @@ export class StructureInstance {
if (!this.outliner) if (!this.outliner)
this.outliner = new StructureOutliner(this); this.outliner = new StructureOutliner(this);
if (!this.verifier) if (!this.verifier)
this.verifier = new StructureVerifier(this, { isEnabled: this.options.verifier.isEnabled, trackPlayerDistance: this.options.verifier.trackPlayerDistance }); this.verifier = new StructureVerifier(this, { isEnabled: this.options.verifier.isEnabled });
if (!this.verificationRenderer)
this.verificationRenderer = new VerificationRenderer(this);
if (!this.materials) if (!this.materials)
this.materials = new StructureMaterials(this); this.materials = new StructureMaterials(this);
this.outliner.refresh(); this.outliner.refresh();
this.verifier.refresh(); this.verifier.refresh();
this.verificationRenderer.refresh();
this.materials.refresh(); this.materials.refresh();
} }
@@ -57,7 +63,7 @@ export class StructureInstance {
} }
getDimension() { getDimension() {
return this.options.getDimension(); return this.options?.getDimension();
} }
getLayer() { getLayer() {
@@ -206,17 +212,18 @@ export class StructureInstance {
setVerifierEnabled(enable) { setVerifierEnabled(enable) {
this.options.setVerifierEnabled(enable); this.options.setVerifierEnabled(enable);
this.verifier.refresh(); this.verifier.refresh();
this.verificationRenderer.refresh();
} }
setVerifierDistance(distance) { setVerifierDistance(distance) {
this.options.setVerifierDistance(distance); this.options.setVerifierDistance(distance);
if (this.options.verifier.trackPlayerDistance === 0) { if (this.options.verifier.trackPlayerDistance === 0) {
const bounds = this.getBounds(); const bounds = this.getBounds();
this.options.verifier.intervalOrLifetime = Math.max(bounds.min.volume(bounds.max) / TicksPerSecond, 2*TicksPerSecond); this.options.verifier.particleLifetime = Math.max(bounds.min.volume(bounds.max) / TicksPerSecond, 2*TicksPerSecond);
} else { } else {
this.options.verifier.intervalOrLifetime = 10; this.options.verifier.particleLifetime = 10;
} }
this.verifier.refresh(); this.verificationRenderer.refresh();
} }
increaseLayer() { increaseLayer() {
@@ -0,0 +1,103 @@
import { TicksPerSecond } from "@minecraft/server";
import { BlockVerificationLevelRender } from "./BlockVerificationLevelRender";
import { system } from "@minecraft/server";
const RENDER_LIFETIME_FACTOR_TICKS = 1;
export class VerificationRenderer {
instance;
lastRenderedChunk;
bounds;
#runner;
#renderQueue = [];
constructor(instance) {
this.instance = instance;
const bounds = this.instance.getActiveBounds();
this.lastRenderedChunk = 0;
}
startContinuousRendering() {
this.#runner = system.runInterval(() => {
if (this.#renderQueue.length === 0)
this.prepareRenderQueue();
this.renderNextChunk();
}, RENDER_LIFETIME_FACTOR_TICKS);
}
stopContinuousRendering() {
if (!this.#runner)
return;
system.clearRun(this.#runner);
this.#runner = void 0;
this.#renderQueue = [];
}
refresh() {
this.stopContinuousRendering();
if (!this.instance.isEnabled() || !this.instance.options.verifier.isEnabled)
return;
this.startContinuousRendering();
}
prepareRenderQueue() {
this.#renderQueue = [];
const bounds = this.instance.getActiveBounds();
for (let y = bounds.min.y; y < bounds.max.y; y++) {
for (let x = bounds.min.x; x < bounds.max.x; x++) {
for (let z = bounds.min.z; z < bounds.max.z; z++) {
this.#renderQueue.push({ x, y, z });
}
}
}
this.lastRenderedChunk = 0;
}
renderNextChunk() {
if (this.shouldUseLargeStructureRendering())
this.renderNextChunkForLargeStructure();
else
this.renderNextChunkForSmallStructure();
}
renderNextChunkForLargeStructure() {
const bounds = this.instance.getActiveBounds();
const maxChunk = (bounds.min.volume(bounds.max) / bounds.max.x) / (bounds.max.y - bounds.min.y);
const lifetime = (maxChunk * RENDER_LIFETIME_FACTOR_TICKS) / TicksPerSecond;
const verificationLevels = this.instance.verifier.getLastVerificationLevels();
const dimension = this.instance.getDimension();
const chunk = this.#renderQueue.splice(0, bounds.max.x);
for (const location of chunk) {
const verificationLevel = verificationLevels[JSON.stringify(location)];
if (!verificationLevel)
continue;
const dimensionLocation = {
dimension: dimension,
location: this.instance.toGlobalCoords(location)
};
new BlockVerificationLevelRender(dimensionLocation, verificationLevel, lifetime);
}
}
renderNextChunkForSmallStructure() {
const bounds = this.instance.getActiveBounds();
const lifetime = (bounds.max.x * (bounds.max.y - bounds.min.y) * bounds.max.z * RENDER_LIFETIME_FACTOR_TICKS) / TicksPerSecond;
const verificationLevels = this.instance.verifier.getLastVerificationLevels();
const dimension = this.instance.getDimension();
for (const location of this.#renderQueue.splice(0, 1)) {
const verificationLevel = verificationLevels[JSON.stringify(location)];
if (!verificationLevel)
continue;
const dimensionLocation = {
dimension: dimension,
location: this.instance.toGlobalCoords(location)
};
new BlockVerificationLevelRender(dimensionLocation, verificationLevel, lifetime);
}
}
shouldUseLargeStructureRendering() {
const bounds = this.instance.getActiveBounds();
return bounds.min.volume(bounds.max) > 300;
}
}
@@ -9,24 +9,23 @@ export class StructureStatistics {
init() { init() {
this.statistics = {}; this.statistics = {};
for (const verificationLevel of Object.values(BlockVerificationLevel)) { for (const verificationLevel of Object.values(BlockVerificationLevel))
this.statistics[verificationLevel] = 0; this.statistics[verificationLevel] = 0;
}
this.statistics.correctlyAir = 0; this.statistics.correctlyAir = 0;
} }
parse() { parse() {
this.init(); this.init();
for (const blockVerificationLevel of Object.values(BlockVerificationLevel)) { for (const blockVerificationLevel of Object.values(BlockVerificationLevel))
this.parseStatistic(blockVerificationLevel); this.parseStatistic(blockVerificationLevel);
} }
}
parseStatistic(blockVerificationLevel) { parseStatistic(blockVerificationLevel) {
for (const verificationlevel of Object.values(this.verification)) { for (const [location, verificationLevel] of Object.entries(this.verification)) {
if (blockVerificationLevel === verificationlevel) { if (location === 'correctlyAir')
this.statistics[verificationlevel]++; continue;
} if (blockVerificationLevel === verificationLevel)
this.statistics[verificationLevel]++;
} }
} }
@@ -1,6 +1,6 @@
import { BlockVerifier } from "./BlockVerifier"; import { BlockVerifier } from "./BlockVerifier";
import { BlockVerificationLevel } from "../Enums/BlockVerificationLevel"; import { BlockVerificationLevel } from "../Enums/BlockVerificationLevel";
import { BlockVerificationLevelRender } from "../Verifier/BlockVerificationLevelRender"; import { BlockVerificationLevelRender } from "../Render/BlockVerificationLevelRender";
import { system, TicksPerSecond } from "@minecraft/server"; import { system, TicksPerSecond } from "@minecraft/server";
import { Vector } from "../../lib/Vector"; import { Vector } from "../../lib/Vector";
@@ -10,22 +10,24 @@ const MIN_LIFETIME = 8;
export class StructureVerifier { export class StructureVerifier {
instance; instance;
intervalOrLifetime; particleLifetime;
locationsToVerify; locationsToVerify;
blockVerificationLevels; blockVerificationLevels;
isLocationPopulationComplete; isLocationPopulationComplete;
isVerificationComplete; isVerificationComplete;
shouldStartNextVerification;
lastCompleteVerificationLevels;
#runner; #runner;
#verifyJob; #verifyJob;
#populateJob = {}; #populateJob = {};
constructor(instance, { isEnabled = false, trackPlayerDistance = 0, intervalOrLifetime = 10, isStandalone: isIndependent = false } = {}) { constructor(instance, { isEnabled = false, trackPlayerDistance = 0, particleLifetime = 10, isStandalone = false } = {}) {
this.instance = instance; this.instance = instance;
this.intervalOrLifetime = Math.max(intervalOrLifetime, MIN_LIFETIME); this.particleLifetime = Math.max(particleLifetime, MIN_LIFETIME);
if (isIndependent) { if (isStandalone) {
this.isIndependent = isIndependent; this.isStandalone = isStandalone;
this.enabled = isEnabled; this.enabled = isEnabled;
this.trackPlayerDistance = trackPlayerDistance; this.trackPlayerDistance = trackPlayerDistance;
} else { } else {
@@ -36,9 +38,11 @@ export class StructureVerifier {
} }
startContinuousVerification() { startContinuousVerification() {
this.shouldStartNextVerification = true;
this.#runner = system.runInterval(() => { this.#runner = system.runInterval(() => {
if (this.shouldStartNextVerification)
this.verifyStructure(); this.verifyStructure();
}, this.intervalOrLifetime); });
} }
stopContinuousVerification() { stopContinuousVerification() {
@@ -56,45 +60,48 @@ export class StructureVerifier {
} }
isEnabled() { isEnabled() {
if (this.isIndependent) if (this.isStandalone)
return this.enabled; return this.enabled;
return this.instance.options.verifier.isEnabled; return this.instance.options.verifier.isEnabled;
} }
getTrackPlayerDistance() { getTrackPlayerDistance() {
let distance; let distance;
if (this.isIndependent) if (this.isStandalone)
distance = this.trackPlayerDistance; distance = this.trackPlayerDistance;
else else
distance = this.instance.options.verifier.trackPlayerDistance distance = this.instance.options.verifier.trackPlayerDistance
return Math.min(MAX_TRACK_PLAYER_DISTANCE, Math.max(MIN_TRACK_PLAYER_DISTANCE, distance)); return Math.min(MAX_TRACK_PLAYER_DISTANCE, Math.max(MIN_TRACK_PLAYER_DISTANCE, distance));
} }
init() { async verifyStructure(shouldRender = false) {
this.locationsToVerify.clear();
this.blockVerificationLevels = { correctlyAir: 0 };
this.isLocationPopulationComplete = false;
this.isVerificationComplete = false;
}
async verifyStructure(shouldRender = true) {
if (!this.isEnabled()) if (!this.isEnabled())
return; return;
this.init(); this.initVerification();
return new Promise(async (resolve) => { return new Promise(async (resolve) => {
await this.populateLocationsToVerify(); await this.populateLocationsToVerify();
if (this.#verifyJob) if (this.#verifyJob)
system.clearJob(this.#verifyJob); system.clearJob(this.#verifyJob);
this.verifyJob = system.runJob(this.verifyBlocks(this.locationsToVerify, shouldRender)); this.#verifyJob = system.runJob(this.verifyBlocks(this.locationsToVerify, shouldRender));
const checker = system.runInterval(() => { const checker = system.runInterval(() => {
if (this.isVerificationComplete) { if (this.isVerificationComplete) {
system.clearRun(checker); system.clearRun(checker);
this.lastCompleteVerificationLevels = JSON.parse(JSON.stringify(this.blockVerificationLevels));
this.shouldStartNextVerification = true;
resolve(this.blockVerificationLevels); resolve(this.blockVerificationLevels);
} }
}, 1); }, 1);
}); });
} }
initVerification() {
this.shouldStartNextVerification = false;
this.locationsToVerify.clear();
this.blockVerificationLevels = { correctlyAir: 0 };
this.isLocationPopulationComplete = false;
this.isVerificationComplete = false;
}
async populateLocationsToVerify() { async populateLocationsToVerify() {
return new Promise((resolve) => { return new Promise((resolve) => {
if (this.getTrackPlayerDistance() === 0) { if (this.getTrackPlayerDistance() === 0) {
@@ -144,7 +151,7 @@ export class StructureVerifier {
this.blockVerificationLevels[JSON.stringify(location)] = verificationLevel; this.blockVerificationLevels[JSON.stringify(location)] = verificationLevel;
if (shouldRender) { if (shouldRender) {
const dimensionLocation = { dimension: this.instance.getDimension(), location: this.instance.toGlobalCoords(location) }; const dimensionLocation = { dimension: this.instance.getDimension(), location: this.instance.toGlobalCoords(location) };
new BlockVerificationLevelRender(dimensionLocation, verificationLevel, this.intervalOrLifetime/TicksPerSecond); new BlockVerificationLevelRender(dimensionLocation, verificationLevel, this.particleLifetime/TicksPerSecond);
} }
} }
yield void 0; yield void 0;
@@ -153,10 +160,16 @@ export class StructureVerifier {
} }
verifyBlock(location) { verifyBlock(location) {
const worldBlock = this.instance.getDimension().getBlock(this.instance.toGlobalCoords(location)); const worldBlock = this.instance.getDimension()?.getBlock(this.instance.toGlobalCoords(location));
if (!worldBlock) if (!worldBlock)
return BlockVerificationLevel.Skipped; return BlockVerificationLevel.Skipped;
const blockVerifier = new BlockVerifier(worldBlock, this.instance); const blockVerifier = new BlockVerifier(worldBlock, this.instance);
return blockVerifier.verify(); return blockVerifier.verify();
} }
getLastVerificationLevels() {
if (!this.lastCompleteVerificationLevels)
return {};
return this.lastCompleteVerificationLevels;
}
} }
@@ -10,7 +10,7 @@ let runner = void 0;
const builderOption = new BuilderOption({ const builderOption = new BuilderOption({
identifier: 'fastEasyPlace', identifier: 'fastEasyPlace',
displayName: 'Fast Easy Place', displayName: 'Fast Easy Place',
description: 'Place structure blocks just by looking at them.', description: 'Place correct structure blocks just by looking at them.',
howToUse: "Look at structure blocks with a paper named 'Easy Place' in your hand to place them.", howToUse: "Look at structure blocks with a paper named 'Easy Place' in your hand to place them.",
}); });