Skip to content

Commit 623b824

Browse files
committed
feat: add edit-structure command
1 parent 750b76f commit 623b824

File tree

6 files changed

+91
-8
lines changed

6 files changed

+91
-8
lines changed

src/lib/form/new.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import { Player } from '@minecraft/server'
22
import { ActionFormData, ActionFormResponse } from '@minecraft/server-ui'
33
import { ActionForm } from 'lib/form/action'
44
import { ask } from 'lib/form/message'
5-
import { showForm } from 'lib/form/utils'
6-
import { Message } from 'lib/i18n/message'
5+
import { FormCallback, showForm } from 'lib/form/utils'
76
import { i18n, noI18n } from 'lib/i18n/text'
87
import { Quest } from 'lib/quest'
9-
import { doNothing, util } from 'lib/util'
8+
import { doNothing } from 'lib/util'
109

1110
export type NewFormCallback = (player: Player, back?: NewFormCallback) => unknown
1211

@@ -133,8 +132,6 @@ class Form {
133132
}
134133

135134
show = async () => {
136-
const callbackExecutor: (fn: VoidFunction) => void = __TEST__ ? f => f() : util.catch
137-
138135
if (!this.buttons.length) this.button(noI18n`Empty`, undefined, this.show)
139136

140137
const response = await showForm(this.form, this.player)
@@ -148,7 +145,17 @@ class Form {
148145
)
149146

150147
if (typeof callback === 'function') {
151-
return callbackExecutor(() => callback(this.player, this.show))
148+
if (__TEST__) {
149+
// Call right here to throw error
150+
await callback(this.player, this.show)
151+
} else {
152+
try {
153+
await callback(this.player, this.show)
154+
} catch (e) {
155+
new FormCallback(this.form, this.player, this.show).error(String(e))
156+
console.error('Form error', e)
157+
}
158+
}
152159
}
153160
}
154161
}

src/lib/region/command.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const tpdb = table<{ type: string; i: number; enabled: boolean }>('regionTpTest'
5454
command
5555
.overload('tp')
5656
.setPermissions('techAdmin')
57+
.setDescription('Входит в режим телепортации по группе регионов. Полезно для поиска данжа')
5758
.setGroup('test')
5859
.executes(ctx => {
5960
const db = tpdb.get(ctx.player.id)

src/lib/region/form.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Player, world } from '@minecraft/server'
2-
import { ArrayForm, BUTTON, FormCallback, inspect, ModalForm, Region, Vec } from 'lib'
32
import { parseArguments, parseLocationArguments } from 'lib/command/utils'
43
import { form, NewFormCallback, NewFormCreator } from 'lib/form/new'
54
import { i18n, noI18n, textTable } from 'lib/i18n/text'
@@ -10,6 +9,12 @@ import { FlattenedSphereArea } from './areas/flattened-sphere'
109
import { RectangleArea } from './areas/rectangle'
1110
import { SphereArea } from './areas/sphere'
1211
import { regionTypes } from './command'
12+
import { Region } from './kinds/region'
13+
import { ArrayForm } from 'lib/form/array'
14+
import { BUTTON, FormCallback } from 'lib/form/utils'
15+
import { ModalForm } from 'lib/form/modal'
16+
import { Vec } from 'lib/vector'
17+
import { inspect } from 'lib/utils/inspect'
1318

1419
export const regionForm = form((f, { player, self }) => {
1520
f.title(noI18n`Управление регионами`)

src/modules/test/edit-structure.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { BlockVolume, LocationInUnloadedChunkError, world } from '@minecraft/server'
2+
import { MinecraftBlockTypes } from '@minecraft/vanilla-data'
3+
import { Region, Vec } from 'lib'
4+
import { StructureDungeonsId } from 'lib/assets/structures'
5+
import { form } from 'lib/form/new'
6+
import { noI18n } from 'lib/i18n/text'
7+
8+
const f = form((f, { player }) => {
9+
for (const [name, id] of Object.entries(StructureDungeonsId)) {
10+
let details = noI18n.error`Не удалось найти структуру`
11+
const structure = world.structureManager.get(id)
12+
if (structure) {
13+
details = Vec.string(structure.size, true)
14+
}
15+
16+
f.button(`${name}\n${details}`, () => {
17+
if (!structure) return player.fail('Не удалось найти структуру')
18+
19+
const box = new BlockVolume(
20+
Vec.floor(player.location),
21+
Vec.add(player.location, structure.size).add(Vec.up).floor(),
22+
)
23+
const dimension = player.dimension
24+
25+
try {
26+
for (const location of dimension
27+
.getBlocks(box, { excludeTypes: [MinecraftBlockTypes.Air] })
28+
.getBlockLocationIterator()) {
29+
player.fail(
30+
noI18n.error`Блок на ${location} (${dimension.getBlock(location)?.typeId}) не является воздухом. Невозможно установить структуру`,
31+
)
32+
return
33+
}
34+
} catch (e) {
35+
if (e instanceof LocationInUnloadedChunkError) return player.fail(noI18n.error`Область не прогружена: ${e}`)
36+
throw e
37+
}
38+
39+
const structureBlock = dimension.getBlock(Vec.floor(player.location))
40+
if (!structureBlock) return player.fail(noI18n.error`Не работает`)
41+
42+
structureBlock.setType(MinecraftBlockTypes.StructureBlock)
43+
44+
const start = Vec.floor(player.location).add(Vec.up)
45+
for (const vector of Vec.forEach(start, Vec.add(start, structure.size).substract(Vec.one))) {
46+
dimension.setBlockType(vector, MinecraftBlockTypes.StructureVoid)
47+
}
48+
49+
world.structureManager.place(structure, dimension, start)
50+
51+
player.success(
52+
'Установлено. Как закончите редактировать, сохраните вручную и удалите топориком пушто майн не дает автоматизировать это :(',
53+
)
54+
})
55+
}
56+
})
57+
58+
new Command('edit-structure')
59+
.setDescription('Редактирует структуру для дальнейшего сохранения. Полезно для настройки лута данжей')
60+
.setPermissions('techAdmin')
61+
.executes(ctx => {
62+
const region = Region.getAt(ctx.player)
63+
if (region)
64+
return ctx.player.fail(
65+
noI18n.error`Невозможно установить структуру в регионе ${region.displayName ?? region.name}, найдите место без региона и других построек`,
66+
)
67+
68+
f.command(ctx)
69+
})

src/modules/test/test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import { requestAirdrop } from 'modules/places/anarchy/airdrop'
5050
import { BaseRegion } from 'modules/places/base/region'
5151
import { skipForBlending } from 'modules/world-edit/utils/blending'
5252
import loot from '../quests/learning/airdrop'
53+
import './edit-structure'
5354
import './enchant'
5455
import './load-chunks'
5556
import './minimap'

src/modules/world-edit/commands/region/set/set-command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const set = new Command('set')
1111
set
1212
.string('block')
1313
.array('mode', Object.keys(REPLACE_MODES), true)
14-
.string('replaceBlock')
14+
.string('replaceBlock', true)
1515
.executes((ctx, block, replaceMode = '', replaceBlock) => {
1616
const we = WorldEdit.forPlayer(ctx.player)
1717
if (!we.selection) return ctx.reply('§cЗона не выделена!')

0 commit comments

Comments
 (0)