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

115 lines
2.4 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.PacketHandler;
import mekanism.common.PacketHandler.Transmission;
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;
public class PacketScubaTankData implements IMekanismPacket
{
public ScubaTankPacket packetType;
public String username;
2013-12-06 21:26:44 +01:00
public boolean value;
2013-12-06 21:26:44 +01:00
@Override
public String getName()
2013-12-06 21:26:44 +01:00
{
return "ScubaTankData";
}
2013-12-06 21:26:44 +01:00
@Override
public IMekanismPacket setParams(Object... data)
{
packetType = (ScubaTankPacket)data[0];
if(packetType == ScubaTankPacket.UPDATE)
2013-12-06 21:26:44 +01:00
{
username = (String)data[1];
2013-12-06 21:26:44 +01:00
value = (Boolean)data[2];
}
2013-12-06 21:26:44 +01:00
return this;
}
@Override
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
{
PacketHandler.sendPacket(Transmission.CLIENTS_DIM, new PacketScubaTankData().setParams(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.getCurrentItemOrArmor(3);
2013-12-06 21:26:44 +01:00
if(stack != null && stack.getItem() instanceof ItemScubaTank)
{
((ItemScubaTank)stack.getItem()).toggleFlowing(stack);
}
}
}
@Override
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
}
public static enum ScubaTankPacket
2013-12-06 21:26:44 +01:00
{
UPDATE,
FULL,
2013-12-06 21:26:44 +01:00
MODE;
}
}