Last active
June 20, 2019 18:27
-
-
Save TheNathanSpace/23fbea7edb3a5e6491f6ae1d4e7ebc25 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Here's an example of sending packets from the client to the server! |
This file contains hidden or 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
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; | |
} | |
} |
This file contains hidden or 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
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 file contains hidden or 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
// 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 file contains hidden or 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
// This is how you use the packet! | |
ModPacketHandler.INSTANCE.sendToServer(new PacketExample(1.0D, "Hello there!")); |
This file contains hidden or 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 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