diff --git a/src/main/java/net/Indyuce/mmoitems/api/ItemAttackResult.java b/src/main/java/net/Indyuce/mmoitems/api/ItemAttackResult.java index d17ffde2..ba66d3d5 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/ItemAttackResult.java +++ b/src/main/java/net/Indyuce/mmoitems/api/ItemAttackResult.java @@ -2,7 +2,7 @@ package net.Indyuce.mmoitems.api; import org.bukkit.entity.LivingEntity; -import net.Indyuce.mmoitems.api.Ability.CastingMode; +import net.Indyuce.mmoitems.api.ability.Ability.CastingMode; import net.Indyuce.mmoitems.api.player.PlayerStats.CachedStats; import net.mmogroup.mmolib.MMOLib; import net.mmogroup.mmolib.api.AttackResult; diff --git a/src/main/java/net/Indyuce/mmoitems/api/Ability.java b/src/main/java/net/Indyuce/mmoitems/api/ability/Ability.java similarity index 85% rename from src/main/java/net/Indyuce/mmoitems/api/Ability.java rename to src/main/java/net/Indyuce/mmoitems/api/ability/Ability.java index 756be281..82bb08bf 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/Ability.java +++ b/src/main/java/net/Indyuce/mmoitems/api/ability/Ability.java @@ -1,4 +1,4 @@ -package net.Indyuce.mmoitems.api; +package net.Indyuce.mmoitems.api.ability; import java.util.Arrays; import java.util.HashMap; @@ -8,14 +8,13 @@ import java.util.Random; import java.util.Set; import org.apache.commons.lang.Validate; -import org.bukkit.Location; -import org.bukkit.Material; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.util.Vector; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.MMOUtils; +import net.Indyuce.mmoitems.api.ItemAttackResult; import net.Indyuce.mmoitems.api.player.PlayerStats.CachedStats; import net.Indyuce.mmoitems.stat.data.AbilityData; @@ -99,23 +98,13 @@ public abstract class Ability { * these methods need to be overriden by ability classes depending on their * ability type */ - public abstract void whenCast(CachedStats playerStats, LivingEntity target, AbilityData data, ItemAttackResult result); + public abstract AbilityResult whenRan(CachedStats stats, LivingEntity target, AbilityData ability, ItemAttackResult result); + + public abstract void whenCast(CachedStats stats, AbilityResult ability, ItemAttackResult result); /* * util methods for abilities */ - protected Location getTargetLocation(Player player, LivingEntity entity) { - return getTargetLocation(player, entity, 50); - } - - protected Location getTargetLocation(Player player, LivingEntity entity, int length) { - if (entity != null) - return entity.getLocation(); - - Location loc = player.getTargetBlock((Set) null, length).getLocation(); - return loc.getBlock().getType() == Material.AIR ? null : loc.add(.5, 1, .5); - } - protected Vector getTargetDirection(Player player, LivingEntity target) { return target == null ? player.getEyeLocation().getDirection() : target.getLocation().add(0, target.getHeight() / 2, 0).subtract(player.getLocation().add(0, 1.3, 0)).toVector().normalize(); } diff --git a/src/main/java/net/Indyuce/mmoitems/api/ability/AbilityResult.java b/src/main/java/net/Indyuce/mmoitems/api/ability/AbilityResult.java new file mode 100644 index 00000000..b3bc8b70 --- /dev/null +++ b/src/main/java/net/Indyuce/mmoitems/api/ability/AbilityResult.java @@ -0,0 +1,21 @@ +package net.Indyuce.mmoitems.api.ability; + +import net.Indyuce.mmoitems.stat.data.AbilityData; + +public abstract class AbilityResult { + private final AbilityData ability; + + public AbilityResult(AbilityData ability) { + this.ability = ability; + } + + public AbilityData getAbility() { + return ability; + } + + public double getModifier(String path) { + return ability.getModifier(path); + } + + public abstract boolean isSuccessful(); +} diff --git a/src/main/java/net/Indyuce/mmoitems/api/ability/LocationAbilityResult.java b/src/main/java/net/Indyuce/mmoitems/api/ability/LocationAbilityResult.java new file mode 100644 index 00000000..e41c1d4b --- /dev/null +++ b/src/main/java/net/Indyuce/mmoitems/api/ability/LocationAbilityResult.java @@ -0,0 +1,37 @@ +package net.Indyuce.mmoitems.api.ability; + +import java.util.Set; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; + +import net.Indyuce.mmoitems.stat.data.AbilityData; + +public class LocationAbilityResult extends AbilityResult { + private final Location target; + + public LocationAbilityResult(AbilityData ability, Player caster, LivingEntity target) { + super(ability); + + this.target = getTargetLocation(caster, target, 50); + } + + public Location getTarget() { + return target; + } + + @Override + public boolean isSuccessful() { + return target != null; + } + + private Location getTargetLocation(Player player, LivingEntity entity, int length) { + if (entity != null) + return entity.getLocation(); + + Location loc = player.getTargetBlock((Set) null, length).getLocation(); + return loc.getBlock().getType() == Material.AIR ? null : loc.add(.5, 1, .5); + } +} diff --git a/src/main/java/net/Indyuce/mmoitems/api/ability/SimpleAbilityResult.java b/src/main/java/net/Indyuce/mmoitems/api/ability/SimpleAbilityResult.java new file mode 100644 index 00000000..ce29f59d --- /dev/null +++ b/src/main/java/net/Indyuce/mmoitems/api/ability/SimpleAbilityResult.java @@ -0,0 +1,22 @@ +package net.Indyuce.mmoitems.api.ability; + +import net.Indyuce.mmoitems.stat.data.AbilityData; + +public class SimpleAbilityResult extends AbilityResult { + private final boolean successful; + + public SimpleAbilityResult(AbilityData ability) { + this(ability, true); + } + + public SimpleAbilityResult(AbilityData ability, boolean successful) { + super(ability); + + this.successful = successful; + } + + @Override + public boolean isSuccessful() { + return successful; + } +} diff --git a/src/main/java/net/Indyuce/mmoitems/api/ability/TargetAbilityResult.java b/src/main/java/net/Indyuce/mmoitems/api/ability/TargetAbilityResult.java new file mode 100644 index 00000000..c23ed062 --- /dev/null +++ b/src/main/java/net/Indyuce/mmoitems/api/ability/TargetAbilityResult.java @@ -0,0 +1,23 @@ +package net.Indyuce.mmoitems.api.ability; + +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; + +import net.Indyuce.mmoitems.MMOUtils; +import net.Indyuce.mmoitems.stat.data.AbilityData; +import net.mmogroup.mmolib.MMOLib; + +public class TargetAbilityResult extends AbilityResult { + private final LivingEntity target; + + public TargetAbilityResult(AbilityData ability, Player caster, LivingEntity target) { + super(ability); + + this.target = target != null ? target : MMOLib.plugin.getVersion().getWrapper().rayTrace(caster, 50, entity -> MMOUtils.canDamage(caster, entity)).getHit(); + } + + @Override + public boolean isSuccessful() { + return target != null; + } +} diff --git a/src/main/java/net/Indyuce/mmoitems/api/player/PlayerData.java b/src/main/java/net/Indyuce/mmoitems/api/player/PlayerData.java index 971df405..4d53b303 100644 --- a/src/main/java/net/Indyuce/mmoitems/api/player/PlayerData.java +++ b/src/main/java/net/Indyuce/mmoitems/api/player/PlayerData.java @@ -24,13 +24,14 @@ import org.bukkit.potion.PotionEffectType; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.MMOUtils; -import net.Indyuce.mmoitems.api.Ability; -import net.Indyuce.mmoitems.api.Ability.CastingMode; import net.Indyuce.mmoitems.api.ConfigFile; import net.Indyuce.mmoitems.api.ItemAttackResult; import net.Indyuce.mmoitems.api.ItemSet; import net.Indyuce.mmoitems.api.ItemSet.SetBonuses; import net.Indyuce.mmoitems.api.Type; +import net.Indyuce.mmoitems.api.ability.Ability; +import net.Indyuce.mmoitems.api.ability.Ability.CastingMode; +import net.Indyuce.mmoitems.api.ability.AbilityResult; import net.Indyuce.mmoitems.api.crafting.CraftingStatus; import net.Indyuce.mmoitems.api.event.AbilityUseEvent; import net.Indyuce.mmoitems.api.item.MMOItem; @@ -388,24 +389,20 @@ public class PlayerData { cast(getStats().newTemporary(), null, new ItemAttackResult(true, DamageType.SKILL), data, true); } - public void cast(CachedStats stats, LivingEntity target, ItemAttackResult result, AbilityData ability, boolean message) { + public void cast(CachedStats stats, LivingEntity target, ItemAttackResult attack, AbilityData ability, boolean message) { AbilityUseEvent event = new AbilityUseEvent(this, ability, target); Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) return; - /* - * check if the player can cast the ability, if he can't just return a - * new instance of of ItemAttackResult with false boolean - */ if (!rpgPlayer.canCast(ability, message)) return; /* - * cast the actual ability and see if it was successfully cast + * check if ability can be cast (custom conditions) */ - ability.getAbility().whenCast(stats, target, ability, result); - if (!result.isSuccessful()) + AbilityResult abilityResult = ability.getAbility().whenRan(stats, target, ability, attack); + if (!abilityResult.isSuccessful()) return; /* @@ -420,6 +417,13 @@ public class PlayerData { double cooldown = ability.getModifier("cooldown"); if (cooldown > 0) applyAbilityCooldown(ability.getAbility(), cooldown); + + /* + * finally cast the ability (BUG FIX) cooldown MUST be applied BEFORE + * the ability is cast otherwise instantaneously damaging abilities like + * Sparkle can trigger deadly crash loops + */ + ability.getAbility().whenCast(stats, abilityResult, attack); } public void log(String... lines) { diff --git a/src/main/java/net/Indyuce/mmoitems/command/MMOItemsCommand.java b/src/main/java/net/Indyuce/mmoitems/command/MMOItemsCommand.java index 43b8fa1b..08065163 100644 --- a/src/main/java/net/Indyuce/mmoitems/command/MMOItemsCommand.java +++ b/src/main/java/net/Indyuce/mmoitems/command/MMOItemsCommand.java @@ -25,10 +25,10 @@ import org.bukkit.potion.PotionEffectType; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.MMOUtils; -import net.Indyuce.mmoitems.api.Ability; import net.Indyuce.mmoitems.api.ConfigFile; import net.Indyuce.mmoitems.api.PluginUpdate; import net.Indyuce.mmoitems.api.Type; +import net.Indyuce.mmoitems.api.ability.Ability; import net.Indyuce.mmoitems.api.crafting.CraftingStation; import net.Indyuce.mmoitems.api.drop.DropItem; import net.Indyuce.mmoitems.api.item.plugin.identify.IdentifiedItem; diff --git a/src/main/java/net/Indyuce/mmoitems/command/completion/MMOItemsCompletion.java b/src/main/java/net/Indyuce/mmoitems/command/completion/MMOItemsCompletion.java index 62bc7d9b..c6d23100 100644 --- a/src/main/java/net/Indyuce/mmoitems/command/completion/MMOItemsCompletion.java +++ b/src/main/java/net/Indyuce/mmoitems/command/completion/MMOItemsCompletion.java @@ -11,8 +11,8 @@ import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; import net.Indyuce.mmoitems.MMOItems; -import net.Indyuce.mmoitems.api.Ability; import net.Indyuce.mmoitems.api.Type; +import net.Indyuce.mmoitems.api.ability.Ability; import net.Indyuce.mmoitems.command.PluginHelp; public class MMOItemsCompletion implements TabCompleter { diff --git a/src/main/java/net/Indyuce/mmoitems/comp/mythicmobs/MythicMobsAbility.java b/src/main/java/net/Indyuce/mmoitems/comp/mythicmobs/MythicMobsAbility.java index cb4930e4..d0a24b44 100644 --- a/src/main/java/net/Indyuce/mmoitems/comp/mythicmobs/MythicMobsAbility.java +++ b/src/main/java/net/Indyuce/mmoitems/comp/mythicmobs/MythicMobsAbility.java @@ -11,8 +11,9 @@ import org.bukkit.entity.LivingEntity; import io.lumine.xikage.mythicmobs.MythicMobs; import io.lumine.xikage.mythicmobs.skills.Skill; -import net.Indyuce.mmoitems.api.Ability; import net.Indyuce.mmoitems.api.ItemAttackResult; +import net.Indyuce.mmoitems.api.ability.Ability; +import net.Indyuce.mmoitems.api.ability.AbilityResult; import net.Indyuce.mmoitems.api.player.PlayerStats.CachedStats; import net.Indyuce.mmoitems.stat.data.AbilityData; @@ -46,7 +47,8 @@ public class MythicMobsAbility extends Ability { } @Override - public void whenCast(CachedStats stats, LivingEntity target, AbilityData data, ItemAttackResult result) { + public void whenCast(CachedStats stats, AbilityResult ability, ItemAttackResult result) { + LivingEntity target = ((MythicMobsAbilityResult) ability).getTarget(); List targets = new ArrayList<>(); targets.add(target == null || selfOnly ? stats.getPlayer() : target); @@ -54,9 +56,33 @@ public class MythicMobsAbility extends Ability { * cache placeholders so they can be retrieved later by MythicMobs math * formulas */ - stats.getPlayerData().getAbilityData().cacheModifiers(this, data); + stats.getPlayerData().getAbilityData().cacheModifiers(this, ability.getAbility()); if (!MythicMobs.inst().getAPIHelper().castSkill(stats.getPlayer(), skill.getInternalName(), stats.getPlayer(), stats.getPlayer().getEyeLocation(), targets, null, 1)) result.setSuccessful(false); } + + @Override + public AbilityResult whenRan(CachedStats playerStats, LivingEntity target, AbilityData data, ItemAttackResult result) { + return new MythicMobsAbilityResult(data, target); + } + + public class MythicMobsAbilityResult extends AbilityResult { + private final LivingEntity target; + + public MythicMobsAbilityResult(AbilityData ability, LivingEntity target) { + super(ability); + + this.target = target; + } + + public LivingEntity getTarget() { + return target; + } + + @Override + public boolean isSuccessful() { + return true; + } + } } diff --git a/src/main/java/net/Indyuce/mmoitems/gui/edition/AbilityEdition.java b/src/main/java/net/Indyuce/mmoitems/gui/edition/AbilityEdition.java index 187f65b8..9fcf5c9c 100644 --- a/src/main/java/net/Indyuce/mmoitems/gui/edition/AbilityEdition.java +++ b/src/main/java/net/Indyuce/mmoitems/gui/edition/AbilityEdition.java @@ -18,10 +18,10 @@ import org.bukkit.inventory.meta.ItemMeta; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.MMOUtils; -import net.Indyuce.mmoitems.api.Ability; -import net.Indyuce.mmoitems.api.Ability.CastingMode; import net.Indyuce.mmoitems.api.ConfigFile; import net.Indyuce.mmoitems.api.Type; +import net.Indyuce.mmoitems.api.ability.Ability; +import net.Indyuce.mmoitems.api.ability.Ability.CastingMode; import net.Indyuce.mmoitems.api.edition.StatEdition; import net.Indyuce.mmoitems.api.util.AltChar; import net.Indyuce.mmoitems.stat.type.ItemStat; diff --git a/src/main/java/net/Indyuce/mmoitems/gui/edition/AbilityListEdition.java b/src/main/java/net/Indyuce/mmoitems/gui/edition/AbilityListEdition.java index 9aae21d8..59173024 100644 --- a/src/main/java/net/Indyuce/mmoitems/gui/edition/AbilityListEdition.java +++ b/src/main/java/net/Indyuce/mmoitems/gui/edition/AbilityListEdition.java @@ -17,10 +17,10 @@ import org.bukkit.inventory.meta.ItemMeta; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.MMOUtils; -import net.Indyuce.mmoitems.api.Ability; -import net.Indyuce.mmoitems.api.Ability.CastingMode; import net.Indyuce.mmoitems.api.ConfigFile; import net.Indyuce.mmoitems.api.Type; +import net.Indyuce.mmoitems.api.ability.Ability; +import net.Indyuce.mmoitems.api.ability.Ability.CastingMode; import net.Indyuce.mmoitems.api.util.AltChar; import net.mmogroup.mmolib.MMOLib; import net.mmogroup.mmolib.api.item.ItemTag; diff --git a/src/main/java/net/Indyuce/mmoitems/listener/PlayerListener.java b/src/main/java/net/Indyuce/mmoitems/listener/PlayerListener.java index b8b95787..455e6ff0 100644 --- a/src/main/java/net/Indyuce/mmoitems/listener/PlayerListener.java +++ b/src/main/java/net/Indyuce/mmoitems/listener/PlayerListener.java @@ -21,9 +21,9 @@ import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.inventory.ItemStack; import net.Indyuce.mmoitems.ability.Magical_Shield; -import net.Indyuce.mmoitems.api.Ability.CastingMode; import net.Indyuce.mmoitems.api.ItemAttackResult; import net.Indyuce.mmoitems.api.SoulboundInfo; +import net.Indyuce.mmoitems.api.ability.Ability.CastingMode; import net.Indyuce.mmoitems.api.player.PlayerData; import net.Indyuce.mmoitems.api.player.PlayerStats; import net.Indyuce.mmoitems.stat.type.ItemStat; diff --git a/src/main/java/net/Indyuce/mmoitems/manager/AbilityManager.java b/src/main/java/net/Indyuce/mmoitems/manager/AbilityManager.java index b4a62481..70c10748 100644 --- a/src/main/java/net/Indyuce/mmoitems/manager/AbilityManager.java +++ b/src/main/java/net/Indyuce/mmoitems/manager/AbilityManager.java @@ -16,7 +16,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.event.Listener; import net.Indyuce.mmoitems.MMOItems; -import net.Indyuce.mmoitems.api.Ability; +import net.Indyuce.mmoitems.api.ability.Ability; import net.Indyuce.mmoitems.comp.mythicmobs.MythicMobsAbility; public class AbilityManager { diff --git a/src/main/java/net/Indyuce/mmoitems/manager/ConfigManager.java b/src/main/java/net/Indyuce/mmoitems/manager/ConfigManager.java index 46eed84f..d1457ab9 100644 --- a/src/main/java/net/Indyuce/mmoitems/manager/ConfigManager.java +++ b/src/main/java/net/Indyuce/mmoitems/manager/ConfigManager.java @@ -20,9 +20,9 @@ import org.bukkit.potion.PotionEffectType; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.MMOUtils; -import net.Indyuce.mmoitems.api.Ability; -import net.Indyuce.mmoitems.api.Ability.CastingMode; import net.Indyuce.mmoitems.api.ConfigFile; +import net.Indyuce.mmoitems.api.ability.Ability; +import net.Indyuce.mmoitems.api.ability.Ability.CastingMode; import net.Indyuce.mmoitems.api.interaction.util.DurabilityState; import net.Indyuce.mmoitems.api.item.plugin.ConfigItem; import net.Indyuce.mmoitems.api.util.AltChar; diff --git a/src/main/java/net/Indyuce/mmoitems/stat/Abilities.java b/src/main/java/net/Indyuce/mmoitems/stat/Abilities.java index 9f4d1867..782c01ec 100644 --- a/src/main/java/net/Indyuce/mmoitems/stat/Abilities.java +++ b/src/main/java/net/Indyuce/mmoitems/stat/Abilities.java @@ -19,9 +19,9 @@ import com.google.gson.JsonSyntaxException; import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.MMOUtils; -import net.Indyuce.mmoitems.api.Ability; -import net.Indyuce.mmoitems.api.Ability.CastingMode; import net.Indyuce.mmoitems.api.ConfigFile; +import net.Indyuce.mmoitems.api.ability.Ability; +import net.Indyuce.mmoitems.api.ability.Ability.CastingMode; import net.Indyuce.mmoitems.api.item.MMOItem; import net.Indyuce.mmoitems.api.item.build.MMOItemBuilder; import net.Indyuce.mmoitems.api.util.AltChar; diff --git a/src/main/java/net/Indyuce/mmoitems/stat/data/AbilityData.java b/src/main/java/net/Indyuce/mmoitems/stat/data/AbilityData.java index 4d6b2b5e..1d81c586 100644 --- a/src/main/java/net/Indyuce/mmoitems/stat/data/AbilityData.java +++ b/src/main/java/net/Indyuce/mmoitems/stat/data/AbilityData.java @@ -9,8 +9,8 @@ import org.bukkit.configuration.ConfigurationSection; import com.google.gson.JsonObject; import net.Indyuce.mmoitems.MMOItems; -import net.Indyuce.mmoitems.api.Ability; -import net.Indyuce.mmoitems.api.Ability.CastingMode; +import net.Indyuce.mmoitems.api.ability.Ability; +import net.Indyuce.mmoitems.api.ability.Ability.CastingMode; import net.Indyuce.mmoitems.api.item.MMOItem; public class AbilityData extends StatData {