diff --git a/src/dark/core/common/blocks/TileEntityGasBlock.java b/src/dark/core/common/blocks/TileEntityGasBlock.java index 396ce3e7..b52ad9ab 100644 --- a/src/dark/core/common/blocks/TileEntityGasBlock.java +++ b/src/dark/core/common/blocks/TileEntityGasBlock.java @@ -71,7 +71,7 @@ public class TileEntityGasBlock extends TileEntity implements ISimplePacketRecei { if (this.stack != null) { - return PacketHandler.instance().getPacket(DarkMain.CHANNEL, this, "Desc", this.stack.writeToNBT(new NBTTagCompound())); + return PacketHandler.instance().getTilePacket(DarkMain.CHANNEL, this, "Desc", this.stack.writeToNBT(new NBTTagCompound())); } return null; } diff --git a/src/dark/core/common/machines/TileEntityBatteryBox.java b/src/dark/core/common/machines/TileEntityBatteryBox.java index 500a940f..dd6a7cba 100644 --- a/src/dark/core/common/machines/TileEntityBatteryBox.java +++ b/src/dark/core/common/machines/TileEntityBatteryBox.java @@ -55,7 +55,7 @@ public class TileEntityBatteryBox extends TileEntityEnergyMachine @Override public Packet getDescriptionPacket() { - return PacketHandler.instance().getPacket(this.getChannel(), this, "energy", this.getEnergyStored()); + return PacketHandler.instance().getTilePacket(this.getChannel(), this, "energy", this.getEnergyStored()); } @Override diff --git a/src/dark/core/common/machines/TileEntityCoalGenerator.java b/src/dark/core/common/machines/TileEntityCoalGenerator.java index e445cfe2..e57f509b 100644 --- a/src/dark/core/common/machines/TileEntityCoalGenerator.java +++ b/src/dark/core/common/machines/TileEntityCoalGenerator.java @@ -105,7 +105,7 @@ public class TileEntityCoalGenerator extends TileEntityEnergyMachine { if (entity != null) { - PacketDispatcher.sendPacketToPlayer(PacketHandler.instance().getPacket(this.getChannel(), this, "gen", this.generateWatts, this.itemCookTime), (Player) entity); + PacketDispatcher.sendPacketToPlayer(PacketHandler.instance().getTilePacket(this.getChannel(), this, "gen", this.generateWatts, this.itemCookTime), (Player) entity); } } diff --git a/src/dark/core/common/machines/TileEntityElectricFurnace.java b/src/dark/core/common/machines/TileEntityElectricFurnace.java index c0b6807e..19540f0a 100644 --- a/src/dark/core/common/machines/TileEntityElectricFurnace.java +++ b/src/dark/core/common/machines/TileEntityElectricFurnace.java @@ -95,7 +95,7 @@ public class TileEntityElectricFurnace extends TileEntityEnergyMachine @Override public Packet getDescriptionPacket() { - return PacketHandler.instance().getPacket(this.getChannel(), this, "processTicks", this.processTicks); + return PacketHandler.instance().getTilePacket(this.getChannel(), this, "processTicks", this.processTicks); } @Override diff --git a/src/dark/core/interfaces/IDamageableTile.java b/src/dark/core/interfaces/IDamageableTile.java index b7acc5ee..598b777d 100644 --- a/src/dark/core/interfaces/IDamageableTile.java +++ b/src/dark/core/interfaces/IDamageableTile.java @@ -22,13 +22,13 @@ public interface IDamageableTile extends IBlockActivated public boolean isAlive(); /** Current health of the tile */ - public float health(); + public float getDamage(); /** Sets the tiles heath * * @param health - amount hit points * @param increase - increase instead of replace */ - public void setHealth(float health); + public void setDamage(float health); /** Max hit points of the object */ public float getMaxHealth(); diff --git a/src/dark/core/network/PacketHandler.java b/src/dark/core/network/PacketHandler.java index b0862925..6b864399 100644 --- a/src/dark/core/network/PacketHandler.java +++ b/src/dark/core/network/PacketHandler.java @@ -178,7 +178,7 @@ public class PacketHandler implements IPacketHandler, IPacketReceiver * * @return */ @SuppressWarnings("resource") - public Packet getPacket(String channelName, TileEntity sender, Object... sendData) + public Packet getTilePacket(String channelName, TileEntity sender, Object... sendData) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); DataOutputStream data = new DataOutputStream(bytes); diff --git a/src/dark/core/prefab/machine/TileEntityMachine.java b/src/dark/core/prefab/machine/TileEntityMachine.java index 01aeee7a..3caf6375 100644 --- a/src/dark/core/prefab/machine/TileEntityMachine.java +++ b/src/dark/core/prefab/machine/TileEntityMachine.java @@ -198,7 +198,7 @@ public abstract class TileEntityMachine extends TileEntityInv implements ISidedI { NBTTagCompound tag = new NBTTagCompound(); this.writeToNBT(tag); - PacketHandler.instance().sendPacketToClients(PacketHandler.instance().getPacket(this.getChannel(), this, SimplePacketTypes.NBT.name, tag), worldObj, new Vector3(this), 64); + PacketHandler.instance().sendPacketToClients(PacketHandler.instance().getTilePacket(this.getChannel(), this, SimplePacketTypes.NBT.name, tag), worldObj, new Vector3(this), 64); } } @@ -207,7 +207,7 @@ public abstract class TileEntityMachine extends TileEntityInv implements ISidedI { if (!this.worldObj.isRemote) { - PacketHandler.instance().sendPacketToClients(PacketHandler.instance().getPacket(this.getChannel(), this, SimplePacketTypes.RUNNING.name, this.functioning), worldObj, new Vector3(this), 64); + PacketHandler.instance().sendPacketToClients(PacketHandler.instance().getTilePacket(this.getChannel(), this, SimplePacketTypes.RUNNING.name, this.functioning), worldObj, new Vector3(this), 64); } } @@ -236,7 +236,7 @@ public abstract class TileEntityMachine extends TileEntityInv implements ISidedI @Override public Packet getDescriptionPacket() { - return PacketHandler.instance().getPacket(this.getChannel(), this, SimplePacketTypes.RUNNING.name, this.functioning); + return PacketHandler.instance().getTilePacket(this.getChannel(), this, SimplePacketTypes.RUNNING.name, this.functioning); } /** NetworkMod channel name */ diff --git a/src/dark/core/prefab/machine/TileEntityMulti.java b/src/dark/core/prefab/machine/TileEntityMulti.java index 81551927..37e8272c 100644 --- a/src/dark/core/prefab/machine/TileEntityMulti.java +++ b/src/dark/core/prefab/machine/TileEntityMulti.java @@ -54,7 +54,7 @@ public class TileEntityMulti extends TileEntity implements IPacketReceiver this.channel = ((BlockMulti) this.getBlockType()).channel; } - return PacketHandler.instance().getPacket(this.channel, this, this.mainBlockPosition.intX(), this.mainBlockPosition.intY(), this.mainBlockPosition.intZ()); + return PacketHandler.instance().getTilePacket(this.channel, this, this.mainBlockPosition.intX(), this.mainBlockPosition.intY(), this.mainBlockPosition.intZ()); } diff --git a/src/dark/core/prefab/sentry/TileEntitySentry.java b/src/dark/core/prefab/sentry/TileEntitySentry.java index da83e312..6a31b25b 100644 --- a/src/dark/core/prefab/sentry/TileEntitySentry.java +++ b/src/dark/core/prefab/sentry/TileEntitySentry.java @@ -1,13 +1,15 @@ package dark.core.prefab.sentry; -import java.util.ArrayList; +import java.io.IOException; import java.util.List; -import cpw.mods.fml.common.FMLCommonHandler; -import cpw.mods.fml.relauncher.Side; +import com.google.common.io.ByteArrayDataInput; + +import cpw.mods.fml.common.network.Player; -import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.network.packet.Packet; import net.minecraft.potion.PotionEffect; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; @@ -15,9 +17,13 @@ import net.minecraft.util.DamageSource; import net.minecraftforge.common.ForgeDirection; import universalelectricity.core.vector.Vector3; import dark.api.ISentryGun; +import dark.core.common.DarkMain; import dark.core.helpers.MathHelper; +import dark.core.helpers.RayTraceHelper; +import dark.core.network.PacketHandler; import dark.core.prefab.EntityTileDamage; import dark.core.prefab.machine.TileEntityMachine; +import dark.core.prefab.machine.TileEntityMachine.SimplePacketTypes; /** Prefab tileEntity for creating senty guns that can be of type aimed, mounted, or automated. * Contains most of the code for a sentry gun to operate short of aiming and operating logic. This @@ -36,7 +42,7 @@ public abstract class TileEntitySentry extends TileEntityMachine implements ISen private float damage = 0.0f; private final float maxDamage; - private Vector3 rotation = new Vector3(), newRotation = new Vector3(), prevRotation = new Vector3(); + private Vector3 rotation = new Vector3(), targetRotation = new Vector3(), prevRotation = new Vector3(); protected float roationSpeed = 10f, minPitch = -30, maxPitch = 30, minYaw = -180, maxYaw = 180, size = 1.0f; public TileEntitySentry(float maxDamage) @@ -98,15 +104,15 @@ public abstract class TileEntitySentry extends TileEntityMachine implements ISen public void updateRotation() { this.prevRotation = this.getRotation(); - this.rotation.x = MathHelper.clamp((float) MathHelper.updateRotation(this.rotation.x, this.newRotation.x, this.roationSpeed), this.minPitch, this.maxPitch); - this.rotation.y = MathHelper.clamp((float) MathHelper.updateRotation(this.rotation.y, this.newRotation.y, this.roationSpeed), this.minYaw, this.maxYaw); + this.rotation.x = MathHelper.clamp((float) MathHelper.updateRotation(this.rotation.x, this.targetRotation.x, this.roationSpeed), this.minPitch, this.maxPitch); + this.rotation.y = MathHelper.clamp((float) MathHelper.updateRotation(this.rotation.y, this.targetRotation.y, this.roationSpeed), this.minYaw, this.maxYaw); } @Override public Vector3 getLook() { - // TODO Auto-generated method stub - return null; + //TODO store this value so a new vector is not created each call + return new Vector3(RayTraceHelper.getLook(this.worldObj, this.getRotation().floatX(), this.getRotation().floatY(), this.size)); } @Override @@ -122,13 +128,13 @@ public abstract class TileEntitySentry extends TileEntityMachine implements ISen @Override public void updateRotation(float pitch, float yaw, float roll) { - if (this.newRotation == null) + if (this.targetRotation == null) { - this.newRotation = this.getRotation(); + this.targetRotation = this.getRotation(); } - this.newRotation.x += pitch; - this.newRotation.y += yaw; - this.newRotation.z += roll; + this.targetRotation.x += pitch; + this.targetRotation.y += yaw; + this.targetRotation.z += roll; } @Override @@ -146,7 +152,31 @@ public abstract class TileEntitySentry extends TileEntityMachine implements ISen @Override public boolean onDamageTaken(DamageSource source, float ammount) { - // TODO Auto-generated method stub + if (source != null && ammount > 0) + { + if (source.equals(DamageSource.onFire)) + { + //TODO cause heat damage slowly but not right away + //TODO check for heat sources around the sentry + //TODO mess with the sentries abilities when over heated + return false; + } + else + { + this.setDamage(this.getDamage() - ammount); + + if (this.getDamage() <= 0) + { + this.isAlive = false; + if (this.entitySentry != null) + { + this.entitySentry.setDead(); + } + this.entitySentry = null; + } + return true; + } + } return false; } @@ -157,13 +187,13 @@ public abstract class TileEntitySentry extends TileEntityMachine implements ISen } @Override - public float health() + public float getDamage() { return this.damage; } @Override - public void setHealth(float health) + public void setDamage(float health) { this.damage = health; } @@ -171,7 +201,6 @@ public abstract class TileEntitySentry extends TileEntityMachine implements ISen @Override public float getMaxHealth() { - return this.maxDamage; } @@ -225,4 +254,71 @@ public abstract class TileEntitySentry extends TileEntityMachine implements ISen // TODO Auto-generated method stub return false; } + + /* ****************************************************** + * Save/Load/PacketHandling + * ****************************************************** */ + + @Override + public void writeToNBT(NBTTagCompound nbt) + { + super.writeToNBT(nbt); + if (this.getRotation() != null) + { + nbt.setFloat("pitch", this.getRotation().floatX()); + nbt.setFloat("yaw", this.getRotation().floatY()); + nbt.setFloat("roll", this.getRotation().floatZ()); + } + if (this.targetRotation != null) + { + nbt.setFloat("npitch", this.targetRotation.floatX()); + nbt.setFloat("nyaw", this.targetRotation.floatY()); + nbt.setFloat("nroll", this.targetRotation.floatZ()); + } + nbt.setFloat("damage", this.getDamage()); + nbt.setByte("mountSide", (byte) this.mountingSide.ordinal()); + } + + @Override + public void readFromNBT(NBTTagCompound nbt) + { + super.readFromNBT(nbt); + this.getRotation().x = nbt.getFloat("pitch"); + this.getRotation().y = nbt.getFloat("yaw"); + this.getRotation().z = nbt.getFloat("roll"); + this.targetRotation = new Vector3(); + this.targetRotation.x = nbt.getFloat("pitch"); + this.targetRotation.y = nbt.getFloat("yaw"); + this.targetRotation.z = nbt.getFloat("roll"); + this.setDamage(nbt.getFloat("damage")); + this.mountingSide = ForgeDirection.getOrientation(nbt.getByte("mountSide")); + } + + @Override + public Packet getDescriptionPacket() + { + return PacketHandler.instance().getTilePacket(DarkMain.CHANNEL, this, "Desc", this.isRunning, this.rotation); + } + + @Override + public boolean simplePacket(String id, ByteArrayDataInput dis, Player player) + { + try + { + if (this.worldObj.isRemote && !super.simplePacket(id, dis, player)) + { + if (id.equalsIgnoreCase("Desc")) + { + this.functioning = dis.readBoolean(); + this.rotation = PacketHandler.readVector3(dis); + return true; + } + } + } + catch (Exception e) + { + e.printStackTrace(); + } + return false; + } } diff --git a/src/dark/core/prefab/terminal/TileEntityTerminal.java b/src/dark/core/prefab/terminal/TileEntityTerminal.java index 75dc13e0..1c93147c 100644 --- a/src/dark/core/prefab/terminal/TileEntityTerminal.java +++ b/src/dark/core/prefab/terminal/TileEntityTerminal.java @@ -58,7 +58,7 @@ public abstract class TileEntityTerminal extends TileEntityEnergyMachine impleme { NBTTagCompound nbt = new NBTTagCompound(); this.writeToNBT(nbt); - return PacketHandler.instance().getPacket(this.getChannel(), this, SimplePacketTypes.NBT.name, nbt); + return PacketHandler.instance().getTilePacket(this.getChannel(), this, SimplePacketTypes.NBT.name, nbt); } /** Sends all Terminal data Server -> Client */ @@ -69,7 +69,7 @@ public abstract class TileEntityTerminal extends TileEntityEnergyMachine impleme data.add(this.getTerminalOuput().size()); data.addAll(this.getTerminalOuput()); - Packet packet = PacketHandler.instance().getPacket(this.getChannel(), this, data.toArray()); + Packet packet = PacketHandler.instance().getTilePacket(this.getChannel(), this, data.toArray()); for (Object entity : this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox(xCoord - 10, yCoord - 10, zCoord - 10, xCoord + 10, yCoord + 10, zCoord + 10))) { @@ -85,7 +85,7 @@ public abstract class TileEntityTerminal extends TileEntityEnergyMachine impleme { if (this.worldObj.isRemote) { - Packet packet = PacketHandler.instance().getPacket(this.getChannel(), this, SimplePacketTypes.GUI_COMMAND.name, entityPlayer.username, cmdInput); + Packet packet = PacketHandler.instance().getTilePacket(this.getChannel(), this, SimplePacketTypes.GUI_COMMAND.name, entityPlayer.username, cmdInput); PacketDispatcher.sendPacketToServer(packet); } }