2020-09-09 16:49:01 +02:00
|
|
|
package de.ellpeck.actuallyadditions.common.network;
|
2016-06-15 16:43:59 +02:00
|
|
|
|
2020-09-09 16:49:01 +02:00
|
|
|
import de.ellpeck.actuallyadditions.common.ActuallyAdditions;
|
2016-06-15 16:43:59 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.network.PacketBuffer;
|
2016-08-15 17:13:43 +02:00
|
|
|
import net.minecraftforge.fml.common.FMLCommonHandler;
|
2016-06-15 16:43:59 +02:00
|
|
|
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
|
|
|
|
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
|
|
|
|
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public class PacketClientToServer implements IMessage {
|
2016-06-15 16:43:59 +02:00
|
|
|
|
|
|
|
private NBTTagCompound data;
|
|
|
|
private IDataHandler handler;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public PacketClientToServer() {
|
2016-06-15 16:43:59 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public PacketClientToServer(NBTTagCompound data, IDataHandler handler) {
|
2016-06-15 16:43:59 +02:00
|
|
|
this.data = data;
|
|
|
|
this.handler = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public void fromBytes(ByteBuf buf) {
|
2016-06-15 16:43:59 +02:00
|
|
|
PacketBuffer buffer = new PacketBuffer(buf);
|
2019-05-02 09:10:29 +02:00
|
|
|
try {
|
2016-11-26 21:32:27 +01:00
|
|
|
this.data = buffer.readCompoundTag();
|
2016-06-15 16:43:59 +02:00
|
|
|
|
|
|
|
int handlerId = buffer.readInt();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (handlerId >= 0 && handlerId < PacketHandler.DATA_HANDLERS.size()) {
|
2016-06-15 16:43:59 +02:00
|
|
|
this.handler = PacketHandler.DATA_HANDLERS.get(handlerId);
|
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
} catch (Exception e) {
|
2018-05-10 11:38:58 +02:00
|
|
|
ActuallyAdditions.LOGGER.error("Something went wrong trying to receive a server packet!", e);
|
2016-06-15 16:43:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public void toBytes(ByteBuf buf) {
|
2016-06-15 16:43:59 +02:00
|
|
|
PacketBuffer buffer = new PacketBuffer(buf);
|
|
|
|
|
2016-11-26 21:32:27 +01:00
|
|
|
buffer.writeCompoundTag(this.data);
|
2016-06-15 16:43:59 +02:00
|
|
|
buffer.writeInt(PacketHandler.DATA_HANDLERS.indexOf(this.handler));
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public static class Handler implements IMessageHandler<PacketClientToServer, IMessage> {
|
2016-06-15 16:43:59 +02:00
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public IMessage onMessage(final PacketClientToServer message, final MessageContext ctx) {
|
2019-02-27 19:53:05 +01:00
|
|
|
FMLCommonHandler.instance().getMinecraftServerInstance().addScheduledTask(() -> {
|
2019-05-02 09:10:29 +02:00
|
|
|
if (message.data != null && message.handler != null) {
|
2019-02-27 19:53:05 +01:00
|
|
|
message.handler.handleData(message.data, ctx);
|
2016-08-15 17:13:43 +02:00
|
|
|
}
|
|
|
|
});
|
2016-06-15 16:43:59 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|