-
Notifications
You must be signed in to change notification settings - Fork 100
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
zardoy
wants to merge
25
commits into
PrismarineJS:master
Choose a base branch
from
zardoy:ts-master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 16 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
f721622
phase 1: simply rename to modules -> *.ts
zardoy 90caa81
phase 2: add typings with script & a few manual fixes, fix a few bugs
zardoy bb15d04
add options types, fix some missing types
zardoy ff4da02
fix: setblock now correctly sets initial block state when no override
zardoy 156c82a
make commands typed, fix many bugs! improve /tp
zardoy ef2393f
fix tsc & use prepare for now
zardoy 76c5626
migrate docs to jsdoc with codemod, not documented are marked as inte…
zardoy 8a38cdb
fix: don't mutate generation options, fix storage provider is null fo…
zardoy 8eb54af
fix tab_complete crash in post-flatenning
zardoy 7705c79
add slash to aliases so its easier to discover them with search
zardoy 0fc0670
allow json resolve
zardoy 9f1fcdf
fix build
zardoy a6efc32
add events, fix ts build
zardoy 0a2a798
improve many types, fix build!
zardoy e8df1db
try ts-standard instead
zardoy 426969d
rename modules back to plugins
zardoy 4fd7926
fix jsdoc newlines
zardoy 9b5fe20
fix docs generator
zardoy 9b2c5f6
tests should be running as before
zardoy 184a9e6
rm old types
zardoy cfc3847
revert a few things...
zardoy 4414705
partially Revert "try ts-standard instead"
zardoy b514e46
rm mcdata refs
zardoy 51cceb4
revert even more things...
zardoy 1fdc39f
finally test that it works
zardoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,22 @@ | |
"name": "flying-squid", | ||
"description": "A minecraft server written in node.js", | ||
"version": "1.8.0", | ||
"main": "index.js", | ||
"main": "dist/index.js", | ||
"bin": "dist/app.js", | ||
"author": "mhsjlw <[email protected]>", | ||
"contributors": [ | ||
{ | ||
"name": "rom1504", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"bin": "app.js", | ||
"scripts": { | ||
"start": "tsc && node dist/app.js", | ||
"build": "tsc && node scripts/genTypes.mjs", | ||
"prepare": "npm run build", | ||
"prepublishOnly": "cp docs/README.md README.md", | ||
"lint": "standard test/*.test.js src/**/*.js src/**/**/*.js src/*.js examples/*.js *.js", | ||
"fix": "standard --fix test/*.test.js src/**/*.js src/**/**/*.js src/*.js examples/*.js *.js", | ||
"lint": "ts-standard test/*.test.js src/ examples/ *.js", | ||
"fix": "ts-standard --fix test/*.test.js src/ examples/ *.js", | ||
"mocha_test": "mocha --reporter spec --timeout 3000 --exit", | ||
"test": "npm run mocha_test", | ||
"pretest": "npm run lint" | ||
|
@@ -53,6 +56,7 @@ | |
"range": "^0.0.3", | ||
"readline": "^1.3.0", | ||
"spiralloop": "^1.0.2", | ||
"typed-emitter": "1.4.0", | ||
"uuid-1345": "^1.0.1", | ||
"vec3": "^0.1.6", | ||
"yargs": "^17.0.1" | ||
|
@@ -71,6 +75,7 @@ | |
"minecraft-wrap": "^1.2.3", | ||
"mineflayer": "^4.17.0", | ||
"mocha": "^10.0.0", | ||
"standard": "^17.0.0" | ||
"standard": "^17.0.0", | ||
"ts-standard": "^12.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import fs from 'fs' | ||
|
||
const targetFile = './dist/types.d.ts'; | ||
let types = fs.readFileSync(targetFile, 'utf8') | ||
const plugins = fs.readdirSync('./dist/lib/plugins').filter(f => f !== 'index') | ||
types = plugins.filter(module => module.endsWith('.d.ts')).map(module => `import "./lib/plugins/${module}"`).join('\n') + '\n' + types | ||
fs.writeFileSync(targetFile, types, 'utf8') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
// make process.platform also accept browser | ||
declare namespace NodeJS { | ||
interface Process { | ||
platform: string; | ||
browser?: boolean | ||
} | ||
interface Process { | ||
// @ts-expect-error | ||
platform: string | ||
browser?: boolean | ||
} | ||
|
||
} | ||
interface NodeRequire { | ||
// webpack bundling | ||
context: (path: string, deep: boolean, filter: RegExp) => { keys: () => string[]; (id: string): any }; | ||
// 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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { createServer, Server as ProtocolServer } from 'minecraft-protocol' | ||
|
||
import { testedVersions, latestSupportedVersion, oldestSupportedVersion } from './lib/version' | ||
import Command from './lib/command' | ||
import * as plugins from './lib/plugins' | ||
import { EventEmitter } from 'events' | ||
|
||
import { IndexedData } from 'minecraft-data' | ||
import './types' // include Server declarations from all plugins | ||
import './modules' | ||
|
||
if (typeof process !== 'undefined' && !process.browser && process.platform !== 'browser' && parseInt(process.versions.node.split('.')[0]) < 18) { | ||
console.error('[\x1b[31mCRITICAL\x1b[0m] Node.JS 18 or newer is required') | ||
console.error('[\x1b[31mCRITICAL\x1b[0m] You can download the new version from https://nodejs.org/') | ||
console.error(`[\x1b[31mCRITICAL\x1b[0m] Your current Node.JS version is: ${process.versions.node}`) | ||
process.exit(1) | ||
} | ||
|
||
require('emit-then').register() | ||
if (process.env.NODE_ENV === 'dev') { | ||
require('longjohn') | ||
} | ||
|
||
type InputOptions = Partial<Options> & Pick<Options, 'version'> | ||
|
||
export function createMCServer (options: InputOptions) { | ||
const mcServer = new MCServer() | ||
mcServer.connect({ | ||
// defaults | ||
'max-entities': 100, | ||
...options | ||
}) | ||
return mcServer as unknown as Server | ||
} | ||
|
||
class MCServer extends EventEmitter { | ||
pluginsReady = false | ||
_server: ProtocolServer = null! | ||
constructor () { | ||
plugins.initPlugins() | ||
super() | ||
} | ||
|
||
connect (options) { | ||
const server = this as unknown as Server | ||
const registry = require('prismarine-registry')(options.version) | ||
if (!registry?.version) throw new Error(`Server version '${registry?.version}' is not supported, no data for version`) | ||
|
||
const versionData = registry.version | ||
if (versionData['>'](latestSupportedVersion)) { | ||
throw new Error(`Server version '${registry?.version}' is not supported. Latest supported version is '${latestSupportedVersion}'.`) | ||
} else if (versionData['<'](oldestSupportedVersion)) { | ||
throw new Error(`Server version '${registry?.version}' is not supported. Oldest supported version is '${oldestSupportedVersion}'.`) | ||
} | ||
|
||
server.commands = new Command({}) | ||
server._server = createServer(options) | ||
|
||
const promises: Array<Promise<any>> = [] | ||
for (const plugin of plugins.builtinPlugins) { | ||
promises.push(plugin.server?.(server, options)) | ||
} | ||
Promise.all(promises).then(() => { | ||
server.emit('pluginsReady') | ||
server.pluginsReady = true | ||
}) | ||
|
||
if (options.logging === true) server.createLog() | ||
server._server.on('error', error => { | ||
server.emit('error', error) | ||
}) | ||
server._server.on('listening', () => { | ||
// @ts-expect-error dont remember the right cast | ||
server.emit('listening', server._server.socketServer.address().port) | ||
}) | ||
server.emit('asap') | ||
} | ||
} | ||
|
||
declare global { | ||
interface Server { | ||
commands: Command | ||
pluginsReady: boolean | ||
_server: ProtocolServer | ||
supportFeature: (feature: string) => boolean | ||
} | ||
} | ||
|
||
export { | ||
testedVersions | ||
} | ||
|
||
export * as Behavior from './lib/behavior' | ||
export * as Command from './lib/command' | ||
export { default as generations } from './lib/generations' | ||
export * as experience from './lib/experience' | ||
export * as UserError from './lib/user_error' | ||
export { default as portal_detector } from './lib/portal_detector' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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