package me.km.networking; import io.netty.buffer.ByteBuf; import java.nio.charset.StandardCharsets; import net.minecraft.client.Minecraft; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; public class PlayerHead implements IMessage { // 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; } @Override public void fromBytes(ByteBuf buf) { action = buf.readByte(); switch(action) { case 1: x = buf.readInt(); y = buf.readInt(); scale = buf.readByte(); index = buf.readByte(); int length = buf.readInt(); name = buf.readBytes(length).toString(StandardCharsets.UTF_8); break; case 2: index = buf.readByte(); break; } } @Override public void toBytes(ByteBuf buf) { buf.writeByte(action); switch(action) { case 1: buf.writeInt(x); buf.writeInt(y); buf.writeByte(scale); buf.writeByte(index); byte[] b = name.getBytes(StandardCharsets.UTF_8); buf.writeInt(b.length); buf.writeBytes(b); break; case 2: buf.writeByte(index); break; } } public static class Handler implements IMessageHandler { @Override public IMessage onMessage(PlayerHead message, MessageContext ctx) { Minecraft mc = net.minecraft.client.Minecraft.getMinecraft(); mc.addScheduledTask(() -> { switch(message.action) { case 1: PlayerHeadGui.INSTANCE.add(message.index, message.x, message.y, message.scale, message.name); break; case 2: PlayerHeadGui.INSTANCE.remove(message.index); break; case 3: PlayerHeadGui.INSTANCE.clear(); break; } }); return null; } } }