mffs/src/main/java/mffs/base/PacketTile.java

69 lines
1.8 KiB
Java
Raw Normal View History

2022-10-28 16:20:12 +02:00
package mffs.base;
2023-01-08 16:58:21 +01:00
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
2022-10-28 16:20:12 +02:00
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import universalelectricity.core.vector.Vector3;
public class PacketTile implements IMessage {
2023-01-08 16:58:21 +01:00
Type type;
Vector3 pos;
NBTTagCompound data;
2022-10-28 16:20:12 +02:00
2023-01-08 16:58:21 +01:00
public PacketTile(Type type, Vector3 pos, NBTTagCompound data) {
this.type = type;
this.pos = pos;
this.data = data;
}
2022-10-28 16:20:12 +02:00
2023-01-08 16:58:21 +01:00
public PacketTile() {}
2022-10-28 16:20:12 +02:00
2023-01-08 16:58:21 +01:00
@Override
public void fromBytes(ByteBuf buf) {
try {
NBTTagCompound nbt = CompressedStreamTools.read(
new DataInputStream(new ByteBufInputStream(buf))
);
2022-10-28 16:20:12 +02:00
2023-01-08 16:58:21 +01:00
this.type = Type.values()[nbt.getInteger("type")];
this.pos = Vector3.readFromNBT(nbt);
this.data = nbt.getCompoundTag("data");
} catch (IOException e) {
e.printStackTrace();
}
2022-10-28 16:20:12 +02:00
}
2023-01-08 16:58:21 +01:00
@Override
public void toBytes(ByteBuf buf) {
try {
NBTTagCompound nbt = new NBTTagCompound();
2022-10-28 16:20:12 +02:00
2023-01-08 16:58:21 +01:00
nbt.setInteger("type", this.type.ordinal());
this.pos.writeToNBT(nbt);
nbt.setTag("data", this.data);
2022-10-28 16:20:12 +02:00
2023-01-08 16:58:21 +01:00
CompressedStreamTools.write(
nbt, new DataOutputStream(new ByteBufOutputStream(buf))
);
} catch (IOException e) {
e.printStackTrace();
}
2022-10-28 16:20:12 +02:00
}
2023-01-08 16:58:21 +01:00
public enum Type {
NONE,
FREQUENCY,
TOGGLE_ACTIVATION,
TOGGLE_MODE,
INVENTORY,
STRING,
}
2022-10-28 16:20:12 +02:00
}