Yet another rework of the PlayerShop plugin.

This commit is contained in:
Len
2022-06-06 18:30:35 +02:00
commit d3b7489573
11 changed files with 720 additions and 0 deletions
@@ -0,0 +1,63 @@
package com.alttd.playershops;
import com.alttd.playershops.config.Config;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
public class PlayerShops extends JavaPlugin {
private static PlayerShops instance;
private Economy econ;
public void onEnable() {
instance = this;
if(!setupEconomy()) {
Bukkit.getLogger().warning("Error loading Vault and economy.\n Disabling plugin");
this.setEnabled(false);
return;
}
Bukkit.getLogger().info("Hooked into Vault economy provided by " + econ.getName());
reloadConfigs();
registerListeners();
registerCommands();
}
private boolean setupEconomy() {
if (getServer().getPluginManager().getPlugin("Vault") == null) {
return false;
}
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
if (rsp == null) {
return false;
}
return true;
}
public Economy getEconomy() {
if(econ == null)
setupEconomy();
return econ;
}
private void registerListeners() {
}
private void registerCommands() {
// for(String command : this.getDescription().getCommands().keySet()) {
// getCommand(command).setExecutor();
// }
}
public void reloadConfigs() {
Config.reload();
}
public static PlayerShops getInstance() {
return instance;
}
}
@@ -0,0 +1,154 @@
package com.alttd.playershops.config;
import com.alttd.playershops.PlayerShops;
import com.alttd.playershops.utils.Logger;
import com.google.common.collect.ImmutableMap;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.awt.*;
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.List;
import java.util.*;
@SuppressWarnings({"unused", "SameParameterValue"})
abstract class AbstractConfig {
File file;
YamlConfiguration yaml;
File configPath;
AbstractConfig(String filename) {
this.configPath = PlayerShops.getInstance().getDataFolder();
this.file = new File(configPath, filename);
this.yaml = new YamlConfiguration();
if (!this.file.getParentFile().exists()) {
if(!this.file.getParentFile().mkdirs()) {
return;
}
}
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();
}
}
}
}
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.addDefault(path, val);
yaml.set(path, val);
}
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();
}
ItemStack getItemStack(String path, ItemStack def) {
yaml.addDefault(path, def);
return yaml.getItemStack(path);
}
void setLocations(String path, ArrayList<Location> locs) {
ArrayList<String> list = new ArrayList<>();
for(Location l : locs) {
String toSave = l.getWorld().getName() + ":" + l.getX() + ":" + l.getY() + ":" + l.getZ();
list.add(toSave);
}
yaml.set(path, list);
}
List<Location> getLocations(String path){
List<String> attempts = yaml.getStringList(path);
if (attempts == null || attempts.isEmpty()) {
return Collections.emptyList();
}
List<Location> locations = new ArrayList<>();
for (String attempt : attempts) {
String[] parts = attempt.split(":");
locations.add(new Location(Bukkit.getWorld(parts[0]), Double.parseDouble(parts[1]), Double.parseDouble(parts[2]), Double.parseDouble(parts[3])));
}
return locations;
}
}
@@ -0,0 +1,22 @@
package com.alttd.playershops.config;
@SuppressWarnings("unused")
public class Config extends AbstractConfig {
private Config() {
super("config.yml");
}
static Config config;
static int version;
public static void reload() {
config = new Config();
version = config.getInt("config-version", 1);
config.set("config-version", 1);
config.readConfig(Config.class, null);
}
}
@@ -0,0 +1,29 @@
package com.alttd.playershops.utils;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
import java.util.logging.Level;
public class Logger
{
public static void info(String str) {
log(Level.INFO,"&e" + str);
}
public static void warn(String str) {
log(Level.SEVERE,"&6" + str);
}
public static void severe(String str) {
log(Level.SEVERE,"&c" + str);
}
public static void log(Level level, String str) {
Bukkit.getLogger().log(level,
ChatColor.translateAlternateColorCodes('&',
"&r " + str));
}
}