SecondaryAbilityEvent now implements Cancellable

Fire SecondaryAbilityEvent for abilities with static chances

Also cleanup some of the event handling for Armor Impact, Greater
Impact and Fast Food
This commit is contained in:
TfT_02 2014-05-11 14:58:08 +02:00
parent 3d242bbdb6
commit e7e62b8d40
10 changed files with 64 additions and 28 deletions

View File

@ -27,6 +27,7 @@ Version 1.5.01-dev
= Fixed bug where Alchemy would not fire BrewEvents = Fixed bug where Alchemy would not fire BrewEvents
= Fixed bug with setting custom names and lore in treasures config = Fixed bug with setting custom names and lore in treasures config
= Fixed bug which would cause a NullPointerException with getFlowerAndGrassXp() = Fixed bug which would cause a NullPointerException with getFlowerAndGrassXp()
! Changed SecondaryAbilityEvent to implement Cancellable and it now gets fired for damage related secondary abilities
! Changed player data saving. Save tasks are now asynchronous ! Changed player data saving. Save tasks are now asynchronous
! Vanished players no longer get hit by AoE effects ! Vanished players no longer get hit by AoE effects
! Changed Alchemy config option 'Prevent_Hopper_Transfer' renamed to 'Prevent_Hopper_Transfer_Ingredients' ! Changed Alchemy config option 'Prevent_Hopper_Transfer' renamed to 'Prevent_Hopper_Transfer_Ingredients'

View File

@ -1,18 +1,20 @@
package com.gmail.nossr50.events.skills.secondaryabilities; package com.gmail.nossr50.events.skills.secondaryabilities;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import com.gmail.nossr50.datatypes.skills.SecondaryAbility; import com.gmail.nossr50.datatypes.skills.SecondaryAbility;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.events.skills.McMMOPlayerSkillEvent; import com.gmail.nossr50.events.skills.McMMOPlayerSkillEvent;
public abstract class SecondaryAbilityEvent extends McMMOPlayerSkillEvent { public class SecondaryAbilityEvent extends McMMOPlayerSkillEvent implements Cancellable {
private SecondaryAbility secondaryAbility; private SecondaryAbility secondaryAbility;
private boolean cancelled;
public SecondaryAbilityEvent(Player player, SecondaryAbility secondaryAbility) { public SecondaryAbilityEvent(Player player, SecondaryAbility secondaryAbility) {
super(player, SkillType.bySecondaryAbility(secondaryAbility)); super(player, SkillType.bySecondaryAbility(secondaryAbility));
this.secondaryAbility = secondaryAbility; this.secondaryAbility = secondaryAbility;
cancelled = false;
} }
/** /**
@ -22,4 +24,12 @@ public abstract class SecondaryAbilityEvent extends McMMOPlayerSkillEvent {
public SecondaryAbility getSecondaryAbility() { public SecondaryAbility getSecondaryAbility() {
return secondaryAbility; return secondaryAbility;
} }
public boolean isCancelled() {
return cancelled;
}
public void setCancelled(boolean newValue) {
this.cancelled = newValue;
}
} }

View File

@ -5,7 +5,6 @@ import org.bukkit.entity.Player;
import com.gmail.nossr50.datatypes.skills.SecondaryAbility; import com.gmail.nossr50.datatypes.skills.SecondaryAbility;
public class SecondaryAbilityWeightedActivationCheckEvent extends SecondaryAbilityEvent { public class SecondaryAbilityWeightedActivationCheckEvent extends SecondaryAbilityEvent {
private double chance; private double chance;
public SecondaryAbilityWeightedActivationCheckEvent(Player player, SecondaryAbility ability, double chance) { public SecondaryAbilityWeightedActivationCheckEvent(Player player, SecondaryAbility ability, double chance) {

View File

@ -102,6 +102,10 @@ public class ArcheryManager extends SkillManager {
* @param arrow The {@link Arrow} that was fired * @param arrow The {@link Arrow} that was fired
*/ */
public double skillShot(LivingEntity target, double damage, Arrow arrow) { public double skillShot(LivingEntity target, double damage, Arrow arrow) {
if (!SkillUtils.activationSuccessful(SecondaryAbility.SKILL_SHOT, getPlayer())) {
return damage;
}
double damageBonusPercent = Math.min(((getSkillLevel() / Archery.skillShotIncreaseLevel) * Archery.skillShotIncreasePercentage), Archery.skillShotMaxBonusPercentage); double damageBonusPercent = Math.min(((getSkillLevel() / Archery.skillShotIncreaseLevel) * Archery.skillShotIncreasePercentage), Archery.skillShotMaxBonusPercentage);
double archeryBonus = Math.min(damage * damageBonusPercent, Archery.skillShotMaxBonusDamage); double archeryBonus = Math.min(damage * damageBonusPercent, Archery.skillShotMaxBonusDamage);

View File

@ -4,17 +4,14 @@ import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.AbilityType; import com.gmail.nossr50.datatypes.skills.AbilityType;
import com.gmail.nossr50.datatypes.skills.SecondaryAbility; import com.gmail.nossr50.datatypes.skills.SecondaryAbility;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.datatypes.skills.ToolType; import com.gmail.nossr50.datatypes.skills.ToolType;
import com.gmail.nossr50.events.skills.secondaryabilities.SecondaryAbilityWeightedActivationCheckEvent;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.skills.SkillManager; import com.gmail.nossr50.skills.SkillManager;
import com.gmail.nossr50.util.ItemUtils; import com.gmail.nossr50.util.ItemUtils;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
import com.gmail.nossr50.util.skills.CombatUtils; import com.gmail.nossr50.util.skills.CombatUtils;
@ -56,6 +53,10 @@ public class AxesManager extends SkillManager {
* @param target The {@link LivingEntity} being affected by the ability * @param target The {@link LivingEntity} being affected by the ability
*/ */
public double axeMastery(LivingEntity target) { public double axeMastery(LivingEntity target) {
if (!SkillUtils.activationSuccessful(SecondaryAbility.AXE_MASTERY, getPlayer())) {
return 0;
}
double axeBonus = Math.min(getSkillLevel() / (Axes.axeMasteryMaxBonusLevel / Axes.axeMasteryMaxBonus), Axes.axeMasteryMaxBonus); double axeBonus = Math.min(getSkillLevel() / (Axes.axeMasteryMaxBonusLevel / Axes.axeMasteryMaxBonus), Axes.axeMasteryMaxBonus);
return CombatUtils.callFakeDamageEvent(getPlayer(), target, axeBonus); return CombatUtils.callFakeDamageEvent(getPlayer(), target, axeBonus);
@ -98,10 +99,7 @@ public class AxesManager extends SkillManager {
for (ItemStack armor : target.getEquipment().getArmorContents()) { for (ItemStack armor : target.getEquipment().getArmorContents()) {
if (ItemUtils.isArmor(armor)) { if (ItemUtils.isArmor(armor)) {
double chance = Axes.impactChance / activationChance; if (SkillUtils.activationSuccessful(SecondaryAbility.ARMOR_IMPACT, getPlayer(), Axes.impactChance, activationChance)) {
SecondaryAbilityWeightedActivationCheckEvent event = new SecondaryAbilityWeightedActivationCheckEvent(getPlayer(), SecondaryAbility.ARMOR_IMPACT, chance);
mcMMO.p.getServer().getPluginManager().callEvent(event);
if ((event.getChance() * activationChance) > Misc.getRandom().nextInt(activationChance)) {
SkillUtils.handleDurabilityChange(armor, durabilityDamage, Axes.impactMaxDurabilityModifier); SkillUtils.handleDurabilityChange(armor, durabilityDamage, Axes.impactMaxDurabilityModifier);
} }
} }
@ -114,10 +112,7 @@ public class AxesManager extends SkillManager {
* @param target The {@link LivingEntity} being affected by the ability * @param target The {@link LivingEntity} being affected by the ability
*/ */
public double greaterImpact(LivingEntity target) { public double greaterImpact(LivingEntity target) {
double chance = Axes.greaterImpactChance / activationChance; if (!SkillUtils.activationSuccessful(SecondaryAbility.GREATER_IMPACT, getPlayer(), Axes.greaterImpactChance, activationChance)) {
SecondaryAbilityWeightedActivationCheckEvent event = new SecondaryAbilityWeightedActivationCheckEvent(getPlayer(), SecondaryAbility.GREATER_IMPACT, chance);
mcMMO.p.getServer().getPluginManager().callEvent(event);
if ((event.getChance() * activationChance) <= Misc.getRandom().nextInt(activationChance)) {
return 0; return 0;
} }

View File

@ -18,7 +18,6 @@ import com.gmail.nossr50.datatypes.skills.SecondaryAbility;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.datatypes.skills.XPGainReason; import com.gmail.nossr50.datatypes.skills.XPGainReason;
import com.gmail.nossr50.events.fake.FakeEntityTameEvent; import com.gmail.nossr50.events.fake.FakeEntityTameEvent;
import com.gmail.nossr50.events.skills.secondaryabilities.SecondaryAbilityWeightedActivationCheckEvent;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.runnables.skills.BleedTimerTask; import com.gmail.nossr50.runnables.skills.BleedTimerTask;
import com.gmail.nossr50.skills.SkillManager; import com.gmail.nossr50.skills.SkillManager;
@ -96,10 +95,9 @@ public class TamingManager extends SkillManager {
* @param damage The damage being absorbed by the wolf * @param damage The damage being absorbed by the wolf
*/ */
public void fastFoodService(Wolf wolf, double damage) { public void fastFoodService(Wolf wolf, double damage) {
double chance = Taming.fastFoodServiceActivationChance / activationChance; if (!SkillUtils.activationSuccessful(SecondaryAbility.FAST_FOOD, getPlayer(), Taming.fastFoodServiceActivationChance, activationChance)) {
SecondaryAbilityWeightedActivationCheckEvent event = new SecondaryAbilityWeightedActivationCheckEvent(getPlayer(), SecondaryAbility.FAST_FOOD, chance); return;
mcMMO.p.getServer().getPluginManager().callEvent(event); }
if ((event.getChance() * activationChance) > Misc.getRandom().nextInt(activationChance)) {
double health = wolf.getHealth(); double health = wolf.getHealth();
double maxHealth = wolf.getMaxHealth(); double maxHealth = wolf.getMaxHealth();
@ -109,7 +107,6 @@ public class TamingManager extends SkillManager {
wolf.setHealth(Math.min(newHealth, maxHealth)); wolf.setHealth(Math.min(newHealth, maxHealth));
} }
} }
}
/** /**
* Apply the Gore ability. * Apply the Gore ability.

View File

@ -60,6 +60,10 @@ public class UnarmedManager extends SkillManager {
} }
public boolean blockCrackerCheck(BlockState blockState) { public boolean blockCrackerCheck(BlockState blockState) {
if (!SkillUtils.activationSuccessful(SecondaryAbility.BLOCK_CRACKER, getPlayer())) {
return false;
}
MaterialData data = blockState.getData(); MaterialData data = blockState.getData();
switch (blockState.getType()) { switch (blockState.getType()) {
@ -135,6 +139,10 @@ public class UnarmedManager extends SkillManager {
* @param target The {@link LivingEntity} being affected by the ability * @param target The {@link LivingEntity} being affected by the ability
*/ */
public double ironArm(LivingEntity target, Map<DamageModifier, Double> modifiers) { public double ironArm(LivingEntity target, Map<DamageModifier, Double> modifiers) {
if (!SkillUtils.activationSuccessful(SecondaryAbility.IRON_ARM, getPlayer())) {
return 0;
}
double unarmedBonus = Math.min(Unarmed.ironArmMinBonusDamage + (getSkillLevel() / Unarmed.ironArmIncreaseLevel), Unarmed.ironArmMaxBonusDamage); double unarmedBonus = Math.min(Unarmed.ironArmMinBonusDamage + (getSkillLevel() / Unarmed.ironArmIncreaseLevel), Unarmed.ironArmMaxBonusDamage);
return CombatUtils.callFakeDamageEvent(getPlayer(), target, unarmedBonus, modifiers); return CombatUtils.callFakeDamageEvent(getPlayer(), target, unarmedBonus, modifiers);

View File

@ -173,5 +173,4 @@ public class WoodcuttingManager extends SkillManager {
applyXpGain(xp, XPGainReason.PVE); applyXpGain(xp, XPGainReason.PVE);
} }
} }

View File

@ -16,6 +16,7 @@ import com.gmail.nossr50.datatypes.party.Party;
import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.skills.AbilityType; import com.gmail.nossr50.datatypes.skills.AbilityType;
import com.gmail.nossr50.datatypes.skills.SecondaryAbility;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.datatypes.skills.XPGainReason; import com.gmail.nossr50.datatypes.skills.XPGainReason;
import com.gmail.nossr50.events.experience.McMMOPlayerLevelChangeEvent; import com.gmail.nossr50.events.experience.McMMOPlayerLevelChangeEvent;
@ -35,6 +36,7 @@ import com.gmail.nossr50.events.skills.abilities.McMMOPlayerAbilityDeactivateEve
import com.gmail.nossr50.events.skills.fishing.McMMOPlayerFishingTreasureEvent; import com.gmail.nossr50.events.skills.fishing.McMMOPlayerFishingTreasureEvent;
import com.gmail.nossr50.events.skills.fishing.McMMOPlayerMagicHunterEvent; import com.gmail.nossr50.events.skills.fishing.McMMOPlayerMagicHunterEvent;
import com.gmail.nossr50.events.skills.repair.McMMOPlayerRepairCheckEvent; import com.gmail.nossr50.events.skills.repair.McMMOPlayerRepairCheckEvent;
import com.gmail.nossr50.events.skills.secondaryabilities.SecondaryAbilityEvent;
import com.gmail.nossr50.events.skills.unarmed.McMMOPlayerDisarmEvent; import com.gmail.nossr50.events.skills.unarmed.McMMOPlayerDisarmEvent;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
@ -47,6 +49,13 @@ public class EventUtils {
return event; return event;
} }
public static SecondaryAbilityEvent callSecondaryAbilityEvent(Player player, SecondaryAbility secondaryAbility) {
SecondaryAbilityEvent event = new SecondaryAbilityEvent(player, secondaryAbility);
mcMMO.p.getServer().getPluginManager().callEvent(event);
return event;
}
public static FakePlayerAnimationEvent callFakeArmSwingEvent(Player player) { public static FakePlayerAnimationEvent callFakeArmSwingEvent(Player player) {
FakePlayerAnimationEvent event = new FakePlayerAnimationEvent(player); FakePlayerAnimationEvent event = new FakePlayerAnimationEvent(player);
mcMMO.p.getServer().getPluginManager().callEvent(event); mcMMO.p.getServer().getPluginManager().callEvent(event);

View File

@ -24,8 +24,10 @@ import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.AbilityType; import com.gmail.nossr50.datatypes.skills.AbilityType;
import com.gmail.nossr50.datatypes.skills.SecondaryAbility; import com.gmail.nossr50.datatypes.skills.SecondaryAbility;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.events.skills.secondaryabilities.SecondaryAbilityEvent;
import com.gmail.nossr50.events.skills.secondaryabilities.SecondaryAbilityWeightedActivationCheckEvent; import com.gmail.nossr50.events.skills.secondaryabilities.SecondaryAbilityWeightedActivationCheckEvent;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.util.EventUtils;
import com.gmail.nossr50.util.ItemUtils; import com.gmail.nossr50.util.ItemUtils;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.StringUtils; import com.gmail.nossr50.util.StringUtils;
@ -205,13 +207,25 @@ public class SkillUtils {
double chance = (maxChance / maxLevel) * Math.min(skillLevel, maxLevel) / activationChance; double chance = (maxChance / maxLevel) * Math.min(skillLevel, maxLevel) / activationChance;
SecondaryAbilityWeightedActivationCheckEvent event = new SecondaryAbilityWeightedActivationCheckEvent(player, skillAbility, chance); SecondaryAbilityWeightedActivationCheckEvent event = new SecondaryAbilityWeightedActivationCheckEvent(player, skillAbility, chance);
mcMMO.p.getServer().getPluginManager().callEvent(event); mcMMO.p.getServer().getPluginManager().callEvent(event);
return (event.getChance() * activationChance) > Misc.getRandom().nextInt(activationChance); return (event.getChance() * activationChance) > Misc.getRandom().nextInt(activationChance) && !event.isCancelled();
}
public static boolean activationSuccessful(SecondaryAbility skillAbility, Player player, double staticChance, int activationChance) {
double chance = staticChance / activationChance;
SecondaryAbilityWeightedActivationCheckEvent event = new SecondaryAbilityWeightedActivationCheckEvent(player, skillAbility, chance);
mcMMO.p.getServer().getPluginManager().callEvent(event);
return (event.getChance() * activationChance) > Misc.getRandom().nextInt(activationChance) && !event.isCancelled();
}
public static boolean activationSuccessful(SecondaryAbility skillAbility, Player player) {
SecondaryAbilityEvent event = EventUtils.callSecondaryAbilityEvent(player, skillAbility);
return !event.isCancelled();
} }
public static boolean treasureDropSuccessful(Player player, double dropChance, int activationChance) { public static boolean treasureDropSuccessful(Player player, double dropChance, int activationChance) {
SecondaryAbilityWeightedActivationCheckEvent event = new SecondaryAbilityWeightedActivationCheckEvent(player, SecondaryAbility.EXCAVATION_TREASURE_HUNTER, dropChance / activationChance); SecondaryAbilityWeightedActivationCheckEvent event = new SecondaryAbilityWeightedActivationCheckEvent(player, SecondaryAbility.EXCAVATION_TREASURE_HUNTER, dropChance / activationChance);
mcMMO.p.getServer().getPluginManager().callEvent(event); mcMMO.p.getServer().getPluginManager().callEvent(event);
return (event.getChance() * activationChance) > (Misc.getRandom().nextDouble() * activationChance); return (event.getChance() * activationChance) > (Misc.getRandom().nextDouble() * activationChance) && !event.isCancelled();
} }
private static boolean isLocalizedSkill(String skillName) { private static boolean isLocalizedSkill(String skillName) {