Search in sources :

Example 11 with Ageable

use of org.bukkit.entity.Ageable in project MagicPlugin by elBukkit.

the class EntityData method modifyPreSpawn.

private boolean modifyPreSpawn(MageController controller, Entity entity) {
    if (entity == null || entity.getType() != type)
        return false;
    boolean isPlayer = (entity instanceof Player);
    if (extraData != null) {
        extraData.apply(entity);
    }
    CompatibilityUtils.setSilent(entity, isSilent);
    entity.setFireTicks(fireTicks);
    if (entity instanceof Ageable) {
        Ageable ageable = (Ageable) entity;
        if (isBaby) {
            ageable.setBaby();
        } else {
            ageable.setAdult();
        }
    }
    if (entity instanceof Tameable) {
        ((Tameable) entity).setTamed(isTamed);
    }
    if (entity instanceof Colorable && dyeColor != null) {
        Colorable colorable = (Colorable) entity;
        colorable.setColor(dyeColor);
    }
    if (tags != null && !tags.isEmpty()) {
        Set<String> entityTags = CompatibilityUtils.getTags(entity);
        entityTags.addAll(tags);
    }
    if (entity instanceof Creature) {
        Creature creature = (Creature) entity;
        creature.setCanPickupItems(canPickupItems);
    }
    if (entity instanceof Painting) {
        Painting painting = (Painting) entity;
        if (art != null) {
            painting.setArt(art, true);
        }
        if (facing != null) {
            painting.setFacingDirection(facing, true);
        }
    } else if (entity instanceof ItemFrame) {
        ItemFrame itemFrame = (ItemFrame) entity;
        itemFrame.setItem(item);
        if (facing != null) {
            itemFrame.setFacingDirection(facing, true);
        }
    } else if (entity instanceof Item) {
        Item droppedItem = (Item) entity;
        droppedItem.setItemStack(item);
    } else if (entity instanceof Wolf && dyeColor != null) {
        Wolf wolf = (Wolf) entity;
        wolf.setCollarColor(dyeColor);
    } else if (entity instanceof Ocelot && ocelotType != null) {
        Ocelot ocelot = (Ocelot) entity;
        ocelot.setCatType(ocelotType);
    } else if (entity instanceof Rabbit && rabbitType != null) {
        Rabbit rabbit = (Rabbit) entity;
        rabbit.setRabbitType(rabbitType);
    } else if (entity instanceof ExperienceOrb && xp != null) {
        ((ExperienceOrb) entity).setExperience(xp);
    }
    if (entity instanceof LivingEntity) {
        LivingEntity li = (LivingEntity) entity;
        if (hasPotionEffects) {
            Collection<PotionEffect> currentEffects = li.getActivePotionEffects();
            for (PotionEffect effect : currentEffects) {
                li.removePotionEffect(effect.getType());
            }
            if (potionEffects != null) {
                for (PotionEffect effect : potionEffects) {
                    li.addPotionEffect(effect);
                }
            }
        }
        try {
            if (!isPlayer) {
                applyAttributes(li);
                copyEquipmentTo(li);
                if (maxHealth != null) {
                    li.setMaxHealth(maxHealth);
                }
            }
            if (health != null) {
                li.setHealth(Math.min(health, li.getMaxHealth()));
            }
            if (airLevel != null) {
                li.setRemainingAir(Math.min(airLevel, li.getRemainingAir()));
            }
            if (!hasAI) {
                li.setAI(hasAI);
            }
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    }
    if (!isPlayer && name != null && name.length() > 0) {
        entity.setCustomName(name);
    }
    boolean needsMage = controller != null && mageData != null;
    if (needsMage) {
        Mage apiMage = controller.getMage(entity);
        if (apiMage instanceof com.elmakers.mine.bukkit.magic.Mage) {
            ((com.elmakers.mine.bukkit.magic.Mage) apiMage).setEntityData(this);
        }
    }
    return true;
}
Also used : Player(org.bukkit.entity.Player) Tameable(org.bukkit.entity.Tameable) Creature(org.bukkit.entity.Creature) PotionEffect(org.bukkit.potion.PotionEffect) Rabbit(org.bukkit.entity.Rabbit) ExperienceOrb(org.bukkit.entity.ExperienceOrb) ItemFrame(org.bukkit.entity.ItemFrame) Ageable(org.bukkit.entity.Ageable) Painting(org.bukkit.entity.Painting) LivingEntity(org.bukkit.entity.LivingEntity) Item(org.bukkit.entity.Item) Ocelot(org.bukkit.entity.Ocelot) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Colorable(org.bukkit.material.Colorable) Wolf(org.bukkit.entity.Wolf)

Example 12 with Ageable

use of org.bukkit.entity.Ageable in project Skills by StarTux.

the class BukkitSkillButcher method onEntityDeath.

@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onEntityDeath(EntityDeathEvent event) {
    LivingEntity entity = event.getEntity();
    if (entity.getCustomName() != null)
        return;
    if (entity instanceof Ageable && !((Ageable) entity).isAdult())
        return;
    if (!(entity.getKiller() instanceof Player))
        return;
    Player player = entity.getKiller();
    if (!allowPlayer(player))
        return;
    giveReward(player, rewardForEntity(entity));
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) Ageable(org.bukkit.entity.Ageable) EventHandler(org.bukkit.event.EventHandler)

Example 13 with Ageable

use of org.bukkit.entity.Ageable in project Citizens2 by CitizensDev.

the class NPCCommands method create.

@Command(aliases = { "npc" }, usage = "create [name] ((-b,u) --at (x:y:z:world) --type (type) --trait ('trait1, trait2...') --b (behaviours))", desc = "Create a new NPC", flags = "bu", modifiers = { "create" }, min = 2, permission = "citizens.npc.create")
@Requirements
public void create(CommandContext args, CommandSender sender, NPC npc) throws CommandException {
    String name = Colorizer.parseColors(args.getJoinedStrings(1).trim());
    EntityType type = EntityType.PLAYER;
    if (args.hasValueFlag("type")) {
        String inputType = args.getFlag("type");
        type = Util.matchEntityType(inputType);
        if (type == null) {
            throw new CommandException(Messaging.tr(Messages.NPC_CREATE_INVALID_MOBTYPE, inputType));
        } else if (!EntityControllers.controllerExistsForType(type)) {
            throw new CommandException(Messaging.tr(Messages.NPC_CREATE_MISSING_MOBTYPE, inputType));
        }
    }
    int nameLength = type == EntityType.PLAYER ? 46 : 64;
    if (name.length() > nameLength) {
        Messaging.sendErrorTr(sender, Messages.NPC_NAME_TOO_LONG);
        name = name.substring(0, nameLength);
    }
    if (name.length() == 0)
        throw new CommandException();
    if (!sender.hasPermission("citizens.npc.create.*") && !sender.hasPermission("citizens.npc.createall") && !sender.hasPermission("citizens.npc.create." + type.name().toLowerCase().replace("_", "")))
        throw new NoPermissionsException();
    npc = npcRegistry.createNPC(type, name);
    String msg = "You created [[" + npc.getName() + "]]";
    int age = 0;
    if (args.hasFlag('b')) {
        if (!Ageable.class.isAssignableFrom(type.getEntityClass()))
            Messaging.sendErrorTr(sender, Messages.MOBTYPE_CANNOT_BE_AGED, type.name().toLowerCase().replace("_", "-"));
        else {
            age = -24000;
            msg += " as a baby";
        }
    }
    // Initialize necessary traits
    if (!Setting.SERVER_OWNS_NPCS.asBoolean()) {
        npc.getTrait(Owner.class).setOwner(sender);
    }
    npc.getTrait(MobType.class).setType(type);
    Location spawnLoc = null;
    if (sender instanceof Player) {
        spawnLoc = args.getSenderLocation();
    } else if (sender instanceof BlockCommandSender) {
        spawnLoc = args.getSenderLocation();
    }
    CommandSenderCreateNPCEvent event = sender instanceof Player ? new PlayerCreateNPCEvent((Player) sender, npc) : new CommandSenderCreateNPCEvent(sender, npc);
    Bukkit.getPluginManager().callEvent(event);
    if (event.isCancelled()) {
        npc.destroy();
        String reason = "Couldn't create NPC.";
        if (!event.getCancelReason().isEmpty())
            reason += " Reason: " + event.getCancelReason();
        throw new CommandException(reason);
    }
    if (args.hasValueFlag("at")) {
        spawnLoc = CommandContext.parseLocation(args.getSenderLocation(), args.getFlag("at"));
    }
    if (spawnLoc == null) {
        npc.destroy();
        throw new CommandException(Messages.INVALID_SPAWN_LOCATION);
    }
    if (!args.hasFlag('u')) {
        npc.spawn(spawnLoc);
    }
    if (args.hasValueFlag("trait")) {
        Iterable<String> parts = Splitter.on(',').trimResults().split(args.getFlag("trait"));
        StringBuilder builder = new StringBuilder();
        for (String tr : parts) {
            Trait trait = CitizensAPI.getTraitFactory().getTrait(tr);
            if (trait == null)
                continue;
            npc.addTrait(trait);
            builder.append(StringHelper.wrap(tr) + ", ");
        }
        if (builder.length() > 0)
            builder.delete(builder.length() - 2, builder.length());
        msg += " with traits " + builder.toString();
    }
    if (args.hasValueFlag("template")) {
        Iterable<String> parts = Splitter.on(',').trimResults().split(args.getFlag("template"));
        StringBuilder builder = new StringBuilder();
        for (String part : parts) {
            Template template = Template.byName(part);
            if (template == null)
                continue;
            template.apply(npc);
            builder.append(StringHelper.wrap(part) + ", ");
        }
        if (builder.length() > 0)
            builder.delete(builder.length() - 2, builder.length());
        msg += " with templates " + builder.toString();
    }
    // Set age after entity spawns
    if (npc.getEntity() instanceof Ageable) {
        npc.getTrait(Age.class).setAge(age);
    }
    selector.select(sender, npc);
    Messaging.send(sender, msg + '.');
}
Also used : PlayerCreateNPCEvent(net.citizensnpcs.api.event.PlayerCreateNPCEvent) Owner(net.citizensnpcs.api.trait.trait.Owner) Player(org.bukkit.entity.Player) ServerCommandException(net.citizensnpcs.api.command.exception.ServerCommandException) CommandException(net.citizensnpcs.api.command.exception.CommandException) Ageable(org.bukkit.entity.Ageable) MobType(net.citizensnpcs.api.trait.trait.MobType) CommandSenderCreateNPCEvent(net.citizensnpcs.api.event.CommandSenderCreateNPCEvent) Template(net.citizensnpcs.npc.Template) EntityType(org.bukkit.entity.EntityType) NoPermissionsException(net.citizensnpcs.api.command.exception.NoPermissionsException) Trait(net.citizensnpcs.api.trait.Trait) WitherTrait(net.citizensnpcs.trait.WitherTrait) SheepTrait(net.citizensnpcs.trait.SheepTrait) ScriptTrait(net.citizensnpcs.trait.ScriptTrait) ArmorStandTrait(net.citizensnpcs.trait.ArmorStandTrait) Age(net.citizensnpcs.trait.Age) Location(org.bukkit.Location) CurrentLocation(net.citizensnpcs.trait.CurrentLocation) BlockCommandSender(org.bukkit.command.BlockCommandSender) Command(net.citizensnpcs.api.command.Command) Requirements(net.citizensnpcs.api.command.Requirements)

Example 14 with Ageable

use of org.bukkit.entity.Ageable in project Prism-Bukkit by prism.

the class EntitySerializer method serialize.

/**
 * Serialize entity.
 * @param entity Entity.
 */
public final void serialize(Entity entity) {
    entityName = entity.getType().name().toLowerCase();
    // Get custom name
    customName = entity.getCustomName();
    // Get animal age
    if (entity instanceof Ageable) {
        isAdult = ((Ageable) entity).isAdult();
    }
    // Owner
    if (entity instanceof Tameable) {
        final Tameable mob = (Tameable) entity;
        if (mob.getOwner() != null) {
            tamingOwner = mob.getOwner().getUniqueId().toString();
        } else if (mob.isTamed()) {
            tamingOwner = "-none-";
        }
    }
    // Sitting
    if (entity instanceof Sittable) {
        sitting = ((Sittable) entity).isSitting();
    }
    EntityDamageEvent damageEvent = entity.getLastDamageCause();
    // Saves us the null check
    if (damageEvent instanceof EntityDamageByEntityEvent && !damageEvent.isCancelled() && damageEvent.getDamage() > ((LivingEntity) entity).getHealth()) {
        EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) damageEvent;
        if (e.getDamager() instanceof Projectile) {
            customDesc = EntityUtils.getCustomProjectileDescription((Projectile) e.getDamager());
        }
    }
    serializer(entity);
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Sittable(org.bukkit.entity.Sittable) Tameable(org.bukkit.entity.Tameable) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) Ageable(org.bukkit.entity.Ageable) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) Projectile(org.bukkit.entity.Projectile)

Aggregations

Ageable (org.bukkit.entity.Ageable)14 LivingEntity (org.bukkit.entity.LivingEntity)10 Player (org.bukkit.entity.Player)8 Zombie (org.bukkit.entity.Zombie)8 Location (org.bukkit.Location)7 Entity (org.bukkit.entity.Entity)6 Slime (org.bukkit.entity.Slime)5 UndoList (com.elmakers.mine.bukkit.api.block.UndoList)4 PigZombie (org.bukkit.entity.PigZombie)4 Skeleton (org.bukkit.entity.Skeleton)4 MageController (com.elmakers.mine.bukkit.api.magic.MageController)3 Target (com.elmakers.mine.bukkit.utility.Target)3 Block (org.bukkit.block.Block)3 EntityType (org.bukkit.entity.EntityType)3 Tameable (org.bukkit.entity.Tameable)3 Mage (com.elmakers.mine.bukkit.api.magic.Mage)2 Age (net.citizensnpcs.trait.Age)2 Projectile (org.bukkit.entity.Projectile)2 Batch (com.elmakers.mine.bukkit.api.batch.Batch)1 BlockData (com.elmakers.mine.bukkit.api.block.BlockData)1