Advertisement
Atijaf

TeleporterTE

Jan 31st, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.27 KB | None | 0 0
  1. public class TeleporterTE extends EnergyTE implements ITickable{
  2.    
  3.     private boolean linked = false;
  4.     private BlockPos targetPos = null;
  5.    
  6.     public TeleporterTE() {
  7.         //super(Inventory Size)
  8.         super(2);
  9.     }
  10.    
  11.     /**
  12.      * Lets keep everything about this block updated here (Except for the activating of the teleporter to teleport players... That'll be called from the player event handler)
  13.      */
  14.     @Override
  15.     public void update() {
  16.         /** Determines what to do next for the te */
  17.         switch(isPosDifferent()){
  18.        
  19.         case 0: {
  20.             if(determineIfLinked()){
  21.                 calculateEupu(this.pos, prev_targetPos);
  22.             }
  23.             break;
  24.         }
  25.        
  26.         /** Source block was changed / Corde Item was removed */
  27.         case 1: {
  28.             this.setEUPU(0);
  29.             this.setLinked(false);
  30.             this.prev_targetPos = null;
  31.             this.markDirty();
  32.             break;
  33.         }
  34.        
  35.         /**
  36.          * Target Block was changed / Corde Item was removed
  37.          *  NOTE, this will be the default case when nothing needs to happen.
  38.          *  Updates target block (Itself)
  39.          */
  40.         case 2:{
  41.             if(this.isLinked()){
  42.                 if(!determineIfLinked()){
  43.                     this.setEUPU(0);
  44.                     this.setLinked(false);
  45.                     this.prev_targetPos = null;
  46.                     this.markDirty();
  47.                 }
  48.             }
  49.             break;
  50.             }
  51.         }
  52.     }
  53.    
  54.     public boolean determineIfLinked(){
  55.         //Make sure it's not pointing to itself -- Return false if so
  56.         if(targetPos.equals(this.pos)) return false;
  57.         //Null check and instance check
  58.         if(worldObj.getTileEntity(targetPos) != null && worldObj.getTileEntity(targetPos) instanceof TeleporterTE){
  59.             //get Teleporter Tile Entity at target location and assign to targetTe
  60.             TeleporterTE targetTe = (TeleporterTE) worldObj.getTileEntity(targetPos);
  61.            
  62.             //If targetTe's targetPos is the pos of this, set both te's linked to true and return true
  63.             if(targetTe.getTargetPos() != null && targetTe.getTargetPos().equals(this.pos)){
  64.                 /**
  65.                  * IS LINKED AT THIS POINT
  66.                  * 1:Set Prev_targetPos to the current target_pos
  67.                  * 2:Set link to true
  68.                  * 3:Mark this block for saving
  69.                  */
  70.                 this.prev_targetPos = getTargetPos();
  71.                 this.setLinked(true);
  72.                 this.markDirty();
  73.                 return true;
  74.             }
  75.         }
  76.         return false;
  77.     }
  78.    
  79.     public void setLinked(boolean linked){
  80.         this.linked = linked;
  81.     }
  82.    
  83.     public boolean isLinked(){
  84.         return this.linked;
  85.     }
  86.    
  87.     /**
  88.      * @param targetPos : sets the te's target Pos as this
  89.      */
  90.     public void setTargetPos(BlockPos targetPos){
  91.         this.targetPos = targetPos;
  92.     }
  93.    
  94.     /**
  95.      * Sets the te's target pos as the corde cache block pos
  96.      * returns false if can't
  97.      */
  98.     public boolean setTargetPos(){
  99.         ItemStack stack = inventory[1];
  100.         if(stack != null && stack.getItem() instanceof CordeCache){
  101.             CordeCache itemCache = (CordeCache) stack.getItem();
  102.             this.targetPos = itemCache.getTargetPos(stack);
  103.             return true;
  104.         }
  105.         else{
  106.             this.targetPos = null;
  107.             return false;
  108.         }
  109.     }
  110.    
  111.     public BlockPos getTargetPos(){
  112.         return targetPos;
  113.     }
  114.    
  115.    
  116.     /**
  117.      * Used to determine if either of the blockposes have changed
  118.      */
  119.     private BlockPos prev_targetPos;
  120.    
  121.     /**
  122.      * return 0: Player put cord item into container, update    ||
  123.      * return 1: Player removed cord item from container, update    ||
  124.      * return 2: Everything is up to date, Check if it's still linked (For target Blocks)
  125.      */
  126.     private int isPosDifferent(){
  127.         if(prev_targetPos == null){
  128.             if(this.targetPos != null){
  129.                 return 0;//When player puts cord item into container.  Previous target pos is updated to targetpos.  Mark dirty
  130.             }
  131.             else{
  132.                 return 2;
  133.                 /** Happens after the cord item was removed (target pos and prev target pos are null )
  134.                  * This is called from the block that doesn't have a coord item
  135.                  */
  136.             }
  137.         }
  138.        
  139.         //this has already returned 0 - thus prev_targetPos isn't null
  140.         else{
  141.             if(this.targetPos != null){
  142.                 return 2;
  143.                 /** A bit more tricky */
  144.                 /** This is called from the target block (The block that still has a cord item. :: NOTE - targetPos isn't null
  145.                  * So after this returns 2, it checks if it's linked.  If it is, double check that (DetermineIfLinked())
  146.                  * If #determineIfLinked() returns false, set linked to false, eupu to false, and #markDirty()
  147.                  */
  148.             }
  149.             else{//cord item was removed, change prev_targetPos to null, update
  150.                 return 1;
  151.             }
  152.         }
  153.     }
  154.    
  155.     private int calculateDistance(BlockPos sourcePos, BlockPos targetPos){
  156.         if(sourcePos != null && targetPos != null){
  157.             /**Update Previous Block Poses for both te's since we're updating the distance (EUPU)*/
  158.             this.prev_targetPos = targetPos;
  159.            
  160.             int source_x = sourcePos.getX();
  161.             int source_y = sourcePos.getY();
  162.             int source_z = sourcePos.getZ();
  163.            
  164.             int target_x = targetPos.getX();
  165.             int target_y = targetPos.getY();
  166.             int target_z = targetPos.getZ();
  167.            
  168.             int x_distance = (source_x > target_x) ? source_x - target_x: target_x - source_x;
  169.             int y_distance = (source_y > target_y) ? source_y - target_y: target_y - source_y;
  170.             int z_distance = (source_z > target_z) ? source_z - target_z: target_z - source_z;
  171.            
  172.            
  173.             long x = (long) Math.pow(x_distance, 2);
  174.             long y = (long) Math.pow(y_distance, 2);
  175.             long z = (long) Math.pow(z_distance, 2);
  176.            
  177.             //Determines diagnal distance across a 2d plane (Flat distance)
  178.             int c =  (int) Math.sqrt(x + z);
  179.             //Determines diagnal distance across a 3d plane (Cube distance?  TRUE DISTANCE )
  180.             c = (int) Math.sqrt(y + Math.pow(c, 2));
  181.             return c;
  182.         }
  183.         return -1;//Hasn't changed, or doesn't have one
  184.     }
  185.    
  186.     /**
  187.      * Called when inventory contents are changed
  188.      */
  189.     @Override
  190.     public void markDirty() {
  191.         super.markDirty();
  192.        
  193.         //Saves currentEnergy for EnergyCapsule, if can't set currentEnergy for te to 0
  194.         if(!saveEnergyCapsule()){
  195.             setCurrentEnergy(0);
  196.         }
  197.         //Saves target pos on te;
  198.         setTargetPos();
  199.     }
  200.    
  201.     private void calculateEupu(BlockPos sourcePos, BlockPos targetPos){
  202.         int distance = calculateDistance(sourcePos, targetPos);
  203.         if(distance != -1){
  204.             this.setEUPU(distance * 10);
  205.         }
  206.         else{
  207.             this.setEUPU(0);
  208.         }
  209.     }
  210.    
  211.     @Override
  212.     public void setInventorySlotContents(int index, ItemStack stack) {
  213.         if (index >= 0 && index <= getSizeInventory()){
  214.             if(stack != null){
  215.                 if(stack.stackSize > this.getInventoryStackLimit()){
  216.                     stack.stackSize = getInventoryStackLimit();
  217.                 }
  218.                 else if(stack.stackSize == 0){
  219.                     stack = null;
  220.                 }
  221.             }
  222.         }
  223.         else{
  224.             return;
  225.         }
  226.         inventory[index] = stack;
  227.         markDirty();
  228.     }
  229.    
  230.    
  231.     @Override
  232.     public void writeToNBT(NBTTagCompound compound) {
  233.         super.writeToNBT(compound);
  234.        
  235.         //Saves the current Energy from the te.
  236.         compound.setInteger("currentEnergy", getCurrentEnergy());
  237.        
  238.         if(setTargetPos()){
  239.             compound.setIntArray("targetPos", new int[]{targetPos.getX(), targetPos.getY(), targetPos.getZ()});
  240.         }
  241.         compound.setInteger("eupu", this.getEUPU());
  242.        
  243.         compound.setBoolean("linked", this.isLinked());
  244.     }
  245.    
  246.     @Override
  247.     public void readFromNBT(NBTTagCompound compound) {
  248.         super.readFromNBT(compound);
  249.        
  250.         setCurrentEnergy(compound.getInteger("currentEnergy"));
  251.        
  252.         if(compound.hasKey("targetPos")){
  253.             int[] targetArr = compound.getIntArray("targetPos");
  254.             targetPos = new BlockPos(targetArr[0], targetArr[1], targetArr[2]);
  255.         }
  256.         setEUPU(compound.getInteger("eupu"));
  257.        
  258.         if(compound.hasKey("linked")){
  259.             setLinked(compound.getBoolean("linked"));
  260.         }
  261.        
  262.     }
  263.  
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement