Mekanism-tilera-Edition/src/main/java/mekanism/common/network/PacketScubaTankData.java

128 lines
2.5 KiB
Java
Raw Normal View History

2013-12-06 21:26:44 +01:00
package mekanism.common.network;
import java.io.DataOutputStream;
import mekanism.common.Mekanism;
import mekanism.common.item.ItemScubaTank;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import com.google.common.io.ByteArrayDataInput;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
2013-12-06 21:26:44 +01:00
public class PacketScubaTankData extends MekanismPacket
2013-12-06 21:26:44 +01:00
{
public ScubaTankPacket packetType;
public String username;
2013-12-06 21:26:44 +01:00
public boolean value;
public PacketScubaTankData(ScubaTankPacket type, String name, boolean state)
2013-12-06 21:26:44 +01:00
{
packetType = type;
if(packetType == ScubaTankPacket.UPDATE)
2013-12-06 21:26:44 +01:00
{
username = name;
value = state;
2013-12-06 21:26:44 +01:00
}
}
public void read(ByteArrayDataInput dataStream, EntityPlayer player, World world) throws Exception
{
packetType = ScubaTankPacket.values()[dataStream.readInt()];
if(packetType == ScubaTankPacket.FULL)
{
Mekanism.gasmaskOn.clear();
int amount = dataStream.readInt();
for(int i = 0; i < amount; i++)
{
Mekanism.gasmaskOn.add(dataStream.readUTF());
}
}
else if(packetType == ScubaTankPacket.UPDATE)
2013-12-06 21:26:44 +01:00
{
String username = dataStream.readUTF();
boolean value = dataStream.readBoolean();
if(value)
{
Mekanism.gasmaskOn.add(username);
}
else {
Mekanism.gasmaskOn.remove(username);
}
if(!world.isRemote)
2013-12-06 21:26:44 +01:00
{
Mekanism.packetPipeline.sendToDimension(new PacketScubaTankData(ScubaTankPacket.UPDATE, username, value), world.provider.dimensionId);
2013-12-06 21:26:44 +01:00
}
}
else if(packetType == ScubaTankPacket.MODE)
2013-12-06 21:26:44 +01:00
{
ItemStack stack = player.getEquipmentInSlot(3);
2013-12-06 21:26:44 +01:00
if(stack != null && stack.getItem() instanceof ItemScubaTank)
{
((ItemScubaTank)stack.getItem()).toggleFlowing(stack);
}
}
}
public void write(DataOutputStream dataStream) throws Exception
{
dataStream.writeInt(packetType.ordinal());
if(packetType == ScubaTankPacket.UPDATE)
2013-12-06 21:26:44 +01:00
{
dataStream.writeUTF(username);
2013-12-06 21:26:44 +01:00
dataStream.writeBoolean(value);
}
else if(packetType == ScubaTankPacket.FULL)
{
dataStream.writeInt(Mekanism.gasmaskOn.size());
for(String username : Mekanism.gasmaskOn)
{
dataStream.writeUTF(username);
}
}
2013-12-06 21:26:44 +01:00
}
@Override
public void write(ChannelHandlerContext ctx, ByteBuf buffer)
{
}
@Override
public void read(ChannelHandlerContext ctx, ByteBuf buffer)
{
}
@Override
public void handleClientSide(EntityPlayer player)
{
}
@Override
public void handleServerSide(EntityPlayer player)
{
}
public static enum ScubaTankPacket
2013-12-06 21:26:44 +01:00
{
UPDATE,
FULL,
2013-12-06 21:26:44 +01:00
MODE;
}
}