@EventHandler void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event) { if (event.getPlayer().hasMetadata("give-pet")) { if (event.getRightClicked() instanceof Tameable) { Tameable pet = (Tameable) event.getRightClicked(); if (pet.isTamed() && pet.getOwner() instanceof Player) { Player player = (Player) pet.getOwner(); if (player.equals(event.getPlayer()) || event.getPlayer().hasPermission("omneity3.give-pet.override")) { OfflinePlayer reciever = plugin .getServer() .getOfflinePlayer( (String) event.getPlayer().getMetadata("give-pet").get(0).value()); pet.setOwner(reciever); event.getPlayer().removeMetadata("give-pet", plugin); } else { event.getPlayer().sendMessage("This pet is not yours to give"); event.getPlayer().removeMetadata("give-pet", plugin); } } else { event.getPlayer().sendMessage("This pet is not tamed"); event.getPlayer().removeMetadata("give-pet", plugin); } } else { event.getPlayer().sendMessage("That entity can not be a pet"); event.getPlayer().removeMetadata("give-pet", plugin); } } }
/** * Handle the Beast Lore ability. * * @param target The entity to examine */ public void beastLore(LivingEntity target) { Player player = getPlayer(); Tameable beast = (Tameable) target; String message = LocaleLoader.getString("Combat.BeastLore") + " "; if (beast.isTamed() && beast.getOwner() != null) { message = message.concat( LocaleLoader.getString("Combat.BeastLoreOwner", beast.getOwner().getName()) + " "); } message = message.concat( LocaleLoader.getString( "Combat.BeastLoreHealth", target.getHealth(), target.getMaxHealth())); player.sendMessage(message); }
public static String getColor(Player player, LivingEntity target) { if ((target instanceof Player)) { for (String arg : default_colours.keySet()) { if (((arg.equals("op")) && (((Player) target).isOp())) || (arg.equals("default")) || (((Player) target).hasPermission(arg))) { String color = (String) default_colours.get(arg); if (color.matches("[0-9a-f]")) { return new StringBuilder().append("§").append(color).toString(); } return ChatColor.valueOf(color.toUpperCase()).toString(); } } return ((Player) target).isOp() ? ChatColor.GOLD.toString() : ChatColor.YELLOW.toString(); } if ((target instanceof Monster)) { if ((player != null) && (player.equals(((Monster) target).getTarget()))) { return ChatColor.RED.toString(); } return ChatColor.YELLOW.toString(); } if ((target instanceof WaterMob)) return ChatColor.GREEN.toString(); if ((target instanceof Flying)) return ChatColor.YELLOW.toString(); if ((target instanceof Animals)) { if ((player != null) && (player.equals(((Animals) target).getTarget()))) return ChatColor.RED.toString(); if ((target instanceof Tameable)) { Tameable pet = (Tameable) target; if (pet.isTamed()) { return ChatColor.GREEN.toString(); } return ChatColor.YELLOW.toString(); } return ChatColor.GRAY.toString(); } return ChatColor.GRAY.toString(); }
/** * Handle EntityTarget events. * * @param event The event to process */ @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onEntityTarget(EntityTargetEvent event) { Entity entity = event.getEntity(); Entity target = event.getTarget(); if (!(entity instanceof Tameable) || !(target instanceof Player)) { return; } Player player = (Player) target; Tameable tameable = (Tameable) entity; if (!CombatUtils.isFriendlyPet(player, tameable)) { return; } // isFriendlyPet ensures that the Tameable is: Tamed, owned by a player, and the owner is in the // same party // So we can make some assumptions here, about our casting and our check if (!(Permissions.friendlyFire(player) && Permissions.friendlyFire((Player) tameable.getOwner()))) { event.setCancelled(true); } }
public static void setEntityTypeData(final Entity entity, final String data) { if (data == "") return; final String parts[] = data.split(","); if (entity instanceof LivingEntity) { ((LivingEntity) entity).setMaxHealth(Double.parseDouble(parts[1])); ((LivingEntity) entity).setHealth(Double.parseDouble(parts[0])); if (!parts[2].equals("null")) ((LivingEntity) entity).setCustomName(parts[2]); if (entity instanceof Animals) { ((Animals) entity).setAge(Integer.parseInt(parts[3])); if (entity instanceof Sheep) { ((Sheep) entity).setSheared(Boolean.parseBoolean(parts[4])); ((Sheep) entity).setColor(sheepColors.get(parts[5])); } else if (entity instanceof Wolf) { if (Boolean.parseBoolean(parts[4])) { ((Wolf) entity).setAngry(Boolean.parseBoolean(parts[4])); } else if (parts.length > 5) { ((Tameable) entity).setTamed(true); ((Tameable) entity).setOwner(getPlayer(parts[5])); ((Wolf) entity).setCollarColor(DyeColor.valueOf(parts[6])); } } else if (entity instanceof Ocelot) { if (parts.length > 4) { ((Tameable) entity).setTamed(true); ((Tameable) entity).setOwner(getPlayer(parts[4])); ((Ocelot) entity).setCatType(catTypes.get(parts[5])); } } else if (entity instanceof Pig) { ((Pig) entity).setSaddle(Boolean.parseBoolean(parts[4])); } else if (entity instanceof Horse) { ((Horse) entity).setVariant(horseVariants.get(parts[4])); ((Horse) entity).setStyle(horseStyles.get(parts[5])); ((Horse) entity).setColor(horseColors.get(parts[6])); ((Horse) entity).setDomestication(Integer.parseInt(parts[7])); ((Horse) entity).setMaxDomestication(Integer.parseInt(parts[8])); ((Horse) entity).setJumpStrength(Double.parseDouble(parts[9])); if (parts.length > 10) { ((Tameable) entity).setTamed(true); if (!parts[10].equals("null")) ((Tameable) entity).setOwner(getPlayer(parts[10])); ((Horse) entity) .getInventory() .setSaddle(ItemStackUtil.stringToItemStack(parts[11])[0]); ((Horse) entity).getInventory().setArmor(ItemStackUtil.stringToItemStack(parts[12])[0]); if (parts.length > 13) { ((Horse) entity).setCarryingChest(true); ((Horse) entity) .getInventory() .setContents(ItemStackUtil.stringToItemStack(parts[13])); } } } } else if (entity instanceof Villager) { ((Villager) entity).setProfession(villagerProfessions.get(parts[3])); ((Villager) entity).setAge(Integer.parseInt(parts[4])); } else if (entity instanceof Creeper) { ((Creeper) entity).setPowered(Boolean.parseBoolean(parts[3])); } else if (entity instanceof Slime) { ((Slime) entity).setSize(Integer.parseInt(parts[3])); } else if (entity instanceof Skeleton) { ((Skeleton) entity).setSkeletonType(skeletonTypes.get(parts[3])); if (parts[3].equals("0")) { ((Skeleton) entity).getEquipment().setItemInHand(new ItemStack(Material.BOW)); } else { ((Skeleton) entity).getEquipment().setItemInHand(new ItemStack(Material.BOW)); } } else if (entity instanceof PigZombie) { ((LivingEntity) entity).getEquipment().setItemInHand(new ItemStack(Material.GOLD_SWORD)); } } }
/** * Handle EntityDamage events that involve modifying the event. * * @param event The event to modify */ @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onEntityDamage(EntityDamageEvent event) { if (event instanceof FakeEntityDamageEvent) { return; } double damage = event.getDamage(); if (damage <= 0) { return; } Entity entity = event.getEntity(); if (entity.hasMetadata(mcMMO.customDamageKey)) { entity.removeMetadata(mcMMO.customDamageKey, plugin); return; } if (Misc.isNPCEntity(entity) || !entity.isValid() || !(entity instanceof LivingEntity)) { return; } LivingEntity livingEntity = (LivingEntity) entity; if (CombatUtils.isInvincible(livingEntity, damage)) { return; } DamageCause cause = event.getCause(); if (livingEntity instanceof Player) { Player player = (Player) entity; McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player); /* Check for invincibility */ if (mcMMOPlayer.getGodMode()) { event.setCancelled(true); return; } switch (cause) { case FALL: if (Config.getInstance().getPreventXPAfterTeleport() && SkillUtils.calculateTimeLeft( (long) mcMMOPlayer.getTeleportATS() * Misc.TIME_CONVERSION_FACTOR, 5, player) > 0) { return; } AcrobaticsManager acrobaticsManager = mcMMOPlayer.getAcrobaticsManager(); if (acrobaticsManager.canRoll()) { event.setDamage(acrobaticsManager.rollCheck(event.getDamage())); if (event.getDamage() == 0) { event.setCancelled(true); return; } } break; case BLOCK_EXPLOSION: MiningManager miningManager = mcMMOPlayer.getMiningManager(); if (miningManager.canUseDemolitionsExpertise()) { event.setDamage(miningManager.processDemolitionsExpertise(event.getDamage())); if (event.getDamage() == 0) { event.setCancelled(true); return; } } break; default: break; } if (event.getDamage() >= 1) { mcMMOPlayer.actualizeRecentlyHurt(); } } else if (livingEntity instanceof Tameable) { Tameable pet = (Tameable) livingEntity; AnimalTamer owner = pet.getOwner(); if (Taming.canPreventDamage(pet, owner)) { Player player = (Player) owner; Wolf wolf = (Wolf) pet; TamingManager tamingManager = UserManager.getPlayer(player).getTamingManager(); switch (cause) { case CONTACT: case FIRE: case LAVA: if (tamingManager.canUseEnvironmentallyAware()) { tamingManager.processEnvironmentallyAware(wolf, event.getDamage()); } return; case FALL: if (tamingManager.canUseEnvironmentallyAware()) { event.setCancelled(true); } return; case ENTITY_ATTACK: case PROJECTILE: if (tamingManager.canUseThickFur()) { event.setDamage(Taming.processThickFur(wolf, event.getDamage())); if (event.getDamage() == 0) { event.setCancelled(true); } } return; case FIRE_TICK: if (tamingManager.canUseThickFur()) { Taming.processThickFurFire(wolf); } return; case MAGIC: case POISON: case WITHER: if (tamingManager.canUseHolyHound()) { Taming.processHolyHound(wolf, event.getDamage()); } return; case BLOCK_EXPLOSION: case ENTITY_EXPLOSION: case LIGHTNING: if (tamingManager.canUseShockProof()) { event.setDamage(Taming.processShockProof(wolf, event.getDamage())); if (event.getDamage() == 0) { event.setCancelled(true); } } return; default: return; } } } }
public List<LivingEntity> spawn(Location location, Player player) { List<LivingEntity> entities = new ArrayList<LivingEntity>(this.num); World world = location.getWorld(); for (int num = 0; num < this.num; num++) { if (spread > 0) { int minX = location.getBlockX() - spread / 2; int minY = location.getBlockY() - spread / 2; int minZ = location.getBlockZ() - spread / 2; int maxX = location.getBlockX() + spread / 2; int maxY = location.getBlockY() + spread / 2; int maxZ = location.getBlockZ() + spread / 2; int tries = spread * 10; boolean found = false; while (tries-- > 0) { int x = minX + RecipeManager.random.nextInt(maxX - minX); int z = minZ + RecipeManager.random.nextInt(maxZ - minZ); int y = 0; for (y = maxY; y >= minY; y--) { if (!Material.getMaterial(world.getBlockTypeIdAt(x, y, z)).isSolid()) { found = true; break; } } if (found) { location.setX(x); location.setY(y); location.setZ(z); break; } } if (!found) { Messages.debug( "Couldn't find suitable location after " + (spread * 10) + " tries, using center."); } location.add(0.5, 0, 0.5); } LivingEntity ent = (LivingEntity) world.spawnEntity(location, type); entities.add(ent); if (!noEffect) { world.playEffect(location, Effect.MOBSPAWNER_FLAMES, 20); } if (name != null) { ent.setCustomName(name); ent.setCustomNameVisible(noHideName); } if (onFire > 0.0f) { ent.setFireTicks((int) Math.ceil(onFire * 20.0)); } if (pickup != null) { ent.setCanPickupItems(pickup); } if (pet && ent instanceof Tameable) { Tameable npc = (Tameable) ent; npc.setOwner(player); npc.setTamed(true); } if (ent instanceof Wolf) { Wolf npc = (Wolf) ent; if (pet) { if (noSit) { npc.setSitting(false); } if (color != null) { npc.setCollarColor(color); } } else if (angry) { npc.setAngry(true); } } if (ent instanceof Ocelot) { Ocelot npc = (Ocelot) ent; if (pet && noSit) { npc.setSitting(false); } if (cat != null) { npc.setCatType(cat); } } if (hp > 0) { ent.setHealth(hp); if (maxHp > 0) { ent.setMaxHealth(maxHp); } } if (ent instanceof Ageable) { Ageable npc = (Ageable) ent; if (baby) { npc.setBaby(); } if (ageLock) { npc.setAgeLock(true); } if (noBreed) { npc.setBreed(false); } } if (saddle && ent instanceof Pig) { Pig npc = (Pig) ent; npc.setSaddle(true); if (mount) { npc.setPassenger(player); } } if (ent instanceof Zombie) { Zombie npc = (Zombie) ent; if (baby) { npc.setBaby(true); } if (zombieVillager) { npc.setVillager(true); } } if (villager != null && ent instanceof Villager) { Villager npc = (Villager) ent; npc.setProfession(villager); } if (poweredCreeper && ent instanceof Creeper) { Creeper npc = (Creeper) ent; npc.setPowered(true); } if (playerIronGolem && ent instanceof IronGolem) { IronGolem npc = (IronGolem) ent; npc.setPlayerCreated(true); // TODO what exacly does this do ? } if (shearedSheep && ent instanceof Sheep) { Sheep npc = (Sheep) ent; npc.setSheared(true); } if (color != null && ent instanceof Colorable) { Colorable npc = (Colorable) ent; npc.setColor(color); } if (skeleton != null && ent instanceof Skeleton) { Skeleton npc = (Skeleton) ent; npc.setSkeletonType(skeleton); } if (target && ent instanceof Creature) { Creature npc = (Creature) ent; npc.setTarget(player); } if (pigAnger > 0 && ent instanceof PigZombie) { PigZombie npc = (PigZombie) ent; npc.setAnger(pigAnger); } if (hit) { ent.damage(0, player); ent.setVelocity(new Vector()); } if (!potions.isEmpty()) { for (PotionEffect effect : potions) { ent.addPotionEffect(effect, true); } } ent.setRemoveWhenFarAway(!noRemove); EntityEquipment eq = ent.getEquipment(); for (int i = 0; i < equip.length; i++) { ItemStack item = equip[i]; if (item == null) { continue; } switch (i) { case 0: eq.setHelmet(item); eq.setHelmetDropChance(drop[i]); break; case 1: eq.setChestplate(item); eq.setChestplateDropChance(drop[i]); break; case 2: eq.setLeggings(item); eq.setLeggingsDropChance(drop[i]); break; case 3: eq.setBoots(item); eq.setBootsDropChance(drop[i]); break; case 4: { if (ent instanceof Enderman) { Enderman npc = (Enderman) ent; npc.setCarriedMaterial(item.getData()); } else { eq.setItemInHand(item); eq.setItemInHandDropChance(drop[i]); } break; } } } } return entities; }
/** * Handle the Call of the Wild ability. * * @param type The type of entity to summon. * @param summonAmount The amount of material needed to summon the entity */ private void callOfTheWild(EntityType type, int summonAmount) { Player player = getPlayer(); ItemStack heldItem = player.getInventory().getItemInMainHand(); int heldItemAmount = heldItem.getAmount(); Location location = player.getLocation(); if (heldItemAmount < summonAmount) { player.sendMessage( LocaleLoader.getString( "Skills.NeedMore", StringUtils.getPrettyItemString(heldItem.getType()))); return; } if (!rangeCheck(type)) { return; } int amount = Config.getInstance().getTamingCOTWAmount(type); int tamingCOTWLength = Config.getInstance().getTamingCOTWLength(type); for (int i = 0; i < amount; i++) { if (!summonAmountCheck(type)) { return; } location = Misc.getLocationOffset(location, 1); LivingEntity entity = (LivingEntity) player.getWorld().spawnEntity(location, type); FakeEntityTameEvent event = new FakeEntityTameEvent(entity, player); mcMMO.p.getServer().getPluginManager().callEvent(event); if (event.isCancelled()) { continue; } entity.setMetadata(mcMMO.entityMetadataKey, mcMMO.metadataValue); ((Tameable) entity).setOwner(player); entity.setRemoveWhenFarAway(false); addToTracker(entity); switch (type) { case OCELOT: ((Ocelot) entity).setCatType(Ocelot.Type.values()[1 + Misc.getRandom().nextInt(3)]); break; case WOLF: entity.setMaxHealth(20.0); entity.setHealth(entity.getMaxHealth()); break; case HORSE: Horse horse = (Horse) entity; entity.setMaxHealth(15.0 + (Misc.getRandom().nextDouble() * 15)); entity.setHealth(entity.getMaxHealth()); horse.setColor( Horse.Color.values()[Misc.getRandom().nextInt(Horse.Color.values().length)]); horse.setStyle( Horse.Style.values()[Misc.getRandom().nextInt(Horse.Style.values().length)]); horse.setJumpStrength( Math.max( AdvancedConfig.getInstance().getMinHorseJumpStrength(), Math.min( Math.min(Misc.getRandom().nextDouble(), Misc.getRandom().nextDouble()) * 2, AdvancedConfig.getInstance().getMaxHorseJumpStrength()))); // TODO: setSpeed, once available break; default: break; } if (Permissions.renamePets(player)) { entity.setCustomName( LocaleLoader.getString( "Taming.Summon.Name.Format", player.getName(), StringUtils.getPrettyEntityTypeString(type))); } ParticleEffectUtils.playCallOfTheWildEffect(entity); } player .getInventory() .setItemInMainHand( heldItemAmount == summonAmount ? null : new ItemStack(heldItem.getType(), heldItemAmount - summonAmount)); String lifeSpan = ""; if (tamingCOTWLength > 0) { lifeSpan = LocaleLoader.getString("Taming.Summon.Lifespan", tamingCOTWLength); } player.sendMessage(LocaleLoader.getString("Taming.Summon.Complete") + lifeSpan); player.playSound(location, SoundAdapter.FIREWORK_BLAST_FAR, 1F, 0.5F); }