From 3b1649dc63e4ab5e9f09ec5599b68994e72723bf Mon Sep 17 00:00:00 2001 From: Silent Date: Sat, 8 Jan 2022 23:50:27 +0100 Subject: [PATCH] update task, documentation --- .gitignore | 1 + pom.xml | 2 +- src/main/java/tsp/headdb/HeadDB.java | 9 ++- src/main/java/tsp/headdb/api/HeadAPI.java | 55 ++++++++++++++++++- .../headdb/database/DatabaseUpdateTask.java | 15 +++++ .../tsp/headdb/inventory/InventoryUtils.java | 4 ++ .../tsp/headdb/listener/MenuListener.java | 1 - .../tsp/headdb/storage/PlayerDataFile.java | 7 --- 8 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 src/main/java/tsp/headdb/database/DatabaseUpdateTask.java diff --git a/.gitignore b/.gitignore index 686bc14..438e9c1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ target/ .idea/ +src/test/ dependency-reduced-pom.xml diff --git a/pom.xml b/pom.xml index 20ff90a..46844bd 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ tsp.headdb HeadDB - 3.0.0 + 3.0.1 jar HeadDB diff --git a/src/main/java/tsp/headdb/HeadDB.java b/src/main/java/tsp/headdb/HeadDB.java index 7e600d0..5b30b66 100644 --- a/src/main/java/tsp/headdb/HeadDB.java +++ b/src/main/java/tsp/headdb/HeadDB.java @@ -6,6 +6,7 @@ import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import tsp.headdb.api.HeadAPI; import tsp.headdb.command.CommandHeadDB; +import tsp.headdb.database.DatabaseUpdateTask; import tsp.headdb.listener.JoinListener; import tsp.headdb.listener.MenuListener; import tsp.headdb.listener.PagedPaneListener; @@ -13,6 +14,8 @@ import tsp.headdb.storage.PlayerDataFile; import tsp.headdb.util.Log; import tsp.headdb.util.Metrics; +import javax.annotation.Nullable; + public class HeadDB extends JavaPlugin { private static HeadDB instance; @@ -39,10 +42,9 @@ public class HeadDB extends JavaPlugin { } long refresh = getConfig().getLong("refresh") * 20; + HeadAPI.getDatabase().updateAsync(heads -> Log.info("Fetched " + HeadAPI.getHeads().size() + " heads!")); // Update database on startup + Bukkit.getScheduler().runTaskTimerAsynchronously(this, new DatabaseUpdateTask(), refresh, refresh); // Update database on set interval (also saves data) HeadAPI.getDatabase().setRefresh(refresh); - Bukkit.getScheduler().runTaskTimerAsynchronously(this, task -> - HeadAPI.getDatabase().updateAsync(heads -> Log.info("Fetched " + HeadAPI.getHeads().size() + " heads!")), - 0L, refresh); new JoinListener(this); new MenuListener(this); @@ -69,6 +71,7 @@ public class HeadDB extends JavaPlugin { return this.economy = economyProvider.getProvider(); } + @Nullable public Economy getEconomy() { return economy; } diff --git a/src/main/java/tsp/headdb/api/HeadAPI.java b/src/main/java/tsp/headdb/api/HeadAPI.java index 2a30ca0..1ee8736 100644 --- a/src/main/java/tsp/headdb/api/HeadAPI.java +++ b/src/main/java/tsp/headdb/api/HeadAPI.java @@ -9,6 +9,7 @@ import tsp.headdb.database.HeadDatabase; import tsp.headdb.inventory.InventoryUtils; import tsp.headdb.storage.PlayerDataFile; +import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; @@ -20,10 +21,14 @@ import java.util.UUID; * * @author TheSilentPro */ +// TODO: Possibly change to singleton class public final class HeadAPI { private HeadAPI() {} + /** + * Main {@link HeadDatabase} that he HeadDB plugin uses. + */ private static final HeadDatabase database = new HeadDatabase(HeadDB.getInstance()); /** @@ -64,6 +69,12 @@ public final class HeadAPI { InventoryUtils.openSearchDatabase(player, search); } + /** + * Opens the database with results of a specific tag search term + * + * @param player Target player + * @param tag Tag search term + */ public static void openTagSearchDatabase(Player player, String tag) { InventoryUtils.openTagSearchDatabase(player, tag); } @@ -90,6 +101,13 @@ public final class HeadAPI { return database.getHeadByUniqueId(uuid); } + /** + * Retrieve a {@link List} of {@link Head}'s by their tag + * + * @param tag The tag + * @return List of heads + */ + @Nonnull public static List getHeadsByTag(String tag) { return database.getHeadsByTag(tag); } @@ -100,6 +118,7 @@ public final class HeadAPI { * @param name The name to match for * @return List of heads */ + @Nonnull public static List getHeadsByName(String name) { return database.getHeadsByName(name); } @@ -111,6 +130,7 @@ public final class HeadAPI { * @param name The name to match for * @return List of heads */ + @Nonnull public static List getHeadsByName(Category category, String name) { return database.getHeadsByName(category, name); } @@ -132,6 +152,7 @@ public final class HeadAPI { * @param category The category to search in * @return List of heads */ + @Nonnull public static List getHeads(Category category) { return database.getHeads(category); } @@ -141,18 +162,38 @@ public final class HeadAPI { * * @return List of all heads */ + @Nonnull public static List getHeads() { return database.getHeads(); } - + + /** + * Add a favorite {@link Head} to the player + * + * @param uuid The player's unique id + * @param textureValue The head's texture value + */ public static void addFavoriteHead(UUID uuid, String textureValue) { HeadDB.getInstance().getPlayerData().modifyFavorite(uuid, textureValue, PlayerDataFile.ModificationType.SET); } - + + /** + * Remove a favorite {@link Head} from the player + * + * @param uuid The player's unique id + * @param textureValue The head's texture value + */ public static void removeFavoriteHead(UUID uuid, String textureValue) { HeadDB.getInstance().getPlayerData().modifyFavorite(uuid, textureValue, PlayerDataFile.ModificationType.REMOVE); } - + + /** + * Retrieve a {@link List} of favorite {@link Head}'s for the player + * + * @param uuid The player's unique id + * @return List of favorite {@link Head}'s for the player + */ + @Nonnull public static List getFavoriteHeads(UUID uuid) { List result = new ArrayList<>(); @@ -164,6 +205,14 @@ public final class HeadAPI { return result; } + /** + * Retrieve a list of {@link LocalHead}'s. + * These are heads from players that have joined the server at least once. + * Requires config option localHeads = true + * + * @return List of {@link LocalHead}'s + */ + @Nonnull public static List getLocalHeads() { List result = new ArrayList<>(); for (String entry : HeadDB.getInstance().getPlayerData().getEntries()) { diff --git a/src/main/java/tsp/headdb/database/DatabaseUpdateTask.java b/src/main/java/tsp/headdb/database/DatabaseUpdateTask.java new file mode 100644 index 0000000..44d75f5 --- /dev/null +++ b/src/main/java/tsp/headdb/database/DatabaseUpdateTask.java @@ -0,0 +1,15 @@ +package tsp.headdb.database; + +import tsp.headdb.HeadDB; +import tsp.headdb.api.HeadAPI; +import tsp.headdb.util.Log; + +public class DatabaseUpdateTask implements Runnable { + + @Override + public void run() { + HeadDB.getInstance().getPlayerData().save(); + HeadAPI.getDatabase().updateAsync(heads -> Log.info("Fetched " + HeadAPI.getHeads().size() + " heads!")); + } + +} diff --git a/src/main/java/tsp/headdb/inventory/InventoryUtils.java b/src/main/java/tsp/headdb/inventory/InventoryUtils.java index 004b6af..dd0fe72 100644 --- a/src/main/java/tsp/headdb/inventory/InventoryUtils.java +++ b/src/main/java/tsp/headdb/inventory/InventoryUtils.java @@ -23,6 +23,10 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +/** + * Class for handling the "dirty" work + * such as inventories and economy. + */ public class InventoryUtils { private static final Map uiLocation = new HashMap<>(); diff --git a/src/main/java/tsp/headdb/listener/MenuListener.java b/src/main/java/tsp/headdb/listener/MenuListener.java index a85faa1..09ae243 100644 --- a/src/main/java/tsp/headdb/listener/MenuListener.java +++ b/src/main/java/tsp/headdb/listener/MenuListener.java @@ -44,7 +44,6 @@ public class MenuListener implements Listener { player.closeInventory(); return; } - player.sendMessage("Clicked on favorites!"); InventoryUtils.openFavoritesMenu(player); return; } diff --git a/src/main/java/tsp/headdb/storage/PlayerDataFile.java b/src/main/java/tsp/headdb/storage/PlayerDataFile.java index d74a266..8ca8f2b 100644 --- a/src/main/java/tsp/headdb/storage/PlayerDataFile.java +++ b/src/main/java/tsp/headdb/storage/PlayerDataFile.java @@ -42,7 +42,6 @@ public class PlayerDataFile { JsonArray favorites = main.get(uuid.toString()).getAsJsonObject().get("favorites").getAsJsonArray(); for (int i = 0; i < favorites.size(); i++) { String str = favorites.get(i).toString(); - Log.debug("str: " + str.substring(1, str.length() - 1)); result.add(str.substring(1, str.length() - 1)); } } @@ -135,12 +134,6 @@ public class PlayerDataFile { return file; } - public void validateFile() { - if (!file.exists()) { - HeadDB.getInstance().saveResource(file.getName(), false); - } - } - public enum ModificationType { SET, REMOVE;