|
@@ -1,49 +1,62 @@
|
|
|
package me.lethunderhawk.custom.item.abstraction.definition;
|
|
package me.lethunderhawk.custom.item.abstraction.definition;
|
|
|
|
|
|
|
|
import me.lethunderhawk.custom.item.abstraction.ability.AbilityDefinition;
|
|
import me.lethunderhawk.custom.item.abstraction.ability.AbilityDefinition;
|
|
|
-import me.lethunderhawk.custom.item.abstraction.ability.AbilityTrigger;
|
|
|
|
|
import me.lethunderhawk.custom.item.abstraction.ability.CustomItemTrigger;
|
|
import me.lethunderhawk.custom.item.abstraction.ability.CustomItemTrigger;
|
|
|
import me.lethunderhawk.custom.item.abstraction.registry.TriggerRegistry;
|
|
import me.lethunderhawk.custom.item.abstraction.registry.TriggerRegistry;
|
|
|
import me.lethunderhawk.fluxapi.FluxService;
|
|
import me.lethunderhawk.fluxapi.FluxService;
|
|
|
-import me.lethunderhawk.fluxapi.util.itemdesign.LoreDesigner;
|
|
|
|
|
import me.lethunderhawk.main.BazaarFlux;
|
|
import me.lethunderhawk.main.BazaarFlux;
|
|
|
-import net.kyori.adventure.text.Component;
|
|
|
|
|
import org.bukkit.Material;
|
|
import org.bukkit.Material;
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
|
import org.bukkit.configuration.ConfigurationSection;
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
+
|
|
|
public final class ItemDefinitionLoader {
|
|
public final class ItemDefinitionLoader {
|
|
|
|
|
|
|
|
private final ItemDefinitionValidator validator;
|
|
private final ItemDefinitionValidator validator;
|
|
|
|
|
+ public static File folder;
|
|
|
|
|
+
|
|
|
|
|
|
|
|
- public ItemDefinitionLoader(ItemDefinitionValidator validator) {
|
|
|
|
|
|
|
+ private ItemDefinitionLoader(ItemDefinitionValidator validator) {
|
|
|
this.validator = validator;
|
|
this.validator = validator;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public static void init(File folderPath){
|
|
|
|
|
+ folder = folderPath;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static Map<String, ItemDefinition> loadAll(){
|
|
|
|
|
+ if(folder == null || !folder.exists()) return null;
|
|
|
|
|
+ return loadAll(folder);
|
|
|
|
|
+ }
|
|
|
/**
|
|
/**
|
|
|
* @param directory to load the files from
|
|
* @param directory to load the files from
|
|
|
* @return A {@link Map} of the item_id and the corresponding ItemDefinition
|
|
* @return A {@link Map} of the item_id and the corresponding ItemDefinition
|
|
|
*/
|
|
*/
|
|
|
|
|
+ public static Map<String, ItemDefinition> loadAll(File directory) {
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- public Map<String, ItemDefinition> loadAll(File directory) {
|
|
|
|
|
Map<String, ItemDefinition> definitions = new HashMap<>();
|
|
Map<String, ItemDefinition> definitions = new HashMap<>();
|
|
|
|
|
|
|
|
for (File file : Objects.requireNonNull(directory.listFiles())) {
|
|
for (File file : Objects.requireNonNull(directory.listFiles())) {
|
|
|
ItemDefinition def = load(file);
|
|
ItemDefinition def = load(file);
|
|
|
- if (definitions.containsKey(def.id())) {
|
|
|
|
|
- throw new IllegalStateException("Duplicate item id: " + def.id());
|
|
|
|
|
|
|
+ if (def == null) continue;
|
|
|
|
|
+
|
|
|
|
|
+ if (definitions.containsKey(def.getId())) {
|
|
|
|
|
+ throw new IllegalStateException("Duplicate item id: " + def.getId());
|
|
|
}
|
|
}
|
|
|
- definitions.put(def.id(), def);
|
|
|
|
|
|
|
+ definitions.put(def.getId(), def);
|
|
|
}
|
|
}
|
|
|
return Map.copyOf(definitions);
|
|
return Map.copyOf(definitions);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private ItemDefinition load(File file) {
|
|
|
|
|
|
|
+ private static ItemDefinition load(File file) {
|
|
|
|
|
+ if(file.isDirectory()) return null;
|
|
|
|
|
+
|
|
|
YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
|
|
YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
|
|
|
|
|
|
|
|
// ---------- BASIC METADATA ----------
|
|
// ---------- BASIC METADATA ----------
|
|
@@ -75,13 +88,16 @@ public final class ItemDefinitionLoader {
|
|
|
"Missing 'name' in item " + id
|
|
"Missing 'name' in item " + id
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
Material realMaterial = Material.getMaterial(material);
|
|
Material realMaterial = Material.getMaterial(material);
|
|
|
- List<Component> lore =
|
|
|
|
|
- LoreDesigner.createLore(cfg.getString("lore"), "This is a reasonable lore");
|
|
|
|
|
|
|
+
|
|
|
|
|
+ String lore =
|
|
|
|
|
+ cfg.getString("lore");
|
|
|
|
|
|
|
|
String headTexture =
|
|
String headTexture =
|
|
|
cfg.getString("head.texture", null);
|
|
cfg.getString("head.texture", null);
|
|
|
|
|
|
|
|
|
|
+ assert realMaterial != null;
|
|
|
ItemVisualDefinition visual = new ItemVisualDefinition(
|
|
ItemVisualDefinition visual = new ItemVisualDefinition(
|
|
|
realMaterial,
|
|
realMaterial,
|
|
|
name,
|
|
name,
|
|
@@ -133,7 +149,6 @@ public final class ItemDefinitionLoader {
|
|
|
List<String> triggerList = abilityCfg.getStringList("triggers");
|
|
List<String> triggerList = abilityCfg.getStringList("triggers");
|
|
|
|
|
|
|
|
if (!triggerList.isEmpty()) {
|
|
if (!triggerList.isEmpty()) {
|
|
|
-
|
|
|
|
|
for (String raw : triggerList) {
|
|
for (String raw : triggerList) {
|
|
|
try {
|
|
try {
|
|
|
TriggerRegistry.parse(raw.toUpperCase()).ifPresent(triggers::add);
|
|
TriggerRegistry.parse(raw.toUpperCase()).ifPresent(triggers::add);
|
|
@@ -143,24 +158,6 @@ public final class ItemDefinitionLoader {
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- } else {
|
|
|
|
|
-
|
|
|
|
|
- // --- Legacy format: trigger: A
|
|
|
|
|
- String singleTrigger = abilityCfg.getString("trigger");
|
|
|
|
|
-
|
|
|
|
|
- if (singleTrigger == null) {
|
|
|
|
|
- throw new IllegalStateException(
|
|
|
|
|
- "Missing ability trigger(s) in item " + id
|
|
|
|
|
- );
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- triggers.add(AbilityTrigger.valueOf(singleTrigger.toUpperCase()));
|
|
|
|
|
- } catch (IllegalArgumentException ex) {
|
|
|
|
|
- throw new IllegalStateException(
|
|
|
|
|
- "Invalid ability trigger '" + singleTrigger + "' in item " + id
|
|
|
|
|
- );
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
String handler_id =
|
|
String handler_id =
|
|
@@ -169,7 +166,8 @@ public final class ItemDefinitionLoader {
|
|
|
abilityCfg.getString("name");
|
|
abilityCfg.getString("name");
|
|
|
String abilityDescription =
|
|
String abilityDescription =
|
|
|
abilityCfg.getString("description");
|
|
abilityCfg.getString("description");
|
|
|
- boolean show_in_lore = abilityCfg.getBoolean("show_in_lore", true);
|
|
|
|
|
|
|
+ boolean show_in_lore =
|
|
|
|
|
+ abilityCfg.getBoolean("show_in_lore", true);
|
|
|
|
|
|
|
|
if (handler_id == null) {
|
|
if (handler_id == null) {
|
|
|
throw new IllegalStateException(
|
|
throw new IllegalStateException(
|
|
@@ -220,8 +218,86 @@ public final class ItemDefinitionLoader {
|
|
|
|
|
|
|
|
// ---------- VALIDATE ----------
|
|
// ---------- VALIDATE ----------
|
|
|
|
|
|
|
|
- validator.validate(definition);
|
|
|
|
|
|
|
+ //validator.validate(definition);
|
|
|
|
|
|
|
|
return definition;
|
|
return definition;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public static void save(File file, ItemDefinition def) {
|
|
|
|
|
+ YamlConfiguration cfg = new YamlConfiguration();
|
|
|
|
|
+
|
|
|
|
|
+ // ---------- BASIC METADATA ----------
|
|
|
|
|
+
|
|
|
|
|
+ cfg.set("id", def.getId());
|
|
|
|
|
+ cfg.set("version", def.getVersion());
|
|
|
|
|
+
|
|
|
|
|
+ // ---------- VISUAL DEFINITION ----------
|
|
|
|
|
+
|
|
|
|
|
+ ItemVisualDefinition visual = def.getVisual();
|
|
|
|
|
+
|
|
|
|
|
+ cfg.set("material", visual.getMaterial().name());
|
|
|
|
|
+ cfg.set("name", visual.getDisplayNameTemplate());
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ if (visual.getLoreTemplate() != null && !visual.getLoreTemplate().isEmpty()) {
|
|
|
|
|
+ String loreLines = visual.getLoreTemplate();
|
|
|
|
|
+ cfg.set("lore", loreLines);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (visual.getHeadTexture().isPresent()) {
|
|
|
|
|
+ cfg.set("head.texture", visual.getHeadTexture().get());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ---------- STATS ----------
|
|
|
|
|
+
|
|
|
|
|
+ if (!def.stats().isEmpty()) {
|
|
|
|
|
+ for (Map.Entry<String, Double> entry : def.stats().entrySet()) {
|
|
|
|
|
+ cfg.set("stats." + entry.getKey(), entry.getValue());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ---------- ABILITIES ----------
|
|
|
|
|
+
|
|
|
|
|
+ if (!def.abilities().isEmpty()) {
|
|
|
|
|
+
|
|
|
|
|
+ for (AbilityDefinition ability : def.abilities()) {
|
|
|
|
|
+
|
|
|
|
|
+ String path = "abilities." + ability.getName().toLowerCase().replace(" ", "_");
|
|
|
|
|
+
|
|
|
|
|
+ List<String> triggers = ability.getTriggers().stream()
|
|
|
|
|
+ .map(trigger -> trigger.getTriggerName().toUpperCase().replace(" ", "_"))
|
|
|
|
|
+ .toList();
|
|
|
|
|
+
|
|
|
|
|
+ cfg.set(path + ".triggers", triggers);
|
|
|
|
|
+
|
|
|
|
|
+ cfg.set(path + ".handler_id", ability.handlerId());
|
|
|
|
|
+ cfg.set(path + ".name", ability.getName());
|
|
|
|
|
+ cfg.set(path + ".description", ability.getDescriptionWithoutParams());
|
|
|
|
|
+ cfg.set(path + ".show_in_lore", ability.isShownInLore());
|
|
|
|
|
+
|
|
|
|
|
+ // Params
|
|
|
|
|
+ if (ability.getParams() != null && !ability.getParams().isEmpty()) {
|
|
|
|
|
+ for (Map.Entry<String, String> param : ability.getParams().entrySet()) {
|
|
|
|
|
+ cfg.set(path + ".params." + param.getKey(), param.getValue());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ---------- SAVE FILE ----------
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ cfg.save(file + "/"+ def.getId() + ".yml");
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ throw new RuntimeException("Failed to save item definition to " + file.getName(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static void saveAll(Iterable<? extends ItemDefinition> all) {
|
|
|
|
|
+ for(ItemDefinition def : all) {
|
|
|
|
|
+ save(folder, def);
|
|
|
|
|
+ }
|
|
|
|
|
+ TriggerRegistry.register(null, "ad");
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|