mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-16 18:58:00 +00:00
- Refactor GamePlayerUtil.
This commit is contained in:
@@ -15,56 +15,59 @@ public final class GamePlayerUtil {
|
|||||||
|
|
||||||
public static void setPlayerName() {
|
public static void setPlayerName() {
|
||||||
|
|
||||||
String playerName = prefs.getPref(FPref.PLAYER_NAME);
|
String oldPlayerName = prefs.getPref(FPref.PLAYER_NAME);
|
||||||
String newName = null;
|
String newPlayerName = null;
|
||||||
|
|
||||||
if (StringUtils.isBlank(playerName)) {
|
if (StringUtils.isBlank(oldPlayerName)) {
|
||||||
newName = (String)JOptionPane.showInputDialog(
|
newPlayerName = getVerifiedPlayerName(getPlayerNameUsingFirstTimePrompt(), oldPlayerName);
|
||||||
|
} else {
|
||||||
|
newPlayerName = getVerifiedPlayerName(getPlayerNameUsingStandardPrompt(oldPlayerName), oldPlayerName);
|
||||||
|
}
|
||||||
|
|
||||||
|
prefs.setPref(FPref.PLAYER_NAME, newPlayerName);
|
||||||
|
prefs.save();
|
||||||
|
|
||||||
|
if (StringUtils.isBlank(oldPlayerName) && newPlayerName != "Human") {
|
||||||
|
showThankYouPrompt(newPlayerName);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void showThankYouPrompt(String playerName) {
|
||||||
|
JOptionPane.showMessageDialog(
|
||||||
|
JOptionPane.getRootFrame(),
|
||||||
|
"Thank you, " + playerName + ". " +
|
||||||
|
"You will not be prompted again but you can change\nyour name at any time using the \"Player Name\" setting in Preferences.\n\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getPlayerNameUsingFirstTimePrompt() {
|
||||||
|
return (String)JOptionPane.showInputDialog(
|
||||||
JOptionPane.getRootFrame(),
|
JOptionPane.getRootFrame(),
|
||||||
"By default, Forge will refer to you as the \"Human\" during gameplay.\n" +
|
"By default, Forge will refer to you as the \"Human\" during gameplay.\n" +
|
||||||
"If you would prefer a different name please enter it now.\n",
|
"If you would prefer a different name please enter it now.\n",
|
||||||
"Personalize Forge Gameplay",
|
"Personalize Forge Gameplay",
|
||||||
JOptionPane.QUESTION_MESSAGE,
|
JOptionPane.QUESTION_MESSAGE,
|
||||||
null, null, null);
|
null, null, null);
|
||||||
} else {
|
|
||||||
newName = getNewPlayerNameFromInputDialog(playerName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newName == null || !StringUtils.isAlphanumericSpace(newName)) {
|
private static String getPlayerNameUsingStandardPrompt(String playerName) {
|
||||||
newName = (StringUtils.isBlank(playerName) ? "Human" : playerName);
|
return (String)JOptionPane.showInputDialog(
|
||||||
} else if (StringUtils.isWhitespace(newName)) {
|
|
||||||
newName = "Human";
|
|
||||||
} else {
|
|
||||||
newName = newName.trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
prefs.setPref(FPref.PLAYER_NAME, newName);
|
|
||||||
prefs.save();
|
|
||||||
|
|
||||||
if (StringUtils.isBlank(playerName) && newName != "Human") {
|
|
||||||
JOptionPane.showMessageDialog(
|
|
||||||
JOptionPane.getRootFrame(),
|
|
||||||
"Thank you, " + newName + ". " +
|
|
||||||
"You will not be prompted again but you can change\nyour name at any time using the \"Player Name\" setting in Preferences.\n\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String getNewPlayerNameFromInputDialog(String playerName) {
|
|
||||||
String newName =
|
|
||||||
(String)JOptionPane.showInputDialog(
|
|
||||||
JOptionPane.getRootFrame(),
|
JOptionPane.getRootFrame(),
|
||||||
"Please enter a new name (alpha-numeric only)\n",
|
"Please enter a new name (alpha-numeric only)\n",
|
||||||
"Personalize Forge Gameplay",
|
"Personalize Forge Gameplay",
|
||||||
JOptionPane.PLAIN_MESSAGE,
|
JOptionPane.PLAIN_MESSAGE,
|
||||||
null, null, playerName);
|
null, null, playerName);
|
||||||
if (newName == null || !StringUtils.isAlphanumericSpace(newName)) {
|
|
||||||
return playerName;
|
|
||||||
} else if (StringUtils.isWhitespace(newName)) {
|
|
||||||
return "Human";
|
|
||||||
} else {
|
|
||||||
return newName.trim();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String getVerifiedPlayerName(String newName, String oldName) {
|
||||||
|
if (newName == null || !StringUtils.isAlphanumericSpace(newName)) {
|
||||||
|
newName = (StringUtils.isBlank(oldName) ? "Human" : oldName);
|
||||||
|
} else if (StringUtils.isWhitespace(newName)) {
|
||||||
|
newName = "Human";
|
||||||
|
} else {
|
||||||
|
newName = newName.trim();
|
||||||
|
}
|
||||||
|
return newName;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user