Skip to content

Commit

Permalink
fix: fix gamerules handling!
Browse files Browse the repository at this point in the history
fix: improve output commands formatting, especially errored ones
  • Loading branch information
zardoy committed Nov 21, 2024
1 parent b9ffbd8 commit 69e2a72
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
20 changes: 15 additions & 5 deletions src/lib/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Command {
return undefined
}

async use (command, ctx: Ctx<false> = {}, op = true) {
async use (command: string, ctx: Ctx<false>, op = true) {
const resultsFound = this.find(command)
let parsedResponse
if (resultsFound) {
Expand All @@ -69,10 +69,20 @@ class Command {
parsedResponse = passedArgs.match(customArgsParser)
}
}
let output
if (parsedResponse) output = await wantedCommand.params.action(parsedResponse, ctx)
else output = await wantedCommand.params.action(resultsFound[1], ctx) // just give back the passed arg
if (output) return '' + output
try {
let output
if (parsedResponse) output = await wantedCommand.params.action(parsedResponse, ctx)
else output = await wantedCommand.params.action(resultsFound[1], ctx) // just give back the passed arg
if (output) return {
success: output
}
} catch (err) {
console.error(`Error in command from ${ctx.player?.username || 'server'} "${command}":`)
console.error(err)
return {
error: `Internal server error: ${err.message}`
}
}
} else {
if (ctx.player) return 'Command not found'
else throw new UserError('Command not found')
Expand Down
11 changes: 7 additions & 4 deletions src/lib/modules/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export const player = function (player: Player, serv: Server, { version }: Optio
player.handleCommand = async (str) => {
try {
const res = await serv.commands.use(str, { player }, player.op)
if (res) player.chat(serv.color.green + res)
const success = typeof res === 'string' ? res : (res && 'success' in res ? res.success : '')
if (success) player.chat(serv.color.green + success)
else if (res && typeof res === 'object' && 'error' in res) player.chat(serv.color.red + res.error)
} catch (err) {
if (err.userError) player.chat(serv.color.red + 'Error: ' + err.message)
else setTimeout(() => { throw err }, 0)
Expand All @@ -23,9 +25,10 @@ export const entity = function (entity: Entity, serv: Server) {
export const server = function (serv: Server, { version }: Options) {
serv.handleCommand = async (str, ctx, op) => {
try {
const res = await serv.commands.use(str, ctx, op)
if (res) serv.info(res)
return res
const res = await serv.commands.use(str, ctx ?? {}, op)
const success = typeof res === 'string' ? res : (res && 'success' in res ? res.success : '')
if (success) serv.info(success)
return typeof res === 'string' ? res : (res && 'success' in res ? res.success : res?.error)
} catch (err) {
if (err.userError) serv.err(err.message)
else setTimeout(() => { throw err }, 0)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/modules/daycycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const server = function (serv: Server) {
serv.on('tick', (delta, count) => {
// TODO
// const disabledByGamerule = 'doDayLightCycle doDayLightcycle DayNightCycle'
const disabledByGamerule = 'doDayLightCycle'.split(' ').some(x => serv.levelData?.GameRules[x] === 'false') || !serv.gamerules.doDayLightCycle
const disabledByGamerule = !serv.gamerules.doDaylightCycle
if (!serv.doDaylightCycle || disabledByGamerule) return
if (count % 20 === 0) {
serv.behavior('changeTime', {
Expand Down
31 changes: 25 additions & 6 deletions src/lib/modules/gamerules.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
export const server = function (serv: Server, options: Options) {
serv.gamerules ??= {}
serv.on('asap', () => {
serv.on('pluginsReady', () => {
for (const [key, val] of Object.entries(serv.levelData?.GameRules ?? {})) {
serv.gamerules[key] = val === 'true'
}
})

const gameRules = {
'doDayLightCycle': 'Wether to enable daylight cycle'
const knownGameRules = [
"doTileDrops",
"doFireTick",
"reducedDebugInfo",
"naturalRegeneration",
"doMobLoot",
"keepInventory",
"doEntityDrops",
"mobGriefing",
"randomTickSpeed",
"commandBlockOutput",
"doMobSpawning",
"logAdminCommands",
"sendCommandFeedback",
"doDaylightCycle",
"showDeathMessages",
]

const gameRules: Partial<typeof serv.gamerules> = {
'doDaylightCycle': 'Wether to enable daylight cycle'
}

serv.commands.add({
Expand All @@ -23,9 +41,10 @@ export const server = function (serv: Server, options: Options) {
if (!rule) {
return `Available these gamerules: ${Object.entries(gameRules).map(([key, desc]) => `${key}: ${desc}`).join(', ')}`
}
if (!knownGameRules.includes(rule)) ctx.player?.chat(serv.color.yellow + `Warning: gamerule ${rule} is not known`)
if (newVal) {
serv.gameMode[rule] = newVal === 'false' ? false : true
return `set ${rule} to ${serv.gamerules[rule]}`
serv.gamerules[rule] = newVal === 'false' ? false : true
return `Set ${rule} to ${serv.gamerules[rule]}`
} else {
const gamerule = serv.gamerules[rule]
return `${rule} is ${gamerule ? 'Enabled' : 'Disabled'} (${gamerule})`
Expand All @@ -36,6 +55,6 @@ export const server = function (serv: Server, options: Options) {

declare global {
interface Server {
gamerules: Record<string, boolean>
gamerules: Partial<NonNullable<NonNullable<Server['levelData']>['GameRules']>>
}
}
2 changes: 1 addition & 1 deletion src/lib/modules/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ declare global {
/** Global spawn and respawn point for every player */
spawnPoint?: Vec3
/** Parsed level.dat of the loaded world (only if worldFolder is specificed) */
levelData?: LevelDatFull & { GameRules?}
levelData?: Partial<LevelDatFull>
worlds: Record<string, CustomWorld>
/** Contains the overworld world. This is where the default spawn point is */
"overworld": CustomWorld
Expand Down

0 comments on commit 69e2a72

Please sign in to comment.