Last active
June 28, 2022 21:56
-
-
Save CatDany/4a3df7fcb3c8270cf70b to your computer and use it in GitHub Desktop.
Packets are Easy (by Dany)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@EventHandler | |
public static void init(FMLInitializationEvent e) | |
{ | |
// this should be in initialization | |
PacketHandler.initPackets(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PacketHandler | |
{ | |
public static SimpleNetworkWrapper net; | |
public static void initPackets() | |
{ | |
net = NetworkRegistry.INSTANCE.newSimpleChannel("YourModId".toUpperCase()); | |
registerMessage(SimplePacket.class, SimplePacket.SimpleMessage.class); | |
} | |
private static int nextPacketId = 0; | |
private static void registerMessage(Class packet, Class message) | |
{ | |
net.registerMessage(packet, message, nextPacketId, Side.CLIENT); | |
net.registerMessage(packet, message, nextPacketId, Side.SERVER); | |
nextPacketId++; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SimplePacket implements IMessageHandler<SimpleMessage, IMessage> | |
{ | |
@Override | |
public IMessage onMessage(SimpleMessage message, MessageContext ctx) | |
{ | |
// just to make sure that the side is correct | |
if (ctx.side.isClient()) | |
{ | |
int integer = message.simpleInt; | |
boolean bool = message.simpleBool; | |
} | |
} | |
public static class SimpleMessage implements IMessage | |
{ | |
private int simpleInt; | |
private boolean simpleBool; | |
// this constructor is required otherwise you'll get errors (used somewhere in fml through reflection) | |
public SimpleMessage() {} | |
public SimpleMessage(int simpleInt, boolean simpleBool) | |
{ | |
this.simpleInt = simpleInt; | |
this.simpleBool = simpleBool; | |
} | |
@Override | |
public void fromBytes(ByteBuf buf) | |
{ | |
// the order is important | |
this.simpleInt = buf.readInt(); | |
this.simpleBool = buf.readBoolean(); | |
} | |
@Override | |
public void toBytes(ByteBuf buf) | |
{ | |
buf.writeInt(simpleInt); | |
buf.writeBoolean(simpleBool); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sending packet to server | |
IMessage msg = new SimplePacket.SimpleMessage(500, true); | |
PacketHandler.net.sendToServer(msg); | |
// Sending packet to client | |
if (player instanceof EntityPlayerMP) | |
{ | |
IMessage msg = new SimplePacket.SimpleMessage(800, false); | |
PacketHandler.net.sendTo(msg, (EntityPlayerMP)player); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment