Skip to content

Commit

Permalink
Relocate and create Configurate API wrapper (#296)
Browse files Browse the repository at this point in the history
- ConfigurationNode has been replaced with the ConfigNode wrapper class in api and api-bukkit
- Configurate is no longer included in the api as a dependency
  • Loading branch information
Archy-X authored Jul 21, 2024
1 parent a197e77 commit 3d56d2d
Show file tree
Hide file tree
Showing 42 changed files with 668 additions and 126 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.aurelium.auraskills.api.item;

import dev.aurelium.auraskills.api.config.ConfigNode;
import dev.aurelium.auraskills.api.skill.Multiplier;
import dev.aurelium.auraskills.api.skill.Skill;
import dev.aurelium.auraskills.api.skill.Skills;
Expand Down Expand Up @@ -193,17 +194,29 @@ public interface ItemManager {
* @param config the Configurate ConfigurationNode to parse keys from, should be a mapping
* @return the parsed ItemStack
*/
ItemStack parseItem(ConfigNode config);

/**
* Use {@link #parseItem(ConfigNode)}
*/
@Deprecated
ItemStack parseItem(ConfigurationNode config);

/**
* Parses a list of ItemStack from a ConfigurationNode if it has a materials list. Only the material
* differs between each item, the amount and all meta remains the same across items. If a regular material
* string is defined, a single item will be parsed like {@link #parseItem(ConfigurationNode)} and the list
* string is defined, a single item will be parsed like {@link #parseItem(ConfigNode)} and the list
* returned will be of size 1.
*
* @param config the Configurate ConfigurationNode to parse keys from, should be a mapping
* @return a list of parsed ItemStack
*/
List<ItemStack> parseMultipleItems(ConfigNode config);

/**
* Use {@link #parseMultipleItems(ConfigNode)}
*/
@Deprecated
List<ItemStack> parseMultipleItems(ConfigurationNode config);

}
3 changes: 0 additions & 3 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ repositories {
}

dependencies {
api("org.spongepowered:configurate-yaml:4.1.2") {
exclude("org.yaml", "snakeyaml")
}
compileOnly("org.jetbrains:annotations:24.1.0")
}

Expand Down
111 changes: 111 additions & 0 deletions api/src/main/java/dev/aurelium/auraskills/api/config/ConfigNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package dev.aurelium.auraskills.api.config;

import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

/**
* An interface that copies the Configurate ConfigurationNode and is
* implemented by it under the hood. This is used in the API to allow
* AuraSkills to relocate Configurate packages while keeping API configuration
* functionality. All methods work exactly like ConfigurationNode from Configurate,
* though not all methods are available.
*/
public interface ConfigNode {

@Nullable Object key();

@Nullable ConfigNode parent();

ConfigNode node(Object... path);

ConfigNode node(Iterable<?> path);

boolean hasChild(Object... path);

boolean hasChild(Iterable<?> path);

boolean virtual();

boolean isNull();

boolean isList();

boolean isMap();

boolean empty();

List<? extends ConfigNode> childrenList();

Map<Object, ? extends ConfigNode> childrenMap();

<V> @Nullable V get(Class<V> type);

<V> V get(Class<V> type, V def);

<V> V get(Class<V> type, Supplier<V> defSupplier);

@Nullable Object get(Type type);

Object get(Type type, Object def);

Object get(Type type, Supplier<?> defSupplier);

<V> @Nullable List<V> getList(Class<V> type);

<V> List<V> getList(Class<V> elementType, List<V> def);

<V> List<V> getList(Class<V> elementType, Supplier<List<V>> defSupplier);

@Nullable String getString();

String getString(String def);

float getFloat();

float getFloat(float def);

double getDouble();

double getDouble(double def);

int getInt();

int getInt(int def);

long getLong();

long getLong(long def);

boolean getBoolean();

boolean getBoolean(boolean def);

ConfigNode set(@Nullable Object value);

<V> ConfigNode set(Class<V> type, @Nullable V value);

ConfigNode set(Type type, @Nullable Object value);

<V> ConfigNode setList(final Class<V> elementType, final @Nullable List<V> items);

@Nullable Object raw();

ConfigNode raw(Object value);

@Nullable Object rawScalar();

ConfigNode from(ConfigNode other);

ConfigNode mergeFrom(ConfigNode other);

boolean removeChild(Object key);

ConfigNode appendListNode();

ConfigNode copy();

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package dev.aurelium.auraskills.api.loot;

import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import dev.aurelium.auraskills.api.config.ConfigNode;

@FunctionalInterface
public interface LootParser {

Loot parse(LootParsingContext context, ConfigurationNode config) throws SerializationException;
Loot parse(LootParsingContext context, ConfigNode config);

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package dev.aurelium.auraskills.api.loot;

import org.spongepowered.configurate.ConfigurationNode;
import dev.aurelium.auraskills.api.config.ConfigNode;

public interface LootParsingContext {

LootValues parseValues(ConfigurationNode config);
LootValues parseValues(ConfigNode config);

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package dev.aurelium.auraskills.api.source;

import dev.aurelium.auraskills.api.AuraSkillsApi;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import dev.aurelium.auraskills.api.config.ConfigNode;

import java.lang.reflect.Array;
import java.util.List;
Expand All @@ -19,14 +18,14 @@ public AuraSkillsApi getApi() {
return api;
}

public ConfigurationNode required(ConfigurationNode node, String path) {
public ConfigNode required(ConfigNode node, String path) {
if (!node.hasChild(path)) {
throw new IllegalArgumentException("Missing required field: " + path);
}
return node.node(path);
}

public <V> V[] requiredPluralizedArray(String key, ConfigurationNode source, Class<V> type) {
public <V> V[] requiredPluralizedArray(String key, ConfigNode source, Class<V> type) {
V[] array = pluralizedArray(key, source, type);
if (array == null) {
throw new IllegalArgumentException("Missing required field '" + key + "' or list '" + key + "s' of type " + type.getName());
Expand All @@ -35,14 +34,14 @@ public <V> V[] requiredPluralizedArray(String key, ConfigurationNode source, Cla
}

@SuppressWarnings("unchecked")
public <V> V[] pluralizedArray(String key, ConfigurationNode source, Class<V> type) {
public <V> V[] pluralizedArray(String key, ConfigNode source, Class<V> type) {
V[] array;
String pluralKey = api.getMessageManager().toPluralForm(key); // Convert key to plural
if (source.hasChild(pluralKey)) {
List<V> list;
try {
list = source.node(pluralKey).getList(type);
} catch (SerializationException e) {
} catch (RuntimeException e) {
throw new IllegalArgumentException("Failed to convert value of key " + pluralKey + " to a list of type " + type.getName());
}
if (list != null) {
Expand All @@ -54,7 +53,7 @@ public <V> V[] pluralizedArray(String key, ConfigurationNode source, Class<V> ty
array = (V[]) Array.newInstance(type, 1);
try {
array[0] = source.node(key).get(type);
} catch (SerializationException e) {
} catch (RuntimeException e) {
throw new IllegalArgumentException("Failed to convert value of key " + key + " to type " + type.getName());
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package dev.aurelium.auraskills.api.source;

import dev.aurelium.auraskills.api.AuraSkillsApi;
import dev.aurelium.auraskills.api.config.ConfigNode;
import dev.aurelium.auraskills.api.registry.NamespacedId;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.configurate.ConfigurationNode;

public class SourceContext extends BaseContext {

private final SourceType sourceType;
private final String sourceName;
protected final SourceType sourceType;
protected final String sourceName;

public SourceContext(AuraSkillsApi api, SourceType sourceType, String sourceName) {
super(api);
Expand All @@ -24,7 +24,7 @@ public String getSourceName() {
return sourceName;
}

public SourceValues parseValues(ConfigurationNode source) {
public SourceValues parseValues(ConfigNode source) {
NamespacedId id = NamespacedId.of(NamespacedId.AURASKILLS, sourceName);
double xp = source.node("xp").getDouble(0.0);
@Nullable String displayName = source.node("display_name").getString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package dev.aurelium.auraskills.api.source;

import dev.aurelium.auraskills.api.config.ConfigNode;
import org.jetbrains.annotations.ApiStatus.Internal;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.configurate.ConfigurationNode;

import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -36,6 +36,6 @@ public interface SourceManager {
String getUnitName(XpSource source, Locale locale);

@Internal
SourceIncome loadSourceIncome(ConfigurationNode source);
SourceIncome loadSourceIncome(ConfigNode source);

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package dev.aurelium.auraskills.api.source;

import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import dev.aurelium.auraskills.api.config.ConfigNode;

@FunctionalInterface
public interface UtilityParser<T> {

T parse(ConfigurationNode source, BaseContext context) throws SerializationException;
T parse(ConfigNode source, BaseContext context);

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package dev.aurelium.auraskills.api.source;

import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
import dev.aurelium.auraskills.api.config.ConfigNode;

@FunctionalInterface
public interface XpSourceParser<T> {

T parse(ConfigurationNode source, SourceContext context) throws SerializationException;
T parse(ConfigNode source, SourceContext context);

}
3 changes: 3 additions & 0 deletions bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ tasks.withType<ShadowJar> {
relocate("net.kyori", "dev.aurelium.auraskills.kyori")
relocate("com.zaxxer.hikari", "dev.aurelium.auraskills.hikari")
relocate("dev.aurelium.slate", "dev.aurelium.auraskills.slate")
relocate("org.spongepowered.configurate", "dev.aurelium.auraskills.configurate")
relocate("io.leangen.geantyref", "dev.aurelium.auraskills.geantyref")
relocate("net.querz", "dev.aurelium.auraskills.querz")
relocate("com.archyx.polyglot", "dev.aurelium.auraskills.polyglot")
relocate("org.atteo.evo.inflector", "dev.aurelium.auraskills.inflector")

exclude("acf-*.properties")

finalizedBy("copyJar")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.aurelium.auraskills.bukkit.item;

import dev.aurelium.auraskills.api.config.ConfigNode;
import dev.aurelium.auraskills.api.item.ItemFilter;
import dev.aurelium.auraskills.api.item.ItemManager;
import dev.aurelium.auraskills.api.item.ModifierType;
Expand All @@ -12,6 +13,7 @@
import dev.aurelium.auraskills.bukkit.AuraSkills;
import dev.aurelium.auraskills.bukkit.item.SkillsItem.MetaType;
import dev.aurelium.auraskills.bukkit.util.ConfigurateItemParser;
import dev.aurelium.auraskills.common.api.implementation.ApiConfigNode;
import org.bukkit.inventory.ItemStack;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.serialize.SerializationException;
Expand Down Expand Up @@ -143,11 +145,27 @@ public boolean passesFilter(ItemStack item, ItemFilter filter) {
}

@Override
public ItemStack parseItem(ConfigNode config) {
return itemParser.parseItem(((ApiConfigNode) config).getBacking());
}

@Override
@SuppressWarnings("deprecation")
public ItemStack parseItem(ConfigurationNode config) {
return itemParser.parseItem(config);
}

@Override
public List<ItemStack> parseMultipleItems(ConfigNode config) {
try {
return itemParser.parseMultipleItems(((ApiConfigNode) config).getBacking());
} catch (SerializationException e) {
throw new RuntimeException(e);
}
}

@Override
@SuppressWarnings("deprecation")
public List<ItemStack> parseMultipleItems(ConfigurationNode config) {
try {
return itemParser.parseMultipleItems(config);
Expand Down
Loading

0 comments on commit 3d56d2d

Please sign in to comment.