!api and comments

This commit is contained in:
Indyuce 2022-01-21 22:39:53 +01:00
parent 420d309ac3
commit 8f37b7bbfb
4 changed files with 131 additions and 12 deletions

View File

@ -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 <code>caster</code> and <code>triggerMeta</code>
* fields for better performance.
* <p>
* 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 <code>caster</code> and <code>triggerMeta</code>
* 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 <code>caster</code> and <code>triggerMeta</code>
* fields for better performance.
* <p>
* 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
* <code>playerData.getSkillLevel(skill)</code>
* @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 <code>caster</code> and <code>triggerMeta</code>
* fields for better performance.
* <p>
* 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);
}
}

View File

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

View File

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

View File

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