Packet handler work
This commit is contained in:
parent
b76756d691
commit
4b55a3b648
18 changed files with 607 additions and 364 deletions
|
@ -1,336 +0,0 @@
|
|||
package universalelectricity.prefab.network;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.nbt.CompressedStreamTools;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.network.INetworkManager;
|
||||
import net.minecraft.network.packet.Packet;
|
||||
import net.minecraft.network.packet.Packet250CustomPayload;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.world.World;
|
||||
import universalelectricity.core.vector.Vector3;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
|
||||
import cpw.mods.fml.common.network.IPacketHandler;
|
||||
import cpw.mods.fml.common.network.PacketDispatcher;
|
||||
import cpw.mods.fml.common.network.Player;
|
||||
|
||||
/**
|
||||
* This class is used for sending and receiving packets between the server and the client. You can
|
||||
* directly use this by registering this packet manager with NetworkMod. Example:
|
||||
*
|
||||
* @NetworkMod(channels = { "BasicComponents" }, clientSideRequired = true, serverSideRequired =
|
||||
* false, packetHandler = PacketManager.class)
|
||||
*
|
||||
* Check out {@link #BasicComponents} for better reference.
|
||||
*
|
||||
* @author Calclavia
|
||||
*/
|
||||
public class PacketManager implements IPacketHandler, IPacketReceiver
|
||||
{
|
||||
public enum PacketType
|
||||
{
|
||||
UNSPECIFIED, TILEENTITY;
|
||||
|
||||
public static PacketType get(int id)
|
||||
{
|
||||
if (id >= 0 && id < PacketType.values().length)
|
||||
{
|
||||
return PacketType.values()[id];
|
||||
}
|
||||
return UNSPECIFIED;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a compressed NBTTagCompound to the OutputStream
|
||||
*/
|
||||
public static void writeNBTTagCompound(NBTTagCompound tag, DataOutputStream dataStream) throws IOException
|
||||
{
|
||||
if (tag == null)
|
||||
{
|
||||
dataStream.writeShort(-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] var2 = CompressedStreamTools.compress(tag);
|
||||
dataStream.writeShort((short) var2.length);
|
||||
dataStream.write(var2);
|
||||
}
|
||||
}
|
||||
|
||||
public static void writeNBTTagCompound(NBTTagCompound tag, ByteArrayDataOutput dataStream) throws IOException
|
||||
{
|
||||
if (tag == null)
|
||||
{
|
||||
dataStream.writeShort(-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] var2 = CompressedStreamTools.compress(tag);
|
||||
dataStream.writeShort((short) var2.length);
|
||||
dataStream.write(var2);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a compressed NBTTagCompount in a ByteStream.
|
||||
*/
|
||||
public static NBTTagCompound readNBTTagCompound(DataInputStream dataStream) throws IOException
|
||||
{
|
||||
short var1 = dataStream.readShort();
|
||||
|
||||
if (var1 < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] var2 = new byte[var1];
|
||||
dataStream.readFully(var2);
|
||||
return CompressedStreamTools.decompress(var2);
|
||||
}
|
||||
}
|
||||
|
||||
public static NBTTagCompound readNBTTagCompound(ByteArrayDataInput dataStream) throws IOException
|
||||
{
|
||||
short var1 = dataStream.readShort();
|
||||
|
||||
if (var1 < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] var2 = new byte[var1];
|
||||
dataStream.readFully(var2);
|
||||
return CompressedStreamTools.decompress(var2);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
public static Packet getPacketWithID(String channelName, int id, Object... sendData)
|
||||
{
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
DataOutputStream data = new DataOutputStream(bytes);
|
||||
|
||||
try
|
||||
{
|
||||
data.writeInt(id);
|
||||
data = encodeDataStream(data, sendData);
|
||||
|
||||
Packet250CustomPayload packet = new Packet250CustomPayload();
|
||||
packet.channel = channelName;
|
||||
packet.data = bytes.toByteArray();
|
||||
packet.length = packet.data.length;
|
||||
|
||||
return packet;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
System.out.println("Failed to create packet.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Packet getPacket(String channelName, Object... sendData)
|
||||
{
|
||||
return getPacketWithID(channelName, PacketType.UNSPECIFIED.ordinal(), sendData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a packet for the tile entity.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("resource")
|
||||
public static Packet getPacket(String channelName, TileEntity sender, Object... sendData)
|
||||
{
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
DataOutputStream data = new DataOutputStream(bytes);
|
||||
|
||||
try
|
||||
{
|
||||
data.writeInt(PacketType.TILEENTITY.ordinal());
|
||||
|
||||
data.writeInt(sender.xCoord);
|
||||
data.writeInt(sender.yCoord);
|
||||
data.writeInt(sender.zCoord);
|
||||
data = encodeDataStream(data, sendData);
|
||||
|
||||
Packet250CustomPayload packet = new Packet250CustomPayload();
|
||||
packet.channel = channelName;
|
||||
packet.data = bytes.toByteArray();
|
||||
packet.length = packet.data.length;
|
||||
|
||||
return packet;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
System.out.println("Failed to create packet.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends packets to clients around a specific coordinate. A wrapper using Vector3. See
|
||||
* {@PacketDispatcher} for detailed information.
|
||||
*/
|
||||
public static void sendPacketToClients(Packet packet, World worldObj, Vector3 position, double range)
|
||||
{
|
||||
try
|
||||
{
|
||||
PacketDispatcher.sendPacketToAllAround(position.x, position.y, position.z, range, worldObj.provider.dimensionId, packet);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println("Sending packet to client failed.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a packet to all the clients on this server.
|
||||
*/
|
||||
public static void sendPacketToClients(Packet packet, World worldObj)
|
||||
{
|
||||
try
|
||||
{
|
||||
PacketDispatcher.sendPacketToAllInDimension(packet, worldObj.provider.dimensionId);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println("Sending packet to client failed.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendPacketToClients(Packet packet)
|
||||
{
|
||||
try
|
||||
{
|
||||
PacketDispatcher.sendPacketToAllPlayers(packet);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println("Sending packet to client failed.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static DataOutputStream encodeDataStream(DataOutputStream data, Object... sendData)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (Object dataValue : sendData)
|
||||
{
|
||||
if (dataValue instanceof Integer)
|
||||
{
|
||||
data.writeInt((Integer) dataValue);
|
||||
}
|
||||
else if (dataValue instanceof Float)
|
||||
{
|
||||
data.writeFloat((Float) dataValue);
|
||||
}
|
||||
else if (dataValue instanceof Double)
|
||||
{
|
||||
data.writeDouble((Double) dataValue);
|
||||
}
|
||||
else if (dataValue instanceof Byte)
|
||||
{
|
||||
data.writeByte((Byte) dataValue);
|
||||
}
|
||||
else if (dataValue instanceof Boolean)
|
||||
{
|
||||
data.writeBoolean((Boolean) dataValue);
|
||||
}
|
||||
else if (dataValue instanceof String)
|
||||
{
|
||||
data.writeUTF((String) dataValue);
|
||||
}
|
||||
else if (dataValue instanceof Short)
|
||||
{
|
||||
data.writeShort((Short) dataValue);
|
||||
}
|
||||
else if (dataValue instanceof Long)
|
||||
{
|
||||
data.writeLong((Long) dataValue);
|
||||
}
|
||||
else if (dataValue instanceof NBTTagCompound)
|
||||
{
|
||||
writeNBTTagCompound((NBTTagCompound) dataValue, data);
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
System.out.println("Packet data encoding failed.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPacketData(INetworkManager network, Packet250CustomPayload packet, Player player)
|
||||
{
|
||||
try
|
||||
{
|
||||
ByteArrayDataInput data = ByteStreams.newDataInput(packet.data);
|
||||
|
||||
int packetTypeID = data.readInt();
|
||||
|
||||
PacketType packetType = PacketType.get(packetTypeID);
|
||||
|
||||
if (packetType == PacketType.TILEENTITY)
|
||||
{
|
||||
int x = data.readInt();
|
||||
int y = data.readInt();
|
||||
int z = data.readInt();
|
||||
|
||||
World world = ((EntityPlayer) player).worldObj;
|
||||
|
||||
if (world != null)
|
||||
{
|
||||
TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
|
||||
|
||||
if (tileEntity != null)
|
||||
{
|
||||
if (tileEntity instanceof IPacketReceiver)
|
||||
{
|
||||
((IPacketReceiver) tileEntity).handlePacketData(network, packetTypeID, packet, ((EntityPlayer) player), data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this.handlePacketData(network, packetTypeID, packet, ((EntityPlayer) player), data);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePacketData(INetworkManager network, int packetType, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput dataStream)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
|
@ -14,10 +14,9 @@ import net.minecraftforge.fluids.FluidStack;
|
|||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import universalelectricity.core.vector.Vector3;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
import universalelectricity.core.vector.Vector3;
|
||||
import dark.core.prefab.helpers.BlockRenderInfo;
|
||||
|
||||
/** @author CovertJaguar <railcraft.wikispaces.com> from BuildCraft , modified by DarkGuardsman */
|
||||
|
|
|
@ -18,10 +18,9 @@ import net.minecraft.world.World;
|
|||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
||||
import universalelectricity.core.vector.Vector3;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
import universalelectricity.core.vector.Vector3;
|
||||
import dark.core.prefab.helpers.BlockRenderInfo;
|
||||
import dark.core.prefab.helpers.EntityFakeBlock;
|
||||
@SideOnly(Side.CLIENT)
|
||||
|
|
|
@ -4,6 +4,7 @@ import java.awt.Color;
|
|||
|
||||
import net.minecraft.world.World;
|
||||
import universalelectricity.core.vector.Vector3;
|
||||
import dark.core.network.PacketManagerEffects;
|
||||
|
||||
public class CommonProxy
|
||||
{
|
||||
|
@ -26,7 +27,7 @@ public class CommonProxy
|
|||
}
|
||||
|
||||
/** Renders a laser beam from one power to another by a set color for a set time
|
||||
*
|
||||
*
|
||||
* @param world - world this laser is to be rendered in
|
||||
* @param position - start vector3
|
||||
* @param target - end vector3
|
||||
|
@ -34,6 +35,7 @@ public class CommonProxy
|
|||
* @param age - life of the beam in 1/20 secs */
|
||||
public void renderBeam(World world, Vector3 position, Vector3 target, Color color, int age)
|
||||
{
|
||||
PacketManagerEffects.sendClientLaserEffect(world, position, target, color, age);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,7 +15,6 @@ import net.minecraftforge.oredict.OreDictionary;
|
|||
import universalelectricity.compatibility.Compatibility;
|
||||
import universalelectricity.core.UniversalElectricity;
|
||||
import universalelectricity.prefab.TranslationHelper;
|
||||
import universalelectricity.prefab.network.PacketManager;
|
||||
import universalelectricity.prefab.ore.OreGenReplaceStone;
|
||||
import universalelectricity.prefab.ore.OreGenerator;
|
||||
import cpw.mods.fml.common.FMLLog;
|
||||
|
@ -49,6 +48,7 @@ import dark.core.common.items.ItemParts.Parts;
|
|||
import dark.core.common.items.ItemTools;
|
||||
import dark.core.common.items.ItemWrench;
|
||||
import dark.core.common.transmit.BlockWire;
|
||||
import dark.core.network.PacketHandler;
|
||||
import dark.core.prefab.BlockMulti;
|
||||
import dark.core.prefab.ModPrefab;
|
||||
import dark.core.prefab.TileEntityMulti;
|
||||
|
@ -57,7 +57,7 @@ import dark.core.prefab.items.ItemBlockHolder;
|
|||
|
||||
/** @author HangCow, DarkGuardsman */
|
||||
@Mod(modid = DarkMain.MOD_ID, name = DarkMain.MOD_NAME, version = DarkMain.VERSION, dependencies = "after:BuildCraft|Energy", useMetadata = true)
|
||||
@NetworkMod(channels = { DarkMain.CHANNEL }, clientSideRequired = true, serverSideRequired = false, packetHandler = PacketManager.class)
|
||||
@NetworkMod(channels = { DarkMain.CHANNEL }, clientSideRequired = true, serverSideRequired = false, packetHandler = PacketHandler.class)
|
||||
public class DarkMain extends ModPrefab
|
||||
{
|
||||
// @Mod Prerequisites
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package dark.core.common;
|
||||
|
||||
import cpw.mods.fml.common.Loader;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import buildcraft.api.transport.IPipeTile;
|
||||
import buildcraft.api.transport.IPipeTile.PipeType;
|
||||
import cpw.mods.fml.common.Loader;
|
||||
|
||||
/** Handles working with other mod without or without the need of the APIs.
|
||||
*
|
||||
|
|
|
@ -3,9 +3,6 @@ package dark.core.common.blocks;
|
|||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.client.renderer.texture.IconRegister;
|
||||
|
@ -15,6 +12,8 @@ import net.minecraft.tileentity.TileEntity;
|
|||
import net.minecraft.util.Icon;
|
||||
import net.minecraftforge.common.Configuration;
|
||||
import net.minecraftforge.oredict.OreDictionary;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import dark.core.common.DarkMain;
|
||||
import dark.core.common.items.EnumMeterials;
|
||||
import dark.core.prefab.IExtraObjectInfo;
|
||||
|
|
17
src/dark/core/common/machines/BlockGenerator.java
Normal file
17
src/dark/core/common/machines/BlockGenerator.java
Normal file
|
@ -0,0 +1,17 @@
|
|||
package dark.core.common.machines;
|
||||
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraftforge.common.Configuration;
|
||||
import dark.core.common.DarkMain;
|
||||
import dark.core.prefab.BlockMachine;
|
||||
|
||||
public class BlockGenerator extends BlockMachine
|
||||
{
|
||||
|
||||
public BlockGenerator(String name, Configuration config, int blockID, Material material)
|
||||
{
|
||||
super("generator", DarkMain.CONFIGURATION, blockID, material);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
}
|
|
@ -1,11 +1,34 @@
|
|||
package dark.core.common.transmit;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumMovingObjectType;
|
||||
import net.minecraft.util.MovingObjectPosition;
|
||||
import net.minecraftforge.common.ForgeDirection;
|
||||
import universalelectricity.core.vector.Vector3;
|
||||
import dark.core.common.DarkMain;
|
||||
import dark.core.prefab.TileEntityMachine;
|
||||
|
||||
public class TileEntityLaserEmitter extends TileEntityMachine
|
||||
{
|
||||
/** Is tile set up to receive power */
|
||||
private boolean receiver = false;
|
||||
/** Demand of connected network */
|
||||
private float powerDemand = 0.0f;
|
||||
/** Supply from other laser emitter */
|
||||
private float powerSupply = 0.0f;
|
||||
private float yaw, pitch, prevYaw, prevPitch, deltaYaw, deltaPitch;
|
||||
/** Color of renderer laser */
|
||||
private Color color = Color.red;
|
||||
/** Linked emitter */
|
||||
TileEntityLaserEmitter linkedEmitter = null;
|
||||
Vector3 laserTarget = null;
|
||||
|
||||
public TileEntityLaserEmitter()
|
||||
{
|
||||
super(.001f/* 1W/t*/, 1f/* 1000W battery*/);
|
||||
}
|
||||
|
||||
/** Facing direction of the tile and not the laser */
|
||||
public ForgeDirection getFacingDirection()
|
||||
|
@ -18,6 +41,55 @@ public class TileEntityLaserEmitter extends TileEntityMachine
|
|||
return ForgeDirection.getOrientation(meta);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateEntity()
|
||||
{
|
||||
super.updateEntity();
|
||||
if (this.running)
|
||||
{
|
||||
this.updateRotation();
|
||||
if (this.linkedEmitter != null && ticks % 20 == 0)
|
||||
{
|
||||
this.createLaser(new Vector3(linkedEmitter));
|
||||
}
|
||||
else if (laserTarget != null)
|
||||
{
|
||||
TileEntity entity = laserTarget.getTileEntity(this.worldObj);
|
||||
if (entity instanceof TileEntityLaserEmitter && ((TileEntityLaserEmitter) entity).receiver != this.receiver)
|
||||
{
|
||||
linkedEmitter = (TileEntityLaserEmitter) entity;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void updateRotation()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected void createLaser(Vector3 target)
|
||||
{
|
||||
Vector3 start = new Vector3(this);
|
||||
double distance = start.distance(target);
|
||||
MovingObjectPosition hit = start.rayTrace(this.worldObj, yaw, pitch, true, distance);
|
||||
if (hit != null)
|
||||
{
|
||||
if (hit.typeOfHit == EnumMovingObjectType.ENTITY)
|
||||
{
|
||||
//TODO damage entity if power is over 1000W
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DarkMain.proxy.renderBeam(this.worldObj, start, target, color, 20);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canConnect(ForgeDirection direction)
|
||||
{
|
||||
|
@ -27,13 +99,20 @@ public class TileEntityLaserEmitter extends TileEntityMachine
|
|||
@Override
|
||||
public float getRequest(ForgeDirection side)
|
||||
{
|
||||
if (!receiver && side == getFacingDirection().getOpposite())
|
||||
{
|
||||
return powerDemand;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getProvide(ForgeDirection direction)
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
if (receiver && direction == getFacingDirection().getOpposite())
|
||||
{
|
||||
return powerSupply;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
17
src/dark/core/network/IPacketManager.java
Normal file
17
src/dark/core/network/IPacketManager.java
Normal file
|
@ -0,0 +1,17 @@
|
|||
package dark.core.network;
|
||||
|
||||
import net.minecraft.network.INetworkManager;
|
||||
import net.minecraft.network.packet.Packet250CustomPayload;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
|
||||
import cpw.mods.fml.common.network.Player;
|
||||
|
||||
public interface IPacketManager
|
||||
{
|
||||
public int getID();
|
||||
|
||||
public void setID(int maxID);
|
||||
|
||||
public void handlePacket(INetworkManager network, Packet250CustomPayload packet, Player player, ByteArrayDataInput data);
|
||||
}
|
342
src/dark/core/network/PacketHandler.java
Normal file
342
src/dark/core/network/PacketHandler.java
Normal file
|
@ -0,0 +1,342 @@
|
|||
package dark.core.network;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.nbt.CompressedStreamTools;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.network.INetworkManager;
|
||||
import net.minecraft.network.packet.Packet;
|
||||
import net.minecraft.network.packet.Packet250CustomPayload;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.world.World;
|
||||
import universalelectricity.core.vector.Vector3;
|
||||
import universalelectricity.prefab.network.IPacketReceiver;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
|
||||
import cpw.mods.fml.common.network.IPacketHandler;
|
||||
import cpw.mods.fml.common.network.PacketDispatcher;
|
||||
import cpw.mods.fml.common.network.Player;
|
||||
|
||||
/** Packet manager based off the PacketManager from UE created by Calclavia
|
||||
*
|
||||
* @author DarkGuardsman */
|
||||
public class PacketHandler implements IPacketHandler, IPacketReceiver
|
||||
{
|
||||
public static PacketHandler instance;
|
||||
|
||||
public static HashMap<Integer, IPacketManager> packetTypes = new HashMap();
|
||||
|
||||
|
||||
public static PacketManagerTile tile = new PacketManagerTile();
|
||||
public static PacketManagerEffects effects = new PacketManagerEffects();
|
||||
|
||||
public static int maxID = 0;
|
||||
static
|
||||
{
|
||||
registerManager(new PacketManagerTile());
|
||||
registerManager(new PacketManagerEffects());
|
||||
}
|
||||
|
||||
public static void registerManager(IPacketManager manager)
|
||||
{
|
||||
if (manager != null)
|
||||
{
|
||||
packetTypes.put(maxID, manager);
|
||||
manager.setID(maxID);
|
||||
maxID++;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static IPacketManager getManager(int id)
|
||||
{
|
||||
return packetTypes.get(id);
|
||||
}
|
||||
|
||||
public static PacketHandler instance()
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = new PacketHandler();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/** Writes a compressed NBTTagCompound to the OutputStream */
|
||||
public void writeNBTTagCompound(NBTTagCompound tag, DataOutputStream dataStream) throws IOException
|
||||
{
|
||||
if (tag == null)
|
||||
{
|
||||
dataStream.writeShort(-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] var2 = CompressedStreamTools.compress(tag);
|
||||
dataStream.writeShort((short) var2.length);
|
||||
dataStream.write(var2);
|
||||
}
|
||||
}
|
||||
|
||||
public void writeNBTTagCompound(NBTTagCompound tag, ByteArrayDataOutput dataStream) throws IOException
|
||||
{
|
||||
if (tag == null)
|
||||
{
|
||||
dataStream.writeShort(-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] var2 = CompressedStreamTools.compress(tag);
|
||||
dataStream.writeShort((short) var2.length);
|
||||
dataStream.write(var2);
|
||||
}
|
||||
}
|
||||
|
||||
/** Reads a compressed NBTTagCompount in a ByteStream. */
|
||||
public NBTTagCompound readNBTTagCompound(DataInputStream dataStream) throws IOException
|
||||
{
|
||||
short var1 = dataStream.readShort();
|
||||
|
||||
if (var1 < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] var2 = new byte[var1];
|
||||
dataStream.readFully(var2);
|
||||
return CompressedStreamTools.decompress(var2);
|
||||
}
|
||||
}
|
||||
|
||||
public NBTTagCompound readNBTTagCompound(ByteArrayDataInput dataStream) throws IOException
|
||||
{
|
||||
short var1 = dataStream.readShort();
|
||||
|
||||
if (var1 < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] var2 = new byte[var1];
|
||||
dataStream.readFully(var2);
|
||||
return CompressedStreamTools.decompress(var2);
|
||||
}
|
||||
}
|
||||
|
||||
public static Vector3 readVector3(ByteArrayDataInput data) throws IOException
|
||||
{
|
||||
return new Vector3(data.readDouble(), data.readDouble(), data.readDouble());
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
public Packet getPacketWithID(String channelName, int id, Object... sendData)
|
||||
{
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
DataOutputStream data = new DataOutputStream(bytes);
|
||||
|
||||
try
|
||||
{
|
||||
data.writeInt(id);
|
||||
data = encodeDataStream(data, sendData);
|
||||
|
||||
Packet250CustomPayload packet = new Packet250CustomPayload();
|
||||
packet.channel = channelName;
|
||||
packet.data = bytes.toByteArray();
|
||||
packet.length = packet.data.length;
|
||||
|
||||
return packet;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
System.out.println("Failed to create packet.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Packet getPacket(String channelName, Object... sendData)
|
||||
{
|
||||
return getPacketWithID(channelName, -1, sendData);
|
||||
}
|
||||
|
||||
/** Gets a packet for the tile entity.
|
||||
*
|
||||
* @return */
|
||||
@SuppressWarnings("resource")
|
||||
public Packet getPacket(String channelName, TileEntity sender, Object... sendData)
|
||||
{
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
DataOutputStream data = new DataOutputStream(bytes);
|
||||
|
||||
try
|
||||
{
|
||||
data.writeInt(this.tile.getID());
|
||||
|
||||
data.writeInt(sender.xCoord);
|
||||
data.writeInt(sender.yCoord);
|
||||
data.writeInt(sender.zCoord);
|
||||
data = encodeDataStream(data, sendData);
|
||||
|
||||
Packet250CustomPayload packet = new Packet250CustomPayload();
|
||||
packet.channel = channelName;
|
||||
packet.data = bytes.toByteArray();
|
||||
packet.length = packet.data.length;
|
||||
|
||||
return packet;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
System.out.println("Failed to create packet.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Sends packets to clients around a specific coordinate. A wrapper using Vector3. See
|
||||
* {@PacketDispatcher} for detailed information. */
|
||||
public void sendPacketToClients(Packet packet, World worldObj, Vector3 position, double range)
|
||||
{
|
||||
try
|
||||
{
|
||||
PacketDispatcher.sendPacketToAllAround(position.x, position.y, position.z, range, worldObj.provider.dimensionId, packet);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println("Sending packet to client failed.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/** Sends a packet to all the clients on this server. */
|
||||
public void sendPacketToClients(Packet packet, World worldObj)
|
||||
{
|
||||
try
|
||||
{
|
||||
PacketDispatcher.sendPacketToAllInDimension(packet, worldObj.provider.dimensionId);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println("Sending packet to client failed.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void sendPacketToClients(Packet packet)
|
||||
{
|
||||
try
|
||||
{
|
||||
PacketDispatcher.sendPacketToAllPlayers(packet);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println("Sending packet to client failed.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public DataOutputStream encodeDataStream(DataOutputStream data, Object... sendData)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (Object dataValue : sendData)
|
||||
{
|
||||
if (dataValue instanceof Vector3)
|
||||
{
|
||||
data.writeDouble(((Vector3) dataValue).x);
|
||||
data.writeDouble(((Vector3) dataValue).y);
|
||||
data.writeDouble(((Vector3) dataValue).z);
|
||||
}
|
||||
else if (dataValue instanceof Integer)
|
||||
{
|
||||
data.writeInt((Integer) dataValue);
|
||||
}
|
||||
else if (dataValue instanceof Float)
|
||||
{
|
||||
data.writeFloat((Float) dataValue);
|
||||
}
|
||||
else if (dataValue instanceof Double)
|
||||
{
|
||||
data.writeDouble((Double) dataValue);
|
||||
}
|
||||
else if (dataValue instanceof Byte)
|
||||
{
|
||||
data.writeByte((Byte) dataValue);
|
||||
}
|
||||
else if (dataValue instanceof Boolean)
|
||||
{
|
||||
data.writeBoolean((Boolean) dataValue);
|
||||
}
|
||||
else if (dataValue instanceof String)
|
||||
{
|
||||
data.writeUTF((String) dataValue);
|
||||
}
|
||||
else if (dataValue instanceof Short)
|
||||
{
|
||||
data.writeShort((Short) dataValue);
|
||||
}
|
||||
else if (dataValue instanceof Long)
|
||||
{
|
||||
data.writeLong((Long) dataValue);
|
||||
}
|
||||
else if (dataValue instanceof NBTTagCompound)
|
||||
{
|
||||
writeNBTTagCompound((NBTTagCompound) dataValue, data);
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
System.out.println("Packet data encoding failed.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onPacketData(INetworkManager network, Packet250CustomPayload packet, Player player)
|
||||
{
|
||||
try
|
||||
{
|
||||
ByteArrayDataInput data = ByteStreams.newDataInput(packet.data);
|
||||
|
||||
int packetTypeID = data.readInt();
|
||||
|
||||
IPacketManager packetType = getManager(packetTypeID);
|
||||
|
||||
if (packetType != null)
|
||||
{
|
||||
packetType.handlePacket(network, packet, player, data);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.handlePacketData(network, packetTypeID, packet, ((EntityPlayer) player), data);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePacketData(INetworkManager network, int packetType, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput dataStream)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
62
src/dark/core/network/PacketManagerEffects.java
Normal file
62
src/dark/core/network/PacketManagerEffects.java
Normal file
|
@ -0,0 +1,62 @@
|
|||
package dark.core.network;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.network.INetworkManager;
|
||||
import net.minecraft.network.packet.Packet;
|
||||
import net.minecraft.network.packet.Packet250CustomPayload;
|
||||
import net.minecraft.world.World;
|
||||
import universalelectricity.core.vector.Vector3;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
|
||||
import cpw.mods.fml.common.network.Player;
|
||||
import dark.core.common.DarkMain;
|
||||
|
||||
public class PacketManagerEffects implements IPacketManager
|
||||
{
|
||||
static int packetID = 0;
|
||||
|
||||
@Override
|
||||
public int getID()
|
||||
{
|
||||
return packetID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setID(int maxID)
|
||||
{
|
||||
packetID = maxID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePacket(INetworkManager network, Packet250CustomPayload packet, Player player, ByteArrayDataInput data)
|
||||
{
|
||||
try
|
||||
{
|
||||
World world = ((EntityPlayer) player).worldObj;
|
||||
String effectName = data.readUTF();
|
||||
if (world != null)
|
||||
{
|
||||
if (effectName.equalsIgnoreCase("laser"))
|
||||
{
|
||||
DarkMain.proxy.renderBeam(world, PacketHandler.readVector3(data), PacketHandler.readVector3(data), new Color(data.readInt(), data.readInt(), data.readInt()), data.readInt());
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println("[CoreMachine] Error reading packet for effect rendering");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void sendClientLaserEffect(World world, Vector3 position, Vector3 target, Color color, int age)
|
||||
{
|
||||
Packet packet = PacketHandler.instance().getPacketWithID(DarkMain.CHANNEL, packetID, position, target, color.getRed(), color.getBlue(), color.getGreen(), age);
|
||||
PacketHandler.instance().sendPacketToClients(packet, world, position, position.distance(target));
|
||||
}
|
||||
|
||||
}
|
62
src/dark/core/network/PacketManagerTile.java
Normal file
62
src/dark/core/network/PacketManagerTile.java
Normal file
|
@ -0,0 +1,62 @@
|
|||
package dark.core.network;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.network.INetworkManager;
|
||||
import net.minecraft.network.packet.Packet250CustomPayload;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.world.World;
|
||||
import universalelectricity.prefab.network.IPacketReceiver;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
|
||||
import cpw.mods.fml.common.network.Player;
|
||||
|
||||
public class PacketManagerTile implements IPacketManager
|
||||
{
|
||||
static int packetID = 0;
|
||||
|
||||
@Override
|
||||
public int getID()
|
||||
{
|
||||
return packetID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setID(int maxID)
|
||||
{
|
||||
packetID = maxID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlePacket(INetworkManager network, Packet250CustomPayload packet, Player player, ByteArrayDataInput data)
|
||||
{
|
||||
try
|
||||
{
|
||||
int x = data.readInt();
|
||||
int y = data.readInt();
|
||||
int z = data.readInt();
|
||||
|
||||
World world = ((EntityPlayer) player).worldObj;
|
||||
|
||||
if (world != null)
|
||||
{
|
||||
TileEntity tileEntity = world.getBlockTileEntity(x, y, z);
|
||||
|
||||
if (tileEntity != null)
|
||||
{
|
||||
if (tileEntity instanceof IPacketReceiver)
|
||||
{
|
||||
((IPacketReceiver) tileEntity).handlePacketData(network, 0, packet, ((EntityPlayer) player), data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println("[CoreMachine] Error reading packet at tile packet manager");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -17,7 +17,6 @@ import universalelectricity.compatibility.TileEntityUniversalElectrical;
|
|||
import universalelectricity.core.electricity.ElectricityPack;
|
||||
import universalelectricity.core.vector.Vector3;
|
||||
import universalelectricity.prefab.network.IPacketReceiver;
|
||||
import universalelectricity.prefab.network.PacketManager;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
|
||||
|
@ -25,9 +24,11 @@ import cpw.mods.fml.relauncher.Side;
|
|||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import dark.api.IDisableable;
|
||||
import dark.api.energy.IPowerLess;
|
||||
import dark.core.common.DarkMain;
|
||||
import dark.core.common.ExternalModHandler;
|
||||
import dark.core.interfaces.IExternalInv;
|
||||
import dark.core.interfaces.IInvBox;
|
||||
import dark.core.network.PacketHandler;
|
||||
import dark.core.prefab.invgui.InvChest;
|
||||
|
||||
/** Prefab for most machines in the CoreMachine set. Provides basic power updates, packet updates,
|
||||
|
@ -305,14 +306,17 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im
|
|||
}
|
||||
|
||||
/** NetworkMod channel name */
|
||||
public abstract String getChannel();
|
||||
public String getChannel()
|
||||
{
|
||||
return DarkMain.CHANNEL;
|
||||
}
|
||||
|
||||
/** Sends a simple true/false am running power update */
|
||||
public void sendPowerUpdate()
|
||||
{
|
||||
if (!this.worldObj.isRemote)
|
||||
{
|
||||
PacketManager.sendPacketToClients(PacketManager.getPacket(this.getChannel(), this, TilePacketTypes.POWER.name, this.running), worldObj, new Vector3(this), 64);
|
||||
PacketHandler.instance().sendPacketToClients(PacketHandler.instance().getPacket(this.getChannel(), this, TilePacketTypes.POWER.name, this.running), worldObj, new Vector3(this), 64);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -323,7 +327,7 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im
|
|||
{
|
||||
NBTTagCompound tag = new NBTTagCompound();
|
||||
this.writeToNBT(tag);
|
||||
PacketManager.sendPacketToClients(PacketManager.getPacket(this.getChannel(), this, TilePacketTypes.NBT.name, tag), worldObj, new Vector3(this), 64);
|
||||
PacketHandler.instance().sendPacketToClients(PacketHandler.instance().getPacket(this.getChannel(), this, TilePacketTypes.NBT.name, tag), worldObj, new Vector3(this), 64);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -332,7 +336,7 @@ public abstract class TileEntityMachine extends TileEntityUniversalElectrical im
|
|||
{
|
||||
NBTTagCompound tag = new NBTTagCompound();
|
||||
this.writeToNBT(tag);
|
||||
return PacketManager.getPacket(this.getChannel(), this, TilePacketTypes.NBT.name, tag);
|
||||
return PacketHandler.instance().getPacket(this.getChannel(), this, TilePacketTypes.NBT.name, tag);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -9,10 +9,11 @@ import net.minecraft.tileentity.TileEntity;
|
|||
import net.minecraft.world.World;
|
||||
import universalelectricity.core.vector.Vector3;
|
||||
import universalelectricity.prefab.network.IPacketReceiver;
|
||||
import universalelectricity.prefab.network.PacketManager;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
|
||||
import dark.core.network.PacketHandler;
|
||||
|
||||
/** This is a multiblock to be used for blocks that are bigger than one block.
|
||||
*
|
||||
* @author Calclavia */
|
||||
|
@ -52,7 +53,7 @@ public class TileEntityMulti extends TileEntity implements IPacketReceiver
|
|||
this.channel = ((BlockMulti) this.getBlockType()).channel;
|
||||
}
|
||||
|
||||
return PacketManager.getPacket(this.channel, this, this.mainBlockPosition.intX(), this.mainBlockPosition.intY(), this.mainBlockPosition.intZ());
|
||||
return PacketHandler.instance().getPacket(this.channel, this, this.mainBlockPosition.intX(), this.mainBlockPosition.intY(), this.mainBlockPosition.intZ());
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package dark.core.prefab.damage;
|
||||
|
||||
import dark.core.interfaces.IBlockActivated;
|
||||
import net.minecraft.potion.PotionEffect;
|
||||
import net.minecraft.util.DamageSource;
|
||||
import dark.core.interfaces.IBlockActivated;
|
||||
|
||||
/** Used by tiles that want to pretend to be living objects. Will require the use of this interface
|
||||
* as well spawning a EntityTileDamage entity as its location
|
||||
|
|
|
@ -13,7 +13,6 @@ import net.minecraft.network.INetworkManager;
|
|||
import net.minecraft.network.packet.Packet;
|
||||
import net.minecraft.network.packet.Packet250CustomPayload;
|
||||
import universalelectricity.prefab.network.IPacketReceiver;
|
||||
import universalelectricity.prefab.network.PacketManager;
|
||||
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
|
||||
|
@ -22,6 +21,7 @@ import cpw.mods.fml.common.network.PacketDispatcher;
|
|||
import cpw.mods.fml.common.network.Player;
|
||||
import dark.api.ISpecialAccess;
|
||||
import dark.api.ITerminal;
|
||||
import dark.core.network.PacketHandler;
|
||||
import dark.core.prefab.TileEntityMachine;
|
||||
import dark.core.prefab.access.AccessLevel;
|
||||
import dark.core.prefab.access.UserAccess;
|
||||
|
@ -84,16 +84,13 @@ public abstract class TileEntityTerminal extends TileEntityMachine implements IS
|
|||
}
|
||||
}
|
||||
|
||||
/** Channel to be used for packets */
|
||||
public abstract String getChannel();
|
||||
|
||||
/** Sends all NBT data. Server -> Client */
|
||||
@Override
|
||||
public Packet getDescriptionPacket()
|
||||
{
|
||||
NBTTagCompound nbt = new NBTTagCompound();
|
||||
this.writeToNBT(nbt);
|
||||
return PacketManager.getPacket(this.getChannel(), this, PacketType.DESCRIPTION_DATA.ordinal(), nbt);
|
||||
return PacketHandler.instance().getPacket(this.getChannel(), this, PacketType.DESCRIPTION_DATA.ordinal(), nbt);
|
||||
}
|
||||
|
||||
/** Sends all Terminal data Server -> Client */
|
||||
|
@ -104,7 +101,7 @@ public abstract class TileEntityTerminal extends TileEntityMachine implements IS
|
|||
data.add(this.getTerminalOuput().size());
|
||||
data.addAll(this.getTerminalOuput());
|
||||
|
||||
Packet packet = PacketManager.getPacket(this.getChannel(), this, data.toArray());
|
||||
Packet packet = PacketHandler.instance().getPacket(this.getChannel(), this, data.toArray());
|
||||
|
||||
for (EntityPlayer player : this.playersUsing)
|
||||
{
|
||||
|
@ -117,7 +114,7 @@ public abstract class TileEntityTerminal extends TileEntityMachine implements IS
|
|||
{
|
||||
if (this.worldObj.isRemote)
|
||||
{
|
||||
Packet packet = PacketManager.getPacket(this.getChannel(), this, PacketType.GUI_COMMAND.ordinal(), entityPlayer.username, cmdInput);
|
||||
Packet packet = PacketHandler.instance().getPacket(this.getChannel(), this, PacketType.GUI_COMMAND.ordinal(), entityPlayer.username, cmdInput);
|
||||
PacketDispatcher.sendPacketToServer(packet);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@ import java.util.List;
|
|||
import java.util.Set;
|
||||
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
import net.minecraftforge.common.ForgeDirection;
|
||||
import universalelectricity.core.vector.Vector3;
|
||||
|
||||
|
|
Loading…
Reference in a new issue