Skip to content

Instantly share code, notes, and snippets.

@TheNathanSpace
Last active June 20, 2019 18:27
Show Gist options
  • Save TheNathanSpace/23fbea7edb3a5e6491f6ae1d4e7ebc25 to your computer and use it in GitHub Desktop.
Save TheNathanSpace/23fbea7edb3a5e6491f6ae1d4e7ebc25 to your computer and use it in GitHub Desktop.
Here's an example of sending packets from the client to the server!
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.simple.SimpleChannel;
public class ModPacketHandler
{
// Creates a new instance of the network channel
private static final String PROTOCOL_VERSION = "1";
public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(
new ResourceLocation(MODID, "main"),
() -> PROTOCOL_VERSION,
PROTOCOL_VERSION::equals,
PROTOCOL_VERSION::equals
);
// This is used to give each packet type a seperate ID
public static int id = 0;
public static int increaseId()
{
id++;
return id;
}
}
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent;
import java.util.function.Supplier;
public class PacketExample
{
// Whatever data needs to be sent to the server should be stored in this object.
private final double packetDouble;
private final String packetString;
// This constructor is called by you when creating a new packet to be sent to the server
public PacketFirebolt(double doubleToSend, String stringTosend)
{
this.packetDouble = doubleToSend;
this.packetString = stringToSend;
}
// Called by the game, not you. The data is written into a PacketBuffer, ready
// to be sent to the server.
public static void encode(PacketExample msg, PacketBuffer buf)
{
buf.writeDouble(msg.packetDouble);
buf.writeString(msg.packetString);
}
// Called by the game, not you. The packet is decoded by the server and a new
// PacketExample object is created to store the recieved data!
public static PacketFirebolt decode(PacketBuffer buf)
{
double recievedDouble = buf.readDouble();
String recievedString = buf.readString(12); // The parameter is the max number of characters that are accepted
return new PacketExample(recievedDouble, recievedString);
}
// Called by the game, not you. This is where you do stuff with the received data!
public static class Handler
{
public static void handle(final PacketExample message, Supplier<NetworkEvent.Context> ctx)
{
ctx.get().enqueueWork(() ->
{
// Work that needs to be threadsafe (most work) goes inside here
EntityPlayerMP sender = ctx.get().getSender();
double recievedDouble = message.packetDouble;
String recievedString = message.packetString;
// Since this part is only called on the server-side, you
// can do whatever you need to do on the server here! You've
// got whatever data you sent, too!
});
ctx.get().setPacketHandled(true);
}
}
}
// This should be called during the initiation phase.
// So, I just put it in the constructor of my main
// mod file, or whichever class has been annotated
// with @Mod.
ModPacketHandler.INSTANCE.registerMessage(
ModPacketHandler.increaseId(),
PacketExample.class,
PacketExample::encode,
PacketExample::decode,
PacketExample.Handler::handle
);
// This is how you use the packet!
ModPacketHandler.INSTANCE.sendToServer(new PacketExample(1.0D, "Hello there!"));
// Sending to one player
ModPacketHandler.INSTANCE.send(PacketDistributor.PLAYER.with(playerMP), new ExamplePacket());
// Send to all players tracking this chunk
ModPacketHandler.INSTANCE.send(PacketDistributor.TRACKING_CHUNK.with(chunk), new ExamplePacket());
// Sending to all connected players
ModPacketHandler.INSTANCE.send(PacketDistributor.ALL.noArg(), new ExamplePacket());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment