mirror of
https://gitlab.com/phoenix-dvpmt/mmoitems.git
synced 2025-01-03 06:37:47 +01:00
Custom stats
This commit is contained in:
parent
a793b80211
commit
85ebca48dc
@ -50,7 +50,6 @@ public class ConfigManager implements Reloadable {
|
|||||||
private static final String[] languages = {"french", "chinese", "spanish", "russian", "polish"};
|
private static final String[] languages = {"french", "chinese", "spanish", "russian", "polish"};
|
||||||
|
|
||||||
public ConfigManager() {
|
public ConfigManager() {
|
||||||
|
|
||||||
mkdir("layouts");
|
mkdir("layouts");
|
||||||
mkdir("item");
|
mkdir("item");
|
||||||
mkdir("language");
|
mkdir("language");
|
||||||
@ -310,6 +309,7 @@ public class ConfigManager implements Reloadable {
|
|||||||
GEN_TEMPLATES("", "gen-templates"),
|
GEN_TEMPLATES("", "gen-templates"),
|
||||||
UPGRADE_TEMPLATES("", "upgrade-templates"),
|
UPGRADE_TEMPLATES("", "upgrade-templates"),
|
||||||
EXAMPLE_MODIFIERS("modifiers", "example-modifiers"),
|
EXAMPLE_MODIFIERS("modifiers", "example-modifiers"),
|
||||||
|
CUSTOM_STATS("", "custom-stats"),
|
||||||
|
|
||||||
// Default language files -> /MMOItems/language
|
// Default language files -> /MMOItems/language
|
||||||
LORE_FORMAT("language", "lore-format"),
|
LORE_FORMAT("language", "lore-format"),
|
||||||
|
@ -4,9 +4,14 @@ import io.lumine.mythic.lib.MythicLib;
|
|||||||
import io.lumine.mythic.lib.element.Element;
|
import io.lumine.mythic.lib.element.Element;
|
||||||
import net.Indyuce.mmoitems.ItemStats;
|
import net.Indyuce.mmoitems.ItemStats;
|
||||||
import net.Indyuce.mmoitems.MMOItems;
|
import net.Indyuce.mmoitems.MMOItems;
|
||||||
|
import net.Indyuce.mmoitems.api.ConfigFile;
|
||||||
import net.Indyuce.mmoitems.api.Type;
|
import net.Indyuce.mmoitems.api.Type;
|
||||||
import net.Indyuce.mmoitems.stat.type.*;
|
import net.Indyuce.mmoitems.stat.type.*;
|
||||||
import net.Indyuce.mmoitems.util.ElementStatType;
|
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.Field;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
@ -36,8 +41,20 @@ public class StatManager {
|
|||||||
if (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers()) && field.get(null) instanceof ItemStat)
|
if (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers()) && field.get(null) instanceof ItemStat)
|
||||||
register((ItemStat<?, ?>) field.get(null));
|
register((ItemStat<?, ?>) field.get(null));
|
||||||
} catch (IllegalArgumentException | IllegalAccessException exception) {
|
} 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
|
||||||
|
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 +187,34 @@ public class StatManager {
|
|||||||
.filter(stat::isCompatible)
|
.filter(stat::isCompatible)
|
||||||
.forEach(type -> type.getAvailableStats().add(stat));
|
.forEach(type -> type.getAvailableStats().add(stat));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public 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");
|
||||||
|
|
||||||
|
final String statId = String.format("custom_%s", name.replace(" ", "_")).toUpperCase();
|
||||||
|
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")};
|
||||||
|
|
||||||
|
switch (type.toLowerCase()) {
|
||||||
|
case "double":
|
||||||
|
register(new DoubleStat(statId, Material.PAPER, name, lore));
|
||||||
|
break;
|
||||||
|
case "boolean":
|
||||||
|
register(new BooleanStat(statId, Material.PAPER, name, lore, new String[]{"!miscellaneous", "!block", "all"}));
|
||||||
|
break;
|
||||||
|
case "text":
|
||||||
|
register(new StringStat(statId, Material.PAPER, name, lore, new String[]{"!miscellaneous", "!block", "all"}));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new RuntimeException("Cannot register a custom stat of type " + type);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
custom-stats:
|
||||||
|
1:
|
||||||
|
name: "MyLuck"
|
||||||
|
type: "double" # can be double, boolean or text
|
||||||
|
2:
|
||||||
|
name: "MyExpMultipler"
|
||||||
|
type: "double"
|
||||||
|
lore:
|
||||||
|
- "This is a test lore"
|
Loading…
Reference in New Issue
Block a user