cleaned ability registration API

This commit is contained in:
Indyuce 2021-08-16 20:08:36 +02:00
parent 7b4e735547
commit 1deb3f6252
11 changed files with 87 additions and 235 deletions

View File

@ -154,7 +154,7 @@ public class MMOItems extends LuminePlugin {
new MMOItemsMetrics();
RecipeBrowserGUI.registerNativeRecipes();
abilityManager.initialize();
abilityManager.loadPluginAbilities();
configManager = new ConfigManager();
final int configVersion = getConfig().contains("config-version", true) ? getConfig().getInt("config-version") : -1;

View File

@ -0,0 +1,31 @@
package net.Indyuce.mmoitems.api;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ability.Ability;
import org.bukkit.plugin.java.JavaPlugin;
public class MMOItemsAPI {
private final JavaPlugin plugin;
/**
* @param plugin Plugin
*/
public MMOItemsAPI(JavaPlugin plugin) {
this.plugin = plugin;
}
/**
* Registers an ability in MMOItems. This must be called before MMOItems enables,
* therefore either using a loadbefore of MMOItems and while the plugin enables,
* or using a dependency and usign #onLoad().
* <p>
* This method does NOT register listeners.
* <p>
* Throws an IAE if anything goes wrong.
*
* @param ability Ability to register
*/
public void registerAbility(Ability ability) {
MMOItems.plugin.getAbilities().registerAbility(ability);
}
}

View File

@ -1,27 +1,19 @@
package net.Indyuce.mmoitems.api.ability;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import org.apache.commons.lang.Validate;
import org.bukkit.entity.LivingEntity;
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;
import org.apache.commons.lang.Validate;
import org.bukkit.entity.LivingEntity;
import java.util.*;
public abstract class Ability {
private final String name, id;
private final List<CastingMode> allowedModes;
private final Map<String, Double> modifiers = new HashMap<>();
private boolean enabled = true;
protected static final Random random = new Random();
@Override
@ -59,10 +51,6 @@ public abstract class Ability {
return name;
}
public boolean isEnabled() {
return enabled;
}
public boolean isAllowedMode(CastingMode castingMode) {
return allowedModes.contains(castingMode);
}
@ -91,9 +79,11 @@ public abstract class Ability {
* Disables an ability. This method must be called before MMOItems registers
* this ability since it is just a boolean check when registering abilities
* through the abilityManager
*
* @deprecated Useless
*/
@Deprecated
public void disable() {
enabled = false;
}
/**

View File

@ -19,7 +19,7 @@ public class AbilityCommandTreeNode extends CommandTreeNode {
super(parent, "ability");
addParameter(new Parameter("<ability>",
(explorer, list) -> MMOItems.plugin.getAbilities().getAllAbilities().forEach(ability -> list.add(ability.getID()))));
(explorer, list) -> MMOItems.plugin.getAbilities().getAll().forEach(ability -> list.add(ability.getID()))));
addParameter(Parameter.PLAYER_OPTIONAL);
// three modifiers but more can be used

View File

@ -19,7 +19,7 @@ public class AbilityCommandTreeNode extends CommandTreeNode {
sender.sendMessage(ChatColor.WHITE + "Here are all the abilities you can bind to items.");
sender.sendMessage(ChatColor.WHITE + "The values inside brackets are " + ChatColor.UNDERLINE + "modifiers" + ChatColor.WHITE
+ " which allow you to change the ability values (cooldown, damage...)");
for (Ability a : MMOItems.plugin.getAbilities().getAllAbilities()) {
for (Ability a : MMOItems.plugin.getAbilities().getAll()) {
String modFormat = ChatColor.GRAY + String.join(ChatColor.WHITE + ", " + ChatColor.GRAY, a.getModifiers());
modFormat = ChatColor.WHITE + "(" + modFormat + ChatColor.WHITE + ")";
sender.sendMessage("* " + ChatColor.LIGHT_PURPLE + a.getName() + " " + modFormat);

View File

@ -1,20 +1,16 @@
package net.Indyuce.mmoitems.command.mmoitems.list;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.mmolibcommands.api.CommandTreeNode;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class ListCommandTreeNode extends CommandTreeNode {
public ListCommandTreeNode(CommandTreeNode parent) {
super(parent, "list");
addChild(new AbilityCommandTreeNode(this));
addChild(new MIAbilityCommandTreeNode(this));
addChild(new MMAbilityCommandTreeNode(this));
addChild(new TPAbilityCommandTreeNode(this));
addChild(new LuteAttackCommandTreeNode(this));
addChild(new StaffSpiritCommandTreeNode(this));
addChild(new TypeCommandTreeNode(this));
@ -28,9 +24,6 @@ public class ListCommandTreeNode extends CommandTreeNode {
sender.sendMessage(ChatColor.LIGHT_PURPLE + "/mi list spirit " + ChatColor.WHITE + "shows all available staff spirits");
sender.sendMessage(ChatColor.LIGHT_PURPLE + "/mi list lute " + ChatColor.WHITE + "shows all available lute attack effects");
sender.sendMessage(ChatColor.LIGHT_PURPLE + "/mi list ability " + ChatColor.WHITE + "shows all available abilities");
sender.sendMessage(ChatColor.LIGHT_PURPLE + "/mi list miability " + ChatColor.WHITE + "shows all available MMOItems abilities");
sender.sendMessage(ChatColor.LIGHT_PURPLE + "/mi list mmability " + ChatColor.WHITE + "shows all available MythicMobs abilities");
sender.sendMessage(ChatColor.LIGHT_PURPLE + "/mi list tpability " + ChatColor.WHITE + "shows all available Third Party abilities");
if (sender instanceof Player) {
sender.sendMessage("");
sender.sendMessage("Spigot Javadoc Links:");

View File

@ -1,29 +0,0 @@
package net.Indyuce.mmoitems.command.mmoitems.list;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ability.Ability;
import io.lumine.mythic.lib.mmolibcommands.api.CommandTreeNode;
public class MIAbilityCommandTreeNode extends CommandTreeNode {
public MIAbilityCommandTreeNode(CommandTreeNode parent) {
super(parent, "miability");
}
@Override
public CommandResult execute(CommandSender sender, String[] args) {
sender.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-----------------[" + ChatColor.LIGHT_PURPLE + " MMOItems Abilities "
+ ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "]-----------------");
sender.sendMessage(ChatColor.WHITE + "Here are all the abilities you can bind to items.");
sender.sendMessage(ChatColor.WHITE + "The values inside brackets are " + ChatColor.UNDERLINE + "modifiers" + ChatColor.WHITE
+ " which allow you to change the ability values (cooldown, damage...)");
for (Ability a : MMOItems.plugin.getAbilities().getAllMMOItemsAbilities()) {
String modFormat = ChatColor.GRAY + String.join(ChatColor.WHITE + ", " + ChatColor.GRAY, a.getModifiers());
modFormat = ChatColor.WHITE + "(" + modFormat + ChatColor.WHITE + ")";
sender.sendMessage("* " + ChatColor.LIGHT_PURPLE + a.getName() + " " + modFormat);
}
return CommandResult.SUCCESS;
}
}

View File

@ -1,29 +0,0 @@
package net.Indyuce.mmoitems.command.mmoitems.list;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ability.Ability;
import io.lumine.mythic.lib.mmolibcommands.api.CommandTreeNode;
public class MMAbilityCommandTreeNode extends CommandTreeNode {
public MMAbilityCommandTreeNode(CommandTreeNode parent) {
super(parent, "mmability");
}
@Override
public CommandResult execute(CommandSender sender, String[] args) {
sender.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-----------------[" + ChatColor.LIGHT_PURPLE + " MythicMobs Abilities "
+ ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "]-----------------");
sender.sendMessage(ChatColor.WHITE + "Here are all the abilities you can bind to items.");
sender.sendMessage(ChatColor.WHITE + "The values inside brackets are " + ChatColor.UNDERLINE + "modifiers" + ChatColor.WHITE
+ " which allow you to change the ability values (cooldown, damage...)");
for (Ability a : MMOItems.plugin.getAbilities().getAllMythicMobsAbilities()) {
String modFormat = ChatColor.GRAY + String.join(ChatColor.WHITE + ", " + ChatColor.GRAY, a.getModifiers());
modFormat = ChatColor.WHITE + "(" + modFormat + ChatColor.WHITE + ")";
sender.sendMessage("* " + ChatColor.LIGHT_PURPLE + a.getName() + " " + modFormat);
}
return CommandResult.SUCCESS;
}
}

View File

@ -1,29 +0,0 @@
package net.Indyuce.mmoitems.command.mmoitems.list;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ability.Ability;
import io.lumine.mythic.lib.mmolibcommands.api.CommandTreeNode;
public class TPAbilityCommandTreeNode extends CommandTreeNode {
public TPAbilityCommandTreeNode(CommandTreeNode parent) {
super(parent, "tpability");
}
@Override
public CommandResult execute(CommandSender sender, String[] args) {
sender.sendMessage(ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "-----------------[" + ChatColor.LIGHT_PURPLE + " Third Party Abilities "
+ ChatColor.DARK_GRAY + "" + ChatColor.STRIKETHROUGH + "]-----------------");
sender.sendMessage(ChatColor.WHITE + "Here are all the abilities you can bind to items.");
sender.sendMessage(ChatColor.WHITE + "The values inside brackets are " + ChatColor.UNDERLINE + "modifiers" + ChatColor.WHITE
+ " which allow you to change the ability values (cooldown, damage...)");
for (Ability a : MMOItems.plugin.getAbilities().getAllThirdPartyAbilities()) {
String modFormat = ChatColor.GRAY + String.join(ChatColor.WHITE + ", " + ChatColor.GRAY, a.getModifiers());
modFormat = ChatColor.WHITE + "(" + modFormat + ChatColor.WHITE + ")";
sender.sendMessage("* " + ChatColor.LIGHT_PURPLE + a.getName() + " " + modFormat);
}
return CommandResult.SUCCESS;
}
}

View File

@ -1,5 +1,13 @@
package net.Indyuce.mmoitems.manager;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ability.Ability;
import net.Indyuce.mmoitems.comp.mythicmobs.MythicMobsAbility;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.Listener;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
@ -10,28 +18,11 @@ import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ability.Ability;
import net.Indyuce.mmoitems.comp.mythicmobs.MythicMobsAbility;
@SuppressWarnings("unused")
public class AbilityManager {
// All abilities
private final Map<String, Ability> abilities = new HashMap<>();
// Abilities from MMOItems
private final Map<String, Ability> miAbilities = new HashMap<>();
// Abilities from MythicMobs
private final Map<String, Ability> mmAbilities = new HashMap<>();
// Abilities from ThirdParty plugins
private final Map<String, Ability> tpAbilities = new HashMap<>();
private boolean registrationIsDone = false;
private boolean registration = true;
public Ability getAbility(String id) {
return abilities.get(id);
@ -42,119 +33,54 @@ public class AbilityManager {
}
/**
* @deprecated use {@link #getAllAbilities()} instead.
* Currently returns the same thing but is a better more futureproof name.
* @return Collection of all active abilities
*/
@Deprecated
public Collection<Ability> getAll() {
return abilities.values();
}
/**
* @return Returns all known abilities, both from MI, MM and Third Party plugins.
*/
public Collection<Ability> getAllAbilities() {
return abilities.values();
}
/**
* @return Returns all known MMOITEMS abilities.
*/
public Collection<Ability> getAllMMOItemsAbilities() {
return miAbilities.values();
}
/**
* @return Returns all known MYTHICMOBS abilities.
*/
public Collection<Ability> getAllMythicMobsAbilities() {
return mmAbilities.values();
}
/**
* @return Returns all known THIRD PARTY abilities.
*/
public Collection<Ability> getAllThirdPartyAbilities() {
return tpAbilities.values();
}
/**
* Add a custom ability into MMOItems.
* Used for Third Party abilities only.
*
* @param ability - A class that extends {@link Ability}
*/
public void registerAbility(Ability ability) {
if (registerAbility(ability, false, true))
MMOItems.plugin.getLogger().log(Level.INFO, "Loaded third party ability: " + ability.getName() + " from " + JavaPlugin.getProvidingPlugin(ability.getClass()).getName() + ".");
}
/**
* Add custom abilities into MMOItems.
* Used for Third Party abilities only.
* Same as {@link #registerAbility(Ability ability)}
* Add multiple abilities at the same time
* but for multiple abilities.
*
* @param abilities - Refer to {@link #registerAbility(Ability ability)}
*/
public void registerAbilities(Ability... abilities) {
int count = 0;
for (Ability ability : abilities) {
if (!registerAbility(ability, false, true))
continue;
count++;
}
MMOItems.plugin.getLogger().log(Level.INFO, "Loaded " + count + " third party abilities from " + JavaPlugin.getProvidingPlugin(abilities[0].getClass()).getName() + ".");
for (Ability ability : abilities)
registerAbility(ability);
}
/**
* Used only internally.
* Registers an ability in MMOItems. This must be called before MMOItems enables,
* therefore either using a loadbefore of MMOItems and while the plugin enables,
* or using a dependency and usign #onLoad().
* <p>
* This method does NOT register listeners.
* <p>
* Throws an IAE if anything goes wrong.
*
* @param ability Ability to register
*/
protected boolean registerAbility(Ability ability, boolean fromMM, boolean fromTP) {
if (registrationIsDone && !fromTP) {
MMOItems.plugin.getLogger().log(Level.WARNING,
"Failed attempt to register ability " + ability.getID() + ". Make sure abilities are registered when MI is loading.");
return false;
}
if (!ability.isEnabled()) {
MMOItems.plugin.getLogger().log(Level.WARNING, "Cannot register disabled ability " + ability.getID() + ".");
return false;
}
if (hasAbility(ability.getID())) {
MMOItems.plugin.getLogger().log(Level.WARNING, "Ability " + ability.getID() + " is already registered!");
return false;
}
if (ability instanceof Listener)
Bukkit.getPluginManager().registerEvents((Listener) ability, MMOItems.plugin);
// Add to MM/MI or TP ability list
if (fromTP)
tpAbilities.put(ability.getID(), ability);
else if (fromMM)
mmAbilities.put(ability.getID(), ability);
else
miAbilities.put(ability.getID(), ability);
public void registerAbility(Ability ability) {
Validate.isTrue(registration, "Ability registration is disabled");
Validate.isTrue(!hasAbility(ability.getID()), "An ability is already registered with the same ID");
// Add to all ability list
abilities.put(ability.getID(), ability);
return true;
}
public void initialize() {
// Load MMOItems abilities
public void loadPluginAbilities() {
// Load MMOItems default abilities
try {
JarFile file = new JarFile(MMOItems.plugin.getJarFile());
for (Enumeration<JarEntry> enu = file.entries(); enu.hasMoreElements();) {
for (Enumeration<JarEntry> enu = file.entries(); enu.hasMoreElements(); ) {
String name = enu.nextElement().getName().replace("/", ".");
if (!name.contains("$") && name.endsWith(".class") && name.startsWith("net.Indyuce.mmoitems.ability.")) {
Ability ability = (Ability) Class.forName(name.substring(0, name.length() - 6)).newInstance();
if (ability.isEnabled())
registerAbility(ability, false, false);
registerAbility(ability);
if (ability instanceof Listener)
Bukkit.getPluginManager().registerEvents((Listener) ability, MMOItems.plugin);
}
}
file.close();
@ -163,26 +89,25 @@ public class AbilityManager {
}
File mythicMobs = new File(MMOItems.plugin.getDataFolder() + "/dynamic/mythic-mobs-abilities");
if (!mythicMobs.exists())
if(!mythicMobs.mkdirs())
if (!mythicMobs.exists() && !mythicMobs.mkdirs())
MMOItems.plugin.getLogger().warning("Failed DIR generation!");
// Load MythicMobs addon skills
if (Bukkit.getPluginManager().getPlugin("MythicMobs") != null) {
int count = 0;
for (File file : mythicMobs.listFiles()) {
for (File file : mythicMobs.listFiles())
try {
registerAbility(new MythicMobsAbility(file.getName().substring(0, file.getName().length() - 4),
YamlConfiguration.loadConfiguration(file)), true, false);
registerAbility(new MythicMobsAbility(file.getName().substring(0, file.getName().length() - 4), YamlConfiguration.loadConfiguration(file)));
count++;
} catch (IllegalArgumentException exception) {
MMOItems.plugin.getLogger().log(Level.WARNING, "Could not load ability from " + file.getName() + ": " + exception.getMessage());
}
}
if (count > 0)
MMOItems.plugin.getLogger().log(Level.INFO, "Loaded " + count + " extra MythicMobs abilities");
}
registrationIsDone = true;
// Finally disable ability registration
registration = false;
}
}

View File

@ -130,7 +130,7 @@ public class ConfigManager implements Reloadable {
messages.save();
ConfigFile abilities = new ConfigFile("/language", "abilities");
for (Ability ability : MMOItems.plugin.getAbilities().getAllAbilities()) {
for (Ability ability : MMOItems.plugin.getAbilities().getAll()) {
String path = ability.getLowerCaseID();
if (!abilities.getConfig().getKeys(true).contains("ability." + path))
abilities.getConfig().set("ability." + path, ability.getName());