!Changed ability API

This commit is contained in:
Indyuce 2019-12-28 13:34:28 +01:00
parent 3357ab4803
commit 3cd9899ef2
17 changed files with 166 additions and 44 deletions

View File

@ -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;

View File

@ -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<Material>) 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();
}

View File

@ -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();
}

View File

@ -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<Material>) null, length).getLocation();
return loc.getBlock().getType() == Material.AIR ? null : loc.add(.5, 1, .5);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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) {

View File

@ -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;

View File

@ -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 {

View File

@ -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<Entity> 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;
}
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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 {

View File

@ -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;

View File

@ -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;

View File

@ -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 {