Skip to content

Commit

Permalink
make commands typed, fix many bugs! improve /tp
Browse files Browse the repository at this point in the history
add block renames
  • Loading branch information
zardoy committed Nov 4, 2023
1 parent e4be9d8 commit b9a2813
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 96 deletions.
103 changes: 103 additions & 0 deletions src/blockRenames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

// postflatenning
const blockItemRenames = {
"1.13.2": {
"blocks": [],
"items": [
]
},
"1.14": {
"blocks": [],
"items": [
["sign", "oak_sign"],
["rose_red", "red_dye"],
["cactus_green", "green_dye"],
["dandelion_yellow", "yellow_dye"]
]
},
"1.14.4": {
"blocks": [
["sign", "oak_sign"],
["wall_sign", "oak_wall_sign"]
],
"items": [
]
},
"1.15.2": {
"blocks": [],
"items": []
},
"1.16.1": {
"blocks": [],
"items": [
["zombie_pigman_spawn_egg", "zombified_piglin_spawn_egg"]
]
},
"1.16.2": {
"blocks": [],
"items": [
]
},
"1.17": {
"blocks": [
["grass_path", "dirt_path"],
],
"items": [
["grass_path", "dirt_path"],
]
},
"1.18": {
"blocks": [],
"items": [
]
},
"1.19": {
"blocks": [],
"items": []
},
"1.19.3": {
"blocks": [],
"items": []
},
"1.19.4": {
"blocks": [],
"items": []
},
"1.20": {
"blocks": [],
"items": [
["pottery_shard_archer", "archer_pottery_sherd"],
["pottery_shard_prize", "prize_pottery_sherd"],
["pottery_shard_arms_up", "arms_up_pottery_sherd"],
["pottery_shard_skull", "skull_pottery_sherd"]
]
}
}

const versionToNumber = (ver: string) => {
const [x, y = '0', z = '0'] = ver.split('.')
return +`${x.padStart(2, '0')}${y.padStart(2, '0')}${z.padStart(2, '0')}`
}

const allRenamesMapFromLatest = Object.fromEntries(
['blocks', 'items'].map(x =>
[
x,
Object.fromEntries(Object.entries(blockItemRenames).flatMap(([ver, t]) => t[x].map(([oldName, newName]) => [
newName,
{ version: versionToNumber(ver), oldName }
])))
])
) as { [thing: string]: Record<string, { version: number, oldName: string }> }

export const adoptBlockOrItemNamesFromLatest = (type: 'blocks' | 'items', version: string, names: string[]) => {
const map = allRenamesMapFromLatest[type]
const ver = versionToNumber(version)
return names.map(name => {
const renamed = map[name] // todo it might be useful if followed by chain
if (renamed && ver < renamed.version) {
return renamed.oldName
}
return name
})
}
8 changes: 8 additions & 0 deletions src/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ interface NodeRequire {
// webpack bundling
context: (path: string, deep: boolean, filter: RegExp) => { keys: () => string[]; (id: string): any }
}

interface ObjectConstructor {
keys<T extends object> (obj: T): Array<StringKeys<T>>
entries<T extends object> (obj: T): Array<[StringKeys<T>, T[keyof T]]>
// todo review https://stackoverflow.com/questions/57390305/trying-to-get-fromentries-type-right
fromEntries<T extends Array<[string, any]>> (obj: T): Record<T[number][0], T[number][1]>
assign<T extends Record<string, any>, K extends Record<string, any>> (target: T, source: K): asserts target is T & K
}
37 changes: 30 additions & 7 deletions src/lib/command.js → src/lib/command.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
//@ts-check
import UserError from './user_error'

type Ctx<P extends boolean> = P extends true ? {
player: Player
} : {
player?: Player
}

type NonFalsey<T> = T extends false ? never : NonNullable<T>

type AddParams<T, P extends boolean = false> = {
base: string,
aliases?: string[],
info: string,
usage: string,
onlyPlayer?: P
op?: boolean
parse?: (string: string, ctx: Ctx<P>) => T
action: (data: NonFalsey<T>, ctx: Ctx<P>) => any
tab?: string[]
}

class Command {
constructor (params, parent, hash) {
this.params = params
this.parent = parent
hash: any
uniqueHash: any
parentBase: any
base: any
path: string

constructor (public params, public parent?, hash?) {
this.hash = parent ? parent.hash : {}
this.uniqueHash = parent ? parent.uniqueHash : {}
this.parentBase = (this.parent && this.parent.base && this.parent.base + ' ') || ''
Expand All @@ -21,7 +44,7 @@ class Command {
return undefined
}

async use (command, ctx = {}, op = true) {
async use (command, ctx: Ctx<false> = {}, op = true) {
const resultsFound = this.find(command)
let parsedResponse
if (resultsFound) {
Expand Down Expand Up @@ -70,11 +93,11 @@ class Command {
this.uniqueHash[this.base] = this
}

add (params) {
add <T, P extends boolean>(params: AddParams<T, P>) {
return new Command(params, this)
}

space (end) {
space (end = false) {
const first = !(this.parent && this.parent.parent)
return this.params.merged || (!end && first) ? '' : ' '
}
Expand Down
12 changes: 6 additions & 6 deletions src/lib/modules/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,18 @@ export const server = function (serv: Server, { version }: Options) {
},
action (params, ctx) {
let res = params.slice(1, 4)
if (ctx.player) res = res.map((val, i) => serv.posFromString(val, ctx.player.position[['x', 'y', 'z'][i]]))
if (ctx.player) res = res.map((val, i) => serv.posFromString(val, ctx.player!.position[['x', 'y', 'z'][i]]))
else res = res.map((val, i) => serv.posFromString(val, new Vec3(0, 128, 0)[['x', 'y', 'z'][i]]))

const blockParam = params[4]
const id = isNaN(+blockParam) ? mcData.blocksByName[skipMcPrefix(blockParam)]?.id : +blockParam
const data = parseInt(params[5] || 0, 10)
const data = parseInt(params[5] || '0', 10)
const stateId = postFlatenning
? data ? (blocks[id].minStateId! + data) : blocks[id].defaultState!
: (id << 4 | data)

if (ctx.player) ctx.player.setBlock(new Vec3(res[0], res[1], res[2]).floored(), stateId)
else serv.setBlock(serv.overworld, new Vec3(res[0], res[1], res[2]).floored(), stateId)
if (ctx.player) ctx.player.setBlock(new Vec3(+res[0], +res[1], +res[2]).floored(), stateId)
else serv.setBlock(serv.overworld, new Vec3(+res[0], +res[1], +res[2]).floored(), stateId)
}
})

Expand All @@ -98,8 +98,8 @@ export const server = function (serv: Server, { version }: Options) {
return results
},
action (params, ctx) {
if (ctx.player) ctx.player.setBlockAction(new Vec3(params[1], params[2], params[3]).floored(), params[4], params[5])
else serv.setBlockAction(serv.overworld, new Vec3(params[1], params[2], params[3]).floored(), params[4], params[5])
if (ctx.player) ctx.player.setBlockAction(new Vec3(+params[1], +params[2], +params[3]).floored(), +params[4], params[5])
else serv.setBlockAction(serv.overworld, new Vec3(+params[1], +params[2], +params[3]).floored(), +params[4], params[5])
}
})
}
Expand Down
Loading

0 comments on commit b9a2813

Please sign in to comment.