Skip to content

Commit

Permalink
fix: improve internal tools
Browse files Browse the repository at this point in the history
  • Loading branch information
leaftail1880 committed Aug 25, 2024
1 parent c118dc4 commit 8f183a4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 34 deletions.
57 changes: 23 additions & 34 deletions src/modules/commands/db.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Player, system, world } from '@minecraft/server'
import { ROLES, getRole, inspect, util } from 'lib'
import { ArrayForm, ROLES, getRole, inspect, util } from 'lib'
import { DatabaseTable, getProvider } from 'lib/database/abstract'
import { ActionForm } from 'lib/form/action'
import { ModalForm } from 'lib/form/modal'
Expand All @@ -23,41 +23,30 @@ function selectTable(player: Player, firstCall?: true) {

function showTable(player: Player, tableId: string, table: DatabaseTable) {
const selfback = () => showTable(player, tableId, table)
const menu = new ActionForm(tableId)

menu.addButton(ActionForm.backText, () => selectTable(player))
menu.addButton('§3Новое значение§r', () => {
changeValue(
player,
null,
(newVal, key) => {
table[key] = newVal
},
selfback,
)
})

menu.addButton('§3Посмотреть в §fRAW', () => {
let raw = getProvider().getRawTableData(tableId)
try {
if (typeof raw === 'string') raw = JSON.parse(raw) as typeof raw
} catch {}

new ActionForm('§3RAW table §f' + tableId, inspect(raw)).addButton('Oк', selectTable).show(player)
})

const keys = Object.keys(table)
for (const key of keys) {
let name = key
if (tableId === 'player') {
const playerDatabase = table as typeof Player.database
name = `${playerDatabase[key].name} ${(ROLES[getRole(key)] as string | undefined) ?? '§7Без роли'}\n§8(${key})`
}

menu.addButton(name, () => tableProperty(key, table, player, selfback))
}
new ArrayForm(`${tableId} ${keys.length}`, keys)
.addCustomButtonBeforeArray(form => {
form
.addButton('§3Новое значение§r', () => {
changeValue(player, null, (newVal, key) => (table[key] = newVal), selfback)
})
.addButtonPrompt('§cОчистить таблицу', 'ДААА УДАЛИТЬ ВСЕ НАФИГ', () => {
Object.keys(table).forEach(e => Reflect.deleteProperty(table, e))
})
})
.back(() => selectTable(player))
.button(key => {
let name = key
if (tableId === 'player') {
const playerDatabase = table as typeof Player.database
name = `${playerDatabase[key].name} ${(ROLES[getRole(key)] as string | undefined) ?? '§7Без роли'}\n§8(${key})`
} else {
name += `\n§7${JSON.stringify(table[key]).replace(/"/g, '')}`
}

menu.show(player)
return [name, () => tableProperty(key, table, player, selfback)] as const
})
.show(player)
}

function tableProperty(key: string, table: DatabaseTable, player: Player, back: VoidFunction) {
Expand Down
1 change: 1 addition & 0 deletions src/modules/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import './camera'
import './db'
import './gamemode'
import './help'
import './pid'
import './kill'
import './leaderboard'
import './mail'
Expand Down
29 changes: 29 additions & 0 deletions src/modules/commands/pid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Player } from '@minecraft/server'
import { is, ModalForm } from 'lib'
import { selectPlayer } from 'lib/form/select-player'

new Command('pid')
.setDescription('Выдает ваш айди')
.executes(ctx =>
pid(
ctx.player.id,
ctx.player.name,
ctx.player,
is(ctx.player.id, 'techAdmin') ? () => playersPid(ctx.player) : () => void 0,
),
)

.overload('get')
.setPermissions('techAdmin')
.setDescription('Открывает форму для получения айди других игроков')
.executes(ctx => playersPid(ctx.player))

function playersPid(player: Player) {
selectPlayer(player, 'PID').then(({ id, name }) => {
pid(id, name, player, () => playersPid(player))
})
}

function pid(id: string, name: string, player: Player, then: VoidFunction) {
new ModalForm('PlayerID').addTextField(name, 'pid', id).show(player, then)
}

0 comments on commit 8f183a4

Please sign in to comment.