Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TeleporterTE extends EnergyTE implements ITickable{
- private boolean linked = false;
- private BlockPos targetPos = null;
- public TeleporterTE() {
- //super(Inventory Size)
- super(2);
- }
- /**
- * 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)
- */
- @Override
- public void update() {
- /** Determines what to do next for the te */
- switch(isPosDifferent()){
- case 0: {
- if(determineIfLinked()){
- calculateEupu(this.pos, prev_targetPos);
- }
- break;
- }
- /** Source block was changed / Corde Item was removed */
- case 1: {
- this.setEUPU(0);
- this.setLinked(false);
- this.prev_targetPos = null;
- this.markDirty();
- break;
- }
- /**
- * Target Block was changed / Corde Item was removed
- * NOTE, this will be the default case when nothing needs to happen.
- * Updates target block (Itself)
- */
- case 2:{
- if(this.isLinked()){
- if(!determineIfLinked()){
- this.setEUPU(0);
- this.setLinked(false);
- this.prev_targetPos = null;
- this.markDirty();
- }
- }
- break;
- }
- }
- }
- public boolean determineIfLinked(){
- //Make sure it's not pointing to itself -- Return false if so
- if(targetPos.equals(this.pos)) return false;
- //Null check and instance check
- if(worldObj.getTileEntity(targetPos) != null && worldObj.getTileEntity(targetPos) instanceof TeleporterTE){
- //get Teleporter Tile Entity at target location and assign to targetTe
- TeleporterTE targetTe = (TeleporterTE) worldObj.getTileEntity(targetPos);
- //If targetTe's targetPos is the pos of this, set both te's linked to true and return true
- if(targetTe.getTargetPos() != null && targetTe.getTargetPos().equals(this.pos)){
- /**
- * IS LINKED AT THIS POINT
- * 1:Set Prev_targetPos to the current target_pos
- * 2:Set link to true
- * 3:Mark this block for saving
- */
- this.prev_targetPos = getTargetPos();
- this.setLinked(true);
- this.markDirty();
- return true;
- }
- }
- return false;
- }
- public void setLinked(boolean linked){
- this.linked = linked;
- }
- public boolean isLinked(){
- return this.linked;
- }
- /**
- * @param targetPos : sets the te's target Pos as this
- */
- public void setTargetPos(BlockPos targetPos){
- this.targetPos = targetPos;
- }
- /**
- * Sets the te's target pos as the corde cache block pos
- * returns false if can't
- */
- public boolean setTargetPos(){
- ItemStack stack = inventory[1];
- if(stack != null && stack.getItem() instanceof CordeCache){
- CordeCache itemCache = (CordeCache) stack.getItem();
- this.targetPos = itemCache.getTargetPos(stack);
- return true;
- }
- else{
- this.targetPos = null;
- return false;
- }
- }
- public BlockPos getTargetPos(){
- return targetPos;
- }
- /**
- * Used to determine if either of the blockposes have changed
- */
- private BlockPos prev_targetPos;
- /**
- * return 0: Player put cord item into container, update ||
- * return 1: Player removed cord item from container, update ||
- * return 2: Everything is up to date, Check if it's still linked (For target Blocks)
- */
- private int isPosDifferent(){
- if(prev_targetPos == null){
- if(this.targetPos != null){
- return 0;//When player puts cord item into container. Previous target pos is updated to targetpos. Mark dirty
- }
- else{
- return 2;
- /** Happens after the cord item was removed (target pos and prev target pos are null )
- * This is called from the block that doesn't have a coord item
- */
- }
- }
- //this has already returned 0 - thus prev_targetPos isn't null
- else{
- if(this.targetPos != null){
- return 2;
- /** A bit more tricky */
- /** This is called from the target block (The block that still has a cord item. :: NOTE - targetPos isn't null
- * So after this returns 2, it checks if it's linked. If it is, double check that (DetermineIfLinked())
- * If #determineIfLinked() returns false, set linked to false, eupu to false, and #markDirty()
- */
- }
- else{//cord item was removed, change prev_targetPos to null, update
- return 1;
- }
- }
- }
- private int calculateDistance(BlockPos sourcePos, BlockPos targetPos){
- if(sourcePos != null && targetPos != null){
- /**Update Previous Block Poses for both te's since we're updating the distance (EUPU)*/
- this.prev_targetPos = targetPos;
- int source_x = sourcePos.getX();
- int source_y = sourcePos.getY();
- int source_z = sourcePos.getZ();
- int target_x = targetPos.getX();
- int target_y = targetPos.getY();
- int target_z = targetPos.getZ();
- int x_distance = (source_x > target_x) ? source_x - target_x: target_x - source_x;
- int y_distance = (source_y > target_y) ? source_y - target_y: target_y - source_y;
- int z_distance = (source_z > target_z) ? source_z - target_z: target_z - source_z;
- long x = (long) Math.pow(x_distance, 2);
- long y = (long) Math.pow(y_distance, 2);
- long z = (long) Math.pow(z_distance, 2);
- //Determines diagnal distance across a 2d plane (Flat distance)
- int c = (int) Math.sqrt(x + z);
- //Determines diagnal distance across a 3d plane (Cube distance? TRUE DISTANCE )
- c = (int) Math.sqrt(y + Math.pow(c, 2));
- return c;
- }
- return -1;//Hasn't changed, or doesn't have one
- }
- /**
- * Called when inventory contents are changed
- */
- @Override
- public void markDirty() {
- super.markDirty();
- //Saves currentEnergy for EnergyCapsule, if can't set currentEnergy for te to 0
- if(!saveEnergyCapsule()){
- setCurrentEnergy(0);
- }
- //Saves target pos on te;
- setTargetPos();
- }
- private void calculateEupu(BlockPos sourcePos, BlockPos targetPos){
- int distance = calculateDistance(sourcePos, targetPos);
- if(distance != -1){
- this.setEUPU(distance * 10);
- }
- else{
- this.setEUPU(0);
- }
- }
- @Override
- public void setInventorySlotContents(int index, ItemStack stack) {
- if (index >= 0 && index <= getSizeInventory()){
- if(stack != null){
- if(stack.stackSize > this.getInventoryStackLimit()){
- stack.stackSize = getInventoryStackLimit();
- }
- else if(stack.stackSize == 0){
- stack = null;
- }
- }
- }
- else{
- return;
- }
- inventory[index] = stack;
- markDirty();
- }
- @Override
- public void writeToNBT(NBTTagCompound compound) {
- super.writeToNBT(compound);
- //Saves the current Energy from the te.
- compound.setInteger("currentEnergy", getCurrentEnergy());
- if(setTargetPos()){
- compound.setIntArray("targetPos", new int[]{targetPos.getX(), targetPos.getY(), targetPos.getZ()});
- }
- compound.setInteger("eupu", this.getEUPU());
- compound.setBoolean("linked", this.isLinked());
- }
- @Override
- public void readFromNBT(NBTTagCompound compound) {
- super.readFromNBT(compound);
- setCurrentEnergy(compound.getInteger("currentEnergy"));
- if(compound.hasKey("targetPos")){
- int[] targetArr = compound.getIntArray("targetPos");
- targetPos = new BlockPos(targetArr[0], targetArr[1], targetArr[2]);
- }
- setEUPU(compound.getInteger("eupu"));
- if(compound.hasKey("linked")){
- setLinked(compound.getBoolean("linked"));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement