Add storage manager

This commit is contained in:
Ryder Belserion 2023-02-28 02:59:44 -05:00
parent 65e390131f
commit 1ad63d1a56
No known key found for this signature in database
GPG Key ID: 8FC2E6C54BBF05FE
6 changed files with 107 additions and 31 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -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());
}
}