Skip to content

Instantly share code, notes, and snippets.

@TeamDman
Created December 27, 2020 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TeamDman/ac947f1cd465294ae4ebc74a572a802b to your computer and use it in GitHub Desktop.
Save TeamDman/ac947f1cd465294ae4ebc74a572a802b to your computer and use it in GitHub Desktop.
Minecraft 1.16 packet handler exmaple
import ca.teamdman.sfm.SFM;
import java.util.function.Supplier;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.network.NetworkEvent.Context;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.simple.SimpleChannel;
class Scratch {
private static final String CHANNEL_NAME = "main";
private static final String PROTOCOL_VERSION = "1";
public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(
new ResourceLocation(SFM.MOD_ID, CHANNEL_NAME),
() -> PROTOCOL_VERSION,
PROTOCOL_VERSION::equals,
PROTOCOL_VERSION::equals
);
public static void main(String[] args) {
int id = 0;
INSTANCE.registerMessage(
id++,
MyPacket.class,
MyPacket::encode,
MyPacket::decode,
MyPacket::handle
);
}
public static void updateProgress(int prog) {
INSTANCE.sendToServer(new MyPacket(prog));
}
public static class MyPacket {
int progress;
public MyPacket(int progress) {
this.progress = progress;
}
public static void encode(MyPacket packet, PacketBuffer buf) {
buf.writeInt(packet.progress);
}
public static MyPacket decode(PacketBuffer buf) {
return new MyPacket(buf.readInt());
}
public static void handle(MyPacket msg, Supplier<Context> ctx) {
ctx.get().enqueueWork(()-> {
System.out.println("Progress is now " + msg.progress);
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment