Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5c29859d49 | ||
|
|
02510127d1 | ||
|
|
f6b5c8f7e0 | ||
|
|
dbfef3094b | ||
|
|
b5c3af752c | ||
|
|
d0366b39e2 | ||
|
|
ed5390d2b5 |
@@ -19,7 +19,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
|
|||||||
fResult.setAccessible(true);
|
fResult.setAccessible(true);
|
||||||
|
|
||||||
ItemStack result = merchantRecipe.getResult();
|
ItemStack result = merchantRecipe.getResult();
|
||||||
Display.displayAndFinalize(result);
|
Display.display(result);
|
||||||
fResult.set(merchantRecipe, result);
|
fResult.set(merchantRecipe, result);
|
||||||
|
|
||||||
// Get NMS MerchantRecipe from CraftMerchantRecipe
|
// Get NMS MerchantRecipe from CraftMerchantRecipe
|
||||||
@@ -31,7 +31,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
|
|||||||
fSelling.setAccessible(true);
|
fSelling.setAccessible(true);
|
||||||
|
|
||||||
ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem);
|
ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem);
|
||||||
Display.displayAndFinalize(selling);
|
Display.display(selling);
|
||||||
fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
|
fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
|
||||||
} catch (IllegalAccessException | NoSuchFieldException e) {
|
} catch (IllegalAccessException | NoSuchFieldException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
|
|||||||
Field fResult = MerchantRecipe.class.getDeclaredField("result");
|
Field fResult = MerchantRecipe.class.getDeclaredField("result");
|
||||||
fResult.setAccessible(true);
|
fResult.setAccessible(true);
|
||||||
ItemStack result = merchantRecipe.getResult();
|
ItemStack result = merchantRecipe.getResult();
|
||||||
Display.displayAndFinalize(result);
|
Display.display(result);
|
||||||
fResult.set(merchantRecipe, result);
|
fResult.set(merchantRecipe, result);
|
||||||
|
|
||||||
// Get NMS MerchantRecipe from CraftMerchantRecipe
|
// Get NMS MerchantRecipe from CraftMerchantRecipe
|
||||||
@@ -30,7 +30,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
|
|||||||
fSelling.setAccessible(true);
|
fSelling.setAccessible(true);
|
||||||
|
|
||||||
ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem);
|
ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem);
|
||||||
Display.displayAndFinalize(selling);
|
Display.display(selling);
|
||||||
|
|
||||||
fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
|
fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
|
||||||
} catch (IllegalAccessException | NoSuchFieldException e) {
|
} catch (IllegalAccessException | NoSuchFieldException e) {
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
|
|||||||
Field fResult = MerchantRecipe.class.getDeclaredField("result");
|
Field fResult = MerchantRecipe.class.getDeclaredField("result");
|
||||||
fResult.setAccessible(true);
|
fResult.setAccessible(true);
|
||||||
ItemStack result = merchantRecipe.getResult();
|
ItemStack result = merchantRecipe.getResult();
|
||||||
Display.displayAndFinalize(result);
|
Display.display(result);
|
||||||
fResult.set(merchantRecipe, result);
|
fResult.set(merchantRecipe, result);
|
||||||
|
|
||||||
// Get NMS MerchantRecipe from CraftMerchantRecipe
|
// Get NMS MerchantRecipe from CraftMerchantRecipe
|
||||||
@@ -30,7 +30,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
|
|||||||
fSelling.setAccessible(true);
|
fSelling.setAccessible(true);
|
||||||
|
|
||||||
ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem);
|
ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem);
|
||||||
Display.displayAndFinalize(selling);
|
Display.display(selling);
|
||||||
|
|
||||||
fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
|
fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
|
||||||
} catch (IllegalAccessException | NoSuchFieldException e) {
|
} catch (IllegalAccessException | NoSuchFieldException e) {
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
|
|||||||
Field fResult = MerchantRecipe.class.getDeclaredField("result");
|
Field fResult = MerchantRecipe.class.getDeclaredField("result");
|
||||||
fResult.setAccessible(true);
|
fResult.setAccessible(true);
|
||||||
ItemStack result = merchantRecipe.getResult();
|
ItemStack result = merchantRecipe.getResult();
|
||||||
Display.displayAndFinalize(result);
|
Display.display(result);
|
||||||
fResult.set(merchantRecipe, result);
|
fResult.set(merchantRecipe, result);
|
||||||
|
|
||||||
// Get NMS MerchantRecipe from CraftMerchantRecipe
|
// Get NMS MerchantRecipe from CraftMerchantRecipe
|
||||||
@@ -30,7 +30,7 @@ public final class VillagerTrade implements VillagerTradeProxy {
|
|||||||
fSelling.setAccessible(true);
|
fSelling.setAccessible(true);
|
||||||
|
|
||||||
ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem);
|
ItemStack selling = CraftItemStack.asBukkitCopy(handle.sellingItem);
|
||||||
Display.displayAndFinalize(selling);
|
Display.display(selling);
|
||||||
|
|
||||||
fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
|
fSelling.set(handle, CraftItemStack.asNMSCopy(selling));
|
||||||
} catch (IllegalAccessException | NoSuchFieldException e) {
|
} catch (IllegalAccessException | NoSuchFieldException e) {
|
||||||
|
|||||||
@@ -1,29 +1,10 @@
|
|||||||
package com.willfp.eco.util.config;
|
package com.willfp.eco.util.config;
|
||||||
|
|
||||||
|
import com.willfp.eco.util.config.internal.AbstractUpdatableConfig;
|
||||||
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
|
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
|
||||||
import org.bukkit.configuration.InvalidConfigurationException;
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
public abstract class BaseConfig extends AbstractUpdatableConfig {
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public abstract class BaseConfig extends StaticBaseConfig {
|
|
||||||
/**
|
|
||||||
* Whether keys not in the base config should be removed on update.
|
|
||||||
*/
|
|
||||||
private final boolean removeUnused;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* List of blacklisted update keys.
|
|
||||||
*/
|
|
||||||
private final List<String> updateBlacklist;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Config implementation for configs present in the plugin's base directory (eg config.yml, lang.yml).
|
* Config implementation for configs present in the plugin's base directory (eg config.yml, lang.yml).
|
||||||
@@ -38,57 +19,22 @@ public abstract class BaseConfig extends StaticBaseConfig {
|
|||||||
protected BaseConfig(@NotNull final String configName,
|
protected BaseConfig(@NotNull final String configName,
|
||||||
final boolean removeUnused,
|
final boolean removeUnused,
|
||||||
@NotNull final AbstractEcoPlugin plugin,
|
@NotNull final AbstractEcoPlugin plugin,
|
||||||
@Nullable final String... updateBlacklist) {
|
@NotNull final String... updateBlacklist) {
|
||||||
super(configName, plugin);
|
super(configName, plugin, "", plugin.getClass(), removeUnused, updateBlacklist);
|
||||||
this.removeUnused = removeUnused;
|
|
||||||
this.updateBlacklist = Arrays.asList(updateBlacklist);
|
|
||||||
|
|
||||||
update();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the config.
|
* Config implementation for configs present in the plugin's base directory (eg config.yml, lang.yml).
|
||||||
* <p>
|
* <p>
|
||||||
* Writes missing values, however removes comments due to how configs are stored internally in bukkit.
|
* Automatically updates.
|
||||||
|
*
|
||||||
|
* @param configName The name of the config
|
||||||
|
* @param removeUnused Whether keys not present in the default config should be removed on update.
|
||||||
|
* @param plugin The plugin.
|
||||||
*/
|
*/
|
||||||
public void update() {
|
protected BaseConfig(@NotNull final String configName,
|
||||||
try {
|
final boolean removeUnused,
|
||||||
config.load(this.getConfigFile());
|
@NotNull final AbstractEcoPlugin plugin) {
|
||||||
|
super(configName, plugin, "", plugin.getClass(), removeUnused, "");
|
||||||
InputStream newIn = this.getPlugin().getResource(this.getName());
|
|
||||||
if (newIn == null) {
|
|
||||||
this.getPlugin().getLog().error(this.getName() + " is null?");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(newIn, StandardCharsets.UTF_8));
|
|
||||||
YamlConfiguration newConfig = new YamlConfiguration();
|
|
||||||
newConfig.load(reader);
|
|
||||||
|
|
||||||
if (newConfig.getKeys(true).equals(config.getKeys(true))) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
newConfig.getKeys(true).forEach((s -> {
|
|
||||||
if (!config.getKeys(true).contains(s)) {
|
|
||||||
if (updateBlacklist.stream().noneMatch(s::contains)) {
|
|
||||||
config.set(s, newConfig.get(s));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (this.removeUnused) {
|
|
||||||
config.getKeys(true).forEach((s -> {
|
|
||||||
if (!newConfig.getKeys(true).contains(s)) {
|
|
||||||
if (updateBlacklist.stream().noneMatch(s::contains)) {
|
|
||||||
config.set(s, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
config.save(this.getConfigFile());
|
|
||||||
} catch (IOException | InvalidConfigurationException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.willfp.eco.util.config;
|
||||||
|
|
||||||
|
import com.willfp.eco.util.config.internal.AbstractUpdatableConfig;
|
||||||
|
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public abstract class ExtendableConfig extends AbstractUpdatableConfig {
|
||||||
|
/**
|
||||||
|
* Config implementation for configs present in the plugin's base directory (eg config.yml, lang.yml).
|
||||||
|
* <p>
|
||||||
|
* Automatically updates.
|
||||||
|
*
|
||||||
|
* @param configName The name of the config
|
||||||
|
* @param removeUnused Whether keys not present in the default config should be removed on update.
|
||||||
|
* @param plugin The plugin.
|
||||||
|
* @param updateBlacklist Substring of keys to not add/remove keys for.
|
||||||
|
* @param subDirectoryPath The subdirectory path.
|
||||||
|
* @param source The class that owns the resource.
|
||||||
|
*/
|
||||||
|
protected ExtendableConfig(@NotNull final String configName,
|
||||||
|
final boolean removeUnused,
|
||||||
|
@NotNull final AbstractEcoPlugin plugin,
|
||||||
|
@NotNull final Class<?> source,
|
||||||
|
@NotNull final String subDirectoryPath,
|
||||||
|
@NotNull final String... updateBlacklist) {
|
||||||
|
super(configName, plugin, subDirectoryPath, source, removeUnused, updateBlacklist);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,38 +1,10 @@
|
|||||||
package com.willfp.eco.util.config;
|
package com.willfp.eco.util.config;
|
||||||
|
|
||||||
import com.willfp.eco.util.StringUtils;
|
import com.willfp.eco.util.config.internal.AbstractConfig;
|
||||||
import com.willfp.eco.util.internal.PluginDependent;
|
|
||||||
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
|
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
|
||||||
import lombok.AccessLevel;
|
|
||||||
import lombok.Getter;
|
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
public abstract class StaticBaseConfig extends PluginDependent {
|
|
||||||
/**
|
|
||||||
* The linked {@link YamlConfiguration} where values are physically stored.
|
|
||||||
*/
|
|
||||||
@Getter(AccessLevel.PUBLIC)
|
|
||||||
protected final YamlConfiguration config;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The physical config file, as stored on disk.
|
|
||||||
*/
|
|
||||||
@Getter(AccessLevel.PROTECTED)
|
|
||||||
private final File configFile;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The full name of the config file (eg config.yml).
|
|
||||||
*/
|
|
||||||
@Getter(AccessLevel.PROTECTED)
|
|
||||||
private final String name;
|
|
||||||
|
|
||||||
|
public abstract class StaticBaseConfig extends AbstractConfig {
|
||||||
/**
|
/**
|
||||||
* Config implementation for configs present in the plugin's base directory (eg config.yml, lang.yml).
|
* Config implementation for configs present in the plugin's base directory (eg config.yml, lang.yml).
|
||||||
* <p>
|
* <p>
|
||||||
@@ -43,272 +15,6 @@ public abstract class StaticBaseConfig extends PluginDependent {
|
|||||||
*/
|
*/
|
||||||
protected StaticBaseConfig(@NotNull final String configName,
|
protected StaticBaseConfig(@NotNull final String configName,
|
||||||
@NotNull final AbstractEcoPlugin plugin) {
|
@NotNull final AbstractEcoPlugin plugin) {
|
||||||
super(plugin);
|
super(configName, plugin, "", plugin.getClass());
|
||||||
this.name = configName + ".yml";
|
|
||||||
|
|
||||||
if (!new File(this.getPlugin().getDataFolder(), this.name).exists()) {
|
|
||||||
createFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.configFile = new File(this.getPlugin().getDataFolder(), this.name);
|
|
||||||
this.config = YamlConfiguration.loadConfiguration(configFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void createFile() {
|
|
||||||
this.getPlugin().saveResource(name, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get if the config contains a key.
|
|
||||||
*
|
|
||||||
* @param path The key to check.
|
|
||||||
* @return If contained.
|
|
||||||
*/
|
|
||||||
public boolean has(@NotNull final String path) {
|
|
||||||
return config.contains(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get configuration section from config.
|
|
||||||
*
|
|
||||||
* @param path The key to check.
|
|
||||||
* @return The configuration section. Throws NPE if not found.
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public ConfigurationSection getSection(@NotNull final String path) {
|
|
||||||
ConfigurationSection section = getSectionOrNull(path);
|
|
||||||
if (section == null) {
|
|
||||||
throw new NullPointerException("Section cannot be null!");
|
|
||||||
} else {
|
|
||||||
return section;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get configuration section from config.
|
|
||||||
*
|
|
||||||
* @param path The key to check.
|
|
||||||
* @return The configuration section, or null if not found.
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
public ConfigurationSection getSectionOrNull(@NotNull final String path) {
|
|
||||||
return config.getConfigurationSection(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get an integer from config.
|
|
||||||
*
|
|
||||||
* @param path The key to fetch the value from.
|
|
||||||
* @return The found value, or 0 if not found.
|
|
||||||
*/
|
|
||||||
public int getInt(@NotNull final String path) {
|
|
||||||
return config.getInt(path, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get an integer from config.
|
|
||||||
*
|
|
||||||
* @param path The key to fetch the value from.
|
|
||||||
* @return The found value, or null if not found.
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
public Integer getIntOrNull(@NotNull final String path) {
|
|
||||||
if (has(path)) {
|
|
||||||
return getInt(path);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get an integer from config with a specified default (not found) value.
|
|
||||||
*
|
|
||||||
* @param path The key to fetch the value from.
|
|
||||||
* @param def The value to default to if not found.
|
|
||||||
* @return The found value, or the default.
|
|
||||||
*/
|
|
||||||
public int getInt(@NotNull final String path,
|
|
||||||
final int def) {
|
|
||||||
return config.getInt(path, def);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a list of integers from config.
|
|
||||||
*
|
|
||||||
* @param path The key to fetch the value from.
|
|
||||||
* @return The found value, or a blank {@link java.util.ArrayList} if not found.
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public List<Integer> getInts(@NotNull final String path) {
|
|
||||||
return config.getIntegerList(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a list of integers from config.
|
|
||||||
*
|
|
||||||
* @param path The key to fetch the value from.
|
|
||||||
* @return The found value, or null if not found.
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
public List<Integer> getIntsOrNull(@NotNull final String path) {
|
|
||||||
if (has(path)) {
|
|
||||||
return getInts(path);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a boolean from config.
|
|
||||||
*
|
|
||||||
* @param path The key to fetch the value from.
|
|
||||||
* @return The found value, or false if not found.
|
|
||||||
*/
|
|
||||||
public boolean getBool(@NotNull final String path) {
|
|
||||||
return config.getBoolean(path, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a boolean from config.
|
|
||||||
*
|
|
||||||
* @param path The key to fetch the value from.
|
|
||||||
* @return The found value, or null if not found.
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
public Boolean getBoolOrNull(@NotNull final String path) {
|
|
||||||
if (has(path)) {
|
|
||||||
return getBool(path);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a list of booleans from config.
|
|
||||||
*
|
|
||||||
* @param path The key to fetch the value from.
|
|
||||||
* @return The found value, or a blank {@link java.util.ArrayList} if not found.
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public List<Boolean> getBools(@NotNull final String path) {
|
|
||||||
return config.getBooleanList(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a list of booleans from config.
|
|
||||||
*
|
|
||||||
* @param path The key to fetch the value from.
|
|
||||||
* @return The found value, or null if not found.
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
public List<Boolean> getBoolsOrNull(@NotNull final String path) {
|
|
||||||
if (has(path)) {
|
|
||||||
return getBools(path);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a string from config.
|
|
||||||
*
|
|
||||||
* @param path The key to fetch the value from.
|
|
||||||
* @return The found value, or an empty string if not found.
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public String getString(@NotNull final String path) {
|
|
||||||
return StringUtils.translate(Objects.requireNonNull(config.getString(path, "")));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a string from config.
|
|
||||||
*
|
|
||||||
* @param path The key to fetch the value from.
|
|
||||||
* @return The found value, or null if not found.
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
public String getStringOrNull(@NotNull final String path) {
|
|
||||||
if (has(path)) {
|
|
||||||
return getString(path);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a list of strings from config.
|
|
||||||
*
|
|
||||||
* @param path The key to fetch the value from.
|
|
||||||
* @return The found value, or a blank {@link java.util.ArrayList} if not found.
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public List<String> getStrings(@NotNull final String path) {
|
|
||||||
return config.getStringList(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a list of strings from config.
|
|
||||||
*
|
|
||||||
* @param path The key to fetch the value from.
|
|
||||||
* @return The found value, or null if not found.
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
public List<String> getStringsOrNull(@NotNull final String path) {
|
|
||||||
if (has(path)) {
|
|
||||||
return getStrings(path);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a decimal from config.
|
|
||||||
*
|
|
||||||
* @param path The key to fetch the value from.
|
|
||||||
* @return The found value, or 0 if not found.
|
|
||||||
*/
|
|
||||||
public double getDouble(@NotNull final String path) {
|
|
||||||
return config.getDouble(path, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a decimal from config.
|
|
||||||
*
|
|
||||||
* @param path The key to fetch the value from.
|
|
||||||
* @return The found value, or null if not found.
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
public Double getDoubleOrNull(@NotNull final String path) {
|
|
||||||
if (has(path)) {
|
|
||||||
return getDouble(path);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a list of decimals from config.
|
|
||||||
*
|
|
||||||
* @param path The key to fetch the value from.
|
|
||||||
* @return The found value, or a blank {@link java.util.ArrayList} if not found.
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public List<Double> getDoubles(@NotNull final String path) {
|
|
||||||
return config.getDoubleList(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a list of decimals from config.
|
|
||||||
*
|
|
||||||
* @param path The key to fetch the value from.
|
|
||||||
* @return The found value, or null if not found.
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
public List<Double> getDoublesOrNull(@NotNull final String path) {
|
|
||||||
if (has(path)) {
|
|
||||||
return getDoubles(path);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.willfp.eco.util.config;
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
public interface ValueGetter {
|
||||||
|
}
|
||||||
@@ -0,0 +1,407 @@
|
|||||||
|
package com.willfp.eco.util.config.internal;
|
||||||
|
|
||||||
|
import com.willfp.eco.util.StringUtils;
|
||||||
|
import com.willfp.eco.util.internal.PluginDependent;
|
||||||
|
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
|
import org.bukkit.configuration.InvalidConfigurationException;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public abstract class AbstractConfig extends PluginDependent {
|
||||||
|
/**
|
||||||
|
* The linked {@link YamlConfiguration} where values are physically stored.
|
||||||
|
*/
|
||||||
|
@Getter(AccessLevel.PUBLIC)
|
||||||
|
protected final YamlConfiguration config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The physical config file, as stored on disk.
|
||||||
|
*/
|
||||||
|
@Getter(AccessLevel.PROTECTED)
|
||||||
|
private final File configFile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The full name of the config file (eg config.yml).
|
||||||
|
*/
|
||||||
|
@Getter(AccessLevel.PROTECTED)
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The subdirectory path.
|
||||||
|
*/
|
||||||
|
@Getter(AccessLevel.PROTECTED)
|
||||||
|
private final String subDirectoryPath;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The provider of the config.
|
||||||
|
*/
|
||||||
|
@Getter(AccessLevel.PROTECTED)
|
||||||
|
private final Class<?> source;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract config.
|
||||||
|
*
|
||||||
|
* @param configName The name of the config
|
||||||
|
* @param plugin The plugin.
|
||||||
|
* @param subDirectoryPath The subdirectory path.
|
||||||
|
* @param source The class that owns the resource.
|
||||||
|
*/
|
||||||
|
protected AbstractConfig(@NotNull final String configName,
|
||||||
|
@NotNull final AbstractEcoPlugin plugin,
|
||||||
|
@NotNull final String subDirectoryPath,
|
||||||
|
@NotNull final Class<?> source) {
|
||||||
|
super(plugin);
|
||||||
|
this.source = source;
|
||||||
|
this.subDirectoryPath = subDirectoryPath;
|
||||||
|
this.name = configName + ".yml";
|
||||||
|
|
||||||
|
File directory = new File(this.getPlugin().getDataFolder(), subDirectoryPath);
|
||||||
|
if (!directory.exists()) {
|
||||||
|
directory.mkdirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!new File(directory, this.name).exists()) {
|
||||||
|
createFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.configFile = new File(directory, this.name);
|
||||||
|
this.config = YamlConfiguration.loadConfiguration(configFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createFile() {
|
||||||
|
String resourcePath = getResourcePath();
|
||||||
|
InputStream in = source.getResourceAsStream(resourcePath);
|
||||||
|
|
||||||
|
File outFile = new File(this.getPlugin().getDataFolder(), resourcePath);
|
||||||
|
int lastIndex = resourcePath.lastIndexOf('/');
|
||||||
|
File outDir = new File(this.getPlugin().getDataFolder(), resourcePath.substring(0, Math.max(lastIndex, 0)));
|
||||||
|
|
||||||
|
if (!outDir.exists()) {
|
||||||
|
outDir.mkdirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!outFile.exists()) {
|
||||||
|
OutputStream out = new FileOutputStream(outFile);
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
int len;
|
||||||
|
while ((len = in.read(buf)) > 0) {
|
||||||
|
out.write(buf, 0, len);
|
||||||
|
}
|
||||||
|
out.close();
|
||||||
|
in.close();
|
||||||
|
}
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get resource path as relative to base directory.
|
||||||
|
*
|
||||||
|
* @return The resource path.
|
||||||
|
*/
|
||||||
|
protected String getResourcePath() {
|
||||||
|
String resourcePath;
|
||||||
|
|
||||||
|
if (subDirectoryPath.isEmpty()) {
|
||||||
|
resourcePath = name;
|
||||||
|
} else {
|
||||||
|
resourcePath = subDirectoryPath + name;
|
||||||
|
}
|
||||||
|
|
||||||
|
return "/" + resourcePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get YamlConfiguration as found in jar.
|
||||||
|
*
|
||||||
|
* @return The YamlConfiguration.
|
||||||
|
*/
|
||||||
|
protected YamlConfiguration getConfigInJar() {
|
||||||
|
InputStream newIn = source.getResourceAsStream(getResourcePath());
|
||||||
|
|
||||||
|
if (newIn == null) {
|
||||||
|
throw new NullPointerException(this.getName() + " is null?");
|
||||||
|
}
|
||||||
|
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(newIn, StandardCharsets.UTF_8));
|
||||||
|
YamlConfiguration newConfig = new YamlConfiguration();
|
||||||
|
|
||||||
|
try {
|
||||||
|
newConfig.load(reader);
|
||||||
|
} catch (IOException | InvalidConfigurationException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return newConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get if the config contains a key.
|
||||||
|
*
|
||||||
|
* @param path The key to check.
|
||||||
|
* @return If contained.
|
||||||
|
*/
|
||||||
|
public boolean has(@NotNull final String path) {
|
||||||
|
return config.contains(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get configuration section from config.
|
||||||
|
*
|
||||||
|
* @param path The key to check.
|
||||||
|
* @return The configuration section. Throws NPE if not found.
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public ConfigurationSection getSection(@NotNull final String path) {
|
||||||
|
ConfigurationSection section = getSectionOrNull(path);
|
||||||
|
if (section == null) {
|
||||||
|
throw new NullPointerException("Section cannot be null!");
|
||||||
|
} else {
|
||||||
|
return section;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get configuration section from config.
|
||||||
|
*
|
||||||
|
* @param path The key to check.
|
||||||
|
* @return The configuration section, or null if not found.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public ConfigurationSection getSectionOrNull(@NotNull final String path) {
|
||||||
|
return config.getConfigurationSection(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get an integer from config.
|
||||||
|
*
|
||||||
|
* @param path The key to fetch the value from.
|
||||||
|
* @return The found value, or 0 if not found.
|
||||||
|
*/
|
||||||
|
public int getInt(@NotNull final String path) {
|
||||||
|
return config.getInt(path, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get an integer from config.
|
||||||
|
*
|
||||||
|
* @param path The key to fetch the value from.
|
||||||
|
* @return The found value, or null if not found.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public Integer getIntOrNull(@NotNull final String path) {
|
||||||
|
if (has(path)) {
|
||||||
|
return getInt(path);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get an integer from config with a specified default (not found) value.
|
||||||
|
*
|
||||||
|
* @param path The key to fetch the value from.
|
||||||
|
* @param def The value to default to if not found.
|
||||||
|
* @return The found value, or the default.
|
||||||
|
*/
|
||||||
|
public int getInt(@NotNull final String path,
|
||||||
|
final int def) {
|
||||||
|
return config.getInt(path, def);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of integers from config.
|
||||||
|
*
|
||||||
|
* @param path The key to fetch the value from.
|
||||||
|
* @return The found value, or a blank {@link java.util.ArrayList} if not found.
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public List<Integer> getInts(@NotNull final String path) {
|
||||||
|
return config.getIntegerList(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of integers from config.
|
||||||
|
*
|
||||||
|
* @param path The key to fetch the value from.
|
||||||
|
* @return The found value, or null if not found.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public List<Integer> getIntsOrNull(@NotNull final String path) {
|
||||||
|
if (has(path)) {
|
||||||
|
return getInts(path);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a boolean from config.
|
||||||
|
*
|
||||||
|
* @param path The key to fetch the value from.
|
||||||
|
* @return The found value, or false if not found.
|
||||||
|
*/
|
||||||
|
public boolean getBool(@NotNull final String path) {
|
||||||
|
return config.getBoolean(path, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a boolean from config.
|
||||||
|
*
|
||||||
|
* @param path The key to fetch the value from.
|
||||||
|
* @return The found value, or null if not found.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public Boolean getBoolOrNull(@NotNull final String path) {
|
||||||
|
if (has(path)) {
|
||||||
|
return getBool(path);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of booleans from config.
|
||||||
|
*
|
||||||
|
* @param path The key to fetch the value from.
|
||||||
|
* @return The found value, or a blank {@link java.util.ArrayList} if not found.
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public List<Boolean> getBools(@NotNull final String path) {
|
||||||
|
return config.getBooleanList(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of booleans from config.
|
||||||
|
*
|
||||||
|
* @param path The key to fetch the value from.
|
||||||
|
* @return The found value, or null if not found.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public List<Boolean> getBoolsOrNull(@NotNull final String path) {
|
||||||
|
if (has(path)) {
|
||||||
|
return getBools(path);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a string from config.
|
||||||
|
*
|
||||||
|
* @param path The key to fetch the value from.
|
||||||
|
* @return The found value, or an empty string if not found.
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public String getString(@NotNull final String path) {
|
||||||
|
return StringUtils.translate(Objects.requireNonNull(config.getString(path, "")));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a string from config.
|
||||||
|
*
|
||||||
|
* @param path The key to fetch the value from.
|
||||||
|
* @return The found value, or null if not found.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public String getStringOrNull(@NotNull final String path) {
|
||||||
|
if (has(path)) {
|
||||||
|
return getString(path);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of strings from config.
|
||||||
|
*
|
||||||
|
* @param path The key to fetch the value from.
|
||||||
|
* @return The found value, or a blank {@link java.util.ArrayList} if not found.
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public List<String> getStrings(@NotNull final String path) {
|
||||||
|
return config.getStringList(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of strings from config.
|
||||||
|
*
|
||||||
|
* @param path The key to fetch the value from.
|
||||||
|
* @return The found value, or null if not found.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public List<String> getStringsOrNull(@NotNull final String path) {
|
||||||
|
if (has(path)) {
|
||||||
|
return getStrings(path);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a decimal from config.
|
||||||
|
*
|
||||||
|
* @param path The key to fetch the value from.
|
||||||
|
* @return The found value, or 0 if not found.
|
||||||
|
*/
|
||||||
|
public double getDouble(@NotNull final String path) {
|
||||||
|
return config.getDouble(path, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a decimal from config.
|
||||||
|
*
|
||||||
|
* @param path The key to fetch the value from.
|
||||||
|
* @return The found value, or null if not found.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public Double getDoubleOrNull(@NotNull final String path) {
|
||||||
|
if (has(path)) {
|
||||||
|
return getDouble(path);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of decimals from config.
|
||||||
|
*
|
||||||
|
* @param path The key to fetch the value from.
|
||||||
|
* @return The found value, or a blank {@link java.util.ArrayList} if not found.
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public List<Double> getDoubles(@NotNull final String path) {
|
||||||
|
return config.getDoubleList(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of decimals from config.
|
||||||
|
*
|
||||||
|
* @param path The key to fetch the value from.
|
||||||
|
* @return The found value, or null if not found.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public List<Double> getDoublesOrNull(@NotNull final String path) {
|
||||||
|
if (has(path)) {
|
||||||
|
return getDoubles(path);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
package com.willfp.eco.util.config.internal;
|
||||||
|
|
||||||
|
import com.willfp.eco.util.plugin.AbstractEcoPlugin;
|
||||||
|
import org.bukkit.configuration.InvalidConfigurationException;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public abstract class AbstractUpdatableConfig extends AbstractConfig {
|
||||||
|
/**
|
||||||
|
* Whether keys not in the base config should be removed on update.
|
||||||
|
*/
|
||||||
|
private final boolean removeUnused;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of blacklisted update keys.
|
||||||
|
*/
|
||||||
|
private final List<String> updateBlacklist;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updatable config.
|
||||||
|
*
|
||||||
|
* @param configName The name of the config
|
||||||
|
* @param plugin The plugin.
|
||||||
|
* @param subDirectoryPath The subdirectory path.
|
||||||
|
* @param source The class that owns the resource.
|
||||||
|
* @param removeUnused Whether keys not present in the default config should be removed on update.
|
||||||
|
* @param updateBlacklist Substring of keys to not add/remove keys for.
|
||||||
|
*/
|
||||||
|
protected AbstractUpdatableConfig(@NotNull final String configName,
|
||||||
|
@NotNull final AbstractEcoPlugin plugin,
|
||||||
|
@NotNull final String subDirectoryPath,
|
||||||
|
@NotNull final Class<?> source,
|
||||||
|
final boolean removeUnused,
|
||||||
|
@NotNull final String... updateBlacklist) {
|
||||||
|
super(configName, plugin, subDirectoryPath, source);
|
||||||
|
this.removeUnused = removeUnused;
|
||||||
|
this.updateBlacklist = new ArrayList<>(Arrays.asList(updateBlacklist));
|
||||||
|
this.updateBlacklist.removeIf(String::isEmpty);
|
||||||
|
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the config.
|
||||||
|
* <p>
|
||||||
|
* Writes missing values, however removes comments due to how configs are stored internally in bukkit.
|
||||||
|
*/
|
||||||
|
public void update() {
|
||||||
|
try {
|
||||||
|
config.load(this.getConfigFile());
|
||||||
|
|
||||||
|
YamlConfiguration newConfig = this.getConfigInJar();
|
||||||
|
|
||||||
|
if (newConfig.getKeys(true).equals(config.getKeys(true))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
newConfig.getKeys(true).forEach((s -> {
|
||||||
|
if (!config.getKeys(true).contains(s)) {
|
||||||
|
if (updateBlacklist.stream().noneMatch(s::contains)) {
|
||||||
|
config.set(s, newConfig.get(s));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
if (this.removeUnused) {
|
||||||
|
config.getKeys(true).forEach((s -> {
|
||||||
|
if (!newConfig.getKeys(true).contains(s)) {
|
||||||
|
if (updateBlacklist.stream().noneMatch(s::contains)) {
|
||||||
|
config.set(s, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
config.save(this.getConfigFile());
|
||||||
|
} catch (IOException | InvalidConfigurationException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@ public final class EcoShapedRecipe extends PluginDependent implements Registerab
|
|||||||
/**
|
/**
|
||||||
* Recipe parts.
|
* Recipe parts.
|
||||||
*/
|
*/
|
||||||
|
@Getter
|
||||||
private final RecipePart[] parts;
|
private final RecipePart[] parts;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import java.util.function.Function;
|
|||||||
@UtilityClass
|
@UtilityClass
|
||||||
public final class RecipePartUtils {
|
public final class RecipePartUtils {
|
||||||
/**
|
/**
|
||||||
* Set of tests that return if the player is telekinetic.
|
* All recipes.
|
||||||
*/
|
*/
|
||||||
private static final Map<String, Function<String, RecipePart>> TESTS = new HashMap<>();
|
private static final Map<String, Function<String, RecipePart>> TESTS = new HashMap<>();
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
version = 3.5.0
|
version = 3.7.0
|
||||||
plugin-name = eco
|
plugin-name = eco
|
||||||
Reference in New Issue
Block a user