1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- 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 PlayerDisplay implements IMessage
- {
- // 1 - add - index, text
- // 2 - remove - index
- // 3 - clear
- private byte action;
- private byte index;
- private String text;
-
- public PlayerDisplay()
- {
- action = -1;
- index = -1;
- text = "";
- }
-
- public PlayerDisplay(byte action, byte index, String text)
- {
- this.action = action;
- this.index = index;
- if(text.length() > 32)
- {
- this.text = text.substring(0, 32);
- }
- else
- {
- this.text = text;
- }
- }
-
- @Override
- public void fromBytes(ByteBuf buf)
- {
- action = buf.readByte();
- index = buf.readByte();
- int length = buf.readInt();
- text = buf.readBytes(length).toString(StandardCharsets.UTF_8);
- }
- @Override
- public void toBytes(ByteBuf buf)
- {
- buf.writeByte(action);
- buf.writeByte(index);
- byte[] b = text.getBytes(StandardCharsets.UTF_8);
- buf.writeInt(b.length);
- buf.writeBytes(b);
- }
- public static class Handler implements IMessageHandler<PlayerDisplay, IMessage>
- {
- @Override
- public IMessage onMessage(PlayerDisplay message, MessageContext ctx)
- {
- Minecraft mc = net.minecraft.client.Minecraft.getMinecraft();
- mc.addScheduledTask(() ->
- {
- switch(message.action)
- {
- case 1:
- PlayerDisplayGui.INSTANCE.add(message.index, message.text);
- break;
- case 2:
- PlayerDisplayGui.INSTANCE.remove(message.index);
- break;
- case 3:
- PlayerDisplayGui.INSTANCE.clear();
- break;
- }
- });
- return null;
- }
- }
- }
|