ElegantNetworking is a packet system framework which seeks to make client-server interaction in MinecraftForge mods as easy as possible.
Example of packet declaration:
@ElegantPacket
@Value //lombok
public class PacketExample implements ClientToServerPacket {
int someValue1;
MyClass someValue2;
IMyInterface someValue3;
Map<String, MyClass> someValue4;
@Override
public void onReceive(EntityPlayerMP player, INetHandlerPlayServer handler) {
//packet handling
}
}
And usage of packet:
new PacketExample(1, new MyClass(...), new ImplOfMyInterface(...), ImmutableMap.of(...)).sendToServer();
And that's all you need!
- Auto-registration of packets
- enough to mark packet class by annotation
- you can forget about the channel and packet id's
- Automatic serialization/deserialization
- supported primitives, collections, data-classes(POJO) and algebric data types overall
- exists able to override logic of serialization
- Sendable data structure and receiving handler is localised in one place
- one packet - one class
- Design of api is offers to do not depend on the version of Minecraft
- Compatible with obfuscators(less tested)
- Add to build.gradle:
dependencies {
implementation "io.gitlab.hohserg.elegant.networking:elegant-networking-1.12:3.14"
annotationProcessor "io.gitlab.hohserg.elegant.networking:annotation-processor:3.14" //use compileOnly or `apt` plugin for gradle less that 4.8
}
- Perform Gradle refresh in your ide
- If you using Lombok than annotation processor of Lombok must be applied before annotation processor of ElegantNetworking, like:
annotationProcessor 'org.projectlombok:lombok:1.18.8', "io.gitlab.hohserg.elegant.networking:annotation-processor:3.14"
- Create new class for packet
If your packet must be sended from client to server than it must implement ClientToServerPacket
If your packet must be sended from server to client than it must implement ServerToClientPacket
-
onReceive
method will be called when packet is received -
Mark you packet class with @ElegantPacket annotation
Optionaly, you can specify a channel name for packet (have sense if your jar contains more that one mod)
- Add fields that represent transmitted information
Able to use Lombok to generate useful constructor, getters and setters
- Optionally, you can override logic of serialization.
Override serialize
method, it must write data to ByteBuf
argument.
And add a unserialization-constructor with single argument ByteBuf
, it must read data from ByteBuf
.
Example:
@ElegantPacket
@Value
public class ExamplePacket implements ServerToClientPacket {
int someInt;
Map<MyClass, String> someMap;
@Override
public void onReceive(Minecraft mc) {
System.out.println("test "+someInt+" "+someMap);
}
}
Just create instance of your packet and call one of send-methods
//Sending `someInt=1` and `someMap={new MyClass("test") -> "lol"}` to all players in dimension `world`
new ExamplePacket(1, ImmutableMap.of(new MyClass("test"), "lol")).sendToDimension(world);
EN is compatible with incremental compilation not so good as we want. If you have some strange issue, try to rebuild project.
- Create folder, i.e.
ElegantNetworking
git clone https://github.com/ElegantNetworking/ElegantNetworkingRoot.git
git clone https://github.com/ElegantNetworking/ElegantNetworkingCommon.git
git clone https://github.com/ElegantNetworking/ElegantNetworkingAnnotationProcessor.git
git clone https://github.com/ElegantNetworking/ElegantNetworking_1.16.git
- Import
ElegantNetworkingRoot
gradle project into your IDE cd ElegantNetworkingRoot
- Use
gradlew :ElegantNetworking_1.16:runClient
for run game - Use
gradlew :ElegantNetworking_1.16:build
for build runtime library - Use
gradlew :ElegantNetworkingAnnotationProcessor:shadowJar
for build annotation processor
Thx @Dahaka934 for discussion and review
Thx @tox1cozZ for drew my attention to the annotation processors
Thx @Plasticable for advice of using gradle 4.4.1 (not actualy, but helpful in due time)
Thx @Icosider for consulting about gradle configuration
Thx @AmaZ1nG for idea about serialization of nbt and other basic types that useful in modding
Thx @CDAGaming for the fork of FG2.1 that compatible with gradle 5+
Thx @Liahim85 for pretty logo
Thx @sqcode06 for some readme edits
Thx @GoogeTan for testing
Thx @anatawa12 for good FG1.2 fork and @GlassSpirit for help with setup anatawa12's FG