Initial commit

This commit is contained in:
2021-09-25 01:56:42 +02:00
commit 5dd6cb44e5
27 changed files with 1366 additions and 0 deletions
@@ -0,0 +1,132 @@
package com.alttd.config;
import com.alttd.VillagerUI;
import com.google.common.collect.ImmutableMap;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
@SuppressWarnings({"unused", "SameParameterValue"})
abstract class AbstractConfig {
File file;
YamlConfiguration yaml;
Logger logger;
AbstractConfig(String filename) {
init(new File(VillagerUI.getInstance().getDataFolder(), filename), filename);
}
AbstractConfig(File file, String filename) {
init(new File(file.getPath() + File.separator + filename), filename);
}
private void init(File file, String filename) {
this.file = file;
this.yaml = new YamlConfiguration();
this.logger = VillagerUI.getInstance().getLogger();
try {
yaml.load(file);
} catch (IOException ignore) {
} catch (InvalidConfigurationException ex) {
logger.severe(String.format("Could not load %s, please correct your syntax errors", filename));
throw new RuntimeException(ex);
}
yaml.options().copyDefaults(true);
}
void readConfig(Class<?> clazz, Object instance) {
for (Method method : clazz.getDeclaredMethods()) {
if (Modifier.isPrivate(method.getModifiers())) {
if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) {
try {
method.setAccessible(true);
method.invoke(instance);
} catch (InvocationTargetException ex) {
throw new RuntimeException(ex.getCause());
} catch (Exception ex) {
logger.severe("Error invoking " + method);
ex.printStackTrace();
}
}
}
}
save();
}
private void save() {
try {
yaml.save(file);
} catch (IOException ex) {
logger.severe("Could not save " + file);
ex.printStackTrace();
}
}
void set(String path, Object val) {
yaml.addDefault(path, val);
yaml.set(path, val);
save();
}
String getString(String path, String def) {
yaml.addDefault(path, def);
return yaml.getString(path, yaml.getString(path));
}
boolean getBoolean(String path, boolean def) {
yaml.addDefault(path, def);
return yaml.getBoolean(path, yaml.getBoolean(path));
}
int getInt(String path, int def) {
yaml.addDefault(path, def);
return yaml.getInt(path, yaml.getInt(path));
}
double getDouble(String path, double def) {
yaml.addDefault(path, def);
return yaml.getDouble(path, yaml.getDouble(path));
}
<T> List<?> getList(String path, T def) {
yaml.addDefault(path, def);
return yaml.getList(path, yaml.getList(path));
}
@NonNull
<T> Map<String, T> getMap(final @NonNull String path, final @Nullable Map<String, T> def) {
final ImmutableMap.Builder<String, T> builder = ImmutableMap.builder();
if (def != null && yaml.getConfigurationSection(path) == null) {
yaml.addDefault(path, def.isEmpty() ? new HashMap<>() : def);
return def;
}
final ConfigurationSection section = yaml.getConfigurationSection(path);
if (section != null) {
for (String key : section.getKeys(false)) {
@SuppressWarnings("unchecked")
final T val = (T) section.get(key);
if (val != null) {
builder.put(key, val);
}
}
}
return builder.build();
}
ConfigurationSection getConfigurationSection(String path) {
return yaml.getConfigurationSection(path);
}
}
+110
View File
@@ -0,0 +1,110 @@
package com.alttd.config;
import com.alttd.VillagerUI;
import com.alttd.objects.VillagerType;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
public final class Config extends AbstractConfig {
static Config config;
static int version;
public Config() {
super(new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + "VillagerShopUI"), "config.yml");
}
public static void reload() {
config = new Config();
version = config.getInt("config-version", 1);
config.set("config-version", 1);
config.readConfig(Config.class, null);
}
public static String INITIAL_VILLAGER_WINDOW = "<trader> price: <percentage>%";
public static String BUY_WINDOW = "<trader> price: <percentage>%";
public static String SELL_WINDOW = "<trader> price: <percentage>%";
private static void loadUI() {
INITIAL_VILLAGER_WINDOW = config.getString("ui.initial-window-name", INITIAL_VILLAGER_WINDOW);
BUY_WINDOW = config.getString("ui.buy-window-name", BUY_WINDOW);
SELL_WINDOW = config.getString("ui.sell-window-name", SELL_WINDOW);
}
public static String HELP_MESSAGE_WRAPPER = "<gold>VillagerShopUI help:\n<commands></gold>";
public static String HELP_MESSAGE = "<green>Show this menu: <gold>/villagerui help</gold></green>";
public static String RELOAD_MESSAGE = "<green>Reload configs: <gold>/villagerui reload</gold></green>";
public static String CREATE_VILLAGER_MESSAGE = "<green>Create a new trading villager: <gold>/villagerui createvillager <type> <x> <y> <z> <yaw> <pitch> <world></gold></green>";
private static void loadHelp() {
HELP_MESSAGE_WRAPPER = config.getString("help.help-wrapper", HELP_MESSAGE_WRAPPER);
HELP_MESSAGE = config.getString("help.help", HELP_MESSAGE);
RELOAD_MESSAGE = config.getString("help.reload", RELOAD_MESSAGE);
CREATE_VILLAGER_MESSAGE = config.getString("help.create-villager", CREATE_VILLAGER_MESSAGE);
}
public static String NO_PERMISSION = "<red>You do not have permission to do that.</red>";
public static String NO_CONSOLE = "<red>You cannot use this command from console.</red>";
private static void loadGeneric() {
NO_PERMISSION = config.getString("generic.no-permission", NO_PERMISSION);
NO_CONSOLE = config.getString("generic.no-console", NO_CONSOLE);
}
public static String VILLAGER_NAME = "<green><name></green>";
private static void loadIDKYET() {//TODO rename
VILLAGER_NAME = config.getString("idkyet.villager-name", VILLAGER_NAME); //TODO change path
}
private static void loadVillagerTypes() {
VillagerType.clearVillagerTypes();
ConfigurationSection configurationSection = config.getConfigurationSection("villager-types");
if (configurationSection == null) {
VillagerUI.getInstance().getLogger().warning("No villager types found in config.");
return;
}
Set<String> keys = configurationSection.getKeys(false);
if (keys.isEmpty())
VillagerUI.getInstance().getLogger().warning("No villager types found in config.");
keys.forEach(key -> {
ConfigurationSection villagerType = configurationSection.getConfigurationSection(key);
if (villagerType == null)
return;
VillagerType.addVillagerType(new VillagerType(
key,
villagerType.getString("name"),
loadProducts(villagerType.getConfigurationSection("buying")),
loadProducts(villagerType.getConfigurationSection("selling")),
villagerType.getDouble("price-modifier"))
);
});
}
private static HashSet<ItemStack> loadProducts(ConfigurationSection productsSection) {
HashSet<ItemStack> products = new HashSet<>();
if (productsSection == null)
return products;
productsSection.getKeys(false).forEach(item -> {
Material material = Material.getMaterial(item);
if (material == null) {
VillagerUI.getInstance().getLogger().warning("Invalid key in products -> " + item);
return;
}
products.add(new ItemStack(material, productsSection.getInt(item)));
});
return products;
}
}
@@ -0,0 +1,44 @@
package com.alttd.config;
import com.alttd.VillagerUI;
import com.alttd.objects.LoadedVillagers;
import com.alttd.objects.VillagerType;
import org.bukkit.configuration.ConfigurationSection;
import java.util.Set;
import java.util.UUID;
public class VillagerConfig extends AbstractConfig {
static VillagerConfig config;
static int version;
public VillagerConfig() {
super("villagerConfig.yml");
}
public static void reload() {
config = new VillagerConfig();
version = config.getInt("config-version", 1);
config.set("config-version", 1);
config.readConfig(VillagerConfig.class, null);
}
private static void loadVillagers() {
LoadedVillagers.clearLoadedVillagers();
config.getConfigurationSection("").getKeys(false).forEach(key -> {
VillagerType villagerType = VillagerType.getVillagerType(config.getString(key, ""));
if (villagerType != null)
LoadedVillagers.addLoadedVillager(UUID.fromString(key), villagerType);
else
VillagerUI.getInstance().getLogger().warning("Invalid config entry " + key + ".");
});
}
public static void addVillager(UUID uuid, VillagerType villagerType) {
config.set(uuid.toString(), villagerType.getName());
}
}
@@ -0,0 +1,47 @@
package com.alttd.config;
import com.alttd.VillagerUI;
import com.alttd.util.Utilities;
import it.unimi.dsi.fastutil.objects.Object2DoubleMap;
import it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import java.io.File;
import java.util.Set;
public class WorthConfig extends AbstractConfig {
static WorthConfig config;
static int version;
public WorthConfig() {
super(new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + "VillagerShopUI"), "worth.yml");
}
public static void reload() {
config = new WorthConfig();
version = config.getInt("config-version", 1);
config.set("config-version", 1);
config.readConfig(WorthConfig.class, null);
}
public static Object2DoubleMap<Material> prices = new Object2DoubleOpenHashMap<>();
private static void loadWorth() {
prices.clear();
ConfigurationSection worth = config.getConfigurationSection("worth");
Set<String> keys = worth.getKeys(false);
for (String key : keys) {
Material material = Material.getMaterial(key);
if (material == null) {
VillagerUI.getInstance().getLogger().warning("Invalid key in worth.yml -> " + key);
continue;
}
int price = (int) (worth.getDouble(key) * 100);
prices.put(Material.getMaterial(key), Utilities.round(price, 2));
}
}
}