From 87d1ff3a18afc7d7aee42a995aa97482cfc2c00f Mon Sep 17 00:00:00 2001 From: Silent Date: Sun, 27 Feb 2022 00:04:32 +0100 Subject: [PATCH] documentation, refactoring --- src/main/java/tsp/headdb/HeadDB.java | 3 + src/main/java/tsp/headdb/api/HeadAPI.java | 60 +++++++++++++++---- .../api/event/PlayerHeadPurchaseEvent.java | 1 + .../tsp/headdb/economy/TreasuryProvider.java | 4 +- .../tsp/headdb/implementation/Category.java | 5 ++ .../implementation/DatabaseUpdateTask.java | 3 + .../java/tsp/headdb/implementation/Head.java | 5 ++ .../tsp/headdb/implementation/LocalHead.java | 5 ++ .../tsp/headdb/inventory/InventoryUtils.java | 2 +- .../tsp/headdb/listener/JoinListener.java | 4 ++ .../tsp/headdb/listener/MenuListener.java | 3 + .../tsp/headdb/storage/PlayerDataFile.java | 3 + src/main/java/tsp/headdb/util/Utils.java | 3 + src/main/resources/messages.yml | 7 ++- 14 files changed, 90 insertions(+), 18 deletions(-) diff --git a/src/main/java/tsp/headdb/HeadDB.java b/src/main/java/tsp/headdb/HeadDB.java index 7cd0a45..695be82 100644 --- a/src/main/java/tsp/headdb/HeadDB.java +++ b/src/main/java/tsp/headdb/HeadDB.java @@ -20,6 +20,9 @@ import tsp.headdb.util.Utils; import javax.annotation.Nullable; import java.io.File; +/** + * Main class of HeadDB + */ public class HeadDB extends JavaPlugin { private static HeadDB instance; diff --git a/src/main/java/tsp/headdb/api/HeadAPI.java b/src/main/java/tsp/headdb/api/HeadAPI.java index 80a6b53..22a4e4a 100644 --- a/src/main/java/tsp/headdb/api/HeadAPI.java +++ b/src/main/java/tsp/headdb/api/HeadAPI.java @@ -1,5 +1,6 @@ package tsp.headdb.api; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; @@ -47,7 +48,9 @@ public final class HeadAPI { * * @param player Target player */ - public static void openDatabase(Player player) { + public static void openDatabase(@Nonnull Player player) { + Validate.notNull(player, "Player can not be null!"); + InventoryUtils.openDatabase(player); } @@ -57,7 +60,10 @@ public final class HeadAPI { * @param player Target player * @param category Category to open */ - public static void openCategoryDatabase(Player player, Category category) { + public static void openCategoryDatabase(@Nonnull Player player, @Nonnull Category category) { + Validate.notNull(player, "Player can not be null!"); + Validate.notNull(category, "Category can not be null!"); + InventoryUtils.openCategoryDatabase(player, category); } @@ -67,7 +73,10 @@ public final class HeadAPI { * @param player Target player * @param search Search term */ - public static void openSearchDatabase(Player player, String search) { + public static void openSearchDatabase(@Nonnull Player player, @Nonnull String search) { + Validate.notNull(player, "Player can not be null!"); + Validate.notNull(search, "Search can not be null!"); + InventoryUtils.openSearchDatabase(player, search); } @@ -77,7 +86,10 @@ public final class HeadAPI { * @param player Target player * @param tag Tag search term */ - public static void openTagSearchDatabase(Player player, String tag) { + public static void openTagSearchDatabase(@Nonnull Player player, @Nonnull String tag) { + Validate.notNull(player, "Player can not be null!"); + Validate.notNull(tag, "Tag can not be null!"); + InventoryUtils.openTagSearchDatabase(player, tag); } @@ -99,7 +111,9 @@ public final class HeadAPI { * @return The head */ @Nullable - public static Head getHeadByUniqueId(UUID uuid) { + public static Head getHeadByUniqueId(@Nonnull UUID uuid) { + Validate.notNull(uuid, "UUID can not be null!"); + return database.getHeadByUniqueId(uuid); } @@ -110,7 +124,9 @@ public final class HeadAPI { * @return List of heads */ @Nonnull - public static List getHeadsByTag(String tag) { + public static List getHeadsByTag(@Nonnull String tag) { + Validate.notNull(tag, "Tag can not be null!"); + return database.getHeadsByTag(tag); } @@ -121,7 +137,9 @@ public final class HeadAPI { * @return List of heads */ @Nonnull - public static List getHeadsByName(String name) { + public static List getHeadsByName(@Nonnull String name) { + Validate.notNull(name, "Name can not be null!"); + return database.getHeadsByName(name); } @@ -133,7 +151,10 @@ public final class HeadAPI { * @return List of heads */ @Nonnull - public static List getHeadsByName(Category category, String name) { + public static List getHeadsByName(@Nonnull Category category, @Nonnull String name) { + Validate.notNull(category, "Category can not be null!"); + Validate.notNull(name, "Name can not be null!"); + return database.getHeadsByName(category, name); } @@ -144,7 +165,9 @@ public final class HeadAPI { * @return The head */ @Nullable - public static Head getHeadByValue(String value) { + public static Head getHeadByValue(@Nonnull String value) { + Validate.notNull(value, "Value can not be null!"); + return database.getHeadByValue(value); } @@ -155,7 +178,9 @@ public final class HeadAPI { * @return List of heads */ @Nonnull - public static List getHeads(Category category) { + public static List getHeads(@Nonnull Category category) { + Validate.notNull(category, "Category can not be null!"); + return database.getHeads(category); } @@ -175,7 +200,10 @@ public final class HeadAPI { * @param uuid The player's unique id * @param textureValue The head's texture value */ - public static void addFavoriteHead(UUID uuid, String textureValue) { + public static void addFavoriteHead(@Nonnull UUID uuid, @Nonnull String textureValue) { + Validate.notNull(uuid, "UUID can not be null!"); + Validate.notNull(textureValue, "Value can not be null!"); + HeadDB.getInstance().getPlayerData().modifyFavorite(uuid, textureValue, PlayerDataFile.ModificationType.SET); } @@ -185,7 +213,10 @@ public final class HeadAPI { * @param uuid The player's unique id * @param textureValue The head's texture value */ - public static void removeFavoriteHead(UUID uuid, String textureValue) { + public static void removeFavoriteHead(@Nonnull UUID uuid, @Nonnull String textureValue) { + Validate.notNull(uuid, "UUID can not be null!"); + Validate.notNull(textureValue, "Value can not be null!"); + HeadDB.getInstance().getPlayerData().modifyFavorite(uuid, textureValue, PlayerDataFile.ModificationType.REMOVE); } @@ -196,9 +227,12 @@ public final class HeadAPI { * @return List of favorite {@link Head}'s for the player */ @Nonnull - public static List getFavoriteHeads(UUID uuid) { + public static List getFavoriteHeads(@Nonnull UUID uuid) { + Validate.notNull(uuid, "UUID can not be null!"); + return HeadDB.getInstance().getPlayerData().getFavoriteHeadsByTexture(uuid).stream() .map(HeadAPI::getHeadByValue) + .filter(head -> head != null) .collect(Collectors.toList()); } diff --git a/src/main/java/tsp/headdb/api/event/PlayerHeadPurchaseEvent.java b/src/main/java/tsp/headdb/api/event/PlayerHeadPurchaseEvent.java index 55cbbc5..a9faf87 100644 --- a/src/main/java/tsp/headdb/api/event/PlayerHeadPurchaseEvent.java +++ b/src/main/java/tsp/headdb/api/event/PlayerHeadPurchaseEvent.java @@ -21,6 +21,7 @@ public class PlayerHeadPurchaseEvent extends Event implements Cancellable { private double cost; public PlayerHeadPurchaseEvent(Player player, Head head, double cost) { + super(true); this.player = player; this.head = head; this.cost = cost; diff --git a/src/main/java/tsp/headdb/economy/TreasuryProvider.java b/src/main/java/tsp/headdb/economy/TreasuryProvider.java index 1db6b9e..480f107 100644 --- a/src/main/java/tsp/headdb/economy/TreasuryProvider.java +++ b/src/main/java/tsp/headdb/economy/TreasuryProvider.java @@ -26,6 +26,7 @@ import java.util.function.Consumer; public class TreasuryProvider implements BasicEconomyProvider { private EconomyProvider provider; + private EconomyTransactionInitiator transactionInitiator; private Currency currency; @Override @@ -58,7 +59,7 @@ public class TreasuryProvider implements BasicEconomyProvider { }).whenComplete((account, ex) -> { account.withdrawBalance( amount, - EconomyTransactionInitiator.createInitiator(EconomyTransactionInitiator.Type.PLUGIN, "HeadDB"), + transactionInitiator, currency, new EconomySubscriber() { @Override @@ -85,6 +86,7 @@ public class TreasuryProvider implements BasicEconomyProvider { } provider = service.get().get(); + transactionInitiator = EconomyTransactionInitiator.createInitiator(EconomyTransactionInitiator.Type.PLUGIN, "HeadDB"); String rawCurrency = HeadDB.getInstance().getConfig().getString("economy.currency"); if (rawCurrency == null || rawCurrency.isEmpty()) { diff --git a/src/main/java/tsp/headdb/implementation/Category.java b/src/main/java/tsp/headdb/implementation/Category.java index 0a3f4a1..155f689 100644 --- a/src/main/java/tsp/headdb/implementation/Category.java +++ b/src/main/java/tsp/headdb/implementation/Category.java @@ -8,6 +8,11 @@ import javax.annotation.Nullable; import java.util.HashMap; import java.util.Map; +/** + * Represents a category for heads + * + * @author TheSilentPro + */ public enum Category { ALPHABET("alphabet", ChatColor.YELLOW, 20), diff --git a/src/main/java/tsp/headdb/implementation/DatabaseUpdateTask.java b/src/main/java/tsp/headdb/implementation/DatabaseUpdateTask.java index 21e00b4..9889629 100644 --- a/src/main/java/tsp/headdb/implementation/DatabaseUpdateTask.java +++ b/src/main/java/tsp/headdb/implementation/DatabaseUpdateTask.java @@ -4,6 +4,9 @@ import tsp.headdb.HeadDB; import tsp.headdb.api.HeadAPI; import tsp.headdb.util.Log; +/** + * Task that updates the database on an interval + */ public class DatabaseUpdateTask implements Runnable { @Override diff --git a/src/main/java/tsp/headdb/implementation/Head.java b/src/main/java/tsp/headdb/implementation/Head.java index 19b0aa5..f30c0ff 100644 --- a/src/main/java/tsp/headdb/implementation/Head.java +++ b/src/main/java/tsp/headdb/implementation/Head.java @@ -14,6 +14,11 @@ import java.util.Arrays; import java.util.List; import java.util.UUID; +/** + * Represents a Head that a player can obtain via the database + * + * @author TheSilentPro + */ public class Head { private String name; diff --git a/src/main/java/tsp/headdb/implementation/LocalHead.java b/src/main/java/tsp/headdb/implementation/LocalHead.java index 0207b43..df47e3c 100644 --- a/src/main/java/tsp/headdb/implementation/LocalHead.java +++ b/src/main/java/tsp/headdb/implementation/LocalHead.java @@ -11,6 +11,11 @@ import java.util.ArrayList; import java.util.List; import java.util.UUID; +/** + * Represents a local player head that can be obtained via the LocalHeads option + * + * @author TheSilentPro + */ public class LocalHead extends Head { private UUID uuid; diff --git a/src/main/java/tsp/headdb/inventory/InventoryUtils.java b/src/main/java/tsp/headdb/inventory/InventoryUtils.java index 93c042e..c58c970 100644 --- a/src/main/java/tsp/headdb/inventory/InventoryUtils.java +++ b/src/main/java/tsp/headdb/inventory/InventoryUtils.java @@ -351,7 +351,7 @@ public class InventoryUtils { } public static void purchaseHead(Player player, Head head, int amount, String category, String description) { - Utils.sendMessage(player, "&7Purchasing &e" + amount + "x " + head.getName() + "&7. Please wait..."); + Utils.sendMessage(player, String.format(localization.getMessage("processPayment"), amount, head.getName())); processPayment(player, amount, category, description, result -> { if (result) { PlayerHeadPurchaseEvent event = new PlayerHeadPurchaseEvent(player, head, getCategoryCost(player, category)); diff --git a/src/main/java/tsp/headdb/listener/JoinListener.java b/src/main/java/tsp/headdb/listener/JoinListener.java index 18044ae..f2f46e7 100644 --- a/src/main/java/tsp/headdb/listener/JoinListener.java +++ b/src/main/java/tsp/headdb/listener/JoinListener.java @@ -6,6 +6,10 @@ import org.bukkit.event.player.PlayerJoinEvent; import tsp.headdb.HeadDB; import tsp.headdb.storage.PlayerDataFile; +/** + * This saves heads from players that join + * Used for local heads option + */ public class JoinListener implements Listener { public JoinListener(HeadDB plugin) { diff --git a/src/main/java/tsp/headdb/listener/MenuListener.java b/src/main/java/tsp/headdb/listener/MenuListener.java index 13dc904..f565b53 100644 --- a/src/main/java/tsp/headdb/listener/MenuListener.java +++ b/src/main/java/tsp/headdb/listener/MenuListener.java @@ -16,6 +16,9 @@ import tsp.headdb.implementation.Category; import tsp.headdb.inventory.InventoryUtils; import tsp.headdb.util.Utils; +/** + * This handles all clicks on the main inventory + */ public class MenuListener implements Listener { public MenuListener(JavaPlugin plugin) { diff --git a/src/main/java/tsp/headdb/storage/PlayerDataFile.java b/src/main/java/tsp/headdb/storage/PlayerDataFile.java index 8ca8f2b..f7999ce 100644 --- a/src/main/java/tsp/headdb/storage/PlayerDataFile.java +++ b/src/main/java/tsp/headdb/storage/PlayerDataFile.java @@ -19,6 +19,9 @@ import java.util.List; import java.util.Set; import java.util.UUID; +/** + * Manages the data file that stores information + */ public class PlayerDataFile { private final File file; diff --git a/src/main/java/tsp/headdb/util/Utils.java b/src/main/java/tsp/headdb/util/Utils.java index 6607fa1..ea4a41e 100644 --- a/src/main/java/tsp/headdb/util/Utils.java +++ b/src/main/java/tsp/headdb/util/Utils.java @@ -18,6 +18,9 @@ import java.net.URLConnection; import java.util.function.Consumer; import java.util.regex.Pattern; +/** + * Several utilities used by the plugin + */ public class Utils { private static final FileConfiguration config = HeadDB.getInstance().getConfig(); diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index 6838218..1f0b546 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -4,9 +4,10 @@ databaseOpen: "&7Opening &cHead Database" invalidPlayer: "&cPlayer is not online!" localFavorites: "&cLocal heads can not be added to favorites!" noEconomy: "&7You received &e%d &7x &e%s&7!" -purchasedHead: "&7You purchased &e%d &7x &e%s &7for &e%.2f&7!" -notEnoughMoney: "&cYou do not have enough to purchase &e%d &cx &e%s&7." -free: "&7You received &e%d &7x &e%s &7for &efree&7!" +purchasedHead: "&7You purchased &e%dx %s &7for &e%.2f&7!" +proccessPayment: "&7Purchasing &e%dx %s&7. Please wait..." +notEnoughMoney: "&cYou do not have enough to purchase &e%dx %s&c." +free: "&7You received &e%dx %s &7for &efree&7!" reloadMessages: "&aReloaded messages file!" menu: