use of org.bukkit.entity.LivingEntity in project MagicPlugin by elBukkit.
the class DisarmAction method perform.
@Override
public SpellResult perform(CastContext context) {
Entity target = context.getTargetEntity();
if (target == null || !(target instanceof LivingEntity)) {
return SpellResult.NO_TARGET;
}
LivingEntity entity = (LivingEntity) target;
EntityEquipment equipment = entity.getEquipment();
ItemStack stack = null;
Integer originalSlot = null;
boolean isMainHand = false;
if (displayName == null) {
stack = equipment.getItemInMainHand();
isMainHand = true;
} else {
// This is not compatible
keepInInventory = false;
// Must be a player in this case
if (!(entity instanceof Player)) {
return SpellResult.PLAYER_REQUIRED;
}
PlayerInventory playerInventory = ((Player) entity).getInventory();
for (originalSlot = 0; originalSlot < playerInventory.getSize(); originalSlot++) {
ItemStack item = playerInventory.getItem(originalSlot);
if (InventoryUtils.isEmpty(item))
continue;
ItemMeta meta = item.getItemMeta();
if (meta == null || !meta.hasDisplayName())
continue;
if (meta.getDisplayName().equals(displayName)) {
stack = item;
isMainHand = originalSlot == playerInventory.getHeldItemSlot();
break;
}
}
}
if (InventoryUtils.isEmpty(stack)) {
return SpellResult.NO_TARGET;
}
// Special case for wands
MageController controller = context.getController();
Mage targetMage = controller.getMage(entity);
if (com.elmakers.mine.bukkit.wand.Wand.isWand(stack) && controller.isMage(entity)) {
Mage mage = context.getMage();
// This gets overridden by superpower...
if (!mage.isSuperPowered() && targetMage.isSuperProtected()) {
return SpellResult.NO_TARGET;
}
Wand activeWand = targetMage.getActiveWand();
if (activeWand != null && isMainHand) {
targetMage.getActiveWand().deactivate();
stack = equipment.getItemInMainHand();
}
}
Integer targetSlot = null;
PlayerInventory targetInventory = null;
ItemStack swapItem = null;
Random random = context.getRandom();
if (entity instanceof Player && keepInInventory) {
Player targetPlayer = (Player) entity;
targetInventory = targetPlayer.getInventory();
originalSlot = targetInventory.getHeldItemSlot();
List<Integer> validSlots = new ArrayList<>();
ItemStack[] contents = targetInventory.getContents();
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);
}
if (displayName != null) {
((Player) entity).getInventory().setItem(originalSlot, null);
} else {
equipment.setItemInMainHand(swapItem);
}
if (targetSlot != null && targetInventory != null) {
targetInventory.setItem(targetSlot, stack);
if (originalSlot != null) {
DisarmUndoAction disarmUndo = new DisarmUndoAction(targetMage, originalSlot, targetSlot);
context.registerForUndo(disarmUndo);
}
} 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 EntityProjectileAction method setEntity.
protected Entity setEntity(MageController controller, @Nonnull Entity entity) {
this.entity = entity;
if (noTarget) {
entity.setMetadata("notarget", new FixedMetadataValue(controller.getPlugin(), true));
}
if (entity instanceof LivingEntity) {
((LivingEntity) entity).setMaxHealth(1000.0);
((LivingEntity) entity).setHealth(1000.0);
}
if (entity instanceof Slime) {
((Slime) entity).setSize(1);
}
// Variant name still used here for backwards compatibility
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 ignored) {
}
} 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 ignored) {
}
}
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 ignored) {
}
wolf.setCollarColor(color);
}
}
if (projectileEffects != null && entity instanceof LivingEntity) {
CompatibilityUtils.applyPotionEffects((LivingEntity) entity, projectileEffects);
}
targeting.ignoreEntity(entity);
return entity;
}
use of org.bukkit.entity.LivingEntity in project MagicPlugin by elBukkit.
the class EntitySelectAction method start.
@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;
}
final Location location = context.getLocation();
List<Entity> allEntities = new ArrayList<>(location.getWorld().getNearbyEntities(location, radius, radius, radius));
entities.clear();
Collections.sort(allEntities, new Comparator<Entity>() {
@Override
public int compare(Entity entity1, Entity entity2) {
return Double.compare(location.distanceSquared(entity1.getLocation()), location.distanceSquared(entity2.getLocation()));
}
});
int index = 0;
for (Entity targetEntity : allEntities) {
if (!context.getTargetsCaster() && targetEntity == player)
continue;
if (targetEntity instanceof LivingEntity && ((LivingEntity) targetEntity).hasPotionEffect(PotionEffectType.INVISIBILITY))
continue;
if (!context.canTarget(targetEntity))
continue;
entities.put(index++, new WeakReference<>(targetEntity));
if (entities.size() >= limit)
break;
}
if (entities.size() == 0)
return SpellResult.NO_TARGET;
String inventoryTitle = context.getMessage("title", "Select Target");
int invSize = ((entities.size() + 9) / 9) * 9;
Inventory displayInventory = CompatibilityUtils.createInventory(null, invSize, inventoryTitle);
for (Map.Entry<Integer, WeakReference<Entity>> entry : entities.entrySet()) {
Entity targetEntity = entry.getValue().get();
if (targetEntity == null)
continue;
String displayName = DeprecatedUtils.getDisplayName(targetEntity);
ItemStack skull = controller.getSkull(targetEntity, displayName);
displayInventory.setItem(entry.getKey(), skull);
}
active = true;
mage.activateGUI(this, displayInventory);
return SpellResult.NO_ACTION;
}
use of org.bukkit.entity.LivingEntity in project MagicPlugin by elBukkit.
the class GrowEntityAction method perform.
@Override
public SpellResult perform(CastContext context) {
Entity targetEntity = context.getTargetEntity();
MageController controller = context.getController();
if (controller.isElemental(targetEntity)) {
double elementalSize = controller.getElementalScale(targetEntity);
elementalSize *= 1.2;
controller.setElementalScale(targetEntity, elementalSize);
return SpellResult.CAST;
}
if (!(targetEntity instanceof LivingEntity))
return SpellResult.NO_TARGET;
LivingEntity li = (LivingEntity) targetEntity;
if (li instanceof Ageable && !((Ageable) li).isAdult() && !(li instanceof Player)) {
context.registerModified(li);
((Ageable) li).setAdult();
} else if (li instanceof Zombie) {
context.registerModified(li);
Zombie zombie = (Zombie) li;
if (!zombie.isBaby()) {
UndoList spawnedList = com.elmakers.mine.bukkit.block.UndoList.getUndoList(li);
Location targetLocation = li.getLocation();
li.remove();
Entity giant = targetLocation.getWorld().spawnEntity(targetLocation, EntityType.GIANT);
context.registerForUndo(giant);
if (spawnedList != null) {
spawnedList.add(giant);
}
} else {
((Zombie) li).setBaby(false);
}
} else if (li instanceof PigZombie && ((PigZombie) li).isBaby()) {
context.registerModified(li);
((PigZombie) li).setBaby(false);
} else if (li instanceof Slime) {
context.registerModified(li);
Slime slime = (Slime) li;
slime.setSize(slime.getSize() + 1);
} else if (li instanceof Skeleton && skeletons && ((Skeleton) li).getSkeletonType() == Skeleton.SkeletonType.NORMAL) {
context.registerModified(li);
Skeleton skeleton = (Skeleton) li;
skeleton.setSkeletonType(Skeleton.SkeletonType.WITHER);
} else {
return SpellResult.NO_TARGET;
}
return SpellResult.CAST;
}
use of org.bukkit.entity.LivingEntity in project MagicPlugin by elBukkit.
the class ChangeWorldAction method perform.
@Override
public SpellResult perform(CastContext context) {
Entity e = context.getTargetEntity();
LivingEntity entity = e != null && e instanceof LivingEntity ? (LivingEntity) e : null;
if (entity == null) {
return SpellResult.NO_TARGET;
}
Location playerLocation = entity.getLocation();
if (targetLocation == null) {
return SpellResult.NO_TARGET;
}
// Sanity check!
targetLocation.setX(Math.min(targetLocation.getX(), 3.0E7D));
targetLocation.setZ(Math.min(targetLocation.getZ(), 3.0E7D));
targetLocation.setYaw(playerLocation.getYaw());
targetLocation.setPitch(playerLocation.getPitch());
setTargetWorldName(context, targetLocation.getWorld().getName());
teleport(context, entity, targetLocation);
return SpellResult.CAST;
}
Aggregations