package me.km.networking; import java.nio.charset.StandardCharsets; import java.util.function.Supplier; import net.minecraft.network.PacketBuffer; import net.minecraftforge.fml.network.NetworkEvent; public class PlayerHead { // 1 - add - index, name, x, y, scale // 2 - remove - index // 3 - clear private byte action; private byte index; private String name; private int x; private int y; private byte scale; public PlayerHead() { action = -1; index = -1; name = ""; x = -1; y = -1; scale = -1; } public PlayerHead(byte action, byte index, String name, int x, int y, byte scale) { this.action = action; this.index = index; if(name.length() > 16) { this.name = name.substring(0, 16); } else { this.name = name; } this.x = x; this.y = y; this.scale = scale; } public static void writeBytes(PlayerHead ph, PacketBuffer buf) { buf.writeByte(ph.action); switch(ph.action) { case 1: buf.writeInt(ph.x); buf.writeInt(ph.y); buf.writeByte(ph.scale); buf.writeByte(ph.index); byte[] b = ph.name.getBytes(StandardCharsets.UTF_8); buf.writeInt(b.length); buf.writeBytes(b); break; case 2: buf.writeByte(ph.index); break; } } public static PlayerHead fromBytes(PacketBuffer buf) { PlayerHead ph = new PlayerHead(); ph.action = buf.readByte(); switch(ph.action) { case 1: ph.x = buf.readInt(); ph.y = buf.readInt(); ph.scale = buf.readByte(); ph.index = buf.readByte(); int length = buf.readInt(); ph.name = buf.readBytes(length).toString(StandardCharsets.UTF_8); break; case 2: ph.index = buf.readByte(); break; } return ph; } public static void handle(PlayerHead ph, Supplier context) { context.get().enqueueWork(() -> { switch(ph.action) { case 1: PlayerHeadGui.INSTANCE.add(ph.index, ph.x, ph.y, ph.scale, ph.name); break; case 2: PlayerHeadGui.INSTANCE.remove(ph.index); break; case 3: PlayerHeadGui.INSTANCE.clear(); break; } }); context.get().setPacketHandled(true); } }