refactoring & some changes

Dieser Commit ist enthalten in:
Silent 2021-07-11 23:09:11 +02:00
Ursprung 6a7cffc10c
Commit ed68213c4d
10 geänderte Dateien mit 93 neuen und 45 gelöschten Zeilen

Datei anzeigen

@ -1,8 +1,6 @@
package tsp.headdb;
import de.leonhard.storage.Config;
import de.leonhard.storage.Json;
import de.leonhard.storage.LightningBuilder;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import tsp.headdb.api.HeadAPI;
@ -12,6 +10,7 @@ import tsp.headdb.listener.PagedPaneListener;
import tsp.headdb.listener.MenuListener;
import tsp.headdb.util.Log;
import tsp.headdb.util.Metrics;
import tsp.headdb.util.Storage;
import tsp.headdb.util.Utils;
import org.bukkit.plugin.RegisteredServiceProvider;
import net.milkbowl.vault.economy.Economy;
@ -19,8 +18,7 @@ import net.milkbowl.vault.economy.Economy;
public class HeadDB extends JavaPlugin {
private static HeadDB instance;
private Config config;
private Json playerdata;
private Storage storage;
private Economy economy = null;
@Override
@ -28,10 +26,9 @@ public class HeadDB extends JavaPlugin {
instance = this;
Log.info("Loading HeadDB - " + getDescription().getVersion());
saveDefaultConfig();
config = LightningBuilder.fromPath("config.yml", "plugins/HeadDB").createConfig().addDefaultsFromInputStream();
playerdata = LightningBuilder.fromPath("playerdata.json", "plugins/HeadDB").createJson();
storage = new Storage().init(this);
if (config.getBoolean("economy.enable")) {
if (storage.getConfig().getBoolean("economy.enable")) {
Log.debug("Starting economy...");
this.economy = this.setupEconomy();
if (this.economy == null) {
@ -52,8 +49,8 @@ public class HeadDB extends JavaPlugin {
Log.debug("Registering commands...");
getCommand("headdb").setExecutor(new Command_headdb());
if (config.getBoolean("fetchStartup")) {
if (config.getBoolean("asyncStartup")) {
if (storage.getConfig().getBoolean("fetchStartup")) {
if (storage.getConfig().getBoolean("asyncStartup")) {
Log.debug("Initializing Database... (ASYNC)");
Bukkit.getScheduler().runTaskAsynchronously(this, () -> HeadAPI.getDatabase().update());
} else {
@ -74,19 +71,20 @@ public class HeadDB extends JavaPlugin {
return this.economy = economyProvider.getProvider();
}
public Config getCfg() {
return config;
public Config getConfiguration() {
return storage.getConfig();
}
public Json getPlayerdata() {
return playerdata;
public Storage getStorage() {
return storage;
}
public Economy getEconomy() {
return this.economy;
}
public static HeadDB getInstance() {
return instance;
}
public Economy getEconomy() {
return this.economy;
}
}

Datei anzeigen

@ -65,7 +65,7 @@ public class Head {
return name;
}
public UUID getUUID() {
public UUID getUniqueId() {
return uuid;
}
@ -90,7 +90,7 @@ public class Head {
return this;
}
public Head withUUID(UUID uuid) {
public Head withUniqueId(UUID uuid) {
this.uuid = uuid;
return this;
}

Datei anzeigen

@ -81,8 +81,8 @@ public final class HeadAPI {
* @return The head
*/
@Nullable
public static Head getHeadByUUID(UUID uuid) {
return database.getHeadByUUID(uuid);
public static Head getHeadByUniqueId(UUID uuid) {
return database.getHeadByUniqueId(uuid);
}
public static List<Head> getHeadsByTag(String tag) {
@ -147,11 +147,11 @@ public final class HeadAPI {
* @param id The ID of the head
*/
public static void addFavoriteHead(UUID uuid, int id) {
List<Integer> favs = HeadDB.getInstance().getPlayerdata().getIntegerList(uuid.toString() + ".favorites");
List<Integer> favs = HeadDB.getInstance().getConfiguration().getIntegerList(uuid.toString() + ".favorites");
if (!favs.contains(id)) {
favs.add(id);
}
HeadDB.getInstance().getPlayerdata().set(uuid.toString() + ".favorites", favs);
HeadDB.getInstance().getStorage().getPlayerData().set(uuid.toString() + ".favorites", favs);
}
/**
@ -161,14 +161,14 @@ public final class HeadAPI {
* @param id The ID of the head
*/
public static void removeFavoriteHead(UUID uuid, int id) {
List<Integer> favs = HeadDB.getInstance().getPlayerdata().getIntegerList(uuid.toString() + ".favorites");
List<Integer> favs = HeadDB.getInstance().getStorage().getPlayerData().getIntegerList(uuid.toString() + ".favorites");
for (int i = 0; i < favs.size(); i++) {
if (favs.get(i) == id) {
favs.remove(i);
break;
}
}
HeadDB.getInstance().getPlayerdata().set(uuid.toString() + ".favorites", favs);
HeadDB.getInstance().getStorage().getPlayerData().set(uuid.toString() + ".favorites", favs);
}
/**
@ -179,7 +179,7 @@ public final class HeadAPI {
*/
public static List<Head> getFavoriteHeads(UUID uuid) {
List<Head> heads = new ArrayList<>();
List<Integer> ids = HeadDB.getInstance().getPlayerdata().getIntegerList(uuid.toString() + ".favorites");
List<Integer> ids = HeadDB.getInstance().getStorage().getPlayerData().getIntegerList(uuid.toString() + ".favorites");
for (int id : ids) {
Head head = getHeadByID(id);
heads.add(head);
@ -196,7 +196,7 @@ public final class HeadAPI {
*/
public static List<LocalHead> getLocalHeads() {
List<LocalHead> heads = new ArrayList<>();
for (String key : HeadDB.getInstance().getPlayerdata().singleLayerKeySet()) {
for (String key : HeadDB.getInstance().getStorage().getPlayerData().singleLayerKeySet()) {
OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(key));
heads.add(new LocalHead(player.getUniqueId())
.withName(player.getName()));

Datei anzeigen

@ -35,7 +35,7 @@ public class LocalHead {
this.uuid = uuid;
}
public UUID getUuid() {
public UUID getUniqueId() {
return uuid;
}
@ -43,7 +43,7 @@ public class LocalHead {
return name;
}
public LocalHead withUUID(UUID uuid) {
public LocalHead withUniqueId(UUID uuid) {
this.uuid = uuid;
return this;
}

Datei anzeigen

@ -16,6 +16,7 @@ import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
/**
* This is the Database that holds all heads
@ -53,10 +54,10 @@ public class HeadDatabase {
return null;
}
public Head getHeadByUUID(UUID uuid) {
public Head getHeadByUniqueId(UUID uuid) {
List<Head> heads = getHeads();
for (Head head : heads) {
if (head.getUUID().equals(uuid)) {
if (head.getUniqueId().equals(uuid)) {
return head;
}
}
@ -118,6 +119,12 @@ public class HeadDatabase {
return getHeads();
}
/**
* Gets all heads from the api provider
*
* @return Map containing each head in it's category. Returns null if the fetching failed.
*/
@Nullable
public Map<Category, List<Head>> getHeadsNoCache() {
Map<Category, List<Head>> result = new HashMap<>();
List<Category> categories = Category.getCategories();
@ -125,6 +132,7 @@ public class HeadDatabase {
int id = 1;
for (Category category : categories) {
Log.debug("Caching heads from: " + category.getName());
long start = System.currentTimeMillis();
List<Head> heads = new ArrayList<>();
try {
String line;
@ -143,10 +151,10 @@ public class HeadDatabase {
for (Object o : array) {
JSONObject obj = (JSONObject) o;
String uuid = obj.get("uuid").toString();
Head head = new Head(id)
.withName(obj.get("name").toString())
.withUUID(uuid.isEmpty() ? UUID.randomUUID() : UUID.fromString(uuid))
.withUniqueId(uuid.isEmpty() ? UUID.randomUUID() : UUID.fromString(uuid))
.withValue(obj.get("value").toString())
.withTags(obj.get("tags") != null ? obj.get("tags").toString() : "None")
.withCategory(category);
@ -154,9 +162,13 @@ public class HeadDatabase {
id++;
heads.add(head);
}
long elapsed = (System.currentTimeMillis() - start);
Log.debug(category.getName() + " -> Done! Time: " + elapsed + "ms (" + TimeUnit.MILLISECONDS.toSeconds(elapsed) + "s)");
} catch (ParseException | IOException e) {
Log.error("Failed to fetch heads (no-cache) | Stack Trace:");
e.printStackTrace();
return null;
}
updated = System.nanoTime();
@ -166,12 +178,23 @@ public class HeadDatabase {
return result;
}
public void update() {
/**
* Updates the cached heads
*
* @return Returns true if the update was successful.
*/
public boolean update() {
Map<Category, List<Head>> heads = getHeadsNoCache();
if (heads == null) {
Log.error("Failed to update database! Check above for any errors.");
return false;
}
HEADS.clear();
for (Map.Entry<Category, List<Head>> entry : heads.entrySet()) {
HEADS.put(entry.getKey(), entry.getValue());
}
return true;
}
public long getLastUpdate() {
@ -181,8 +204,8 @@ public class HeadDatabase {
}
public boolean isLastUpdateOld() {
if (HeadDB.getInstance().getCfg() == null && getLastUpdate() >= 3600) return true;
return getLastUpdate() >= HeadDB.getInstance().getCfg().getLong("refresh");
if (HeadDB.getInstance().getConfiguration() == null && getLastUpdate() >= 3600) return true;
return getLastUpdate() >= HeadDB.getInstance().getConfiguration().getLong("refresh");
}
}

Datei anzeigen

@ -32,8 +32,8 @@ public class InventoryUtils {
if (uiLocation.containsKey(category)) return uiLocation.get(category);
// Try to get the value from the config file.
if (HeadDB.getInstance().getCfg().contains("ui.category." + category + ".location")) {
uiLocation.put(category, HeadDB.getInstance().getCfg().getInt("ui.category." + category + ".location"));
if (HeadDB.getInstance().getConfiguration().contains("ui.category." + category + ".location")) {
uiLocation.put(category, HeadDB.getInstance().getConfiguration().getInt("ui.category." + category + ".location"));
return uiLocation.get(category);
}
@ -47,8 +47,8 @@ public class InventoryUtils {
if (uiItem.containsKey(category)) return uiItem.get(category);
// Try to get a head from the config file.
if (HeadDB.getInstance().getCfg().contains("ui.category." + category + ".head")) {
int id = HeadDB.getInstance().getCfg().getInt("ui.category." + category + ".head");
if (HeadDB.getInstance().getConfiguration().contains("ui.category." + category + ".head")) {
int id = HeadDB.getInstance().getConfiguration().getInt("ui.category." + category + ".head");
Head head = HeadAPI.getHeadByID(id);
if (head != null) {
uiItem.put(category, head.getItemStack());
@ -57,8 +57,8 @@ public class InventoryUtils {
}
// Try to get an item from the config file.
if (HeadDB.getInstance().getCfg().contains("ui.category." + category + ".item")) {
String cfg = HeadDB.getInstance().getCfg().getString("ui.category." + category + ".item");
if (HeadDB.getInstance().getConfiguration().contains("ui.category." + category + ".item")) {
String cfg = HeadDB.getInstance().getConfiguration().getString("ui.category." + category + ".item");
Material mat = Material.matchMaterial(cfg);
// AIR is allowed as the fill material for the menu, but not as a category item.
@ -272,7 +272,7 @@ public class InventoryUtils {
if (player.hasPermission("headdb.economy.free") || player.hasPermission("headdb.economy." + category + ".free")) return 0;
// Otherwise, get the price for this category from the config file.
return HeadDB.getInstance().getCfg().getDouble("economy.cost." + category);
return HeadDB.getInstance().getConfiguration().getDouble("economy.cost." + category);
}
public static boolean processPayment(Player player, int amount, String category, String description) {

Datei anzeigen

@ -13,7 +13,7 @@ public class JoinListener implements Listener {
@EventHandler
public void onJoin(PlayerJoinEvent e) {
HeadDB.getInstance().getPlayerdata().set(e.getPlayer().getUniqueId().toString() + ".username", e.getPlayer().getName());
HeadDB.getInstance().getStorage().getPlayerData().set(e.getPlayer().getUniqueId().toString() + ".username", e.getPlayer().getName());
}
}

Datei anzeigen

@ -35,7 +35,7 @@ public class Log {
}
public static void log(LogLevel level, String message) {
if (level == LogLevel.DEBUG && !HeadDB.getInstance().getCfg().getBoolean("debug")) {
if (level == LogLevel.DEBUG && !HeadDB.getInstance().getStorage().getConfig().getBoolean("debug")) {
return;
}
Bukkit.getConsoleSender().sendMessage(Utils.colorize("&7[&9&l" + name + "&7] " + level.getColor() + "[" + level.name() + "]: " + message));

Datei anzeigen

@ -0,0 +1,27 @@
package tsp.headdb.util;
import de.leonhard.storage.Config;
import de.leonhard.storage.Json;
import de.leonhard.storage.LightningBuilder;
import org.bukkit.plugin.java.JavaPlugin;
public class Storage {
private Config config;
private Json playerdata;
public Storage init(JavaPlugin plugin) {
config = LightningBuilder.fromPath("config.yml", plugin.getDataFolder().getAbsolutePath()).createConfig().addDefaultsFromInputStream();
playerdata = LightningBuilder.fromPath("playerdata.json", plugin.getDataFolder().getAbsolutePath()).createJson();
return this;
}
public Config getConfig() {
return config;
}
public Json getPlayerData() {
return playerdata;
}
}

Datei anzeigen

@ -4,7 +4,7 @@ description: ${project.description}
main: tsp.headdb.HeadDB
version: ${project.version}
softdepend: [Vault]
api-version: 1.16
api-version: 1.13
author: Silent
commands: