diff --git a/pom.xml b/pom.xml
index 421e714..e7237f3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
tsp.headdb
HeadDB
- 2.4.5
+ 3.0.0
jar
HeadDB
@@ -43,12 +43,6 @@
1.18-R0.1-SNAPSHOT
provided
-
-
- com.googlecode.json-simple
- json-simple
- 1.1.1
-
com.mojang
@@ -56,12 +50,6 @@
1.5.21
provided
-
-
- com.github.simplix-softworks
- simplixstorage
- 3.2.0
-
net.wesjd
@@ -111,7 +99,7 @@
- tsp.headdb.HeadDB
+ tsp.headdb.legacy.HeadDBLegacy
diff --git a/src/main/java/tsp/headdb/HeadDB.java b/src/main/java/tsp/headdb/HeadDB.java
index 5c9aad4..7e600d0 100644
--- a/src/main/java/tsp/headdb/HeadDB.java
+++ b/src/main/java/tsp/headdb/HeadDB.java
@@ -1,32 +1,34 @@
package tsp.headdb;
-import de.leonhard.storage.Config;
+import net.milkbowl.vault.economy.Economy;
+import org.bukkit.Bukkit;
+import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import tsp.headdb.api.HeadAPI;
-import tsp.headdb.command.Command_headdb;
+import tsp.headdb.command.CommandHeadDB;
import tsp.headdb.listener.JoinListener;
-import tsp.headdb.listener.PagedPaneListener;
import tsp.headdb.listener.MenuListener;
+import tsp.headdb.listener.PagedPaneListener;
+import tsp.headdb.storage.PlayerDataFile;
import tsp.headdb.util.Log;
import tsp.headdb.util.Metrics;
-import tsp.headdb.util.Storage;
-import org.bukkit.plugin.RegisteredServiceProvider;
-import net.milkbowl.vault.economy.Economy;
public class HeadDB extends JavaPlugin {
private static HeadDB instance;
- private Storage storage;
- private Economy economy = null;
+ private Economy economy;
+ private PlayerDataFile playerData;
@Override
public void onEnable() {
instance = this;
Log.info("Loading HeadDB - " + getDescription().getVersion());
saveDefaultConfig();
- storage = new Storage().init(this);
- if (storage.getConfig().getBoolean("economy.enable")) {
+ this.playerData = new PlayerDataFile("player_data.json");
+ this.playerData.load();
+
+ if (getConfig().getBoolean("economy.enable")) {
Log.debug("Starting economy...");
this.economy = this.setupEconomy();
if (this.economy == null) {
@@ -36,29 +38,28 @@ public class HeadDB extends JavaPlugin {
}
}
- if (storage.getConfig().getBoolean("fetchStartup")) {
- if (storage.getConfig().getBoolean("asyncStartup")) {
- Log.debug("Initializing Database... (ASYNC)");
- HeadAPI.getDatabase().updateAsync();
- } else {
- Log.debug("Initializing Database... (SYNC)");
- HeadAPI.updateDatabase();
- }
- }
+ long refresh = getConfig().getLong("refresh") * 20;
+ HeadAPI.getDatabase().setRefresh(refresh);
+ Bukkit.getScheduler().runTaskTimerAsynchronously(this, task ->
+ HeadAPI.getDatabase().updateAsync(heads -> Log.info("Fetched " + HeadAPI.getHeads().size() + " heads!")),
+ 0L, refresh);
- Log.debug("Registering listeners...");
- new PagedPaneListener(this);
- new MenuListener(this);
new JoinListener(this);
+ new MenuListener(this);
+ new PagedPaneListener(this);
- Log.debug("Registering commands...");
- getCommand("headdb").setExecutor(new Command_headdb());
+ getCommand("headdb").setExecutor(new CommandHeadDB());
Log.debug("Starting metrics...");
new Metrics(this, 9152);
Log.info("Done!");
}
+ @Override
+ public void onDisable() {
+ this.playerData.save();
+ }
+
private Economy setupEconomy() {
if (!this.getServer().getPluginManager().isPluginEnabled("Vault")) return null;
@@ -68,16 +69,12 @@ public class HeadDB extends JavaPlugin {
return this.economy = economyProvider.getProvider();
}
- public Config getConfiguration() {
- return storage.getConfig();
- }
-
- public Storage getStorage() {
- return storage;
- }
-
public Economy getEconomy() {
- return this.economy;
+ return economy;
+ }
+
+ public PlayerDataFile getPlayerData() {
+ return playerData;
}
public static HeadDB getInstance() {
diff --git a/src/main/java/tsp/headdb/api/Head.java b/src/main/java/tsp/headdb/api/Head.java
index 13a188f..db754f1 100644
--- a/src/main/java/tsp/headdb/api/Head.java
+++ b/src/main/java/tsp/headdb/api/Head.java
@@ -23,6 +23,7 @@ public class Head {
private Category category;
private int id;
private List tags;
+ private ItemStack itemStack;
public Head() {}
@@ -31,35 +32,39 @@ public class Head {
}
public ItemStack getItemStack() {
- Validate.notNull(name, "name must not be null!");
- Validate.notNull(uuid, "uuid must not be null!");
- Validate.notNull(value, "value must not be null!");
+ if (itemStack == null) {
+ Validate.notNull(name, "name must not be null!");
+ Validate.notNull(uuid, "uuid must not be null!");
+ Validate.notNull(value, "value must not be null!");
- ItemStack item = new ItemStack(Material.PLAYER_HEAD);
- SkullMeta meta = (SkullMeta) item.getItemMeta();
- meta.setDisplayName(Utils.colorize(category != null ? category.getColor() + name : "&8" + name));
- // set skull owner
- GameProfile profile = new GameProfile(uuid, name);
- profile.getProperties().put("textures", new Property("textures", value));
- Field profileField;
- try {
- profileField = meta.getClass().getDeclaredField("profile");
- profileField.setAccessible(true);
- profileField.set(meta, profile);
- } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException ex) {
- Log.error("Could not set skull owner for " + uuid.toString() + " | Stack Trace:");
- ex.printStackTrace();
+ ItemStack item = new ItemStack(Material.PLAYER_HEAD);
+ SkullMeta meta = (SkullMeta) item.getItemMeta();
+ meta.setDisplayName(Utils.colorize(category != null ? category.getColor() + name : "&8" + name));
+ // set skull owner
+ GameProfile profile = new GameProfile(uuid, name);
+ profile.getProperties().put("textures", new Property("textures", value));
+ Field profileField;
+ try {
+ profileField = meta.getClass().getDeclaredField("profile");
+ profileField.setAccessible(true);
+ profileField.set(meta, profile);
+ } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException ex) {
+ Log.error("Could not set skull owner for " + uuid.toString() + " | Stack Trace:");
+ ex.printStackTrace();
+ }
+
+ meta.setLore(Arrays.asList(
+ Utils.colorize("&cID: " + id),
+ Utils.colorize("&e" + buildTagLore(tags)),
+ "",
+ Utils.colorize("&8Right-Click to add/remove from favorites.")
+ ));
+
+ item.setItemMeta(meta);
+ itemStack = item;
}
- meta.setLore(Arrays.asList(
- Utils.colorize("&cID: " + id),
- Utils.colorize("&e" + buildTagLore(tags)),
- "",
- Utils.colorize("&8Right-Click to add/remove from favorites.")
- ));
- item.setItemMeta(meta);
-
- return item;
+ return itemStack;
}
public String getName() {
@@ -86,36 +91,36 @@ public class Head {
return tags;
}
- public Head withName(String name) {
+ public Head name(String name) {
this.name = name;
return this;
}
- public Head withUniqueId(UUID uuid) {
+ public Head uniqueId(UUID uuid) {
this.uuid = uuid;
return this;
}
- public Head withValue(String value) {
+ public Head value(String value) {
this.value = value;
return this;
}
- public Head withCategory(Category category) {
+ public Head category(Category category) {
this.category = category;
return this;
}
- public Head withId(int id) {
+ public Head id(int id) {
this.id = id;
return this;
}
-
- public Head withTags(String tags) {
+
+ public Head tags(String tags) {
this.tags = Arrays.asList(tags.split(","));
return this;
}
-
+
private String buildTagLore(List tags) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < tags.size(); i++) {
@@ -124,7 +129,7 @@ public class Head {
builder.append(",");
}
}
-
+
return builder.toString();
}
diff --git a/src/main/java/tsp/headdb/api/HeadAPI.java b/src/main/java/tsp/headdb/api/HeadAPI.java
index a8efe4b..2a30ca0 100644
--- a/src/main/java/tsp/headdb/api/HeadAPI.java
+++ b/src/main/java/tsp/headdb/api/HeadAPI.java
@@ -7,6 +7,7 @@ import tsp.headdb.HeadDB;
import tsp.headdb.database.Category;
import tsp.headdb.database.HeadDatabase;
import tsp.headdb.inventory.InventoryUtils;
+import tsp.headdb.storage.PlayerDataFile;
import javax.annotation.Nullable;
import java.util.ArrayList;
@@ -143,77 +144,34 @@ public final class HeadAPI {
public static List getHeads() {
return database.getHeads();
}
-
- /**
- * Add a {@link Head} to a players favorites
- *
- * @param uuid The UUID of the player
- * @param id The ID of the head
- */
- public static void addFavoriteHead(UUID uuid, int id) {
- List favs = HeadDB.getInstance().getConfiguration().getIntegerList(uuid.toString() + ".favorites");
- if (!favs.contains(id)) {
- favs.add(id);
- }
- HeadDB.getInstance().getStorage().getPlayerData().set(uuid.toString() + ".favorites", favs);
+
+ public static void addFavoriteHead(UUID uuid, String textureValue) {
+ HeadDB.getInstance().getPlayerData().modifyFavorite(uuid, textureValue, PlayerDataFile.ModificationType.SET);
}
-
- /**
- * Remove a {@link Head} from a players favorites
- *
- * @param uuid The UUID of the player
- * @param id The ID of the head
- */
- public static void removeFavoriteHead(UUID uuid, int id) {
- List 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().getStorage().getPlayerData().set(uuid.toString() + ".favorites", favs);
+
+ 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} for a player
- *
- * @param uuid The UUID of the player
- * @return List of favorite heads
- */
+
public static List getFavoriteHeads(UUID uuid) {
- List heads = new ArrayList<>();
- List ids = HeadDB.getInstance().getStorage().getPlayerData().getIntegerList(uuid.toString() + ".favorites");
- for (int id : ids) {
- Head head = getHeadByID(id);
- heads.add(head);
+ List result = new ArrayList<>();
+
+ List textures = HeadDB.getInstance().getPlayerData().getFavoriteHeadsByTexture(uuid);
+ for (String texture : textures) {
+ result.add(HeadAPI.getHeadByValue(texture));
}
- return heads;
+ return result;
}
- /**
- * Retrieve a {@link List} of local heads.
- * These heads are from players that have joined the server at least once.
- *
- * @return List of {@link LocalHead}'s
- */
public static List getLocalHeads() {
- List heads = new ArrayList<>();
- for (String key : HeadDB.getInstance().getStorage().getPlayerData().singleLayerKeySet()) {
- OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(key));
- heads.add(new LocalHead(player.getUniqueId())
- .withName(player.getName()));
+ List result = new ArrayList<>();
+ for (String entry : HeadDB.getInstance().getPlayerData().getEntries()) {
+ OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(entry));
+ result.add(new LocalHead(player.getUniqueId()).name(player.getName()));
}
- return heads;
- }
-
- /**
- * Update the Head Database
- */
- public static boolean updateDatabase() {
- return database.update();
+ return result;
}
}
diff --git a/src/main/java/tsp/headdb/api/LocalHead.java b/src/main/java/tsp/headdb/api/LocalHead.java
index 111a7be..0460c12 100644
--- a/src/main/java/tsp/headdb/api/LocalHead.java
+++ b/src/main/java/tsp/headdb/api/LocalHead.java
@@ -68,13 +68,13 @@ public class LocalHead extends Head {
}
@Override
- public LocalHead withUniqueId(UUID uuid) {
+ public LocalHead uniqueId(UUID uuid) {
this.uuid = uuid;
return this;
}
@Override
- public LocalHead withName(String name) {
+ public LocalHead name(String name) {
this.name = name;
return this;
}
diff --git a/src/main/java/tsp/headdb/command/Command_headdb.java b/src/main/java/tsp/headdb/command/CommandHeadDB.java
similarity index 71%
rename from src/main/java/tsp/headdb/command/Command_headdb.java
rename to src/main/java/tsp/headdb/command/CommandHeadDB.java
index a01c2df..fd11ed7 100644
--- a/src/main/java/tsp/headdb/command/Command_headdb.java
+++ b/src/main/java/tsp/headdb/command/CommandHeadDB.java
@@ -13,7 +13,8 @@ import tsp.headdb.util.Utils;
import java.util.concurrent.TimeUnit;
-public class Command_headdb implements CommandExecutor {
+// TODO: Cleaner way to handle this command
+public class CommandHeadDB implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
@@ -28,16 +29,16 @@ public class Command_headdb implements CommandExecutor {
}
Player player = (Player) sender;
- Utils.sendMessage(player, "Opening &cHead Database");
+ Utils.sendMessage(player, "&7Opening &cHead Database");
HeadAPI.openDatabase(player);
return true;
}
String sub = args[0];
if (sub.equalsIgnoreCase("info") || sub.equalsIgnoreCase("i")) {
- Utils.sendMessage(sender, "Running &cHeadDB v" + HeadDB.getInstance().getDescription().getVersion());
- Utils.sendMessage(sender, "Created by &c" + HeadDB.getInstance().getDescription().getAuthors());
- Utils.sendMessage(sender, "There are currently &c" + HeadAPI.getHeads().size() + " &7heads in the database.");
+ Utils.sendMessage(sender, "&7Running &cHeadDB - " + HeadDB.getInstance().getDescription().getVersion());
+ Utils.sendMessage(sender, "&7Created by &c" + HeadDB.getInstance().getDescription().getAuthors());
+ Utils.sendMessage(sender, "&7There are currently &c" + HeadAPI.getHeads().size() + " &7heads in the database.");
return true;
}
@@ -64,7 +65,7 @@ public class Command_headdb implements CommandExecutor {
}
}
String name = builder.toString();
- Utils.sendMessage(sender, "Searching for &e" + name);
+ Utils.sendMessage(sender, "&7Searching for &e" + name);
HeadAPI.openSearchDatabase(player, name);
return true;
}
@@ -85,7 +86,7 @@ public class Command_headdb implements CommandExecutor {
Player player = (Player) sender;
String tag = args[1];
- Utils.sendMessage(sender, "Searching for heads with tag &e" + tag);
+ Utils.sendMessage(sender, "&7Searching for heads with tag &e" + tag);
HeadAPI.openTagSearchDatabase(player, tag);
return true;
}
@@ -136,38 +137,24 @@ public class Command_headdb implements CommandExecutor {
Utils.sendMessage(sender, "Updating...");
long start = System.currentTimeMillis();
- boolean result = HeadAPI.updateDatabase();
- if (result) {
- Utils.sendMessage(sender, "&aDone! Took: " + TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - start) + " seconds");
- } else {
- Utils.sendMessage(sender, "&cFailed! Check console for errors.");
- }
- return true;
- }
-
- if (sub.equalsIgnoreCase("updateasync") || sub.equalsIgnoreCase("ua")) {
- if (!sender.hasPermission("headdb.update")) {
- Utils.sendMessage(sender, "&cNo permission!");
- return true;
- }
-
- Utils.sendMessage(sender, "Updating...");
- HeadAPI.getDatabase().updateAsync();
+ HeadAPI.getDatabase().updateAsync(heads -> {
+ Utils.sendMessage(sender, "&7Done! Took: &a" + TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - start) + " &7seconds");
+ Utils.sendMessage(sender, "&7There are &a" + HeadAPI.getHeads() + " &7heads in the database!");
+ });
return true;
}
Utils.sendMessage(sender, " ");
Utils.sendMessage(sender, "&c&lHeadDB &c- &5Commands");
Utils.sendMessage(sender, "&7&oParameters:&c command &9(aliases)&c arguments... &7- Description");
- Utils.sendMessage(sender, " > &c/hdb &7- Opens the database");
- Utils.sendMessage(sender, " > &c/hdb info &9(i) &7- Plugin Information");
- Utils.sendMessage(sender, " > &c/hdb search &9(s) &c &7- Search for heads matching a name");
- Utils.sendMessage(sender, " > &c/hdb tagsearch &9(ts) &c &7- Search for heads matching a tag");
- Utils.sendMessage(sender, " > &c/hdb update &9(u) &7- Forcefully update the database");
- Utils.sendMessage(sender, " > &c/hdb updateasync &9(ua) &7- Forcefully update the database ASYNCHRONOUSLY");
- Utils.sendMessage(sender, " > &c/hdb give &9(g) &c &6[amount] &7- Give player a head");
+ Utils.sendMessage(sender, "&7 > &c/hdb &7- Opens the database");
+ Utils.sendMessage(sender, "&7 > &c/hdb info &9(i) &7- Plugin Information");
+ Utils.sendMessage(sender, "&7 > &c/hdb search &9(s) &c &7- Search for heads matching a name");
+ Utils.sendMessage(sender, "&7 > &c/hdb tagsearch &9(ts) &c &7- Search for heads matching a tag");
+ 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, " ");
return true;
}
-}
+}
\ No newline at end of file
diff --git a/src/main/java/tsp/headdb/database/Category.java b/src/main/java/tsp/headdb/database/Category.java
index 6214ad2..29114a3 100644
--- a/src/main/java/tsp/headdb/database/Category.java
+++ b/src/main/java/tsp/headdb/database/Category.java
@@ -25,7 +25,7 @@ public enum Category {
private final ChatColor color;
private final int location;
private final Map item = new HashMap<>();
- private static final Category[] values = values();
+ public static final Category[] cache = values();
Category(String name, ChatColor color, int location) {
this.name = name;
@@ -55,7 +55,7 @@ public enum Category {
}
public static Category getByName(String name) {
- for (Category category : values) {
+ for (Category category : cache) {
if (category.getName().equalsIgnoreCase(name)) {
return category;
}
@@ -64,8 +64,4 @@ public enum Category {
return null;
}
- public static Category[] getValues() {
- return values;
- }
-
}
diff --git a/src/main/java/tsp/headdb/database/HeadDatabase.java b/src/main/java/tsp/headdb/database/HeadDatabase.java
index f04c039..e6a2c15 100644
--- a/src/main/java/tsp/headdb/database/HeadDatabase.java
+++ b/src/main/java/tsp/headdb/database/HeadDatabase.java
@@ -23,8 +23,8 @@ import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
+import java.util.function.Consumer;
import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
/**
* This is the Database that holds all heads
@@ -127,7 +127,14 @@ public class HeadDatabase {
@Nonnull
public List getHeads() {
if (HEADS.isEmpty() || isLastUpdateOld()) {
- update();
+ // Technically this should never be reached due to the update task in the main class.
+ updateAsync(result -> {
+ if (result != null) {
+ for (Category category : result.keySet()) {
+ HEADS.put(category, result.get(category));
+ }
+ }
+ });
}
List heads = new ArrayList<>();
@@ -137,92 +144,80 @@ public class HeadDatabase {
return heads;
}
- /**
- * Gets all heads from the api provider
- *
- * @return Map containing each head in its category. Returns null if the fetching failed.
- */
- @Nullable
- public Map> getHeadsNoCache() {
- Map> result = new HashMap<>();
- Category[] categories = Category.getValues();
+ public void getHeadsNoCacheAsync(Consumer