Jump to content

[1.8] Send Message to Client on Disconnect


Apace

Recommended Posts

Hey guys,

it's me again.

 

I'm trying to get the configuration for my mod sent from the server to the client, so that the client can set its variables to that of the server.

I'm using an IMessage to send two variables from the server to the client, the message gets sent using the PlayerLoggedInEvent:

@SubscribeEvent
public void JoinServer(PlayerLoggedInEvent event) {
SolidStone.network.sendTo(new SSMessage(SSConfig.solidity, SSConfig.smeltingEnabled), (EntityPlayerMP)event.player);
}

 

This works fine. The client accepts the new values.

When trying to reset the values for the client, I use a different message "SSMessageReset" without any arguments and try to send it when the PlayerLoggedOutEvent triggers:

@SubscribeEvent
public void LeaveServer(PlayerLoggedOutEvent event) {
SolidStone.network.sendTo(new SSMessageReset(), (EntityPlayerMP)event.player);
}

 

This however, doesn't seem to produce any results. The LeaveServer method gets called, but the IMessageHandler for the SSMessageReset IMessage doesn't get called. I'm wondering if this is because the client has already disconnected?

 

FYI, the messages are registered like this, in the preInit method of my main mod class:

network = NetworkRegistry.INSTANCE.newSimpleChannel("SSChannel");
network.registerMessage(SSMessage.Handler.class, SSMessage.class, 0, Side.CLIENT);
network.registerMessage(SSMessageReset.Handler.class, SSMessageReset.class, 1, Side.CLIENT);

 

The only work-around I see for this, is that instead of using the same variables for both singleplayer and multiplayer games, the client stores two seperate sets of variables, effectively removing the need for a reset of the sent variables.

 

That option doesn't seem as elegant to me though, and it really (really) bugs me, that my first approach doesn't work.

I'm wondering if there's anything I might be doing wrong here, or if it really is true that the client has already disconnected when the PlayerLoggedOutEvent gets called.

 

Any answers to this?

 

Thanks in advance,

- Apace

Link to comment
Share on other sites

Logged in and out are called only on server, the client won't get any packet after logging out.

 

You shouldn't have problems here tho.

No matter if you join SP or MP, the server will still send you new values and update values from previous setting.

 

Problem might only occur if you'd try to login on server that doesn't have mod you have on your client.

The workaround is like you told yourself - before setting new values, save old ones somewhere and restore them after.

 

EDIT

Problem actually is the fact that - I could swear there was event like that  - I can't find client's event ClientConnectedToServer - or something like that.

You need to restore defaults (previous values) before receiving new ones from forge server, or not recieving any from vanilla server (because forge can connect to vanilla).

 

EDIT 2

There it is!

 

ClientConnectedToServerEvent

 

In net.minecraftforge.fml.common.network.FMLNetworkEvent

 

Hidden as fk.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Logged in and out are called only on server,

Yeah, that's what I figured.

the client won't get any packet after logging out.

Alright, good to know. Makes sense, now that I think about it. It is called "LoggedOut" and not "LoggingOut", after all.

 

 

You shouldn't have problems here tho.

No matter if you join SP or MP, the server will still send you new values and update values from previous setting.

Okay, I didn't notice that I would receive the config values from the integrated servers upon starting single-player as well. That simplifies things. :)

 

I can't find client's event ClientConnectedToServer - or something like that.

/snip

Hidden as fk.

I was looking around for something like that the past few hours as well, it sure is very hidden. :/

 

You need to restore defaults (previous values) before receiving new ones from forge server,

Okay, this part I don't understand. I mean, I did this now (the config reloads from the file whenever the client joins a game, using the ClientConnectedToServerEvent) and it works very well (awesome, thank you for that!) but why is this the case?

Wouldn't Java just set the variables to whatever the client receives when it joins a game?

 

I have one more problem now, though: When the client joins a vanilla server, it shouldn't load the default values, but different ones. How would I go about figuring out if the server has my mod installed or not?

Link to comment
Share on other sites

There are cases:

- You launch game, client loads block's properties from client's config.

- You login on server (doesn't matter if SP/MP)

- Server send you new properties.

- You log out of server and login on other server:

1. Next server is Vanilla:

- You need to bring back vanilla client configuration (or any other setup you wish to have), in order to do that I told you to use ClientConnectedToServerEvent since server won't call LoggedIn (It's vanilla after all).

- The values must be stored somewhere, that's why you need to cache them. When loading client's config whenever you do setBlockHardness (for example) you will also have to put vanilla hardness to some map.

- Then when connecting, you will restore that map with ClientConnectedToServerEvent.

 

2. Next server is Forge:

- You don't need to restore anything since server will send you values after all. Now where problem might appear is when your client has hanged more values than server will send new ones (so some of old values will not be changed).

- Again - you need to restore defaults (vanilla or whatever, like in 1. above).

 

Notice that client's ClientConnectedToServerEvent is fired BEFORE server's logged in, so actually both 1 and 2 will come together to one solution: You will be always setting defaults 1st, and then if the server would be forge - you will get new ones.

 

EDIT

Sry, I didn't answered last question, but only way i know works for server only.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Okay, I understand that. But previously, when I was testing it (Using the LoggedIn server event) it went like this:

 

1. I started Minecraft - the client loaded the config.

2. I joined Singleplayer - the client's values were in effect.

3. I joined Multiplayer (a server with the mod, but a different config) - the server's values got transferred to the client.

4. I joined Singleplayer again - the server's values were still in effect, instead of the client's values.

 

To clarify: I don't have a problem at the moment, everything works fine. It's just that I don't understand why things happened like they did previously.

 

EDIT: Fixed a mistake I made.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.