diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/ConfigManager.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/ConfigManager.java index 25709a0f..074d818b 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/ConfigManager.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/ConfigManager.java @@ -50,7 +50,6 @@ public class ConfigManager implements Reloadable { private static final String[] languages = {"french", "chinese", "spanish", "russian", "polish"}; public ConfigManager() { - mkdir("layouts"); mkdir("item"); mkdir("language"); @@ -310,6 +309,7 @@ public class ConfigManager implements Reloadable { GEN_TEMPLATES("", "gen-templates"), UPGRADE_TEMPLATES("", "upgrade-templates"), EXAMPLE_MODIFIERS("modifiers", "example-modifiers"), + CUSTOM_STATS("", "custom-stats"), // Default language files -> /MMOItems/language LORE_FORMAT("language", "lore-format"), diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/StatManager.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/StatManager.java index aaac421e..0961ba27 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/StatManager.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/manager/StatManager.java @@ -4,11 +4,17 @@ import io.lumine.mythic.lib.MythicLib; import io.lumine.mythic.lib.element.Element; import net.Indyuce.mmoitems.ItemStats; import net.Indyuce.mmoitems.MMOItems; +import net.Indyuce.mmoitems.api.ConfigFile; import net.Indyuce.mmoitems.api.Type; import net.Indyuce.mmoitems.stat.type.*; import net.Indyuce.mmoitems.util.ElementStatType; +import org.apache.commons.lang3.Validate; +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.jetbrains.annotations.NotNull; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Modifier; import java.util.*; import java.util.logging.Level; @@ -36,8 +42,11 @@ public class StatManager { if (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers()) && field.get(null) instanceof ItemStat) register((ItemStat) field.get(null)); } catch (IllegalArgumentException | IllegalAccessException exception) { - MMOItems.plugin.getLogger().log(Level.WARNING, "Couldn't register stat called '%s'".formatted(field.getName()), exception.getMessage()); + MMOItems.plugin.getLogger().log(Level.WARNING, String.format("Couldn't register stat called '%s'", field.getName()), exception.getMessage()); } + + // Custom stats + loadCustom(); } /** @@ -52,6 +61,25 @@ public class StatManager { // Register elemental stats loadElements(); + + // Register custom stats + loadCustom(); + } + + /** + * Load custom stats + */ + public void loadCustom() { + ConfigManager.DefaultFile.CUSTOM_STATS.checkFile(); + ConfigFile config = new ConfigFile("custom-stats"); + ConfigurationSection section = config.getConfig().getConfigurationSection("custom-stats"); + Validate.notNull(section, "Custom stats section is null"); + section.getKeys(true) + .stream() + .filter(section::isConfigurationSection) + .map(section::getConfigurationSection) + .filter(Objects::nonNull) + .forEach(this::registerCustomStat); } /** @@ -170,4 +198,50 @@ public class StatManager { .filter(stat::isCompatible) .forEach(type -> type.getAvailableStats().add(stat)); } + + private void registerCustomStat(@NotNull ConfigurationSection section) { + final String name = section.getString("name"); + final String type = section.getString("type"); + + Validate.notNull(section, "Cannot register a custom stat from a null section"); + Validate.notNull(name, "Cannot register a custom stat without a name"); + Validate.notNull(type, "Cannot register a custom stat without a type"); + + Class> statClass; + switch (type.toLowerCase()) { + case "double": + statClass = DoubleStat.class; + break; + case "boolean": + statClass = BooleanStat.class; + break; + case "text": + statClass = StringStat.class; + break; + case "text-list": + statClass = StringListStat.class; + break; + default: + throw new RuntimeException("Cannot register a custom stat of type " + type); + } + + final String statId = String.format("custom_%s", name.replace(" ", "_")).toUpperCase(); + + // Lore + String[] lore = new String[0]; + if (section.isList("lore")) + lore = section.getStringList("lore").toArray(new String[]{}); + else if (section.isString("lore")) + lore = new String[]{section.getString("lore")}; + + // Create a new stat instance + try { + ItemStat stat = statClass.getConstructor(String.class, Material.class, String.class, String[].class, String[].class, Material[].class) + .newInstance(statId, Material.PAPER, name, lore, new String[]{"!miscellaneous", "!block", "all"}, new Material[0]); + register(stat); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | + NoSuchMethodException e) { + throw new RuntimeException("Unable to create a custom stat of type " + type, e); + } + } } diff --git a/MMOItems-Dist/src/main/resources/default/custom-stats.yml b/MMOItems-Dist/src/main/resources/default/custom-stats.yml new file mode 100644 index 00000000..8bc37deb --- /dev/null +++ b/MMOItems-Dist/src/main/resources/default/custom-stats.yml @@ -0,0 +1,22 @@ +# Add as many custom stats as you want below +# - +# For each custom stat you add, you must also add a corresponding +# entry in the lore-format.yml and stats.yml files +custom-stats: + + # The key doesn't really matter, although it must be unique + # The lore for each stat can be defined in language/stats.yml + 1: + # The name will be used in the edition GUI and also for the stat id + # Format: "MyLuck" will be replaced with "CUSTOM_MYLUCK" + name: "MyLuck" + + # Allowed stats type: + # - double (numbers) + # - text (Will not work as placeholder. You have get this value from ntb tag of the item with your custom plugin) + type: "double" + + # The lore must be a list of strings + lore: + - "This is a test line #1" + - "This is a test line #2" \ No newline at end of file diff --git a/MMOItems-Dist/src/main/resources/default/language/lore-format.yml b/MMOItems-Dist/src/main/resources/default/language/lore-format.yml index 7396a44f..3c3b2058 100644 --- a/MMOItems-Dist/src/main/resources/default/language/lore-format.yml +++ b/MMOItems-Dist/src/main/resources/default/language/lore-format.yml @@ -30,6 +30,7 @@ lore-format: - '#profession-woodcutting#' # - '{bar}&8&m--------&f&l &nStatistics&8 &m-------------' - '{bar}' + - '#custom-myluck#' - '#can-identify#' - '#can-deconstruct#' - '#can-deskin#' diff --git a/MMOItems-Dist/src/main/resources/default/language/stats.yml b/MMOItems-Dist/src/main/resources/default/language/stats.yml index c229f61f..402883fb 100644 --- a/MMOItems-Dist/src/main/resources/default/language/stats.yml +++ b/MMOItems-Dist/src/main/resources/default/language/stats.yml @@ -145,4 +145,7 @@ elemental-damage: '{color}{icon}&7 {value} {element} Damage' elemental-damage-percent: '{color}{icon}&7 +{value}% {element} Damage' elemental-defense: '{color}{icon}&7 {value} {element} Defense' elemental-defense-percent: '{color}{icon}&7 +{value}% {element} Defense' -elemental-weakness: '{color}{icon}&7 {value}% {element} Weakness' \ No newline at end of file +elemental-weakness: '{color}{icon}&7 {value}% {element} Weakness' + +# Custom stats +custom-myluck: '&3 &7■ Luck: &f{value}' \ No newline at end of file diff --git a/MMOItems-Dist/src/main/resources/language/chinese/stats.yml b/MMOItems-Dist/src/main/resources/language/chinese/stats.yml index 4a85b463..45624366 100644 --- a/MMOItems-Dist/src/main/resources/language/chinese/stats.yml +++ b/MMOItems-Dist/src/main/resources/language/chinese/stats.yml @@ -146,3 +146,6 @@ elemental-damage-percent: '{color}{icon}&7 +{value}% {element} Damage' elemental-defense: '{color}{icon}&7 {value} {element} Defense' elemental-defense-percent: '{color}{icon}&7 +{value}% {element} Defense' elemental-weakness: '{color}{icon}&7 {value}% {element} Weakness' + +# Custom stats +custom-myluck: '&3 &7■ 机会: &f{value}' \ No newline at end of file diff --git a/MMOItems-Dist/src/main/resources/language/french/stats.yml b/MMOItems-Dist/src/main/resources/language/french/stats.yml index 57cb6902..db7545bd 100644 --- a/MMOItems-Dist/src/main/resources/language/french/stats.yml +++ b/MMOItems-Dist/src/main/resources/language/french/stats.yml @@ -146,3 +146,6 @@ elemental-damage-percent: '{color}{icon}&7 +{value}% {element} Damage' elemental-defense: '{color}{icon}&7 {value} {element} Defense' elemental-defense-percent: '{color}{icon}&7 +{value}% {element} Defense' elemental-weakness: '{color}{icon}&7 {value}% {element} Weakness' + +# Custom stats +custom-myluck: '&3 &7■ Chance: &f{value}' \ No newline at end of file diff --git a/MMOItems-Dist/src/main/resources/language/polish/stats.yml b/MMOItems-Dist/src/main/resources/language/polish/stats.yml index eac09883..83960763 100644 --- a/MMOItems-Dist/src/main/resources/language/polish/stats.yml +++ b/MMOItems-Dist/src/main/resources/language/polish/stats.yml @@ -146,3 +146,6 @@ elemental-damage-percent: '{color}{icon}&7 +{value}% {element} Damage' elemental-defense: '{color}{icon}&7 {value} {element} Defense' elemental-defense-percent: '{color}{icon}&7 +{value}% {element} Defense' elemental-weakness: '{color}{icon}&7 {value}% {element} Weakness' + +# Custom stats +custom-myluck: '&3 &7■ Szansa: &f{value}' \ No newline at end of file diff --git a/MMOItems-Dist/src/main/resources/language/russian/stats.yml b/MMOItems-Dist/src/main/resources/language/russian/stats.yml index 2f3a3eab..93c68801 100644 --- a/MMOItems-Dist/src/main/resources/language/russian/stats.yml +++ b/MMOItems-Dist/src/main/resources/language/russian/stats.yml @@ -146,3 +146,6 @@ elemental-damage-percent: '{color}{icon}&7 +{value}% {element} Damage' elemental-defense: '{color}{icon}&7 {value} {element} Defense' elemental-defense-percent: '{color}{icon}&7 +{value}% {element} Defense' elemental-weakness: '{color}{icon}&7 {value}% {element} Weakness' + +# Custom stats +custom-myluck: '&3 &7■ Шанс: &f{value}' \ No newline at end of file diff --git a/MMOItems-Dist/src/main/resources/language/spanish/stats.yml b/MMOItems-Dist/src/main/resources/language/spanish/stats.yml index 788a96f6..f7c9bc4f 100644 --- a/MMOItems-Dist/src/main/resources/language/spanish/stats.yml +++ b/MMOItems-Dist/src/main/resources/language/spanish/stats.yml @@ -146,3 +146,6 @@ elemental-damage-percent: '{color}{icon}&7 +{value}% {element} Damage' elemental-defense: '{color}{icon}&7 {value} {element} Defense' elemental-defense-percent: '{color}{icon}&7 +{value}% {element} Defense' elemental-weakness: '{color}{icon}&7 {value}% {element} Weakness' + +# Custom stats +custom-myluck: '&3 &7■ Oportunidad: &f{value}' \ No newline at end of file