hassansyyid

PoweredTileEntity

Jun 24th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.75 KB | None | 0 0
  1. package com.arisux.avp.entities.tile;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.ListIterator;
  7.  
  8. import net.minecraft.nbt.NBTTagCompound;
  9. import net.minecraft.network.NetworkManager;
  10. import net.minecraft.network.Packet;
  11. import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
  12. import net.minecraft.tileentity.TileEntity;
  13. import net.minecraft.util.EnumFacing;
  14.  
  15. import com.arisux.avp.interfaces.IPowerDevice;
  16.  
  17. public abstract class PoweredTileEntity extends TileEntity implements IPowerDevice
  18. {
  19.     public double voltage, amps;
  20.  
  21.     @Override
  22.     public void updateEntity()
  23.     {
  24.         super.updateEntity();
  25.        
  26.         if (this.isVoltageInOperatingRange())
  27.         {
  28.             this.onVoltageTick();
  29.         }
  30.        
  31.         if (this.voltage > this.getMaxOperatingVoltage())
  32.         {
  33.             this.onOverloadTick();
  34.         }
  35.        
  36.         if (this.voltage <= this.getMinOperatingVoltage())
  37.         {
  38.             this.onUnderloadTick();
  39.         }
  40.        
  41.         if(!this.isConnectedToPower())
  42.         {
  43.             this.voltage = 0;
  44.         }
  45.  
  46.         if (this.getPowerSourceTile() == null)
  47.         {
  48.             this.voltage = 0;
  49.         }
  50.        
  51.         if (this.canOutputPower())
  52.         {
  53.             this.outputPower();
  54.         }
  55.     }
  56.    
  57.     public boolean isConnectedToPower(){
  58.         List<PoweredTileEntity> list = new ArrayList<PoweredTileEntity>();
  59.         list.add(this.getTop());
  60.         list.add(this.getBack());
  61.         list.add(this.getBottom());
  62.         list.add(this.getLeft());
  63.         list.add(this.getRight());
  64.         list.add(this.getFront());
  65.         ListIterator<PoweredTileEntity> ite = list.listIterator();
  66.         while(ite.hasNext()){
  67.             PoweredTileEntity e = ite.next();
  68.             if (e instanceof TileEntitySolarPanel || e instanceof TileEntityRepulsionGenerator){
  69.                 return true;
  70.             }
  71.             else if (e instanceof TileEntityPowerline){
  72.                 ite.add(e.getTop());
  73.                 ite.add(e.getBack());
  74.                 ite.add(e.getBottom());
  75.                 ite.add(e.getLeft());
  76.                 ite.add(e.getRight());
  77.                 ite.add(e.getFront());
  78.                 ite = list.listIterator();
  79.             }
  80.             else if(e == null){
  81.                 break;
  82.             }
  83.         }
  84.         return false;
  85.     }
  86.     public void outputPower()
  87.     {
  88.         double voltage = this.getVoltageAfterApplyingResistance();
  89.        
  90.         this.outputPowerToTile(this.getTop(), voltage);
  91.         this.outputPowerToTile(this.getBottom(), voltage);
  92.         this.outputPowerToTile(this.getFront(), voltage);
  93.         this.outputPowerToTile(this.getBack(), voltage);
  94.         this.outputPowerToTile(this.getLeft(), voltage);
  95.         this.outputPowerToTile(this.getRight(), voltage);
  96.     }
  97.    
  98.     public void outputPowerToTile(PoweredTileEntity tile, double voltage)
  99.     {
  100.         if (tile != null && tile.getPowerSourceTile() == this)
  101.         {
  102.             tile.setVoltage(voltage);
  103.         }
  104.         else if(tile != null && tile instanceof TileEntityPowerline && tile.getVoltage() == 0){
  105.             tile.setVoltage(this.getVoltage());
  106.         }
  107.     }
  108.  
  109.     @Override
  110.     public abstract void onVoltageTick();
  111.  
  112.     @Override
  113.     public abstract void onOverloadTick();
  114.  
  115.     @Override
  116.     public abstract void onUnderloadTick();
  117.  
  118.     @Override
  119.     public Packet getDescriptionPacket()
  120.     {
  121.         NBTTagCompound nbtTag = new NBTTagCompound();
  122.         this.writeToNBT(nbtTag);
  123.         return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 1, nbtTag);
  124.     }
  125.  
  126.     @Override
  127.     public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet)
  128.     {
  129.         readFromNBT(packet.func_148857_g());
  130.     }
  131.  
  132.     @Override
  133.     public void writeToNBT(NBTTagCompound tag)
  134.     {
  135.         super.writeToNBT(tag);
  136.  
  137.         tag.setDouble("Volts", this.voltage);
  138.         tag.setDouble("Amps", this.amps);
  139.     }
  140.  
  141.     @Override
  142.     public void readFromNBT(NBTTagCompound tag)
  143.     {
  144.         super.readFromNBT(tag);
  145.  
  146.         this.voltage = tag.getDouble("Volts");
  147.         this.amps = tag.getDouble("Amps");
  148.     }
  149.  
  150.     public void setVoltage(double voltageIn)
  151.     {
  152.         this.voltage = voltageIn;
  153.     }
  154.  
  155.     public double getVoltageAfterApplyingResistance()
  156.     {
  157.         return this.getVoltage() - this.getResistance() <= 0 ? 0 : this.getVoltage() - this.getResistance();
  158.     }
  159.  
  160.     public void setAmps(double ampsIn)
  161.     {
  162.         this.amps = ampsIn;
  163.     }
  164.  
  165.     public PoweredTileEntity getPoweredTileAt(int x, int y, int z)
  166.     {
  167.         return this.worldObj.getTileEntity(x, y, z) instanceof PoweredTileEntity ? (PoweredTileEntity) this.worldObj.getTileEntity(x, y, z) : null;
  168.     }
  169.  
  170.     public PoweredTileEntity getPowerSourceTile()
  171.     {
  172.         if (getTop() != null && getTop().isOutputVoltageHigherThan(this) && getTop().canOutputPower())
  173.         {
  174.             return getTop();
  175.         }
  176.         if (getBottom() != null && getBottom().isOutputVoltageHigherThan(this) && getBottom().canOutputPower())
  177.         {
  178.             return getBottom();
  179.         }
  180.         if (getFront() != null && getFront().isOutputVoltageHigherThan(this) && getFront().canOutputPower())
  181.         {
  182.             return getFront();
  183.         }
  184.         if (getBack() != null && getBack().isOutputVoltageHigherThan(this) && getBack().canOutputPower())
  185.         {
  186.             return getBack();
  187.         }
  188.         if (getLeft() != null && getLeft().isOutputVoltageHigherThan(this) && getLeft().canOutputPower())
  189.         {
  190.             return getLeft();
  191.         }
  192.         if (getRight() != null && getRight().isOutputVoltageHigherThan(this) && getRight().canOutputPower())
  193.         {
  194.             return getRight();
  195.         }
  196.         if (getTop() instanceof TileEntityTransformer)
  197.         {
  198.             TileEntityTransformer t = (TileEntityTransformer)getTop();
  199.             if(!t.isOriginalPowerSourceAttached()){
  200.                 return null;
  201.             }
  202.             return (PoweredTileEntity) t.getPowerSource();
  203.         }
  204.         if (getBottom() instanceof TileEntityTransformer)
  205.         {
  206.             TileEntityTransformer t = (TileEntityTransformer)getBottom();
  207.             if(!t.isOriginalPowerSourceAttached()){
  208.                 return null;
  209.             }
  210.             return (PoweredTileEntity) t.getPowerSource();
  211.         }
  212.         if (getFront() instanceof TileEntityTransformer)
  213.         {
  214.             TileEntityTransformer t = (TileEntityTransformer)getFront();
  215.             if(!t.isOriginalPowerSourceAttached()){
  216.                 return null;
  217.             }
  218.             return (PoweredTileEntity) t.getPowerSource();
  219.         }
  220.         if (getBack() instanceof TileEntityTransformer)
  221.         {
  222.             TileEntityTransformer t = (TileEntityTransformer)getBack();
  223.             if(!t.isOriginalPowerSourceAttached()){
  224.                 return null;
  225.             }
  226.             return (PoweredTileEntity) t.getPowerSource();
  227.         }
  228.         if (getLeft() instanceof TileEntityTransformer)
  229.         {
  230.             TileEntityTransformer t = (TileEntityTransformer)getLeft();
  231.             if(!t.isOriginalPowerSourceAttached()){
  232.                 return null;
  233.             }
  234.             return (PoweredTileEntity) t.getPowerSource();
  235.         }
  236.         if (getRight() instanceof TileEntityTransformer)
  237.         {
  238.             TileEntityTransformer t = (TileEntityTransformer)getRight();
  239.             if(!t.isOriginalPowerSourceAttached()){
  240.                 return null;
  241.             }
  242.             return (PoweredTileEntity) t.getPowerSource();
  243.         }
  244.         if (getTop() instanceof TileEntityNegativeTransformer)
  245.         {
  246.             TileEntityNegativeTransformer t = (TileEntityNegativeTransformer)getTop();
  247.             if(!t.isOriginalPowerSourceAttached()){
  248.                 return null;
  249.             }
  250.             return (PoweredTileEntity) t.getPowerSource();
  251.         }
  252.         if (getBottom() instanceof TileEntityNegativeTransformer)
  253.         {
  254.             TileEntityNegativeTransformer t = (TileEntityNegativeTransformer)getBottom();
  255.             if(!t.isOriginalPowerSourceAttached()){
  256.                 return null;
  257.             }
  258.             return (PoweredTileEntity) t.getPowerSource();
  259.         }
  260.         if (getFront() instanceof TileEntityNegativeTransformer)
  261.         {
  262.             TileEntityNegativeTransformer t = (TileEntityNegativeTransformer)getFront();
  263.             if(!t.isOriginalPowerSourceAttached()){
  264.                 return null;
  265.             }
  266.             return (PoweredTileEntity) t.getPowerSource();
  267.         }
  268.         if (getBack() instanceof TileEntityNegativeTransformer)
  269.         {
  270.             TileEntityNegativeTransformer t = (TileEntityNegativeTransformer)getBack();
  271.             if(!t.isOriginalPowerSourceAttached()){
  272.                 return null;
  273.             }
  274.             return (PoweredTileEntity) t.getPowerSource();
  275.         }
  276.         if (getLeft() instanceof TileEntityNegativeTransformer)
  277.         {
  278.             TileEntityNegativeTransformer t = (TileEntityNegativeTransformer)getLeft();
  279.             if(!t.isOriginalPowerSourceAttached()){
  280.                 return null;
  281.             }
  282.             return (PoweredTileEntity) t.getPowerSource();
  283.         }
  284.         if (getRight() instanceof TileEntityNegativeTransformer)
  285.         {
  286.             TileEntityNegativeTransformer t = (TileEntityNegativeTransformer)getRight();
  287.             if(!t.isOriginalPowerSourceAttached()){
  288.                 return null;
  289.             }
  290.             return (PoweredTileEntity) t.getPowerSource();
  291.         }
  292.         return null;
  293.     }
  294.    
  295.     public boolean isVoltageInOperatingRange()
  296.     {
  297.         return this.getVoltage() < this.getMaxOperatingVoltage() && this.getVoltage() > this.getMinOperatingVoltage();
  298.     }
  299.  
  300.     public boolean isPowered()
  301.     {
  302.         return this.getVoltage() != 0;
  303.     }
  304.  
  305.     public boolean isOutputVoltageLessThan(PoweredTileEntity device)
  306.     {
  307.         return this.getVoltage() < device.getVoltage();
  308.     }
  309.  
  310.     public boolean isOutputVoltageHigherThan(PoweredTileEntity device)
  311.     {
  312.         return this.getVoltage() > device.getVoltage();
  313.     }
  314.  
  315.     public PoweredTileEntity getTop()
  316.     {
  317.         return this.getPoweredTileAt(this.xCoord, this.yCoord + 1, this.zCoord);
  318.     }
  319.  
  320.     public PoweredTileEntity getBottom()
  321.     {
  322.         return this.getPoweredTileAt(this.xCoord, this.yCoord - 1, this.zCoord);
  323.     }
  324.  
  325.     public PoweredTileEntity getFront()
  326.     {
  327.         return this.getPoweredTileAt(this.xCoord, this.yCoord, this.zCoord - 1);
  328.     }
  329.  
  330.     public PoweredTileEntity getBack()
  331.     {
  332.         return this.getPoweredTileAt(this.xCoord, this.yCoord, this.zCoord + 1);
  333.     }
  334.  
  335.     public PoweredTileEntity getRight()
  336.     {
  337.         return this.getPoweredTileAt(this.xCoord - 1, this.yCoord, this.zCoord);
  338.     }
  339.  
  340.     public PoweredTileEntity getLeft()
  341.     {
  342.         return this.getPoweredTileAt(this.xCoord + 1, this.yCoord, this.zCoord);
  343.     }
  344.  
  345.     @Override
  346.     public double getVoltage()
  347.     {
  348.         return this.voltage;
  349.     }
  350.  
  351.     @Override
  352.     public double getMaxOperatingVoltage()
  353.     {
  354.         return 140;
  355.     }
  356.  
  357.     @Override
  358.     public double getMinOperatingVoltage()
  359.     {
  360.         return 110;
  361.     }
  362.  
  363.     @Override
  364.     public double getAmperage()
  365.     {
  366.         return this.amps;
  367.     }
  368.  
  369.     @Override
  370.     public double getMaxOperatingAmps()
  371.     {
  372.         return 4;
  373.     }
  374.  
  375.     @Override
  376.     public double getMinOperatingAmps()
  377.     {
  378.         return 1;
  379.     }
  380.  
  381.     @Override
  382.     public double getResistance()
  383.     {
  384.         return 0.1;
  385.     }
  386.  
  387.     @Override
  388.     public boolean canOutputPower()
  389.     {
  390.         return false;
  391.     }
  392. }
Advertisement
Add Comment
Please, Sign In to add comment