From 8f37b7bbfb0da9cf43a90875e1a0340667f9b8ab Mon Sep 17 00:00:00 2001 From: Indyuce Date: Fri, 21 Jan 2022 22:39:53 +0100 Subject: [PATCH] !api and comments --- .../net/Indyuce/mmocore/api/MMOCoreAPI.java | 116 ++++++++++++++++++ .../mmocore/api/player/PlayerData.java | 4 - .../Indyuce/mmocore/manager/SkillManager.java | 13 +- .../net/Indyuce/mmocore/skill/ClassSkill.java | 10 +- 4 files changed, 131 insertions(+), 12 deletions(-) create mode 100644 src/main/java/net/Indyuce/mmocore/api/MMOCoreAPI.java diff --git a/src/main/java/net/Indyuce/mmocore/api/MMOCoreAPI.java b/src/main/java/net/Indyuce/mmocore/api/MMOCoreAPI.java new file mode 100644 index 00000000..03fdfa92 --- /dev/null +++ b/src/main/java/net/Indyuce/mmocore/api/MMOCoreAPI.java @@ -0,0 +1,116 @@ +package net.Indyuce.mmocore.api; + +import io.lumine.mythic.lib.api.player.EquipmentSlot; +import io.lumine.mythic.lib.player.PlayerMetadata; +import io.lumine.mythic.lib.skill.SimpleSkill; +import io.lumine.mythic.lib.skill.Skill; +import io.lumine.mythic.lib.skill.handler.SkillHandler; +import io.lumine.mythic.lib.skill.result.SkillResult; +import io.lumine.mythic.lib.skill.trigger.TriggerMetadata; +import net.Indyuce.mmocore.MMOCore; +import net.Indyuce.mmocore.api.player.PlayerData; +import net.Indyuce.mmocore.party.AbstractParty; +import net.Indyuce.mmocore.skill.CastableSkill; +import net.Indyuce.mmocore.skill.ClassSkill; +import net.Indyuce.mmocore.skill.RegisteredSkill; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +public class MMOCoreAPI { + private final JavaPlugin plugin; + + public MMOCoreAPI(JavaPlugin plugin) { + this.plugin = plugin; + } + + public PlayerData getPlayerData(OfflinePlayer player) { + return PlayerData.get(player.getUniqueId()); + } + + public boolean isInSameParty(Player player1, Player player2) { + AbstractParty party = MMOCore.plugin.partyModule.getParty(PlayerData.get(player1)); + return party != null && party.hasMember(player2); + } + + /** + * Forces a player to cast a skill. It has the effect of caching + * the player stats so when casting multiple skills at the same + * time, it's better to copy and paste the content of this method + * and cache the content of the caster and triggerMeta + * fields for better performance. + *

+ * Throws a NPE if no skill with such ID can be found + * + * @param playerData Player casting the skill + * @param skillId Name of skill being cast + * @param level Level of cast skill + * @return Skill result (if it's canceled) + */ + public SkillResult cast(PlayerData playerData, String skillId, int level) { + return cast(playerData, MMOCore.plugin.skillManager.getSkillOrThrow(skillId), level); + } + + /** + * Forces a player to cast a skill. It has the effect of caching + * the player stats so when casting multiple skills at the same + * time, it's better to copy and paste the content of this method + * and cache the content of the caster and triggerMeta + * fields for better performance. + * + * @param playerData Player casting the skill + * @param skill Skill being cast + * @return Skill result (if it's canceled) + */ + public SkillResult cast(PlayerData playerData, ClassSkill skill) { + PlayerMetadata casterMeta = playerData.getMMOPlayerData().getStatMap().cache(EquipmentSlot.MAIN_HAND); + TriggerMetadata triggerMeta = new TriggerMetadata(casterMeta, null, null); + return new CastableSkill(skill, playerData.getSkillLevel(skill.getSkill())).cast(triggerMeta); + } + + /** + * Forces a player to cast a skill. It has the effect of caching + * the player stats so when casting multiple skills at the same + * time, it's better to copy and paste the content of this method + * and cache the content of the caster and triggerMeta + * fields for better performance. + *

+ * This method casts a skill with default modifier formulas. + * + * @param playerData Player casting the skill + * @param skill Skill being cast + * @param level Level of cast skill. This could be the returned value of + * playerData.getSkillLevel(skill) + * @return Skill result (if it's canceled) + */ + public SkillResult cast(PlayerData playerData, RegisteredSkill skill, int level) { + PlayerMetadata casterMeta = playerData.getMMOPlayerData().getStatMap().cache(EquipmentSlot.MAIN_HAND); + TriggerMetadata triggerMeta = new TriggerMetadata(casterMeta, null, null); + return new CastableSkill(new ClassSkill(skill, 0), level).cast(triggerMeta); + } + + /** + * Forces a player to cast a skill. It has the effect of caching + * the player stats so when casting multiple skills at the same + * time, it's better to copy and paste the content of this method + * and cache the content of the caster and triggerMeta + * fields for better performance. + *

+ * Since the provided skill handler does NOT provide information about + * the skill modifiers, MMOCore tries to find a corresponding registered + * MMOCore skill in the MMOCore database. If it exists, it uses the + * modifiers from this skill, otherwise all modifiers are set to 0 + * + * @param playerData Player casting the skill + * @param skill Skill being cast + * @param level Level of cast skill + * @return Skill result (if it's canceled) + */ + public SkillResult cast(PlayerData playerData, SkillHandler skill, int level) { + PlayerMetadata casterMeta = playerData.getMMOPlayerData().getStatMap().cache(EquipmentSlot.MAIN_HAND); + TriggerMetadata triggerMeta = new TriggerMetadata(casterMeta, null, null); + RegisteredSkill registered = MMOCore.plugin.skillManager.getSkill(skill.getId()); + Skill cast = registered == null ? new SimpleSkill(skill) : new CastableSkill(new ClassSkill(registered, 0), level); + return cast.cast(triggerMeta); + } +} diff --git a/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java b/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java index 51db96c9..4d39884a 100644 --- a/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java +++ b/src/main/java/net/Indyuce/mmocore/api/player/PlayerData.java @@ -776,10 +776,6 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc skills.remove(skill); } - /** - * @deprecated use {@link PlayerClass#findSkill(RegisteredSkill)} instead - */ - @Deprecated public boolean hasSkillUnlocked(RegisteredSkill skill) { return getProfess().hasSkill(skill.getHandler().getId()) && hasSkillUnlocked(getProfess().getSkill(skill.getHandler().getId())); } diff --git a/src/main/java/net/Indyuce/mmocore/manager/SkillManager.java b/src/main/java/net/Indyuce/mmocore/manager/SkillManager.java index cba0fc0d..f5949003 100644 --- a/src/main/java/net/Indyuce/mmocore/manager/SkillManager.java +++ b/src/main/java/net/Indyuce/mmocore/manager/SkillManager.java @@ -6,15 +6,14 @@ import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.api.ConfigFile; import net.Indyuce.mmocore.api.util.MMOCoreUtils; import net.Indyuce.mmocore.skill.RegisteredSkill; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; -import java.util.Arrays; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.Map; +import java.util.*; import java.util.logging.Level; public class SkillManager implements MMOCoreManager { @@ -24,10 +23,16 @@ public class SkillManager implements MMOCoreManager { skills.put(skill.getHandler().getId().toUpperCase(), skill); } + @Nullable public RegisteredSkill getSkill(String id) { return skills.get(id.toUpperCase()); } + @NotNull + public RegisteredSkill getSkillOrThrow(String id) { + return Objects.requireNonNull(skills.get(id.toUpperCase()), "Could not find skill with ID '" + id + "'"); + } + public boolean hasSkill(String id) { return skills.containsKey(id.toUpperCase()); } diff --git a/src/main/java/net/Indyuce/mmocore/skill/ClassSkill.java b/src/main/java/net/Indyuce/mmocore/skill/ClassSkill.java index 3a799234..d4a70658 100644 --- a/src/main/java/net/Indyuce/mmocore/skill/ClassSkill.java +++ b/src/main/java/net/Indyuce/mmocore/skill/ClassSkill.java @@ -64,12 +64,13 @@ public class ClassSkill implements CooldownObject { return maxSkillLevel; } - /* - * this method can only OVERRIDE default modifiers + /** + * This method can only override default modifiers and + * will throw an error when trying to define non existing modifiers */ public void addModifier(String modifier, LinearValue linear) { - if (modifiers.containsKey(modifier)) - modifiers.put(modifier, linear); + Validate.isTrue(modifiers.containsKey(modifier), "Could not find modifier '" + modifier + "'"); + modifiers.put(modifier, linear); } public double getModifier(String modifier, int level) { @@ -117,6 +118,7 @@ public class ClassSkill implements CooldownObject { return new PassiveSkill("MMOCorePassiveSkill", skill.getTrigger(), toCastable(caster), EquipmentSlot.OTHER, ModifierSource.OTHER); } + @Override public String getCooldownPath() { return "skill_" + skill.getHandler().getId(); }