Make the networking simpler and universal for everyone #70
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.
It's not a secret BTA networking is kind of a mystery for a lot of people.
They're also the fact that BTA got two kind of configuration, the client which we have the rendering and the server which with have the world logic. But this would forget that BTA also support an hybrid between the two called SinglePlayer.
Also the Packet system in BTA is prone too conflict since id are only one byte and the number of id available for everyone is limited.
This PR do two things to help this :
EnvironmenttHelper
where we can easily know if we are : Server, Client, SinglePlayerNetworkMessage
NetworkMessage
is an Interface with a role similar as Packet except I don't force you to extend from a class and most important don't make you depend directly on the way BTA since sometime if you don't consume every byte send by BTA, some strange network error can occurred or more often garbage data will be read as invalid packet (trust me, this is weird like bug).Here an example of a
NetworkMessage
Here you can see our message is separate in three part :
UniversalPacket
UniversalPacket
to reconstruct our messagePlease note the network system I present you will skip the encoding and decoding if we try to send a packet from SinglePlayer for performance reason.
In my example you can also see something I called the
UniversalPacket
, this a class that instead from the BTAPacket
and is 100% inspired by JavaDataInput
/DataOutput
, nettyByteBuf
or modern MinecraftPacketByteBuf
This class has some more advantage than a Java data stream since it will never throw
IOException
or need try catch.Also this class got some more Minecraft oriented helper like be capable to write or read NBT data directly in it.
So now we got our NetworkMessage, we need to register in the start of our game, for that it's very easy since we can literally register it our ModInitializer
The
NetworkHandler
is the master brain of the messaging system, yourNetworkMessage
and your message is ready to be send and receive from everywhere without any conflict ! It's magic !.Now you can just use the
NetworkHandler
from everywhere to do thing like this :It's that easy, to recap all you need at the end using this abstraction is :
NetworkMessage
with encoding, decoding and handlingNetworkMessage
in your mod Initializer.NetworkHander
from everywhere to send your message and profit !For the full example in a tiny project you can look at here https://github.com/gungun974/halplibe-network-message-example
In this example I added the command
/test openInventory
which will send to the client console a message that contain a custom string from the server and will open the inventory for the player.Please note this is a demonstration of the messaging system, the code for opening the player inventory is a bit broken to be honest.
Of course this PR is a proposal and I would hope to discuss on what we can approve. I think adding to
halplibe
a standard way to communicate the client server with SinglePlayer compatibility is something important for the future of BTA modding.Thank for reading all of this ^^