3
0
Mirror von https://github.com/TheSilentPro/HeadDB.git synchronisiert 2024-12-27 11:20:05 +01:00

documentation, refactoring

Dieser Commit ist enthalten in:
Silent 2022-02-27 00:04:32 +01:00
Ursprung c551871f2e
Commit 87d1ff3a18
14 geänderte Dateien mit 90 neuen und 18 gelöschten Zeilen

Datei anzeigen

@ -20,6 +20,9 @@ import tsp.headdb.util.Utils;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.io.File; import java.io.File;
/**
* Main class of HeadDB
*/
public class HeadDB extends JavaPlugin { public class HeadDB extends JavaPlugin {
private static HeadDB instance; private static HeadDB instance;

Datei anzeigen

@ -1,5 +1,6 @@
package tsp.headdb.api; package tsp.headdb.api;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -47,7 +48,9 @@ public final class HeadAPI {
* *
* @param player Target player * @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); InventoryUtils.openDatabase(player);
} }
@ -57,7 +60,10 @@ public final class HeadAPI {
* @param player Target player * @param player Target player
* @param category Category to open * @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); InventoryUtils.openCategoryDatabase(player, category);
} }
@ -67,7 +73,10 @@ public final class HeadAPI {
* @param player Target player * @param player Target player
* @param search Search term * @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); InventoryUtils.openSearchDatabase(player, search);
} }
@ -77,7 +86,10 @@ public final class HeadAPI {
* @param player Target player * @param player Target player
* @param tag Tag search term * @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); InventoryUtils.openTagSearchDatabase(player, tag);
} }
@ -99,7 +111,9 @@ public final class HeadAPI {
* @return The head * @return The head
*/ */
@Nullable @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); return database.getHeadByUniqueId(uuid);
} }
@ -110,7 +124,9 @@ public final class HeadAPI {
* @return List of heads * @return List of heads
*/ */
@Nonnull @Nonnull
public static List<Head> getHeadsByTag(String tag) { public static List<Head> getHeadsByTag(@Nonnull String tag) {
Validate.notNull(tag, "Tag can not be null!");
return database.getHeadsByTag(tag); return database.getHeadsByTag(tag);
} }
@ -121,7 +137,9 @@ public final class HeadAPI {
* @return List of heads * @return List of heads
*/ */
@Nonnull @Nonnull
public static List<Head> getHeadsByName(String name) { public static List<Head> getHeadsByName(@Nonnull String name) {
Validate.notNull(name, "Name can not be null!");
return database.getHeadsByName(name); return database.getHeadsByName(name);
} }
@ -133,7 +151,10 @@ public final class HeadAPI {
* @return List of heads * @return List of heads
*/ */
@Nonnull @Nonnull
public static List<Head> getHeadsByName(Category category, String name) { public static List<Head> 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); return database.getHeadsByName(category, name);
} }
@ -144,7 +165,9 @@ public final class HeadAPI {
* @return The head * @return The head
*/ */
@Nullable @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); return database.getHeadByValue(value);
} }
@ -155,7 +178,9 @@ public final class HeadAPI {
* @return List of heads * @return List of heads
*/ */
@Nonnull @Nonnull
public static List<Head> getHeads(Category category) { public static List<Head> getHeads(@Nonnull Category category) {
Validate.notNull(category, "Category can not be null!");
return database.getHeads(category); return database.getHeads(category);
} }
@ -175,7 +200,10 @@ public final class HeadAPI {
* @param uuid The player's unique id * @param uuid The player's unique id
* @param textureValue The head's texture value * @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); 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 uuid The player's unique id
* @param textureValue The head's texture value * @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); 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 * @return List of favorite {@link Head}'s for the player
*/ */
@Nonnull @Nonnull
public static List<Head> getFavoriteHeads(UUID uuid) { public static List<Head> getFavoriteHeads(@Nonnull UUID uuid) {
Validate.notNull(uuid, "UUID can not be null!");
return HeadDB.getInstance().getPlayerData().getFavoriteHeadsByTexture(uuid).stream() return HeadDB.getInstance().getPlayerData().getFavoriteHeadsByTexture(uuid).stream()
.map(HeadAPI::getHeadByValue) .map(HeadAPI::getHeadByValue)
.filter(head -> head != null)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }

Datei anzeigen

@ -21,6 +21,7 @@ public class PlayerHeadPurchaseEvent extends Event implements Cancellable {
private double cost; private double cost;
public PlayerHeadPurchaseEvent(Player player, Head head, double cost) { public PlayerHeadPurchaseEvent(Player player, Head head, double cost) {
super(true);
this.player = player; this.player = player;
this.head = head; this.head = head;
this.cost = cost; this.cost = cost;

Datei anzeigen

@ -26,6 +26,7 @@ import java.util.function.Consumer;
public class TreasuryProvider implements BasicEconomyProvider { public class TreasuryProvider implements BasicEconomyProvider {
private EconomyProvider provider; private EconomyProvider provider;
private EconomyTransactionInitiator<?> transactionInitiator;
private Currency currency; private Currency currency;
@Override @Override
@ -58,7 +59,7 @@ public class TreasuryProvider implements BasicEconomyProvider {
}).whenComplete((account, ex) -> { }).whenComplete((account, ex) -> {
account.withdrawBalance( account.withdrawBalance(
amount, amount,
EconomyTransactionInitiator.createInitiator(EconomyTransactionInitiator.Type.PLUGIN, "HeadDB"), transactionInitiator,
currency, currency,
new EconomySubscriber<BigDecimal>() { new EconomySubscriber<BigDecimal>() {
@Override @Override
@ -85,6 +86,7 @@ public class TreasuryProvider implements BasicEconomyProvider {
} }
provider = service.get().get(); provider = service.get().get();
transactionInitiator = EconomyTransactionInitiator.createInitiator(EconomyTransactionInitiator.Type.PLUGIN, "HeadDB");
String rawCurrency = HeadDB.getInstance().getConfig().getString("economy.currency"); String rawCurrency = HeadDB.getInstance().getConfig().getString("economy.currency");
if (rawCurrency == null || rawCurrency.isEmpty()) { if (rawCurrency == null || rawCurrency.isEmpty()) {

Datei anzeigen

@ -8,6 +8,11 @@ import javax.annotation.Nullable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
/**
* Represents a category for heads
*
* @author TheSilentPro
*/
public enum Category { public enum Category {
ALPHABET("alphabet", ChatColor.YELLOW, 20), ALPHABET("alphabet", ChatColor.YELLOW, 20),

Datei anzeigen

@ -4,6 +4,9 @@ import tsp.headdb.HeadDB;
import tsp.headdb.api.HeadAPI; import tsp.headdb.api.HeadAPI;
import tsp.headdb.util.Log; import tsp.headdb.util.Log;
/**
* Task that updates the database on an interval
*/
public class DatabaseUpdateTask implements Runnable { public class DatabaseUpdateTask implements Runnable {
@Override @Override

Datei anzeigen

@ -14,6 +14,11 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
/**
* Represents a Head that a player can obtain via the database
*
* @author TheSilentPro
*/
public class Head { public class Head {
private String name; private String name;

Datei anzeigen

@ -11,6 +11,11 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
/**
* Represents a local player head that can be obtained via the LocalHeads option
*
* @author TheSilentPro
*/
public class LocalHead extends Head { public class LocalHead extends Head {
private UUID uuid; private UUID uuid;

Datei anzeigen

@ -351,7 +351,7 @@ public class InventoryUtils {
} }
public static void purchaseHead(Player player, Head head, int amount, String category, String description) { 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 -> { processPayment(player, amount, category, description, result -> {
if (result) { if (result) {
PlayerHeadPurchaseEvent event = new PlayerHeadPurchaseEvent(player, head, getCategoryCost(player, category)); PlayerHeadPurchaseEvent event = new PlayerHeadPurchaseEvent(player, head, getCategoryCost(player, category));

Datei anzeigen

@ -6,6 +6,10 @@ import org.bukkit.event.player.PlayerJoinEvent;
import tsp.headdb.HeadDB; import tsp.headdb.HeadDB;
import tsp.headdb.storage.PlayerDataFile; import tsp.headdb.storage.PlayerDataFile;
/**
* This saves heads from players that join
* Used for local heads option
*/
public class JoinListener implements Listener { public class JoinListener implements Listener {
public JoinListener(HeadDB plugin) { public JoinListener(HeadDB plugin) {

Datei anzeigen

@ -16,6 +16,9 @@ import tsp.headdb.implementation.Category;
import tsp.headdb.inventory.InventoryUtils; import tsp.headdb.inventory.InventoryUtils;
import tsp.headdb.util.Utils; import tsp.headdb.util.Utils;
/**
* This handles all clicks on the main inventory
*/
public class MenuListener implements Listener { public class MenuListener implements Listener {
public MenuListener(JavaPlugin plugin) { public MenuListener(JavaPlugin plugin) {

Datei anzeigen

@ -19,6 +19,9 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
/**
* Manages the data file that stores information
*/
public class PlayerDataFile { public class PlayerDataFile {
private final File file; private final File file;

Datei anzeigen

@ -18,6 +18,9 @@ import java.net.URLConnection;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/**
* Several utilities used by the plugin
*/
public class Utils { public class Utils {
private static final FileConfiguration config = HeadDB.getInstance().getConfig(); private static final FileConfiguration config = HeadDB.getInstance().getConfig();

Datei anzeigen

@ -4,9 +4,10 @@ databaseOpen: "&7Opening &cHead Database"
invalidPlayer: "&cPlayer is not online!" invalidPlayer: "&cPlayer is not online!"
localFavorites: "&cLocal heads can not be added to favorites!" localFavorites: "&cLocal heads can not be added to favorites!"
noEconomy: "&7You received &e%d &7x &e%s&7!" noEconomy: "&7You received &e%d &7x &e%s&7!"
purchasedHead: "&7You purchased &e%d &7x &e%s &7for &e%.2f&7!" purchasedHead: "&7You purchased &e%dx %s &7for &e%.2f&7!"
notEnoughMoney: "&cYou do not have enough to purchase &e%d &cx &e%s&7." proccessPayment: "&7Purchasing &e%dx %s&7. Please wait..."
free: "&7You received &e%d &7x &e%s &7for &efree&7!" 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!" reloadMessages: "&aReloaded messages file!"
menu: menu: