@Override
  public SpellResult start(CastContext context) {
    Mage mage = context.getMage();
    MageController controller = context.getController();
    Player player = mage.getPlayer();
    if (player == null) {
      return SpellResult.PLAYER_REQUIRED;
    }

    List<Player> allPlayers = null;
    players.clear();

    if (allowCrossWorld) {
      allPlayers = new ArrayList<>(controller.getPlugin().getServer().getOnlinePlayers());
    } else {
      allPlayers = context.getLocation().getWorld().getPlayers();
    }

    Collections.sort(
        allPlayers,
        new Comparator<Player>() {
          @Override
          public int compare(Player p1, Player p2) {
            return p1.getDisplayName().compareTo(p2.getDisplayName());
          }
        });

    int index = 0;
    for (Player targetPlayer : allPlayers) {
      if (!context.getTargetsCaster() && targetPlayer == player) continue;
      if (targetPlayer.hasPotionEffect(PotionEffectType.INVISIBILITY)) continue;
      if (!context.canTarget(targetPlayer)) continue;
      players.put(index++, new WeakReference<>(targetPlayer));
    }
    if (players.size() == 0) return SpellResult.NO_TARGET;

    String inventoryTitle = context.getMessage("title", "Select Player");
    int invSize = ((players.size() + 9) / 9) * 9;
    Inventory displayInventory = CompatibilityUtils.createInventory(null, invSize, inventoryTitle);
    for (Map.Entry<Integer, WeakReference<Player>> entry : players.entrySet()) {
      Player targetPlayer = entry.getValue().get();
      if (targetPlayer == null) continue;

      String name = targetPlayer.getName();
      String displayName = targetPlayer.getDisplayName();
      ItemStack playerItem = InventoryUtils.getPlayerSkull(targetPlayer, displayName);
      if (!name.equals(displayName)) {
        ItemMeta meta = playerItem.getItemMeta();
        List<String> lore = new ArrayList<>();
        lore.add(name);
        meta.setLore(lore);
        playerItem.setItemMeta(meta);
      }
      displayInventory.setItem(entry.getKey(), playerItem);
    }
    active = true;
    mage.activateGUI(this, displayInventory);

    return SpellResult.NO_ACTION;
  }
Exemple #2
0
  @Override
  public SpellResult perform(CastContext context) {
    Entity entity = context.getTargetEntity();
    if (entity == null || !(entity instanceof Damageable) || entity.isDead()) {
      return SpellResult.NO_TARGET;
    }

    double damage = 1;

    Damageable targetEntity = (Damageable) entity;
    LivingEntity livingTarget = (entity instanceof LivingEntity) ? (LivingEntity) entity : null;
    context.registerDamaged(targetEntity);
    Mage mage = context.getMage();
    MageController controller = context.getController();

    double previousKnockbackResistance = 0D;
    try {
      if (knockbackResistance != null && livingTarget != null) {
        AttributeInstance knockBackAttribute =
            livingTarget.getAttribute(Attribute.GENERIC_KNOCKBACK_RESISTANCE);
        previousKnockbackResistance = knockBackAttribute.getBaseValue();
        knockBackAttribute.setBaseValue(knockbackResistance);
      }
      if (controller.isElemental(entity)) {
        damage = elementalDamage;
        controller.damageElemental(
            entity, damage * mage.getDamageMultiplier(), 0, mage.getCommandSender());
      } else {
        if (percentage != null) {
          damage = percentage * targetEntity.getMaxHealth();
        } else if (targetEntity instanceof Player) {
          damage = playerDamage;
        } else {
          damage = entityDamage;
        }
        damage *= mage.getDamageMultiplier();
        if (magicDamage && (magicEntityDamage || targetEntity instanceof Player)) {
          CompatibilityUtils.magicDamage(targetEntity, damage, mage.getEntity());
        } else {
          CompatibilityUtils.damage(targetEntity, damage, mage.getEntity());
        }
      }
    } finally {
      if (knockbackResistance != null && livingTarget != null) {
        AttributeInstance knockBackAttribute =
            livingTarget.getAttribute(Attribute.GENERIC_KNOCKBACK_RESISTANCE);
        knockBackAttribute.setBaseValue(previousKnockbackResistance);
      }
    }

    return SpellResult.CAST;
  }
  protected Entity setEntity(MageController controller, Entity entity) {
    this.entity = entity;
    if (noTarget) {
      entity.setMetadata("notarget", new FixedMetadataValue(controller.getPlugin(), true));
    }
    if (customName != null) {
      entity.setCustomName(customName);
      entity.setCustomNameVisible(true);
    }

    if (entity instanceof LivingEntity) {
      ((LivingEntity) entity).setMaxHealth(1000.0);
      ((LivingEntity) entity).setHealth(1000.0);
    }
    if (entity instanceof Slime) {
      ((Slime) entity).setSize(1);
    }

    if (entity instanceof Ageable) {
      if (isBaby) {
        ((Ageable) entity).setBaby();
      } else {
        ((Ageable) entity).setAdult();
      }
    } else if (entity instanceof Zombie) {
      ((Zombie) entity).setBaby(isBaby);
    } else if (entity instanceof PigZombie) {
      ((PigZombie) entity).setBaby(isBaby);
    } else if (entity instanceof Slime && isBaby) {
      Slime slime = (Slime) entity;
      slime.setSize(0);
    }

    if (entity instanceof Horse) {
      Horse.Variant variant = Horse.Variant.UNDEAD_HORSE;
      if (variantName != null) {
        try {
          variant = Horse.Variant.valueOf(variantName.toUpperCase());
        } catch (Exception ex) {
        }
      } else {
        variant = Horse.Variant.UNDEAD_HORSE;
      }

      ((Horse) entity).setVariant(variant);
    }

    if (entity instanceof Ocelot) {
      Ocelot ocelot = (Ocelot) entity;
      Ocelot.Type variant = Ocelot.Type.WILD_OCELOT;
      if (variantName != null) {
        try {
          variant = Ocelot.Type.valueOf(variantName.toUpperCase());
        } catch (Exception ex) {
        }
      } else {
        variant = Ocelot.Type.WILD_OCELOT;
      }

      ocelot.setCatType(variant);
    }
    if (entity instanceof Sheep) {
      Sheep sheep = (Sheep) entity;
      DyeColor color = DyeColor.WHITE;
      if (variantName != null) {
        try {
          color = DyeColor.valueOf(variantName.toUpperCase());
        } catch (Exception ex) {

        }
      }
      sheep.setColor(color);
    }
    if (entity instanceof Wolf) {
      Wolf wolf = (Wolf) entity;
      if (variantName != null) {
        // Only set collar color if a variant is set..
        // this makes it a dog, versus a wolf. Technically.
        DyeColor color = DyeColor.RED;
        try {
          color = DyeColor.valueOf(variantName.toUpperCase());
          wolf.setTamed(true);
        } catch (Exception ex) {

        }
        wolf.setCollarColor(color);
      }
    }
    targeting.ignoreEntity(entity);
    return entity;
  }
  @Override
  public SpellResult perform(CastContext context) {
    Block targetBlock = context.getTargetBlock();
    Entity currentEntity = current == null ? null : current.get();
    current = null;
    if (currentEntity != null) {
      currentEntity.remove();
    }

    targetBlock = targetBlock.getRelative(BlockFace.UP);

    Location spawnLocation = targetBlock.getLocation();
    Location sourceLocation = context.getLocation();
    spawnLocation.setPitch(sourceLocation.getPitch());
    spawnLocation.setYaw(sourceLocation.getYaw());

    MageController controller = context.getController();
    if (entityData == null) {
      String randomType = RandomUtils.weightedRandom(entityTypeProbability);
      try {
        entityData = controller.getMob(randomType);
        if (entityData == null) {
          entityData =
              new com.elmakers.mine.bukkit.entity.EntityData(
                  EntityType.valueOf(randomType.toUpperCase()));
        }
      } catch (Throwable ex) {
        entityData = null;
      }
    }
    if (entityData == null) {
      return SpellResult.FAIL;
    }

    if (force) {
      controller.setForceSpawn(true);
    }
    Entity spawnedEntity = null;
    try {
      spawnedEntity = entityData.spawn(context.getController(), spawnLocation, spawnReason);
    } catch (Exception ex) {
      ex.printStackTrace();
    }

    if (force) {
      controller.setForceSpawn(false);
    }

    if (spawnedEntity == null) {
      return SpellResult.FAIL;
    }

    if (!loot) {
      spawnedEntity.setMetadata("nodrops", new FixedMetadataValue(controller.getPlugin(), true));
    }
    if (speed > 0) {
      Vector motion = direction;
      if (motion == null) {
        motion = context.getDirection();
      } else {
        motion = motion.clone();
      }

      if (dyOffset != 0) {
        motion.setY(motion.getY() + dyOffset);
      }
      motion.normalize();
      motion.multiply(speed);
      CompatibilityUtils.setEntityMotion(spawnedEntity, motion);
    }

    Collection<EffectPlayer> projectileEffects = context.getEffects("spawned");
    for (EffectPlayer effectPlayer : projectileEffects) {
      effectPlayer.start(spawnedEntity.getLocation(), spawnedEntity, null, null);
    }
    context.registerForUndo(spawnedEntity);

    if (track) {
      current = new WeakReference<Entity>(spawnedEntity);
    }
    if (setTarget) {
      context.setTargetEntity(spawnedEntity);
    }
    return SpellResult.CAST;
  }