package me.km.networking; import io.netty.buffer.ByteBuf; import java.nio.charset.StandardCharsets; 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 StatusDisplay implements IMessage { // 1 - add - index, text // 2 - remove - index // 3 - clear private byte action; private byte index; private int time; private String text; public StatusDisplay() { action = -1; index = -1; text = ""; } public StatusDisplay(byte action, byte index, int time, String text) { this.action = action; this.index = index; this.time = time; if(text.length() > 32) { this.text = text.substring(0, 32); } else { this.text = text; } } public StatusDisplay(byte action, byte index, String text) { this(action, index, Integer.MAX_VALUE, text); } @Override public void fromBytes(ByteBuf buf) { action = buf.readByte(); index = buf.readByte(); time = buf.readInt(); int length = buf.readInt(); text = buf.readBytes(length).toString(StandardCharsets.UTF_8); } @Override public void toBytes(ByteBuf buf) { buf.writeByte(action); buf.writeByte(index); buf.writeInt(time); byte[] b = text.getBytes(StandardCharsets.UTF_8); buf.writeInt(b.length); buf.writeBytes(b); } public static class Handler implements IMessageHandler { @Override public IMessage onMessage(StatusDisplay message, MessageContext ctx) { switch(message.action) { case 1: StatusDisplayGui.INSTANCE.add(message.index, message.time, message.text); break; case 2: StatusDisplayGui.INSTANCE.remove(message.index); break; case 3: StatusDisplayGui.INSTANCE.clear(); break; } return null; } } }