Skip to content

Commit

Permalink
fix: world edit
Browse files Browse the repository at this point in the history
  • Loading branch information
leaftail1880 committed Sep 4, 2024
1 parent 13b111b commit d749a29
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 36 deletions.
40 changes: 15 additions & 25 deletions src/modules/world-edit/lib/big-structure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ import { WE_CONFIG } from '../config'
import { Cuboid } from './ccuboid'

export class BigStructure extends Cuboid {
id

savePromise

private structures: {
name: string
id: string
min: Vector3
max: Vector3
}[] = []
Expand All @@ -22,57 +18,51 @@ export class BigStructure extends Cuboid {
* @param {Vector3} pos2
*/
constructor(
public prefix: string,
private prefix: string,
pos1: Vector3,
pos2: Vector3,
public dimension: Dimension,
public name = '',
) {
super(pos1, pos2)
this.id = Date.now().toString(32)
this.prefix = `${prefix}|${this.id}`
this.prefix = `${prefix}|${Date.now().toString(32)}`

this.savePromise = this.save()
this.save()
}

// eslint-disable-next-line @typescript-eslint/require-await
private async save() {
save() {
this.structures = []
const cubes = this.split(WE_CONFIG.STRUCTURE_CHUNK_SIZE)
// console.debug({ cubes: cubes.map(e => Vector.string(Vector.subtract(e.max, e.min)), true) })

const options = { errors: 0, total: 0 }

for (const [i, cube] of cubes.entries()) {
const name = `mystructure:${this.prefix}|${i}`
const id = `mystructure:${this.prefix}|${i}`
const min = cube.pos1
const max = cube.pos2

try {
world.structureManager.delete(name)
world.structureManager.delete(id)
} catch {}
world.structureManager.createFromWorld(name, this.dimension, min, max, {

world.structureManager.createFromWorld(id, this.dimension, min, max, {
includeEntities: false,
includeBlocks: true,
saveMode: StructureSaveMode.Memory,
})

// console.log(t`Created from world: ${Vector.string(min)} ${Vector.string(max)}, name: ${name}`)

this.structures.push({
name,
min,
max,
})
this.structures.push({ id, min, max })
}

// console.debug(options.total, this.structures.length)
if (options.errors > 0)
throw new Error(
`§c${options.errors}§f/${options.total}§c не сохранено. Возможно, часть области была непрогруженна. Попробуйте снова, перед этим встав в центр.`,
)
}

delete() {
this.structures.forEach(e => world.structureManager.delete(e.id))
}

async load(position = this.min, dimension = this.dimension, placeOptions?: StructurePlaceOptions) {
const { structures, min } = this

Expand All @@ -94,7 +84,7 @@ export class BigStructure extends Cuboid {
}

// console.log(`/structure load "${file.name}" ${Vector.string(to)}`)
world.structureManager.place(file.name, dimension, to, placeOptions)
world.structureManager.place(file.id, dimension, to, placeOptions)
} catch (e) {
console.error(e)
errors++
Expand Down
15 changes: 4 additions & 11 deletions src/modules/world-edit/lib/world-edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,29 +141,23 @@ export class WorldEdit {
* @param pos2 Position 2 of cuboid location
* @param history Save location where you want the to store your backup
*/
async backup(
name: string,
pos1: Vector3 = this.pos1,
pos2: Vector3 = this.pos2,
history: BigStructure[] = this.history,
) {
backup(name: string, pos1: Vector3 = this.pos1, pos2: Vector3 = this.pos2, history: BigStructure[] = this.history) {
if (this.history.length === this.historyLimit) {
console.log('Player', this.player.name, 'has reached history limit (', this.historyLimit, ')')
if (this.hasWarnAboutHistoryLimit) {
console.log('Player', this.player.name, 'has reached history limit (', this.historyLimit, ')')
this.player.warn(
`Вы превысили лимит отменяемых действий WorldEdit'а. Вы сможете восстановить лишь последние ${this.historyLimit} действий.`,
)
this.hasWarnAboutHistoryLimit = true
}

world.structureManager.delete(history[0].id)
history[0].delete()
history.splice(0, 1)
}

const structrure = new BigStructure(WE_CONFIG.BACKUP_PREFIX, pos1, pos2, this.player.dimension, name)

history.push(structrure)
await structrure.savePromise
}

/** Loads specified amount of backups from history array */
Expand Down Expand Up @@ -232,7 +226,6 @@ export class WorldEdit {
this.pos2,
this.player.dimension,
)
await this.currentCopy.savePromise
this.player.info(
`Скопирована область размером ${selection.size}\n§3От: ${Vector.string(this.pos1, true)}\n§3До: ${Vector.string(
this.pos2,
Expand Down Expand Up @@ -285,7 +278,7 @@ export class WorldEdit {
if (!this.currentCopy) return this.player.fail('§cВы ничего не копировали!')
const { pastePos1, pastePos2 } = this.pastePositions(rotation, this.currentCopy)

await this.backup('Вставка (paste)', pastePos1, pastePos2)
this.backup('Вставка (paste)', pastePos1, pastePos2)
await this.currentCopy.load(pastePos1, undefined, {
rotation,
mirror,
Expand Down

0 comments on commit d749a29

Please sign in to comment.