Created
December 27, 2020 22:29
-
-
Save TeamDman/ac947f1cd465294ae4ebc74a572a802b to your computer and use it in GitHub Desktop.
Minecraft 1.16 packet handler exmaple
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
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