Attempt to fix all player offline memory leak
This commit is contained in:
@@ -3,7 +3,7 @@ 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 { world, system, 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";
|
import { VerificationRenderer } from "../Render/VerificationRenderer";
|
||||||
@@ -20,6 +20,7 @@ export class StructureInstance {
|
|||||||
this.structure = new Structure(structureId);
|
this.structure = new Structure(structureId);
|
||||||
this.options = new InstanceOptions(instanceName, structureId);
|
this.options = new InstanceOptions(instanceName, structureId);
|
||||||
this.refreshBox();
|
this.refreshBox();
|
||||||
|
this.subscribeToEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
delete() {
|
delete() {
|
||||||
@@ -50,6 +51,10 @@ export class StructureInstance {
|
|||||||
this.materials.refresh();
|
this.materials.refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
subscribeToEvents() {
|
||||||
|
this.disableInstanceWhenNoPlayersOnline();
|
||||||
|
}
|
||||||
|
|
||||||
getName() {
|
getName() {
|
||||||
return this.options.instanceName;
|
return this.options.instanceName;
|
||||||
}
|
}
|
||||||
@@ -240,6 +245,24 @@ export class StructureInstance {
|
|||||||
this.setLayer(this.options.currentLayer - 1);
|
this.setLayer(this.options.currentLayer - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
disableInstanceWhenNoPlayersOnline() {
|
||||||
|
// This prevents a memory leak when no players are online.
|
||||||
|
world.beforeEvents.playerLeave.subscribe(event => {
|
||||||
|
system.run(() => {
|
||||||
|
if (world.getAllPlayers().length === 0) {
|
||||||
|
this.disable();
|
||||||
|
this.shouldEnableOnJoin = true;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
world.afterEvents.playerJoin.subscribe(event => {
|
||||||
|
if (this.shouldEnableOnJoin) {
|
||||||
|
this.enable();
|
||||||
|
this.shouldEnableOnJoin = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
toGlobalCoords(structureLocation) {
|
toGlobalCoords(structureLocation) {
|
||||||
return Vector.from(structureLocation).add(this.options.worldLocation);
|
return Vector.from(structureLocation).add(this.options.worldLocation);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user