Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to only get existing profiles #495

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -463,6 +464,48 @@ public GlobalProfile getGlobalProfile(String playerName, UUID playerUUID) {
return profile;
}

@Override
public Optional<GlobalProfile> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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<GlobalProfile> 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!");
}
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.onarandombox.multiverseinventories.profile.container.ContainerType;

import java.io.IOException;
import java.util.Optional;
import java.util.UUID;

/**
Expand Down Expand Up @@ -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<GlobalProfile> getExistingGlobalProfile(String playerName, UUID playerUUID);

/**
* Update the file for a player's global profile.
*
Expand Down