public void onEntityBlocked(EntityLivingBase entity) {
   if (this.worldObj.isRemote) {
     if (PowerNodeRegistry.For(getWorldObj()).checkPower(this, PowerTypes.DARK, 50)) {
       entity.attackEntityFrom(DamageSource.magic, 5);
       PowerNodeRegistry.For(getWorldObj()).consumePower(this, PowerTypes.DARK, 50);
     }
   }
 }
  @Override
  public void updateEntity() {
    super.updateEntity();

    if (worldObj.isRemote) {
      if (particleCounter == 0 || particleCounter++ > 1000) {
        particleCounter = 1;
        radiant =
            (AMParticle)
                AMCore.proxy.particleManager.spawn(
                    worldObj, "radiant", xCoord + 0.5f, yCoord + 0.5f, zCoord + 0.5f);
        if (radiant != null) {
          radiant.setMaxAge(1000);
          radiant.setRGBColorF(0.1f, 0.1f, 0.1f);
          radiant.setParticleScale(0.1f);
          radiant.AddParticleController(new ParticleHoldPosition(radiant, 1000, 1, false));
        }
      }
    } else {
      if (!isActive()) {
        if (inventory[0] != null) {
          current_deconstruction_time = 1;
        }
      } else {
        if (inventory[0] == null) {
          current_deconstruction_time = 0;
          deconstructionRecipe = null;
          worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
        } else {
          if (PowerNodeRegistry.For(worldObj)
              .checkPower(this, PowerTypes.DARK, DECONSTRUCTION_POWER_COST)) {
            if (deconstructionRecipe == null) {
              if (!getDeconstructionRecipe()) {
                transferOrEjectItem(inventory[0]);
                setInventorySlotContents(0, null);
              }
            } else {
              if (current_deconstruction_time++ >= DECONSTRUCTION_TIME) {
                for (ItemStack stack : deconstructionRecipe) {
                  transferOrEjectItem(stack);
                }
                deconstructionRecipe = null;
                decrStackSize(0, 1);
                current_deconstruction_time = 0;
              }
              if (current_deconstruction_time % 10 == 0)
                worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
            }
            PowerNodeRegistry.For(worldObj)
                .consumePower(this, PowerTypes.DARK, DECONSTRUCTION_POWER_COST);
          }
        }
      }
    }
  }
  @Override
  public void updateEntity() {

    if (surroundingCheckTicks++ % 100 == 0) {
      checkNearbyBlockState();
      surroundingCheckTicks = 1;
      if (!worldObj.isRemote
          && PowerNodeRegistry.For(this.worldObj).checkPower(this, this.capacity * 0.1f)) {
        List<EntityPlayer> nearbyPlayers =
            worldObj.getEntitiesWithinAABB(
                EntityPlayer.class,
                AxisAlignedBB.getBoundingBox(
                    this.xCoord - 2,
                    this.yCoord,
                    this.zCoord - 2,
                    this.xCoord + 2,
                    this.yCoord + 3,
                    this.zCoord + 2));
        for (EntityPlayer p : nearbyPlayers) {
          if (p.isPotionActive(BuffList.manaRegen.id)) continue;
          p.addPotionEffect(new BuffEffectManaRegen(600, 1));
        }
      }
    }

    if (onlyChargeAtNight == isNight()) {
      PowerNodeRegistry.For(this.worldObj)
          .insertPower(this, PowerTypes.LIGHT, 0.25f * powerMultiplier);
      if (worldObj.isRemote) {

        if (particleCounter++ % 180 == 0) {
          particleCounter = 1;
          AMCore.proxy.particleManager.RibbonFromPointToPoint(
              worldObj,
              xCoord + worldObj.rand.nextFloat(),
              yCoord + (worldObj.rand.nextFloat() * 2),
              zCoord + worldObj.rand.nextFloat(),
              xCoord + worldObj.rand.nextFloat(),
              yCoord + (worldObj.rand.nextFloat() * 2),
              zCoord + worldObj.rand.nextFloat());
        }
      }
    }
    super.callSuperUpdate();
  }
 public boolean canActivate() {
   boolean allGood = true;
   allGood &= worldObj.isAirBlock(xCoord, yCoord - 1, zCoord);
   allGood &= worldObj.isAirBlock(xCoord, yCoord - 2, zCoord);
   allGood &= worldObj.isAirBlock(xCoord, yCoord - 3, zCoord);
   allGood &= checkStructure();
   allGood &= PowerNodeRegistry.For(this.worldObj).checkPower(this);
   allGood &= !this.isActive;
   return allGood;
 }
  private void pickPowerType(ItemStack stack) {
    if (this.currentMainPowerTypes != PowerTypes.NONE) return;
    int flags = stack.getItemDamage() - ItemEssence.META_MAX;
    PowerTypes highestValid = PowerTypes.NONE;
    float amt = 0;
    for (PowerTypes type : PowerTypes.all()) {
      float tmpAmt = PowerNodeRegistry.For(worldObj).getPower(this, type);
      if (tmpAmt > amt) highestValid = type;
    }

    this.currentMainPowerTypes = highestValid;
  }
  public void setActive(long key) {
    this.isActive = true;
    this.key = key;
    int myMeta = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);

    if (PowerNodeRegistry.For(worldObj).getHighestPowerType(this) == PowerTypes.DARK) {
      myMeta |= 8;
    } else if (PowerNodeRegistry.For(worldObj).getHighestPowerType(this) == PowerTypes.LIGHT) {
      myMeta |= 4;
    }

    if (!this.worldObj.isRemote) {
      for (Object player : this.worldObj.playerEntities) {
        if (player instanceof EntityPlayerMP
            && new AMVector3((EntityPlayerMP) player).distanceSqTo(new AMVector3(this)) <= 4096) {
          ((EntityPlayerMP) player).playerNetServerHandler.sendPacket(getDescriptionPacket());
        }
      }
    } else {
      worldObj.playSound(xCoord, yCoord, zCoord, "arsmagica2:misc.gateway.open", 1.0f, 1.0f, true);
    }
  }
 private void updatePowerRequestData() {
   ItemStack stack = getNextPlannedItem();
   if (stack != null
       && stack.getItem() instanceof ItemEssence
       && stack.getItemDamage() > ItemEssence.META_MAX) {
     if (switchIsOn()) {
       int flags = stack.getItemDamage() - ItemEssence.META_MAX;
       setPowerRequests();
       pickPowerType(stack);
       if (this.currentMainPowerTypes != PowerTypes.NONE
           && PowerNodeRegistry.For(this.worldObj)
               .checkPower(this, this.currentMainPowerTypes, 100)) {
         currentConsumedPower +=
             PowerNodeRegistry.For(worldObj)
                 .consumePower(
                     this,
                     this.currentMainPowerTypes,
                     Math.min(100, stack.stackSize - currentConsumedPower));
       }
       if (currentConsumedPower >= stack.stackSize) {
         PowerNodeRegistry.For(this.worldObj).setPower(this, this.currentMainPowerTypes, 0);
         if (!worldObj.isRemote)
           addItemToRecipe(
               new ItemStack(
                   ItemsCommonProxy.essence, stack.stackSize, ItemEssence.META_MAX + flags));
         currentConsumedPower = 0;
         currentMainPowerTypes = PowerTypes.NONE;
         setNoPowerRequests();
         flipSwitch();
       }
     } else {
       setNoPowerRequests();
     }
   } else {
     setNoPowerRequests();
   }
 }
  private void setCrafting(boolean crafting) {
    this.isCrafting = crafting;
    if (!worldObj.isRemote) {
      AMDataWriter writer = new AMDataWriter();
      writer.add(xCoord);
      writer.add(yCoord);
      writer.add(zCoord);
      writer.add(CRAFTING_CHANGED);
      writer.add(crafting);
      AMNetHandler.INSTANCE.sendPacketToAllClientsNear(
          worldObj.provider.dimensionId,
          xCoord,
          yCoord,
          zCoord,
          32,
          AMPacketIDs.CRAFTING_ALTAR_DATA,
          writer.generate());
    }
    if (crafting) {
      allAddedItems.clear();
      currentAddedItems.clear();

      spellDef.clear();
      for (ArrayList<KeyValuePair<ISpellPart, byte[]>> groups : shapeGroups) groups.clear();

      // find otherworld auras
      IPowerNode[] nodes =
          PowerNodeRegistry.For(worldObj)
              .getAllNearbyNodes(worldObj, new AMVector3(this), PowerTypes.DARK);
      for (IPowerNode node : nodes) {
        if (node instanceof TileEntityOtherworldAura) {
          ((TileEntityOtherworldAura) node).setActive(true, this);
          break;
        }
      }
    }
  }
  private void doTeleport(Entity entity) {
    deactivate();

    AMVector3 newLocation =
        AMCore.instance.proxy.blocks.getNextKeystonePortalLocation(
            this.worldObj, xCoord, yCoord, zCoord, false, this.key);
    AMVector3 myLocation = new AMVector3(xCoord, yCoord, zCoord);

    double distance = myLocation.distanceTo(newLocation);
    float essenceCost = (float) (Math.pow(distance, 2) * 0.00175f);

    int meta =
        worldObj.getBlockMetadata((int) newLocation.x, (int) newLocation.y, (int) newLocation.z);

    if (AMCore.config.getHazardousGateways()) {
      // uh-oh!  Not enough power!  The teleporter will still send you though, but I wonder where...
      float charge = PowerNodeRegistry.For(this.worldObj).getHighestPower(this);
      if (charge < essenceCost) {
        essenceCost = charge;
        // get the distance that our charge *will* take us towards the next point
        double distanceWeCanGo = MathHelper.sqrt_double(charge / 0.00175);
        // get the angle between the 2 vectors
        double deltaZ = newLocation.z - myLocation.z;
        double deltaX = newLocation.x - myLocation.x;
        double angleH = Math.atan2(deltaZ, deltaX);
        // interpolate the distance at that angle - this is the new position
        double newX = myLocation.x + (Math.cos(angleH) * distanceWeCanGo);
        double newZ = myLocation.z + (Math.sin(angleH) * distanceWeCanGo);
        double newY = myLocation.y;

        while (worldObj.isAirBlock((int) newX, (int) newY, (int) newZ)) {
          newY++;
        }

        newLocation = new AMVector3(newX, newY, newZ);
      }
    } else {
      this.worldObj.playSoundEffect(
          newLocation.x, newLocation.y, newLocation.z, "mob.endermen.portal", 1.0F, 1.0F);
      return;
    }

    float newRotation = 0;
    switch (meta) {
      case 0:
        newRotation = 270;
        break;
      case 1:
        newRotation = 180;
        break;
      case 2:
        newRotation = 90;
        break;
      case 3:
        newRotation = 0;
        break;
    }
    entity.setPositionAndRotation(
        newLocation.x + 0.5,
        newLocation.y - entity.height,
        newLocation.z + 0.5,
        newRotation,
        entity.rotationPitch);

    PowerNodeRegistry.For(this.worldObj)
        .consumePower(
            this, PowerNodeRegistry.For(this.worldObj).getHighestPowerType(this), essenceCost);

    this.worldObj.playSoundEffect(
        myLocation.x, myLocation.y, myLocation.z, "mob.endermen.portal", 1.0F, 1.0F);
    this.worldObj.playSoundEffect(
        newLocation.x, newLocation.y, newLocation.z, "mob.endermen.portal", 1.0F, 1.0F);
  }
  @Override
  public void updateEntity() {
    super.updateEntity();

    int radius = getRadius();

    if (IsActive()) {
      PowerNodeRegistry.For(this.worldObj)
          .consumePower(
              this, PowerNodeRegistry.For(worldObj).getHighestPowerType(this), 0.35f * radius);
    }

    if (worldObj.isRemote) {
      if (IsActive()) {
        if (displayAura) {
          AMParticle effect =
              (AMParticle)
                  AMCore.instance.proxy.particleManager.spawn(
                      worldObj, "symbols", xCoord, yCoord + 0.5, zCoord);
          if (effect != null) {
            effect.setIgnoreMaxAge(false);
            effect.setMaxAge(100);
            effect.setParticleScale(0.5f);
            effect.AddParticleController(
                new ParticleOrbitPoint(effect, xCoord + 0.5, yCoord + 0.5, zCoord + 0.5, 1, false)
                    .SetOrbitSpeed(0.03)
                    .SetTargetDistance(radius));
          }
        }

        particleTickCounter++;

        if (particleTickCounter >= 15) {

          particleTickCounter = 0;

          String particleName = "";
          AMParticle effect =
              (AMParticle)
                  AMCore.instance.proxy.particleManager.spawn(
                      worldObj,
                      "sparkle",
                      xCoord + 0.5,
                      yCoord + 0.1 + rand.nextDouble() * 0.5,
                      zCoord + 0.5);
          if (effect != null) {
            effect.setIgnoreMaxAge(false);
            effect.setMaxAge(100);
            effect.setParticleScale(0.5f);
            float color = rand.nextFloat() * 0.2f + 0.8f;
            effect.AddParticleController(
                new ParticleOrbitPoint(effect, xCoord + 0.5, yCoord + 0.5, zCoord + 0.5, 1, false)
                    .SetOrbitSpeed(0.005)
                    .SetTargetDistance(rand.nextDouble() * 0.6 - 0.3));
            effect.AddParticleController(new ParticleHoldPosition(effect, 80, 2, true));
            effect.AddParticleController(new ParticleFadeOut(effect, 3, false).setFadeSpeed(0.05f));
          }
        }
      }
    }
  }
 public boolean IsActive() {
   return PowerNodeRegistry.For(this.worldObj).checkPower(this, 0.35f * getRadius())
       && worldObj.isBlockIndirectlyGettingPowered(this.xCoord, this.yCoord, this.zCoord)
       && getRadius() > 0;
 }
  @Override
  public void updateEntity() {
    if (worldObj.isRemote) {
      this.rotation += this.rotationIncrement;
    } else {
      surroundingCheckTicks++;
    }

    if (worldObj.isRemote || ticksSinceLastEntityScan++ > 25) {
      updateNearbyEntities();
      ticksSinceLastEntityScan = 0;
    }

    Iterator<EntityLivingBase> it = cachedEntities.iterator();
    while (it.hasNext()) {

      EntityLivingBase ent = it.next();

      if (ent.isDead) {
        it.remove();
        continue;
      }

      MovingObjectPosition mop =
          this.worldObj.rayTraceBlocks(
              Vec3.createVectorHelper(xCoord + 0.5, yCoord + 1.5, zCoord + 0.5),
              Vec3.createVectorHelper(ent.posX, ent.posY + ent.getEyeHeight(), ent.posZ),
              false);

      if (EntityUtilities.isSummon(ent) || mop != null) {
        continue;
      }

      ent.motionY = 0;
      ent.motionX = 0;
      ent.motionZ = 0;
      double deltaX = this.xCoord + 0.5f - ent.posX;
      double deltaZ = this.zCoord + 0.5f - ent.posZ;
      double deltaY = this.yCoord - ent.posY;
      double angle = Math.atan2(deltaZ, deltaX);

      double offsetX = Math.cos(angle) * 0.1;
      double offsetZ = Math.sin(angle) * 0.1;
      double offsetY = 0.05f;

      double distanceHorizontal = deltaX * deltaX + deltaZ * deltaZ;
      double distanceVertical = this.yCoord - ent.posY;
      boolean spawnedParticles = false;

      if (distanceHorizontal < 1.3) {
        if (distanceVertical < -1.5) {
          if (worldObj.isRemote && worldObj.rand.nextInt(10) < 3) {
            AMCore.proxy.particleManager.BoltFromPointToPoint(
                worldObj,
                xCoord + 0.5,
                yCoord + 1.3,
                zCoord + 0.5,
                ent.posX,
                ent.posY,
                ent.posZ,
                4,
                0x000000);
          }
        }
        if (distanceVertical < -2) {
          offsetY = 0;
          if (!worldObj.isRemote) {
            if (ent.attackEntityFrom(DamageSources.darkNexus, 4)) {
              if (ent.getHealth() <= 0) {
                ent.setDead();
                float power =
                    ((int) Math.ceil((ent.getMaxHealth() * (ent.ticksExisted / 20)) % 5000))
                        * this.powerMultiplier;
                PowerNodeRegistry.For(this.worldObj).insertPower(this, PowerTypes.DARK, power);
              }
            }
          }
        }
      }

      if (worldObj.isRemote) {
        if (!arcs.containsKey(ent)) {
          AMLineArc arc =
              (AMLineArc)
                  AMCore.proxy.particleManager.spawn(
                      worldObj,
                      "textures/blocks/oreblocksunstone.png",
                      xCoord + 0.5,
                      yCoord + 1.3,
                      zCoord + 0.5,
                      ent);
          if (arc != null) {
            arc.setExtendToTarget();
            arc.setRBGColorF(1, 1, 1);
          }
          arcs.put(ent, arc);
        }
        Iterator arcIterator = arcs.keySet().iterator();
        ArrayList<Entity> toRemove = new ArrayList<Entity>();
        while (arcIterator.hasNext()) {
          Entity arcEnt = (Entity) arcIterator.next();
          AMLineArc arc = (AMLineArc) arcs.get(arcEnt);
          if (arcEnt == null
              || arcEnt.isDead
              || arc == null
              || arc.isDead
              || new AMVector3(ent).distanceSqTo(new AMVector3(xCoord, yCoord, zCoord)) > 100)
            toRemove.add(arcEnt);
        }

        for (Entity e : toRemove) {
          arcs.remove(e);
        }
      }
      if (!worldObj.isRemote) ent.moveEntity(offsetX, offsetY, offsetZ);
    }
    if (surroundingCheckTicks % 100 == 0) {
      checkNearbyBlockState();
      surroundingCheckTicks = 1;
      if (!worldObj.isRemote
          && PowerNodeRegistry.For(this.worldObj).checkPower(this, this.capacity * 0.1f)) {
        List<EntityPlayer> nearbyPlayers =
            worldObj.getEntitiesWithinAABB(
                EntityPlayer.class,
                AxisAlignedBB.getBoundingBox(
                    this.xCoord - 2,
                    this.yCoord,
                    this.zCoord - 2,
                    this.xCoord + 2,
                    this.yCoord + 3,
                    this.zCoord + 2));
        for (EntityPlayer p : nearbyPlayers) {
          if (p.isPotionActive(BuffList.manaRegen.id)) continue;
          p.addPotionEffect(new BuffEffectManaRegen(600, 3));
        }
      }

      // TODO:
      /*if (rand.nextDouble() < (this.getCharge() / this.getCapacity()) * 0.01){
      	int maxSev = (int)Math.ceil((this.getCharge() / this.getCapacity()) * 2) + rand.nextInt(2);
      	IllEffectsManager.instance.ApplyRandomBadThing(this, IllEffectSeverity.values()[maxSev], BadThingTypes.DARKNEXUS);
      }*/
    }

    super.callSuperUpdate();
  }