From f587a73779f22fc8d93858f7df765cf5b72a9ab8 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Sun, 6 Mar 2022 15:53:06 +0800 Subject: [PATCH] Add method to only get existing profiles Helps reduce unnecessary files created --- .../FlatFileProfileDataSource.java | 43 +++++++++++++++++++ .../InventoriesListener.java | 35 ++++++++------- .../profile/ProfileDataSource.java | 11 +++++ 3 files changed, 73 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/onarandombox/multiverseinventories/FlatFileProfileDataSource.java b/src/main/java/com/onarandombox/multiverseinventories/FlatFileProfileDataSource.java index 040d31f6..197864af 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/FlatFileProfileDataSource.java +++ b/src/main/java/com/onarandombox/multiverseinventories/FlatFileProfileDataSource.java @@ -25,6 +25,7 @@ import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Optional; import java.util.UUID; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; @@ -463,6 +464,48 @@ public GlobalProfile getGlobalProfile(String playerName, UUID playerUUID) { return profile; } + @Override + public Optional getExistingGlobalProfile(String playerName, UUID playerUUID) { + GlobalProfile cached = globalProfileCache.getIfPresent(playerUUID); + if (cached != null) { + return Optional.of(cached); + } + + File playerFile; + + // Migrate old data if necessary + try { + playerFile = getGlobalFile(playerName, false); + } catch (IOException e) { + // This won't ever happen + e.printStackTrace(); + return Optional.empty(); + } + if (playerFile.exists()) { + GlobalProfile profile = loadGlobalProfile(playerFile, playerName, playerUUID); + if (!migrateGlobalProfileToUUID(profile, playerFile)) { + Logging.warning("Could not properly migrate player global data file for " + playerName); + } + globalProfileCache.put(playerUUID, profile); + return Optional.of(profile); + } + + // Load current format + try { + playerFile = getGlobalFile(playerUUID.toString(), false); + } catch (IOException e) { + e.printStackTrace(); + return Optional.empty(); + } + if (playerFile.exists()) { + GlobalProfile profile = loadGlobalProfile(playerFile, playerName, playerUUID); + globalProfileCache.put(playerUUID, profile); + return Optional.of(profile); + } else { + return Optional.empty(); + } + } + private boolean migrateGlobalProfileToUUID(GlobalProfile profile, File playerFile) { updateGlobalProfile(profile); return playerFile.delete(); diff --git a/src/main/java/com/onarandombox/multiverseinventories/InventoriesListener.java b/src/main/java/com/onarandombox/multiverseinventories/InventoriesListener.java index 8e1d8e4d..5508b5a1 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/InventoriesListener.java +++ b/src/main/java/com/onarandombox/multiverseinventories/InventoriesListener.java @@ -37,6 +37,7 @@ import java.io.File; import java.io.IOException; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; /** @@ -120,23 +121,25 @@ public void playerPreLogin(AsyncPlayerPreLoginEvent event) { Logging.finer("Loading global profile for Player{name:'%s', uuid:'%s'}.", event.getName(), event.getUniqueId()); - GlobalProfile globalProfile = inventories.getData().getGlobalProfile(event.getName(), event.getUniqueId()); - if (!globalProfile.getLastKnownName().equalsIgnoreCase(event.getName())) { - // Data must be migrated - Logging.info("Player %s changed name from '%s' to '%s'. Attempting to migrate playerdata...", - event.getUniqueId(), globalProfile.getLastKnownName(), event.getName()); - try { - inventories.getData().migratePlayerData(globalProfile.getLastKnownName(), event.getName(), - event.getUniqueId(), true); - } catch (IOException e) { - Logging.severe("An error occurred while trying to migrate playerdata."); - e.printStackTrace(); - } + Optional existingGlobalProfile = inventories.getData().getExistingGlobalProfile(event.getName(), event.getUniqueId()); + existingGlobalProfile.ifPresent(globalProfile -> { + if (!globalProfile.getLastKnownName().equalsIgnoreCase(event.getName())) { + // Data must be migrated + Logging.info("Player %s changed name from '%s' to '%s'. Attempting to migrate playerdata...", + event.getUniqueId(), globalProfile.getLastKnownName(), event.getName()); + try { + inventories.getData().migratePlayerData(globalProfile.getLastKnownName(), event.getName(), + event.getUniqueId(), true); + } catch (IOException e) { + Logging.severe("An error occurred while trying to migrate playerdata."); + e.printStackTrace(); + } - globalProfile.setLastKnownName(event.getName()); - inventories.getData().updateGlobalProfile(globalProfile); - Logging.info("Migration complete!"); - } + globalProfile.setLastKnownName(event.getName()); + inventories.getData().updateGlobalProfile(globalProfile); + Logging.info("Migration complete!"); + } + }); } /** diff --git a/src/main/java/com/onarandombox/multiverseinventories/profile/ProfileDataSource.java b/src/main/java/com/onarandombox/multiverseinventories/profile/ProfileDataSource.java index 15427986..0596efca 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/profile/ProfileDataSource.java +++ b/src/main/java/com/onarandombox/multiverseinventories/profile/ProfileDataSource.java @@ -3,6 +3,7 @@ import com.onarandombox.multiverseinventories.profile.container.ContainerType; import java.io.IOException; +import java.util.Optional; import java.util.UUID; /** @@ -61,6 +62,16 @@ public interface ProfileDataSource { */ GlobalProfile getGlobalProfile(String playerName, UUID playerUUID); + /** + * Retrieves the global profile for a player which contains meta-data for the player. + * Does not create profile for new players. + * + * @param playerName The name of the player to retrieve for. This is required for updating name last known as. + * @param playerUUID The UUID of the player. + * @return the global profile for the player with the given UUID, if exists. + */ + Optional getExistingGlobalProfile(String playerName, UUID playerUUID); + /** * Update the file for a player's global profile. *