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;
}
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));
}
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 + '.');
}
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);
}
Aggregations