123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- package com.gmail.nossr50.skills.taming;
- import com.gmail.nossr50.core.MetadataConstants;
- import com.gmail.nossr50.datatypes.experience.XPGainReason;
- import com.gmail.nossr50.datatypes.interactions.NotificationType;
- import com.gmail.nossr50.datatypes.player.McMMOPlayer;
- import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
- import com.gmail.nossr50.datatypes.skills.SubSkillType;
- import com.gmail.nossr50.events.fake.FakeEntityTameEvent;
- import com.gmail.nossr50.mcMMO;
- import com.gmail.nossr50.skills.SkillManager;
- import com.gmail.nossr50.util.Misc;
- import com.gmail.nossr50.util.Permissions;
- import com.gmail.nossr50.util.StringUtils;
- import com.gmail.nossr50.util.random.RandomChanceSkillStatic;
- import com.gmail.nossr50.util.skills.ParticleEffectUtils;
- import com.gmail.nossr50.util.skills.SkillActivationType;
- import org.bukkit.Location;
- import org.bukkit.Sound;
- import org.bukkit.entity.*;
- import org.bukkit.inventory.ItemStack;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- public class TamingManager extends SkillManager {
- private HashMap<EntityType, List<TrackedTamingEntity>> summonedEntities = new HashMap<>();
- public TamingManager(mcMMO pluginRef, McMMOPlayer mcMMOPlayer) {
- super(pluginRef, mcMMOPlayer, PrimarySkillType.TAMING);
- }
- protected void addToTracker(LivingEntity livingEntity) {
- TrackedTamingEntity trackedEntity = new TrackedTamingEntity(livingEntity);
- if (!summonedEntities.containsKey(livingEntity.getType())) {
- summonedEntities.put(livingEntity.getType(), new ArrayList<>());
- }
- summonedEntities.get(livingEntity.getType()).add(trackedEntity);
- }
- protected List<TrackedTamingEntity> getTrackedEntities(EntityType entityType) {
- return summonedEntities.get(entityType);
- }
- protected void removeFromTracker(TrackedTamingEntity trackedEntity) {
- summonedEntities.get(trackedEntity.getLivingEntity().getType()).remove(trackedEntity);
- }
- public boolean canUseThickFur() {
- return pluginRef.getRankTools().hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_THICK_FUR)
- && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.TAMING_THICK_FUR);
- }
- public boolean canUseEnvironmentallyAware() {
- return pluginRef.getRankTools().hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_ENVIRONMENTALLY_AWARE)
- && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.TAMING_ENVIRONMENTALLY_AWARE);
- }
- public boolean canUseShockProof() {
- return pluginRef.getRankTools().hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_SHOCK_PROOF)
- && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.TAMING_SHOCK_PROOF);
- }
- public boolean canUseHolyHound() {
- return pluginRef.getRankTools().hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_ENVIRONMENTALLY_AWARE)
- && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.TAMING_HOLY_HOUND);
- }
- public boolean canUseFastFoodService() {
- return pluginRef.getRankTools().hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_FAST_FOOD_SERVICE)
- && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.TAMING_FAST_FOOD_SERVICE);
- }
- public boolean canUseSharpenedClaws() {
- return pluginRef.getRankTools().hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_SHARPENED_CLAWS)
- && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.TAMING_SHARPENED_CLAWS);
- }
- public boolean canUseGore() {
- if (!pluginRef.getRankTools().hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_GORE))
- return false;
- return Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.TAMING_GORE);
- }
- public boolean canUseBeastLore() {
- if (!pluginRef.getRankTools().hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_BEAST_LORE))
- return false;
- return Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.TAMING_BEAST_LORE);
- }
- /**
- * Award XP for taming.
- *
- * @param entity The LivingEntity to award XP for
- */
- public void awardTamingXP(LivingEntity entity) {
- applyXpGain(pluginRef.getDynamicSettingsManager().getExperienceManager().getTamingXp(entity.getType()), XPGainReason.PVE);
- }
- /**
- * Apply the Fast Food Service ability.
- *
- * @param wolf The wolf using the ability
- * @param damage The damage being absorbed by the wolf
- */
- public void fastFoodService(Wolf wolf, double damage) {
- //chance (3rd param)
- if (!pluginRef.getRandomChanceTools().isActivationSuccessful(SkillActivationType.RANDOM_STATIC_CHANCE, SubSkillType.TAMING_FAST_FOOD_SERVICE, getPlayer())) {
- return;
- }
- double health = wolf.getHealth();
- double maxHealth = wolf.getMaxHealth();
- if (health < maxHealth) {
- double newHealth = health + damage;
- wolf.setHealth(Math.min(newHealth, maxHealth));
- }
- }
- /**
- * Apply the Gore ability.
- *
- * @param target The LivingEntity to apply Gore on
- * @param damage The initial damage
- */
- public double gore(LivingEntity target, double damage) {
- if (!pluginRef.getRandomChanceTools().isActivationSuccessful(SkillActivationType.RANDOM_LINEAR_100_SCALE_WITH_CAP, SubSkillType.TAMING_GORE, getPlayer())) {
- return 0;
- }
- pluginRef.getBleedTimerTask().add(target, getPlayer(), Taming.getInstance().getGoreBleedTicks(), 1, 2);
- if (target instanceof Player) {
- pluginRef.getNotificationManager().sendPlayerInformation((Player) target, NotificationType.SUBSKILL_MESSAGE, "Combat.StruckByGore");
- }
- pluginRef.getNotificationManager().sendPlayerInformation(getPlayer(), NotificationType.SUBSKILL_MESSAGE, "Combat.Gore");
- damage = (damage * Taming.getInstance().getGoreModifier()) - damage;
- return damage;
- }
- public double getSharpenedClawsDamage() {
- return Taming.getInstance().getSharpenedClawsBonusDamage();
- }
- /**
- * Summon an ocelot to your side.
- */
- public void summonOcelot() {
- if (!pluginRef.getRankTools().hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_CALL_OF_THE_WILD))
- return;
- if (!Permissions.callOfTheWild(getPlayer(), EntityType.OCELOT)) {
- return;
- }
- callOfTheWild(EntityType.OCELOT, MainConfig.getInstance().getTamingCOTWCost(EntityType.OCELOT));
- }
- /**
- * Summon a wolf to your side.
- */
- public void summonWolf() {
- if (!pluginRef.getRankTools().hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_CALL_OF_THE_WILD))
- return;
- if (!Permissions.callOfTheWild(getPlayer(), EntityType.WOLF)) {
- return;
- }
- callOfTheWild(EntityType.WOLF, MainConfig.getInstance().getTamingCOTWCost(EntityType.WOLF));
- }
- /**
- * Summon a horse to your side.
- */
- public void summonHorse() {
- if (!pluginRef.getRankTools().hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_CALL_OF_THE_WILD))
- return;
- if (!Permissions.callOfTheWild(getPlayer(), EntityType.HORSE)) {
- return;
- }
- callOfTheWild(EntityType.HORSE, MainConfig.getInstance().getTamingCOTWCost(EntityType.HORSE));
- }
- /**
- * 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 = pluginRef.getLocaleManager().getString("Combat.BeastLore") + " ";
- if (beast.isTamed() && beast.getOwner() != null) {
- message = message.concat(pluginRef.getLocaleManager().getString("Combat.BeastLoreOwner", beast.getOwner().getName()) + " ");
- }
- message = message.concat(pluginRef.getLocaleManager().getString("Combat.BeastLoreHealth", target.getHealth(), target.getMaxHealth()));
- player.sendMessage(message);
- }
- public void processEnvironmentallyAware(Wolf wolf, double damage) {
- if (damage > wolf.getHealth()) {
- return;
- }
- Player owner = getPlayer();
- wolf.teleport(owner);
- pluginRef.getNotificationManager().sendPlayerInformation(owner, NotificationType.SUBSKILL_MESSAGE, "Taming.Listener.Wolf");
- }
- public void pummel(LivingEntity target, Wolf wolf) {
- if (!pluginRef.getRankTools().hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_PUMMEL))
- return;
- if (!pluginRef.getRandomChanceTools().checkRandomChanceExecutionSuccess(new RandomChanceSkillStatic(AdvancedConfig.getInstance().getPummelChance(), getPlayer(), SubSkillType.TAMING_PUMMEL)))
- return;
- ParticleEffectUtils.playGreaterImpactEffect(target);
- target.setVelocity(wolf.getLocation().getDirection().normalize().multiply(1.5D));
- if (target instanceof Player) {
- Player defender = (Player) target;
- if (pluginRef.getNotificationManager().doesPlayerUseNotifications(defender)) {
- pluginRef.getNotificationManager().sendPlayerInformation(defender, NotificationType.SUBSKILL_MESSAGE, "Taming.SubSkill.Pummel.TargetMessage");
- }
- }
- }
- public void attackTarget(LivingEntity target) {
- if (target instanceof Tameable) {
- Tameable tameable = (Tameable) target;
- if (tameable.getOwner() == getPlayer()) {
- return;
- }
- }
- double range = 5;
- Player player = getPlayer();
- for (Entity entity : player.getNearbyEntities(range, range, range)) {
- if (entity.getType() != EntityType.WOLF) {
- continue;
- }
- Wolf wolf = (Wolf) entity;
- if (!wolf.isTamed() || (wolf.getOwner() != player) || wolf.isSitting()) {
- continue;
- }
- wolf.setTarget(target);
- }
- }
- /**
- * 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) {
- int moreAmount = summonAmount - heldItemAmount;
- pluginRef.getNotificationManager().sendPlayerInformation(player, NotificationType.REQUIREMENTS_NOT_MET, "Item.NotEnough", String.valueOf(moreAmount), StringUtils.getPrettyItemString(heldItem.getType()));
- return;
- }
- if (!rangeCheck(type)) {
- return;
- }
- int amount = MainConfig.getInstance().getTamingCOTWAmount(type);
- int tamingCOTWLength = MainConfig.getInstance().getTamingCOTWLength(type);
- for (int i = 0; i < amount; i++) {
- if (!summonAmountCheck(type)) {
- return;
- }
- location = Misc.getLocationOffset(location, 1);
- LivingEntity callOfWildEntity = (LivingEntity) player.getWorld().spawnEntity(location, type);
- FakeEntityTameEvent event = new FakeEntityTameEvent(callOfWildEntity, player);
- pluginRef.getServer().getPluginManager().callEvent(event);
- if (event.isCancelled()) {
- continue;
- }
- callOfWildEntity.setMetadata(MetadataConstants.UNNATURAL_MOB_METAKEY, MetadataConstants.metadataValue);
- ((Tameable) callOfWildEntity).setOwner(player);
- callOfWildEntity.setRemoveWhenFarAway(false);
- addToTracker(callOfWildEntity);
- switch (type) {
- case OCELOT:
- ((Ocelot) callOfWildEntity).setCatType(Ocelot.Type.values()[1 + Misc.getRandom().nextInt(3)]);
- break;
- case WOLF:
- callOfWildEntity.setMaxHealth(20.0);
- callOfWildEntity.setHealth(callOfWildEntity.getMaxHealth());
- break;
- case HORSE:
- Horse horse = (Horse) callOfWildEntity;
- callOfWildEntity.setMaxHealth(15.0 + (Misc.getRandom().nextDouble() * 15));
- callOfWildEntity.setHealth(callOfWildEntity.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)) {
- callOfWildEntity.setCustomName(pluginRef.getLocaleManager().getString("Taming.Summon.Name.Format", player.getName(), StringUtils.getPrettyEntityTypeString(type)));
- }
- ParticleEffectUtils.playCallOfTheWildEffect(callOfWildEntity);
- }
- ItemStack leftovers = new ItemStack(heldItem);
- leftovers.setAmount(heldItemAmount - summonAmount);
- player.getInventory().setItemInMainHand(heldItemAmount == summonAmount ? null : leftovers);
- String lifeSpan = "";
- if (tamingCOTWLength > 0) {
- lifeSpan = pluginRef.getLocaleManager().getString("Taming.Summon.Lifespan", tamingCOTWLength);
- }
- pluginRef.getNotificationManager().sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE, "Taming.Summon.Complete", lifeSpan);
- player.playSound(location, Sound.ENTITY_FIREWORK_ROCKET_BLAST_FAR, 1F, 0.5F);
- }
- private boolean rangeCheck(EntityType type) {
- double range = MainConfig.getInstance().getTamingCOTWRange();
- Player player = getPlayer();
- if (range == 0) {
- return true;
- }
- for (Entity entity : player.getNearbyEntities(range, range, range)) {
- if (entity.getType() == type) {
- pluginRef.getNotificationManager().sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE_FAILED, Taming.getInstance().getCallOfTheWildFailureMessage(type));
- return false;
- }
- }
- return true;
- }
- private boolean summonAmountCheck(EntityType entityType) {
- Player player = getPlayer();
- int maxAmountSummons = MainConfig.getInstance().getTamingCOTWMaxAmount(entityType);
- if (maxAmountSummons <= 0) {
- return true;
- }
- List<TrackedTamingEntity> trackedEntities = getTrackedEntities(entityType);
- int summonAmount = trackedEntities == null ? 0 : trackedEntities.size();
- if (summonAmount >= maxAmountSummons) {
- pluginRef.getNotificationManager().sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE_FAILED, "Taming.Summon.Fail.TooMany", String.valueOf(maxAmountSummons));
- return false;
- }
- return true;
- }
- }
|