diff --git a/core/src/main/java/us/crazycrew/crazyauctions/data/ProfileCache.java b/core/src/main/java/us/crazycrew/crazyauctions/data/ProfileCache.java deleted file mode 100644 index cb88007..0000000 --- a/core/src/main/java/us/crazycrew/crazyauctions/data/ProfileCache.java +++ /dev/null @@ -1,15 +0,0 @@ -package us.crazycrew.crazyauctions.data; - -import java.util.UUID; - -public interface ProfileCache { - - void load(); - - void save(); - - void add(final UUID uuid); - - String getName(final UUID uuid); - -} \ No newline at end of file diff --git a/core/src/main/java/us/crazycrew/crazyauctions/data/UserCache.java b/core/src/main/java/us/crazycrew/crazyauctions/data/UserCache.java new file mode 100644 index 0000000..5b603e9 --- /dev/null +++ b/core/src/main/java/us/crazycrew/crazyauctions/data/UserCache.java @@ -0,0 +1,39 @@ +package us.crazycrew.crazyauctions.data; + +import java.nio.file.Path; +import java.util.UUID; + +public interface UserCache { + + /** + * Add a player to the hashmap if absent. + * + * @param uuid player uuid + */ + void addPlayer(final UUID uuid); + + /** + * Remove the player from the hashmap. + * + * @param uuid player uuid + */ + void removePlayer(final UUID uuid); + + /** + * Fetch the player if online or offline. + * + * @param uuid player uuid + * @return player object + */ + String getPlayerName(final UUID uuid); + + /** + * Fetch the uuid file of the player. + * + * @param path the path i.e. 'CrazyAuctions/userdata/random-uuid.' + * @param uuid the player uuid + * @return the complete path + */ + Path getFile(final Path path, UUID uuid); + +} \ No newline at end of file diff --git a/platforms/paper/src/main/java/us/crazycrew/crazyauctions/CrazyAuctions.java b/platforms/paper/src/main/java/us/crazycrew/crazyauctions/CrazyAuctions.java index 730666f..ca6044f 100644 --- a/platforms/paper/src/main/java/us/crazycrew/crazyauctions/CrazyAuctions.java +++ b/platforms/paper/src/main/java/us/crazycrew/crazyauctions/CrazyAuctions.java @@ -2,6 +2,7 @@ package us.crazycrew.crazyauctions; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; +import us.crazycrew.crazyauctions.configs.StorageManager; import us.crazycrew.crazycore.CrazyLogger; import us.crazycrew.crazycore.paper.PaperCore; import java.io.File; @@ -16,6 +17,8 @@ public class CrazyAuctions extends JavaPlugin { private final File users; + private StorageManager storageManager; + public CrazyAuctions(PaperCore paperCore) { this.paperCore = paperCore; @@ -35,6 +38,8 @@ public class CrazyAuctions extends JavaPlugin { public void onEnable() { // Enable the player registry. getCrazyCore().createPlayerRegistry(this); + + this.storageManager = new StorageManager(); } @Override @@ -51,6 +56,10 @@ public class CrazyAuctions extends JavaPlugin { } public Path getUsers() { - return users.toPath(); + return this.users.toPath(); + } + + public StorageManager getStorageManager() { + return this.storageManager; } } \ No newline at end of file diff --git a/platforms/paper/src/main/java/us/crazycrew/crazyauctions/configs/StorageManager.java b/platforms/paper/src/main/java/us/crazycrew/crazyauctions/configs/StorageManager.java index 1177d3f..2f41aba 100644 --- a/platforms/paper/src/main/java/us/crazycrew/crazyauctions/configs/StorageManager.java +++ b/platforms/paper/src/main/java/us/crazycrew/crazyauctions/configs/StorageManager.java @@ -1,5 +1,62 @@ package us.crazycrew.crazyauctions.configs; -public class StorageManager { +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; +import us.crazycrew.crazyauctions.api.interfaces.Universal; +import us.crazycrew.crazyauctions.configs.data.PlayerData; +import us.crazycrew.crazyauctions.data.UserCache; +import java.nio.file.Path; +import java.util.UUID; +public class StorageManager implements Universal, UserCache { + + /** + * Add a player to the hashmap if absent. + * + * @param uuid player uuid + */ + @Override + public void addPlayer(UUID uuid) { + PlayerData.auctions.putIfAbsent(uuid, ""); + } + + /** + * Remove the player from the hashmap. + * + * @param uuid player uuid + */ + @Override + public void removePlayer(UUID uuid) { + PlayerData.auctions.remove(uuid); + } + + /** + * Fetch the player if online or offline. + * + * @param uuid player uuid + * @return player object + */ + @Override + public String getPlayerName(UUID uuid) { + Player player = plugin.getServer().getPlayer(uuid); + + assert player != null; + if (player.isOnline()) return player.getName(); + + OfflinePlayer offlinePlayer = plugin.getServer().getOfflinePlayer(uuid); + + return offlinePlayer.getName(); + } + + /** + * Fetch the uuid file of the player. + * + * @param path the path i.e. 'CrazyAuctions/userdata/random-uuid.' + * @param uuid the player uuid + * @return the complete path + */ + @Override + public Path getFile(Path path, UUID uuid) { + return path.resolve(uuid + ".json"); + } } \ No newline at end of file diff --git a/platforms/paper/src/main/java/us/crazycrew/crazyauctions/configs/data/PlayerData.java b/platforms/paper/src/main/java/us/crazycrew/crazyauctions/configs/data/PlayerData.java index 541f3a7..44b122b 100644 --- a/platforms/paper/src/main/java/us/crazycrew/crazyauctions/configs/data/PlayerData.java +++ b/platforms/paper/src/main/java/us/crazycrew/crazyauctions/configs/data/PlayerData.java @@ -3,7 +3,6 @@ package us.crazycrew.crazyauctions.configs.data; import com.google.gson.annotations.Expose; import us.crazycrew.crazyauctions.api.interfaces.Universal; import us.crazycrew.crazycore.files.FileExtension; -import java.nio.file.Path; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; @@ -23,14 +22,4 @@ public class PlayerData extends FileExtension implements Universal { public static void save(UUID uuid) { plugin.getCrazyCore().getFileHandler().saveFile(new PlayerData(uuid)); } - - /** - * Fetch the uuid file of the player. - * - * @param playerData the path i.e. 'CrazyAuctions/userdata/random-uuid.' - * @return the complete path - */ - public static Path getFile(Path playerData, UUID uuid) { - return playerData.resolve(uuid + ".json"); - } } \ No newline at end of file diff --git a/platforms/paper/src/main/java/us/crazycrew/crazyauctions/events/TestListener.java b/platforms/paper/src/main/java/us/crazycrew/crazyauctions/events/TestListener.java index 811c4d6..0d60cd5 100644 --- a/platforms/paper/src/main/java/us/crazycrew/crazyauctions/events/TestListener.java +++ b/platforms/paper/src/main/java/us/crazycrew/crazyauctions/events/TestListener.java @@ -6,7 +6,6 @@ import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import us.crazycrew.crazyauctions.api.interfaces.Universal; import us.crazycrew.crazyauctions.configs.data.PlayerData; -import java.io.File; public class TestListener implements Listener, Universal { @@ -17,8 +16,6 @@ public class TestListener implements Listener, Universal { @EventHandler public void onPlayerQuit(PlayerQuitEvent event) { - File playerData = PlayerData.getFile(plugin.getUsers(), event.getPlayer().getUniqueId()).toFile(); - PlayerData.save(event.getPlayer().getUniqueId()); } } \ No newline at end of file