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

Allow to create custom client & communication between clients #1254

Merged
merged 9 commits into from
Dec 27, 2023

Conversation

zardoy
Copy link
Contributor

@zardoy zardoy commented Aug 17, 2023

For PrismarineJS/flying-squid#491
Also allow creating custom clients so don't need to patch it with webpack

src/client.js Outdated
@@ -13,9 +13,11 @@ const createDecipher = require('./transforms/encryption').createDecipher
const closeTimeout = 30 * 1000

class Client extends EventEmitter {
constructor (isServer, version, customPackets, hideErrors = false) {
constructor (isServer, version, customPackets, hideErrors = false, customCommunication = undefined) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, pretty sure it would be better if it was an object like

Suggested change
constructor (isServer, version, customPackets, hideErrors = false, customCommunication = undefined) {
constructor (isServer, version, additionalOptions) {

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this whole change makes a lot of sense.

What about instead letting the user provide its own Client class ?

This is overriding pretty much everything so I don't think it really fits in Client

src/client.js Outdated
this.serializer.write({ name, params })

if (this.customCommunication) {
this.customCommunication.sendData({ name, params, state: this.state })
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't support writeBundle and writeRaw as I'm not sure how flying-squid could use it

@rom1504
Copy link
Member

rom1504 commented Aug 25, 2023

please open a pr on flying squid showing how you use it

@zardoy
Copy link
Contributor Author

zardoy commented Aug 26, 2023

please open a pr on flying squid showing how you use it

Oke! I've included some other important changes to the same branch, so I have to rebase it first I guess. Will look into it tomorrow!

src/client.js Outdated Show resolved Hide resolved
src/client.js Outdated Show resolved Hide resolved
@@ -32,7 +32,8 @@ function createClient (options) {
options.protocolVersion = version.version
const hideErrors = options.hideErrors || false

const client = new Client(false, version.minecraftVersion, options.customPackets, hideErrors)
const Client = options.customClient ?? DefaultClient
Copy link
Member

Choose a reason for hiding this comment

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

Why isn't that option enough? Seems like it is to me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't it to allow users to provide custom implementation of the Client class? Like you said in #1254 (comment)

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 think I got it. I refactored all it. See latest commits

@zardoy
Copy link
Contributor Author

zardoy commented Sep 18, 2023

Also FYI setProtocol call should be moved before tcpDns since the latter is emitting connect event before the an event listener for it is added in setProtocol (when you use stream option)

@extremeheat
Copy link
Member

Why can't you implement this functionality with a custom options.customClient in createClient defined outside nmp? If the client and server are both embedded there is no reason to go through the authentication, compression or serialization steps here, as you can simply share one EventEmitter between the two.

@zardoy
Copy link
Contributor Author

zardoy commented Sep 19, 2023

You are totally right! I'm sorry I missed the intention to have custom client implementation outside of nmp (node minecraft protocol) in #1254 (comment). Since then we can have just custom server and client options here. And yes, I don't skip auth currently, most probably I could just emit login event directly..

@zardoy
Copy link
Contributor Author

zardoy commented Sep 23, 2023

done. I simply moved this custom client to pwc, I'm still not sure about skipping auth even in the context of the local server, I feel it can break mineflayer...

@@ -34,7 +34,8 @@ automatically logged in and validated against mojang's auth.
* enforceSecureProfile (optional) : Kick clients that do not have chat signing keys from Mojang (1.19+)
* generatePreview (optional) : Function to generate chat previews. Takes the raw message string and should return the message preview as a string. (1.19-1.19.2)
* socketType (optional) : either `tcp` or `ipc`. Switches from a tcp connection to a ipc socket connection (or named pipes on windows). With the `ipc` option `host` becomes the path off the ipc connection on the local filesystem. Example: `\\.\pipe\minecraft-ipc` (Windows) `/tmp/minecraft-ipc.sock` (unix based systems). See the ipcConnection example for an example.

* Server : You can pass a custom server class to use instead of the default one.
Copy link
Member

Choose a reason for hiding this comment

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

it would be really good to say here what methods this class must expose

* realms : An object which should contain one of the following properties: `realmId` or `pickRealm`. When defined will attempt to join a Realm without needing to specify host/port. **The authenticated account must either own the Realm or have been invited to it**
* realmId : The id of the Realm to join.
* pickRealm(realms) : A function which will have an array of the user Realms (joined/owned) passed to it. The function should return a Realm.
* Client : You can pass a custom client class to use instead of the default one, which would allow you to create completely custom communication. Also note that you can use the `stream` option instead where you can supply custom duplex, but this will still use serialization/deserialization of packets.
Copy link
Member

Choose a reason for hiding this comment

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

it would be good to say here what methods this class must expose

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 guess there is the typing for that (with all signatures etc)

@rom1504
Copy link
Member

rom1504 commented Dec 18, 2023

left a comment regarding the doc but otherwise lgtm

@rom1504
Copy link
Member

rom1504 commented Dec 27, 2023

ok

@rom1504 rom1504 merged commit 9e99109 into PrismarineJS:master Dec 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Closed
Development

Successfully merging this pull request may close these issues.

3 participants