-
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
Refactor: allow usage in browser #633
Conversation
New and removed dependencies detected. Learn more about Socket for GitHub ↗︎
🚮 Removed packages: [email protected] |
@@ -21,7 +19,11 @@ | |||
"test": "npm run mocha_test", | |||
"pretest": "npm run lint" | |||
}, | |||
"keywords": [], | |||
"keywords": [ |
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.
FYI just added some keywords (why not)
@@ -38,23 +38,23 @@ function createMCServer (options) { | |||
|
|||
class MCServer extends EventEmitter { | |||
constructor () { | |||
plugins.initPlugins() |
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.
we now load plugins lazily
if (isInNode) { | ||
import(/* webpackIgnore: true */ 'exit-hook').then((hook) => { | ||
hook.default(() => { | ||
for (const serv of _servers) { |
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.
In the context of this refactor also fixed an important bug: worlds are not saved when server crashed or stopped by ctrl+c
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 understand the intent and I agree the feature is nice. However that couple the lib and the app parts of flying-squid
What about instead define this as some ServerManager class and use it in app.js ?
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.
What about exit-hook: this is totally okay to have it as part of the library and why not have it so "better behavior is enabled by default"?
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.
Hm, as I understand there is also no way to disable readline, right?
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.
"exit-hook: this is totally okay to have it as part of the library"
no that's exactly the part I don't like.
I think it's good if the library exposes a function that makes this possible, and then it's called by default in https://github.com/PrismarineJS/flying-squid/blob/master/app.js
having the library actually call it forces the user into a specific application setup
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.
no that's exactly the part I don't like.
Alright, I don't mind making it an opt-in behavior, however, then I think it would make sense to make creating readline interface optional as well. What do you think?
Otherwise I might be missing use-cases of this library (sorry)
I think it's good if the library exposes a function that makes this possible
Will extract & doc it later today or tomorrow, not a problem
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 think it's good if the library exposes a function that makes this possible, and then it's called by default
Maybe this should be done as option? like registerExitHook: true
?
src/lib/plugins/login.js
Outdated
try { | ||
const player = serv.initEntity('player', null, serv.overworld, new Vec3(0, 0, 0)) | ||
player._client = client | ||
player._client.socket ??= { remoteAddress: '' } |
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.
same here
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.
What about instead checking if it's defined where it's used ?
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 removed mocking remoteAddress
since nothing will crash if it is undefined
, but maybe it should be documented in some way I don't really know (so other plugins can know it can be undefined)
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.
where did you remove it ? I don't understand
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.
src/lib/plugins/login.js
Outdated
|
||
module.exports.server = function (serv, options) { | ||
serv._server.on('connection', client => | ||
client.on('error', error => serv.emit('clientError', client, error))) | ||
|
||
serv._server.on('login', async (client) => { | ||
if (client.socket.listeners('end').length === 0) return // TODO: should be fixed properly in nmp instead | ||
if (client.socket?.listeners('end').length === 0) return // TODO: should be fixed properly in nmp instead | ||
if (!serv.worldsReady) throw new Error('World is still preparing.') |
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.
As I remember this is another bugfix: when a player connects when world is not ready yet it crashes
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.
what about returning instead of throwing if this is not an error ?
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.
Yes of course it makes sense I fixed it here 923a829
theItem = mcData.blocksByName[itemName] | ||
} | ||
const itemName = item.id.value.slice(10) // skip game brand prefix | ||
const theItem = mcData.itemsByName[itemName] || mcData.blocksByName[itemName] |
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.
just refactored thats it
player.emit('spawned') | ||
|
||
await player.waitPlayerLogin() | ||
player.sendRestMap() | ||
sendChunkWhenMove() | ||
|
||
player.save() |
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.
by doing this a new entry in playerdata/ appears immediately after a new player joins
|
||
module.exports.server = function (serv, settings) { | ||
_servers.push(serv) |
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.
What is _servers ?
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 thought it was possible to create multiple servers. However, now I understand that I could just inline exit-hook handler here.
Overall looks good, but left some comments |
overall lgtm, merged |
/makerelease |
Depends on PrismarineJS/node-minecraft-protocol#1254
fixes #491
Usage:
Both esbuild and webpack needs Node.js modules polyfilling.
module: { parser: { javascript: { commonjsMagicComments: true } } }
First of all, I'm really sorry for the delay, I wish I could do it early.
Anyway, right now I decided to squash all changes from here into one commit.
Will try to provide a brief overview of the changes I made here in comments.
Refactored plugin loading so its compatible with webpack (at least I tested the first version of it) and esbuild that I use currently.
I did initially a huge refactor by adding types, but I reverted them so its easier to review. Anyway, feel free to ask any questions