Refactored LaserTargetting packet sender

This commit is contained in:
LemADEC 2014-09-14 17:12:06 +02:00
parent bca3975ad2
commit 7b54cc48dd
2 changed files with 43 additions and 77 deletions

View file

@ -17,6 +17,7 @@ import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World; import net.minecraft.world.World;
import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.network.IPacketHandler; import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player; import cpw.mods.fml.common.network.Player;
import cr0s.WarpDrive.data.Vector3; import cr0s.WarpDrive.data.Vector3;
import cr0s.WarpDrive.machines.TileEntityCamera; import cr0s.WarpDrive.machines.TileEntityCamera;
@ -24,25 +25,16 @@ import cr0s.WarpDrive.machines.TileEntityLaser;
import cr0s.WarpDrive.machines.TileEntityMonitor; import cr0s.WarpDrive.machines.TileEntityMonitor;
import net.minecraft.client.multiplayer.WorldClient; import net.minecraft.client.multiplayer.WorldClient;
public class PacketHandler implements IPacketHandler public class PacketHandler implements IPacketHandler {
{
@Override @Override
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) {
{ if (packet.channel.equals("WarpDriveBeam")) {
if (packet.channel.equals("WarpDriveBeam"))
{
handleBeam(packet, (EntityPlayer)player); handleBeam(packet, (EntityPlayer)player);
} } else if (packet.channel.equals("WarpDriveFreq")) {
else if (packet.channel.equals("WarpDriveFreq"))
{
handleFreqUpdate(packet, (EntityPlayer)player); handleFreqUpdate(packet, (EntityPlayer)player);
} } else if (packet.channel.equals("WarpDriveLaserT")) {
else if (packet.channel.equals("WarpDriveLaserT"))
{
handleLaserTargeting(packet, (EntityPlayer)player); handleLaserTargeting(packet, (EntityPlayer)player);
} } else if (packet.channel.equals("WarpDriveCloaks")) {
else if (packet.channel.equals("WarpDriveCloaks"))
{
handleCloak(packet, (EntityPlayer)player); handleCloak(packet, (EntityPlayer)player);
} }
} }
@ -130,12 +122,10 @@ public class PacketHandler implements IPacketHandler
} }
} }
public static void handleLaserTargeting(Packet250CustomPayload packet, EntityPlayer player) public static void handleLaserTargeting(Packet250CustomPayload packet, EntityPlayer player) {
{
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data)); DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));
try try {
{
int x = inputStream.readInt(); int x = inputStream.readInt();
int y = inputStream.readInt(); int y = inputStream.readInt();
int z = inputStream.readInt(); int z = inputStream.readInt();
@ -145,14 +135,9 @@ public class PacketHandler implements IPacketHandler
TileEntity te = player.worldObj.getBlockTileEntity(x, y, z); TileEntity te = player.worldObj.getBlockTileEntity(x, y, z);
if (te != null && te instanceof TileEntityLaser) { if (te != null && te instanceof TileEntityLaser) {
TileEntityLaser laser = (TileEntityLaser)te; TileEntityLaser laser = (TileEntityLaser)te;
laser.yaw = yaw; laser.initiateBeamEmission(yaw, pitch);
laser.pitch = pitch;
laser.delayTicks = 0;
laser.isEmitting = true;
} }
} } catch (Exception e) {
catch (Exception e)
{
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -185,8 +170,7 @@ public class PacketHandler implements IPacketHandler
} }
} }
private void handleBeam(Packet250CustomPayload packet, EntityPlayer player) private void handleBeam(Packet250CustomPayload packet, EntityPlayer player) {
{
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data)); DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(packet.data));
Vector3 source, target; Vector3 source, target;
double sx, sy, sz; double sx, sy, sz;
@ -196,8 +180,7 @@ public class PacketHandler implements IPacketHandler
int energy; int energy;
World worldObj = player.worldObj; World worldObj = player.worldObj;
try try {
{
// Read source vector // Read source vector
sx = inputStream.readDouble(); sx = inputStream.readDouble();
sy = inputStream.readDouble(); sy = inputStream.readDouble();
@ -221,16 +204,13 @@ public class PacketHandler implements IPacketHandler
// WarpDrive.debugPrint("Received beam packet from " + source + " to " + target + " as RGB " + r + " " + g + " " + b + " age " + age +" energy " + energy); // WarpDrive.debugPrint("Received beam packet from " + source + " to " + target + " as RGB " + r + " " + g + " " + b + " age " + age +" energy " + energy);
// To avoid NPE at logging in // To avoid NPE at logging in
if (worldObj == null) if (worldObj == null) {
{ WarpDrive.debugPrint("WorldObj is null, ignoring beam packet");
WarpDrive.debugPrint("WorldObj is null");
return; return;
} }
WarpDrive.proxy.renderBeam(worldObj, source.clone(), target.clone(), r, g, b, age, energy); WarpDrive.proxy.renderBeam(worldObj, source.clone(), target.clone(), r, g, b, age, energy);
} } catch (Exception e) {
catch (IOException e)
{
e.printStackTrace(); e.printStackTrace();
return; return;
} }
@ -258,4 +238,29 @@ public class PacketHandler implements IPacketHandler
// WarpDrive.debugPrint("Packet '" + packet.channel + "' sent (" + xCoord + ", " + yCoord + ", " + zCoord + ") '" + frequency + "'"); // WarpDrive.debugPrint("Packet '" + packet.channel + "' sent (" + xCoord + ", " + yCoord + ", " + zCoord + ") '" + frequency + "'");
} }
} }
// LaserCamera shooting at target (client -> server)
public static void sendLaserTargetingPacket(int x, int y, int z, float yaw, float pitch) {
if (FMLCommonHandler.instance().getEffectiveSide().isClient()) {
ByteArrayOutputStream bos = new ByteArrayOutputStream(8);
DataOutputStream outputStream = new DataOutputStream(bos);
try {
outputStream.writeInt(x);
outputStream.writeInt(y);
outputStream.writeInt(z);
outputStream.writeFloat(yaw);
outputStream.writeFloat(pitch);
} catch (Exception e) {
e.printStackTrace();
}
Packet250CustomPayload packet = new Packet250CustomPayload();
packet.channel = "WarpDriveLaserT";
packet.data = bos.toByteArray();
packet.length = bos.size();
PacketDispatcher.sendPacketToServer(packet);
WarpDrive.debugPrint("Packet '" + packet.channel + "' sent (" + x + ", " + y + ", " + z + ") yaw " + yaw + " pitch " + pitch);
}
}
} }

View file

@ -2,6 +2,7 @@ package cr0s.WarpDrive.render;
import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.network.PacketDispatcher; import cpw.mods.fml.common.network.PacketDispatcher;
import cr0s.WarpDrive.PacketHandler;
import cr0s.WarpDrive.WarpDrive; import cr0s.WarpDrive.WarpDrive;
import cr0s.WarpDrive.WarpDriveConfig; import cr0s.WarpDrive.WarpDriveConfig;
@ -127,7 +128,7 @@ public final class EntityCamera extends EntityLivingBase
// Make a shoot with camera-laser // Make a shoot with camera-laser
if (blockID == WarpDriveConfig.laserCamID) { if (blockID == WarpDriveConfig.laserCamID) {
sendTargetPacket(); PacketHandler.sendLaserTargetingPacket(xCoord, yCoord, zCoord, mc.renderViewEntity.rotationYaw, mc.renderViewEntity.rotationPitch);
} }
} }
} else { } else {
@ -232,44 +233,4 @@ public final class EntityCamera extends EntityLivingBase
public ItemStack[] getLastActiveItems() { public ItemStack[] getLastActiveItems() {
return null; return null;
} }
// Camera orientation refresh to server packet
public void sendTargetPacket() {
if (FMLCommonHandler.instance().getEffectiveSide().isClient()) {
ByteArrayOutputStream bos = new ByteArrayOutputStream(8);
DataOutputStream outputStream = new DataOutputStream(bos);
try {
outputStream.writeInt(xCoord);
outputStream.writeInt(yCoord);
outputStream.writeInt(zCoord);
outputStream.writeFloat(mc.renderViewEntity.rotationYaw);
outputStream.writeFloat(mc.renderViewEntity.rotationPitch);
} catch (Exception e) {
e.printStackTrace();
}
Packet250CustomPayload packet = new Packet250CustomPayload();
packet.channel = "WarpDriveLaserT";
packet.data = bos.toByteArray();
packet.length = bos.size();
PacketDispatcher.sendPacketToServer(packet);
WarpDrive.debugPrint("" + this + " Packet '" + packet.channel + "' sent (" + xCoord + ", " + yCoord + ", " + zCoord + ") yawn " + mc.renderViewEntity.rotationYaw + " pitch " + mc.renderViewEntity.rotationPitch);
}
}
/*
@Override
public void sendChatToPlayer(ChatMessageComponent chatmessagecomponent) {
}
@Override
public boolean canCommandSenderUseCommand(int i, String s) {
return false;
}
@Override
public ChunkCoordinates getPlayerCoordinates() {
return new ChunkCoordinates(xCoord, yCoord, zCoord);
}*/
} }