Remove old data support
This commit is contained in:
parent
0604cc4e1d
commit
ae0959c028
|
@ -1,9 +1,10 @@
|
|||
package com.badbones69.crazyauctions.api.enums.support;
|
||||
|
||||
/*
|
||||
import com.badbones69.crazyauctions.storage.interfaces.UserManager;
|
||||
import com.badbones69.crazyauctions.storage.types.file.yaml.YamlUserManager;
|
||||
|
||||
public enum DataSupport {
|
||||
|
||||
//json(JsonUserManager.class),
|
||||
yaml(YamlUserManager.class);
|
||||
|
||||
private final Class<? extends UserManager> classObject;
|
||||
|
@ -15,5 +16,4 @@ public enum DataSupport {
|
|||
public Class<? extends UserManager> getClassObject() {
|
||||
return this.classObject;
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
package com.badbones69.crazyauctions.storage;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import com.badbones69.crazyauctions.api.interfaces.Universal;
|
||||
import com.badbones69.crazyauctions.storage.data.PlayerData;
|
||||
import com.badbones69.crazyauctions.data.UserCache;
|
||||
import java.nio.file.Path;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Description: Handles player data for auctions.
|
||||
*/
|
||||
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");
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
package com.badbones69.crazyauctions.storage.data;
|
||||
|
||||
import com.badbones69.crazyauctions.frame.storage.FileExtension;
|
||||
import com.badbones69.crazyauctions.frame.storage.enums.StorageType;
|
||||
import com.google.gson.annotations.Expose;
|
||||
import com.badbones69.crazyauctions.api.interfaces.Universal;
|
||||
import java.io.File;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class PlayerData extends FileExtension implements Universal {
|
||||
|
||||
@Expose
|
||||
public static ConcurrentHashMap<UUID, String> auctions = new ConcurrentHashMap<>();
|
||||
|
||||
public PlayerData(UUID uuid) {
|
||||
super("hi", new File("").toPath(), StorageType.YAML);
|
||||
//super(uuid + ".json", plugin.getUsers(), FileType.YAML);
|
||||
}
|
||||
|
||||
public static void load(UUID uuid) {
|
||||
//plugin.getCrazyCore().getFileHandler().addFile(new PlayerData(uuid));
|
||||
}
|
||||
|
||||
public static void save(UUID uuid) {
|
||||
//plugin.getCrazyCore().getFileHandler().saveFile(new PlayerData(uuid));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.badbones69.crazyauctions.storage.interfaces;
|
||||
|
||||
import com.badbones69.crazyauctions.frame.storage.enums.StorageType;
|
||||
import com.badbones69.crazyauctions.storage.objects.UserData;
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface UserManager {
|
||||
|
||||
void load(UUID uuid);
|
||||
|
||||
void saveSingular(UUID uuid, boolean serverExit);
|
||||
|
||||
void save(UUID uuid, boolean serverExit);
|
||||
|
||||
void convert(File file, UUID uuid, StorageType storageType);
|
||||
|
||||
void addAuction(UUID uuid);
|
||||
|
||||
Path getFile(Path path, UUID uuid);
|
||||
|
||||
UserData getUser(UUID uuid);
|
||||
|
||||
Map<UUID, UserData> getUsers();
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.badbones69.crazyauctions.storage.objects;
|
||||
|
||||
import com.badbones69.crazyauctions.CrazyAuctions;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class UserData {
|
||||
|
||||
private final CrazyAuctions plugin = JavaPlugin.getPlugin(CrazyAuctions.class);
|
||||
|
||||
private final UUID uuid;
|
||||
|
||||
private ConcurrentHashMap<String, String> auctionData = new ConcurrentHashMap<>();
|
||||
|
||||
public UserData(UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return this.plugin.getServer().getPlayer(this.uuid);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,108 @@
|
|||
package com.badbones69.crazyauctions.storage.types.file.yaml;
|
||||
|
||||
import com.badbones69.crazyauctions.CrazyAuctions;
|
||||
import com.badbones69.crazyauctions.frame.storage.enums.StorageType;
|
||||
import com.badbones69.crazyauctions.frame.storage.types.file.yaml.keys.FilePath;
|
||||
import com.badbones69.crazyauctions.storage.interfaces.UserManager;
|
||||
import com.badbones69.crazyauctions.storage.objects.UserData;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class YamlUserManager extends YamlConfiguration implements UserManager {
|
||||
|
||||
private final CrazyAuctions plugin = JavaPlugin.getPlugin(CrazyAuctions.class);
|
||||
|
||||
private final File file;
|
||||
|
||||
private final ConcurrentHashMap<UUID, UserData> userData = new ConcurrentHashMap<>();
|
||||
|
||||
public YamlUserManager(Path path, UUID uuid) {
|
||||
this.file = path.resolve(uuid + ".yml").toFile();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(UUID uuid) {
|
||||
try {
|
||||
if (!this.file.exists()) this.file.createNewFile();
|
||||
|
||||
load(this.file);
|
||||
} catch (IOException | InvalidConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveSingular(UUID uuid, boolean serverExit) {
|
||||
// If user data empty return.
|
||||
if (this.userData.isEmpty()) return;
|
||||
|
||||
// Check if user data contains keys.
|
||||
if (this.userData.containsKey(uuid)) {
|
||||
// Remove user when done.
|
||||
this.userData.remove(uuid);
|
||||
|
||||
// Save the file then load the changes back in.
|
||||
reload(uuid, serverExit);
|
||||
}
|
||||
}
|
||||
|
||||
private void reload(UUID uuid, boolean serverExit) {
|
||||
try {
|
||||
save(this.file);
|
||||
if (!serverExit) load(uuid);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(UUID uuid, boolean serverExit) {
|
||||
// If user data empty return.
|
||||
if (this.userData.isEmpty()) return;
|
||||
|
||||
// If the player is not leaving, continue here as we are stopping the server or doing periodic save.
|
||||
this.userData.forEach((id, user) -> {
|
||||
//user.getKeys().forEach((crateMap, keys) -> set("users." + id + "." + crateMap, keys));
|
||||
|
||||
// Save the file then load the changes back in.
|
||||
reload(uuid, serverExit);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void convert(File file, UUID uuid, StorageType storageType) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAuction(UUID uuid) {
|
||||
Player player = this.plugin.getServer().getPlayer(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path getFile(Path path, UUID uuid) {
|
||||
return path.resolve(uuid + ".yml");
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserData getUser(UUID uuid) {
|
||||
Player player = this.plugin.getServer().getPlayer(uuid);
|
||||
|
||||
// Return with their user data.
|
||||
return this.userData.get(uuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<UUID, UserData> getUsers() {
|
||||
return Collections.unmodifiableMap(this.userData);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue