hassansyyid

PoweredTileEntity

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