use of org.bukkit.entity.LivingEntity in project MagicPlugin by elBukkit.
the class DisarmSpell method onCast.
@Override
public SpellResult onCast(ConfigurationSection parameters) {
Target target = getTarget();
if (!target.hasEntity() || !(target.getEntity() instanceof LivingEntity)) {
return SpellResult.NO_TARGET;
}
LivingEntity entity = (LivingEntity) target.getEntity();
EntityEquipment equipment = entity.getEquipment();
ItemStack stack = equipment.getItemInHand();
if (stack == null || stack.getType() == Material.AIR) {
return SpellResult.NO_TARGET;
}
// Special case for wands
if (Wand.isWand(stack) && controller.isMage(entity)) {
Mage targetMage = controller.getMage(entity);
// This gets overridden by superpower...
if (!mage.isSuperPowered() && isSuperProtected(targetMage)) {
return SpellResult.NO_TARGET;
}
if (targetMage.getActiveWand() != null) {
targetMage.getActiveWand().deactivate();
}
}
Integer targetSlot = null;
PlayerInventory targetInventory = null;
ItemStack swapItem = null;
if (entity instanceof Player && parameters.getBoolean("keep_in_inventory", false)) {
Player targetPlayer = (Player) entity;
targetInventory = targetPlayer.getInventory();
List<Integer> validSlots = new ArrayList<>();
ItemStack[] contents = targetInventory.getContents();
int minSlot = parameters.getInt("min_slot", Wand.HOTBAR_SIZE);
int maxSlot = parameters.getInt("max_slot", contents.length - 1);
for (int i = minSlot; i <= maxSlot; i++) {
if (contents[i] == null || contents[i].getType() == Material.AIR) {
validSlots.add(i);
}
}
// Randomly choose a slot if no empty one was found
if (validSlots.size() == 0) {
int swapSlot = random.nextInt(maxSlot - minSlot) + minSlot;
swapItem = targetInventory.getItem(swapSlot);
validSlots.add(swapSlot);
}
int chosen = random.nextInt(validSlots.size());
targetSlot = validSlots.get(chosen);
}
equipment.setItemInHand(swapItem);
if (targetSlot != null && targetInventory != null) {
targetInventory.setItem(targetSlot, stack);
} else {
Location location = entity.getLocation();
location.setY(location.getY() + 1);
Item item = entity.getWorld().dropItemNaturally(location, stack);
Vector velocity = item.getVelocity();
velocity.setY(velocity.getY() * 5);
SafetyUtils.setVelocity(item, velocity);
}
return SpellResult.CAST;
}
use of org.bukkit.entity.LivingEntity in project MagicPlugin by elBukkit.
the class FamiliarSpell method spawnFamiliar.
@Nullable
protected LivingEntity spawnFamiliar(Location target, EntityType famType, Location targetLocation, LivingEntity targetEntity, boolean setTarget) {
LivingEntity familiar = null;
try {
World world = getWorld();
Entity famEntity;
try {
Method spawnMethod = world.getClass().getMethod("spawn", Location.class, Class.class, CreatureSpawnEvent.SpawnReason.class);
famEntity = (Entity) spawnMethod.invoke(world, target, famType.getEntityClass(), spawnReason);
} catch (Exception ex) {
famEntity = getWorld().spawnEntity(target, famType);
}
if (famEntity == null || !(famEntity instanceof LivingEntity))
return null;
familiar = (LivingEntity) famEntity;
if (familiar instanceof Skeleton) {
Skeleton skellie = (Skeleton) familiar;
skellie.getEquipment().setItemInMainHand(new ItemStack(Material.BOW));
}
if (targetLocation != null && setTarget) {
// CompatibilityUtils.setTarget(familiar, targetLocation);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return familiar;
}
use of org.bukkit.entity.LivingEntity in project MagicPlugin by elBukkit.
the class FamiliarSpell method onCast.
@SuppressWarnings("deprecation")
@Override
public SpellResult onCast(ConfigurationSection parameters) {
spawnCount = 0;
Target target = getTarget();
if (!target.hasTarget()) {
return SpellResult.NO_TARGET;
}
Block originalTarget = target.getBlock();
Block targetBlock = originalTarget;
LivingEntity targetEntity = null;
boolean track = parameters.getBoolean("track", true);
boolean loot = parameters.getBoolean("loot", false);
boolean setTarget = parameters.getBoolean("set_target", true);
double spawnRange = parameters.getInt("spawn_range", 0);
String entityName = parameters.getString("name", "");
if (hasFamiliar() && track) {
// Dispel familiars if you target them and cast
boolean isFamiliar = target.hasEntity() && isFamiliar(target.getEntity());
if (isFamiliar) {
checkListener();
releaseFamiliar(target.getEntity());
return SpellResult.DEACTIVATE;
}
releaseFamiliars();
}
if (target.hasEntity()) {
targetBlock = targetBlock.getRelative(BlockFace.SOUTH);
Entity e = target.getEntity();
if (e instanceof LivingEntity) {
targetEntity = (LivingEntity) e;
}
}
targetBlock = targetBlock.getRelative(BlockFace.UP);
Location centerLoc = targetBlock.getLocation();
Location caster = getLocation();
if (spawnRange > 0) {
double distanceSquared = targetBlock.getLocation().distanceSquared(caster);
if (spawnRange * spawnRange < distanceSquared) {
Vector direction = caster.getDirection().normalize().multiply(spawnRange);
centerLoc = caster.clone().add(direction);
for (int i = 0; i < spawnRange; i++) {
Material blockType = centerLoc.getBlock().getType();
if (blockType == Material.AIR || blockType == Material.WATER || blockType != Material.STATIONARY_WATER) {
break;
}
centerLoc = centerLoc.add(0, 1, 0);
}
}
}
EntityType famType = null;
int famCount = parameters.getInt("count", 1);
String famTypeName = parameters.getString("type", null);
if (famTypeName != null && !famTypeName.isEmpty()) {
try {
famType = EntityType.valueOf(famTypeName.toUpperCase());
} catch (Throwable ex) {
sendMessage("Unknown entity type: " + famTypeName);
return SpellResult.FAIL;
}
}
if (originalTarget.getType() == Material.WATER || originalTarget.getType() == Material.STATIONARY_WATER) {
famType = EntityType.SQUID;
}
boolean spawnBaby = parameters.getBoolean("baby", false);
List<LivingEntity> newFamiliars = new ArrayList<>();
for (int i = 0; i < famCount; i++) {
EntityType entityType = famType;
if (entityType == null) {
String randomType = RandomUtils.weightedRandom(entityTypeProbability);
try {
entityType = EntityType.fromName(randomType);
} catch (Throwable ex) {
sendMessage("Unknown entity type: " + randomType);
return SpellResult.FAIL;
}
}
if (parameters.contains("reason")) {
String reasonText = parameters.getString("reason").toUpperCase();
try {
spawnReason = CreatureSpawnEvent.SpawnReason.valueOf(reasonText);
} catch (Exception ex) {
sendMessage("Unknown spawn reason: " + reasonText);
return SpellResult.FAIL;
}
}
final Location targetLoc = centerLoc.clone();
if (famCount > 1) {
targetLoc.setX(targetLoc.getX() + rand.nextInt(2 * famCount) - famCount);
targetLoc.setZ(targetLoc.getZ() + rand.nextInt(2 * famCount) - famCount);
}
targetLoc.setPitch(caster.getPitch());
targetLoc.setYaw(caster.getYaw());
if (entityType != null) {
final LivingEntity entity = spawnFamiliar(targetLoc, entityType, targetBlock.getLocation(), targetEntity, setTarget);
if (entity != null) {
if (entityName != null && !entityName.isEmpty()) {
entity.setCustomName(entityName);
}
if (!loot) {
entity.setMetadata("nodrops", new FixedMetadataValue(mage.getController().getPlugin(), true));
}
if (spawnBaby && entity instanceof Ageable) {
Ageable ageable = (Ageable) entity;
ageable.setBaby();
}
entity.teleport(targetLoc);
newFamiliars.add(entity);
spawnCount++;
registerForUndo(entity);
}
}
}
registerForUndo();
if (track) {
setFamiliars(newFamiliars);
checkListener();
}
return SpellResult.CAST;
}
use of org.bukkit.entity.LivingEntity in project MagicPlugin by elBukkit.
the class UndoableSpell method applyPotionEffects.
public void applyPotionEffects(Location location, int radius, Collection<PotionEffect> potionEffects) {
if (potionEffects == null || radius <= 0 || potionEffects.size() == 0)
return;
int radiusSquared = radius * 2;
List<Entity> entities = CompatibilityUtils.getNearbyEntities(location, radius, radius, radius);
for (Entity entity : entities) {
if (entity instanceof LivingEntity) {
Mage targetMage = null;
if (controller.isMage(entity)) {
targetMage = controller.getMage(entity);
}
boolean isSourcePlayer = entity == mage.getEntity();
if (isSourcePlayer && getTargetType() != TargetType.ANY && getTargetType() != TargetType.SELF) {
continue;
}
// Check for protected players
if (targetMage != null && isSuperProtected(targetMage) && !isSourcePlayer) {
continue;
}
if (!canTarget(entity))
continue;
if (entity.getLocation().distanceSquared(location) < radiusSquared) {
registerPotionEffects(entity);
CompatibilityUtils.applyPotionEffects((LivingEntity) entity, potionEffects);
if (targetMage != null) {
String playerMessage = getMessage("cast_player_message");
if (playerMessage.length() > 0) {
playerMessage = playerMessage.replace("$spell", getName());
targetMage.sendMessage(playerMessage);
}
}
}
}
}
}
use of org.bukkit.entity.LivingEntity in project MagicPlugin by elBukkit.
the class MagicController method getEntityName.
protected String getEntityName(Entity target, boolean display) {
if (target == null) {
return "Unknown";
}
if (target instanceof Player) {
return display ? ((Player) target).getDisplayName() : ((Player) target).getName();
}
if (isElemental(target)) {
return "Elemental";
}
if (display) {
if (target instanceof LivingEntity) {
LivingEntity li = (LivingEntity) target;
String customName = li.getCustomName();
if (customName != null && customName.length() > 0) {
return customName;
}
} else if (target instanceof Item) {
Item item = (Item) target;
ItemStack itemStack = item.getItemStack();
if (itemStack.hasItemMeta()) {
ItemMeta meta = itemStack.getItemMeta();
if (meta.hasDisplayName()) {
return meta.getDisplayName();
}
}
MaterialAndData material = new MaterialAndData(itemStack);
return material.getName();
}
}
return target.getType().name().toLowerCase().replace('_', ' ');
}
Aggregations