Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full TypeScript rewrite. Best type defs #651

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f721622
phase 1: simply rename to modules -> *.ts
zardoy Nov 2, 2023
90caa81
phase 2: add typings with script & a few manual fixes, fix a few bugs
zardoy Nov 3, 2023
bb15d04
add options types, fix some missing types
zardoy Nov 3, 2023
ff4da02
fix: setblock now correctly sets initial block state when no override
zardoy Nov 4, 2023
156c82a
make commands typed, fix many bugs! improve /tp
zardoy Nov 4, 2023
ef2393f
fix tsc & use prepare for now
zardoy Nov 4, 2023
76c5626
migrate docs to jsdoc with codemod, not documented are marked as inte…
zardoy Nov 5, 2023
8a38cdb
fix: don't mutate generation options, fix storage provider is null fo…
zardoy Nov 5, 2023
8eb54af
fix tab_complete crash in post-flatenning
zardoy Nov 11, 2023
7705c79
add slash to aliases so its easier to discover them with search
zardoy Nov 9, 2023
0fc0670
allow json resolve
zardoy Jan 8, 2024
9f1fcdf
fix build
zardoy Jan 8, 2024
a6efc32
add events, fix ts build
zardoy Jan 17, 2024
0a2a798
improve many types, fix build!
zardoy Jan 17, 2024
e8df1db
try ts-standard instead
zardoy Jan 17, 2024
426969d
rename modules back to plugins
zardoy Jan 17, 2024
4fd7926
fix jsdoc newlines
zardoy Jan 17, 2024
9b5fe20
fix docs generator
zardoy Jan 17, 2024
9b2c5f6
tests should be running as before
zardoy Jan 17, 2024
184a9e6
rm old types
zardoy Jan 17, 2024
cfc3847
revert a few things...
zardoy Jan 18, 2024
4414705
partially Revert "try ts-standard instead"
zardoy Jan 18, 2024
b514e46
rm mcdata refs
zardoy Jan 18, 2024
51cceb4
revert even more things...
zardoy Jan 18, 2024
1fdc39f
finally test that it works
zardoy Jan 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
make commands typed, fix many bugs! improve /tp
add block renames

(cherry picked from commit b9a2813)
  • Loading branch information
zardoy committed Jan 17, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 156c82a7d61c6f2240cc81bd594987600e085148
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
@@ -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 ? {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i tried to not add a lot of generics, but this one here is just super useful for other plugins

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 + ' ') || ''
@@ -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) {
@@ -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) ? '' : ' '
}
12 changes: 6 additions & 6 deletions src/lib/modules/blocks.ts
Original file line number Diff line number Diff line change
@@ -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) ? registry.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)
}
})

@@ -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])
}
})
}
Loading