diff --git a/src/main/java/tsp/headdb/HeadDB.java b/src/main/java/tsp/headdb/HeadDB.java
index cc866b8..3d81696 100644
--- a/src/main/java/tsp/headdb/HeadDB.java
+++ b/src/main/java/tsp/headdb/HeadDB.java
@@ -67,7 +67,7 @@ public class HeadDB extends JavaPlugin {
initMetrics();
Utils.isLatestVersion(this, 84967, latest -> {
- if (!latest) {
+ if (!Boolean.TRUE.equals(latest)) {
Log.warning("There is a new update available for HeadDB on spigot!");
Log.warning("Download: https://www.spigotmc.org/resources/84967");
}
@@ -112,10 +112,6 @@ public class HeadDB extends JavaPlugin {
private void initMetrics() {
Metrics metrics = new Metrics(this, 9152);
- if (!metrics.isEnabled()) {
- Log.debug("Metrics are disabled.");
- return;
- }
metrics.addCustomChart(new Metrics.SimplePie("economy_provider", () -> {
if (this.getEconomyProvider() != null) {
diff --git a/src/main/java/tsp/headdb/api/HeadAPI.java b/src/main/java/tsp/headdb/api/HeadAPI.java
index 22a4e4a..2be3278 100644
--- a/src/main/java/tsp/headdb/api/HeadAPI.java
+++ b/src/main/java/tsp/headdb/api/HeadAPI.java
@@ -2,7 +2,6 @@ package tsp.headdb.api;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
-import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import tsp.headdb.HeadDB;
import tsp.headdb.implementation.Category;
@@ -14,8 +13,8 @@ import tsp.headdb.storage.PlayerDataFile;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
-import java.util.ArrayList;
import java.util.List;
+import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
@@ -29,6 +28,11 @@ public final class HeadAPI {
private HeadAPI() {}
+ private static final String VALIDATION_PLAYER_NULL = "Player can not be null!";
+ private static final String VALIDATION_CATEGORY_NULL = "Category can not be null!";
+ private static final String VALIDATION_UUID_NULL = "UUID can not be null!";
+ private static final String VALIDATION_VALUE_NULL = "Value can not be null!";
+
/**
* Main {@link HeadDatabase} that he HeadDB plugin uses.
*/
@@ -49,7 +53,7 @@ public final class HeadAPI {
* @param player Target player
*/
public static void openDatabase(@Nonnull Player player) {
- Validate.notNull(player, "Player can not be null!");
+ Validate.notNull(player, VALIDATION_PLAYER_NULL);
InventoryUtils.openDatabase(player);
}
@@ -61,8 +65,8 @@ public final class HeadAPI {
* @param category Category to open
*/
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!");
+ Validate.notNull(player, VALIDATION_PLAYER_NULL);
+ Validate.notNull(category, VALIDATION_CATEGORY_NULL);
InventoryUtils.openCategoryDatabase(player, category);
}
@@ -74,7 +78,7 @@ public final class HeadAPI {
* @param search Search term
*/
public static void openSearchDatabase(@Nonnull Player player, @Nonnull String search) {
- Validate.notNull(player, "Player can not be null!");
+ Validate.notNull(player, VALIDATION_PLAYER_NULL);
Validate.notNull(search, "Search can not be null!");
InventoryUtils.openSearchDatabase(player, search);
@@ -87,7 +91,7 @@ public final class HeadAPI {
* @param tag Tag search term
*/
public static void openTagSearchDatabase(@Nonnull Player player, @Nonnull String tag) {
- Validate.notNull(player, "Player can not be null!");
+ Validate.notNull(player, VALIDATION_PLAYER_NULL);
Validate.notNull(tag, "Tag can not be null!");
InventoryUtils.openTagSearchDatabase(player, tag);
@@ -112,7 +116,7 @@ public final class HeadAPI {
*/
@Nullable
public static Head getHeadByUniqueId(@Nonnull UUID uuid) {
- Validate.notNull(uuid, "UUID can not be null!");
+ Validate.notNull(uuid, VALIDATION_UUID_NULL);
return database.getHeadByUniqueId(uuid);
}
@@ -152,7 +156,7 @@ public final class HeadAPI {
*/
@Nonnull
public static List
getHeadsByName(@Nonnull Category category, @Nonnull String name) {
- Validate.notNull(category, "Category can not be null!");
+ Validate.notNull(category, VALIDATION_CATEGORY_NULL);
Validate.notNull(name, "Name can not be null!");
return database.getHeadsByName(category, name);
@@ -166,7 +170,7 @@ public final class HeadAPI {
*/
@Nullable
public static Head getHeadByValue(@Nonnull String value) {
- Validate.notNull(value, "Value can not be null!");
+ Validate.notNull(value, VALIDATION_VALUE_NULL);
return database.getHeadByValue(value);
}
@@ -179,7 +183,7 @@ public final class HeadAPI {
*/
@Nonnull
public static List getHeads(@Nonnull Category category) {
- Validate.notNull(category, "Category can not be null!");
+ Validate.notNull(category, VALIDATION_CATEGORY_NULL);
return database.getHeads(category);
}
@@ -201,8 +205,8 @@ public final class HeadAPI {
* @param textureValue The head's texture value
*/
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!");
+ Validate.notNull(uuid, VALIDATION_UUID_NULL);
+ Validate.notNull(textureValue, VALIDATION_VALUE_NULL);
HeadDB.getInstance().getPlayerData().modifyFavorite(uuid, textureValue, PlayerDataFile.ModificationType.SET);
}
@@ -214,8 +218,8 @@ public final class HeadAPI {
* @param textureValue The head's texture value
*/
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!");
+ Validate.notNull(uuid, VALIDATION_UUID_NULL);
+ Validate.notNull(textureValue, VALIDATION_VALUE_NULL);
HeadDB.getInstance().getPlayerData().modifyFavorite(uuid, textureValue, PlayerDataFile.ModificationType.REMOVE);
}
@@ -228,12 +232,12 @@ public final class HeadAPI {
*/
@Nonnull
public static List getFavoriteHeads(@Nonnull UUID uuid) {
- Validate.notNull(uuid, "UUID can not be null!");
+ Validate.notNull(uuid, VALIDATION_UUID_NULL);
return HeadDB.getInstance().getPlayerData().getFavoriteHeadsByTexture(uuid).stream()
.map(HeadAPI::getHeadByValue)
- .filter(head -> head != null)
- .collect(Collectors.toList());
+ .filter(Objects::nonNull)
+ .toList();
}
/**
@@ -248,7 +252,7 @@ public final class HeadAPI {
return HeadDB.getInstance().getPlayerData().getEntries().stream()
.map(entry -> Bukkit.getOfflinePlayer(UUID.fromString(entry)))
.map(player -> new LocalHead(player.getUniqueId()).name(player.getName()))
- .collect(Collectors.toList());
+ .toList();
}
}
diff --git a/src/main/java/tsp/headdb/command/HeadDBCommand.java b/src/main/java/tsp/headdb/command/HeadDBCommand.java
index e9f7516..da0f779 100644
--- a/src/main/java/tsp/headdb/command/HeadDBCommand.java
+++ b/src/main/java/tsp/headdb/command/HeadDBCommand.java
@@ -11,23 +11,21 @@ import tsp.headdb.util.Utils;
public class HeadDBCommand implements CommandExecutor {
- @Override
- public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
+ private void handle(CommandSender sender, String[] args) {
Localization localization = HeadDB.getInstance().getLocalization();
if (args.length == 0) {
if (!sender.hasPermission("headdb.open")) {
Utils.sendMessage(sender, localization.getMessage("noPermission"));
- return true;
+ return;
}
- if (!(sender instanceof Player)) {
+ if (!(sender instanceof Player player)) {
Utils.sendMessage(sender, localization.getMessage("onlyPlayers"));
- return true;
+ return;
}
- Player player = (Player) sender;
Utils.sendMessage(player, localization.getMessage("databaseOpen"));
HeadAPI.openDatabase(player);
- return true;
+ return;
}
String sub = args[0];
@@ -48,7 +46,7 @@ public class HeadDBCommand implements CommandExecutor {
if (subCommand != null) {
subCommand.handle(sender, args);
- return true;
+ return;
}
Utils.sendMessage(sender, " ");
@@ -62,6 +60,11 @@ public class HeadDBCommand implements CommandExecutor {
Utils.sendMessage(sender, "&7 > &c/hdb update &9(u) &7- Forcefully update the database");
Utils.sendMessage(sender, "&7 > &c/hdb give &9(g) &c &6[amount] &7- Give player a head");
Utils.sendMessage(sender, " ");
+ }
+
+ @Override
+ public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
+ handle(sender, args);
return true;
}
diff --git a/src/main/java/tsp/headdb/command/SearchCommand.java b/src/main/java/tsp/headdb/command/SearchCommand.java
index e709b93..792e276 100644
--- a/src/main/java/tsp/headdb/command/SearchCommand.java
+++ b/src/main/java/tsp/headdb/command/SearchCommand.java
@@ -13,11 +13,10 @@ public class SearchCommand implements HeadSubCommand {
Utils.sendMessage(sender, getLocalization().getMessage("noPermission"));
return;
}
- if (!(sender instanceof Player)) {
+ if (!(sender instanceof Player player)) {
Utils.sendMessage(sender, getLocalization().getMessage("onlyPlayers"));
return;
}
- Player player = (Player) sender;
if (args.length < 2) {
Utils.sendMessage(player, "&c/hdb search ");
@@ -34,7 +33,6 @@ public class SearchCommand implements HeadSubCommand {
String name = builder.toString();
Utils.sendMessage(sender, "&7Searching for &e" + name);
HeadAPI.openSearchDatabase(player, name);
- return;
}
}
diff --git a/src/main/java/tsp/headdb/command/TagSearchCommand.java b/src/main/java/tsp/headdb/command/TagSearchCommand.java
index 664c409..bf601e2 100644
--- a/src/main/java/tsp/headdb/command/TagSearchCommand.java
+++ b/src/main/java/tsp/headdb/command/TagSearchCommand.java
@@ -19,16 +19,14 @@ public class TagSearchCommand implements HeadSubCommand {
return;
}
- if (!(sender instanceof Player)) {
+ if (!(sender instanceof Player player)) {
Utils.sendMessage(sender, getLocalization().getMessage("onlyPlayers"));
return;
}
- Player player = (Player) sender;
String tag = args[1];
Utils.sendMessage(sender, "&7Searching for heads with tag &e" + tag);
HeadAPI.openTagSearchDatabase(player, tag);
- return;
}
}
diff --git a/src/main/java/tsp/headdb/economy/TreasuryProvider.java b/src/main/java/tsp/headdb/economy/TreasuryProvider.java
index 480f107..6b56682 100644
--- a/src/main/java/tsp/headdb/economy/TreasuryProvider.java
+++ b/src/main/java/tsp/headdb/economy/TreasuryProvider.java
@@ -34,16 +34,14 @@ public class TreasuryProvider implements BasicEconomyProvider {
EconomySubscriber
.asFuture(s -> provider.hasPlayerAccount(player.getUniqueId(), s))
.thenCompose(val -> {
- if (val) {
+ if (Boolean.TRUE.equals(val)) {
return EconomySubscriber.asFuture(s -> provider.retrievePlayerAccount(player.getUniqueId(), s));
} else {
return EconomySubscriber.asFuture(s -> provider.createPlayerAccount(player.getUniqueId(), s));
}
})
.thenCompose(account -> EconomySubscriber.asFuture(s -> account.retrieveBalance(currency, s)))
- .whenComplete((bal, ex) -> {
- result.accept(bal.compareTo(cost) >= 0);
- });
+ .whenComplete((bal, ex) -> result.accept(bal.compareTo(cost) >= 0));
}
@Override
@@ -51,29 +49,27 @@ public class TreasuryProvider implements BasicEconomyProvider {
EconomySubscriber
.asFuture(s -> provider.hasPlayerAccount(player.getUniqueId(), s))
.thenCompose(val -> {
- if (val) {
+ if (Boolean.TRUE.equals(val)) {
return EconomySubscriber.asFuture(s -> provider.retrievePlayerAccount(player.getUniqueId(), s));
} else {
return EconomySubscriber.asFuture(s -> provider.createPlayerAccount(player.getUniqueId(), s));
}
- }).whenComplete((account, ex) -> {
- account.withdrawBalance(
- amount,
- transactionInitiator,
- currency,
- new EconomySubscriber() {
- @Override
- public void succeed(@NotNull BigDecimal bigDecimal) {
- result.accept(true);
- }
+ }).whenComplete((account, ex) -> account.withdrawBalance(
+ amount,
+ transactionInitiator,
+ currency,
+ new EconomySubscriber() {
+ @Override
+ public void succeed(@NotNull BigDecimal bigDecimal) {
+ result.accept(true);
+ }
- @Override
- public void fail(@NotNull EconomyException exception) {
- result.accept(false);
- exception.printStackTrace();
- }
- });
- });
+ @Override
+ public void fail(@NotNull EconomyException exception) {
+ result.accept(false);
+ Log.error(ex);
+ }
+ }));
}
@Override
@@ -92,9 +88,10 @@ public class TreasuryProvider implements BasicEconomyProvider {
if (rawCurrency == null || rawCurrency.isEmpty()) {
currency = provider.getPrimaryCurrency();
} else {
- currency = provider.getCurrencies().stream()
+ provider.getCurrencies().stream()
.filter(currency -> currency.getIdentifier().equalsIgnoreCase(rawCurrency))
- .findFirst().get();
+ .findFirst()
+ .ifPresentOrElse(c -> currency = c, () -> Log.error("Could not find currency: " + rawCurrency));
}
}
diff --git a/src/main/java/tsp/headdb/implementation/Category.java b/src/main/java/tsp/headdb/implementation/Category.java
index 155f689..a5d1d19 100644
--- a/src/main/java/tsp/headdb/implementation/Category.java
+++ b/src/main/java/tsp/headdb/implementation/Category.java
@@ -1,12 +1,13 @@
package tsp.headdb.implementation;
import org.bukkit.ChatColor;
+import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import tsp.headdb.api.HeadAPI;
import javax.annotation.Nullable;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
/**
* Represents a category for heads
@@ -29,8 +30,7 @@ public enum Category {
private final String name;
private final ChatColor color;
private final int location;
- private final Map item = new HashMap<>();
- public static final Category[] cache = values();
+ private static final Category[] cache = values();
Category(String name, ChatColor color, int location) {
this.name = name;
@@ -56,9 +56,15 @@ public enum Category {
* @return First valid head
*/
public ItemStack getItem() {
- return HeadAPI.getHeads(this).stream()
- .filter(head -> head != null)
- .findFirst().get().getMenuItem();
+ Optional result = HeadAPI.getHeads(this).stream()
+ .filter(Objects::nonNull)
+ .findFirst();
+
+ if (result.isPresent()) {
+ return result.get().getMenuItem();
+ } else {
+ return new ItemStack(Material.PLAYER_HEAD);
+ }
}
/**
@@ -78,4 +84,8 @@ public enum Category {
return null;
}
+ public static Category[] getCache() {
+ return cache;
+ }
+
}
diff --git a/src/main/java/tsp/headdb/implementation/Head.java b/src/main/java/tsp/headdb/implementation/Head.java
index f30c0ff..a915576 100644
--- a/src/main/java/tsp/headdb/implementation/Head.java
+++ b/src/main/java/tsp/headdb/implementation/Head.java
@@ -28,7 +28,6 @@ public class Head {
private int id;
private List tags;
private ItemStack menuItem;
- //Unimplemented private ItemStack itemStack;
public Head() {}
@@ -55,7 +54,7 @@ public class Head {
profileField.set(meta, profile);
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException ex) {
Log.error("Could not set skull owner for " + uuid.toString() + " | Stack Trace:");
- ex.printStackTrace();
+ Log.error(ex);
}
meta.setLore(Arrays.asList(
@@ -72,24 +71,6 @@ public class Head {
return menuItem;
}
- /* For some reason this causes issues with some heads in categories not having lore
- public ItemStack getItemStack() {
- if (itemStack == null) {
- itemStack = menuItem;
- ItemMeta meta = itemStack.getItemMeta();
- meta.setDisplayName(HeadDB.getInstance().getLocalization().getMessage("head.name")
- .replace("%name%", name)
- .replace("%id%", String.valueOf(id))
- .replace("%value%", value)
- .replace("%tags%", buildTagLore(tags)));
- meta.setLore(HeadDB.getInstance().getLocalization().getData().getStringList("head.lore"));
- itemStack.setItemMeta(meta);
- }
-
- return itemStack;
- }
- */
-
public String getName() {
return name;
}
diff --git a/src/main/java/tsp/headdb/implementation/HeadDatabase.java b/src/main/java/tsp/headdb/implementation/HeadDatabase.java
index 488d3c8..b53f50b 100644
--- a/src/main/java/tsp/headdb/implementation/HeadDatabase.java
+++ b/src/main/java/tsp/headdb/implementation/HeadDatabase.java
@@ -19,6 +19,7 @@ import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@@ -36,7 +37,7 @@ import javax.annotation.Nonnull;
public class HeadDatabase {
private final JavaPlugin plugin;
- private final Map> HEADS = new HashMap<>();
+ private final EnumMap> heads = new EnumMap<>(Category.class);
private long refresh;
private int timeout;
private long updated;
@@ -49,8 +50,8 @@ public class HeadDatabase {
}
public Head getHeadByValue(String value) {
- List heads = getHeads();
- for (Head head : heads) {
+ List fetched = getHeads();
+ for (Head head : fetched) {
if (head.getValue().equals(value)) {
return head;
}
@@ -60,8 +61,8 @@ public class HeadDatabase {
}
public Head getHeadByID(int id) {
- List heads = getHeads();
- for (Head head : heads) {
+ List fetched = getHeads();
+ for (Head head : fetched) {
if (head.getId() == id) {
return head;
}
@@ -71,8 +72,8 @@ public class HeadDatabase {
}
public Head getHeadByUniqueId(UUID uuid) {
- List heads = getHeads();
- for (Head head : heads) {
+ List fetched = getHeads();
+ for (Head head : fetched) {
if (head.getUniqueId().equals(uuid)) {
return head;
}
@@ -83,9 +84,9 @@ public class HeadDatabase {
public List getHeadsByTag(String tag) {
List result = new ArrayList<>();
- List heads = getHeads();
+ List fetched = getHeads();
tag = tag.toLowerCase(Locale.ROOT);
- for (Head head : heads) {
+ for (Head head : fetched) {
for (String t : head.getTags()) {
if (t.toLowerCase(Locale.ROOT).contains(tag)) {
result.add(head);
@@ -98,8 +99,8 @@ public class HeadDatabase {
public List getHeadsByName(Category category, String name) {
List result = new ArrayList<>();
- List heads = getHeads(category);
- for (Head head : heads) {
+ List fetched = getHeads(category);
+ for (Head head : fetched) {
String hName = ChatColor.stripColor(head.getName().toLowerCase(Locale.ROOT));
if (hName.contains(ChatColor.stripColor(name.toLowerCase(Locale.ROOT)))) {
result.add(head);
@@ -111,7 +112,7 @@ public class HeadDatabase {
public List getHeadsByName(String name) {
List result = new ArrayList<>();
- for (Category category : Category.values()) {
+ for (Category category : Category.getCache()) {
result.addAll(getHeadsByName(category, name));
}
@@ -120,7 +121,7 @@ public class HeadDatabase {
@Nonnull
public List getHeads(Category category) {
- return HEADS.get(category) != null ? Collections.unmodifiableList(HEADS.get(category)) : new ArrayList<>();
+ return heads.get(category) != null ? Collections.unmodifiableList(heads.get(category)) : new ArrayList<>();
}
/**
@@ -130,18 +131,18 @@ public class HeadDatabase {
*/
@Nonnull
public List getHeads() {
- List heads = new ArrayList<>();
- for (Category category : HEADS.keySet()) {
- heads.addAll(getHeads(category));
+ List result = new ArrayList<>();
+ for (Category category : heads.keySet()) {
+ result.addAll(getHeads(category));
}
- return heads;
+ return result;
}
public void getHeadsNoCache(Consumer