- Refactor GamePlayerUtil.

This commit is contained in:
spr
2013-10-08 10:04:32 +00:00
parent 7650ec9323
commit 6e1e22d6cb

View File

@@ -15,56 +15,59 @@ public final class GamePlayerUtil {
public static void setPlayerName() {
String playerName = prefs.getPref(FPref.PLAYER_NAME);
String newName = null;
String oldPlayerName = prefs.getPref(FPref.PLAYER_NAME);
String newPlayerName = null;
if (StringUtils.isBlank(playerName)) {
newName = (String)JOptionPane.showInputDialog(
JOptionPane.getRootFrame(),
"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",
"Personalize Forge Gameplay",
JOptionPane.QUESTION_MESSAGE,
null, null, null);
if (StringUtils.isBlank(oldPlayerName)) {
newPlayerName = getVerifiedPlayerName(getPlayerNameUsingFirstTimePrompt(), oldPlayerName);
} else {
newName = getNewPlayerNameFromInputDialog(playerName);
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(),
"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",
"Personalize Forge Gameplay",
JOptionPane.QUESTION_MESSAGE,
null, null, null);
}
private static String getPlayerNameUsingStandardPrompt(String playerName) {
return (String)JOptionPane.showInputDialog(
JOptionPane.getRootFrame(),
"Please enter a new name (alpha-numeric only)\n",
"Personalize Forge Gameplay",
JOptionPane.PLAIN_MESSAGE,
null, null, playerName);
}
private static String getVerifiedPlayerName(String newName, String oldName) {
if (newName == null || !StringUtils.isAlphanumericSpace(newName)) {
newName = (StringUtils.isBlank(playerName) ? "Human" : playerName);
newName = (StringUtils.isBlank(oldName) ? "Human" : oldName);
} 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(),
"Please enter a new name (alpha-numeric only)\n",
"Personalize Forge Gameplay",
JOptionPane.PLAIN_MESSAGE,
null, null, playerName);
if (newName == null || !StringUtils.isAlphanumericSpace(newName)) {
return playerName;
} else if (StringUtils.isWhitespace(newName)) {
return "Human";
} else {
return newName.trim();
}
return newName;
}
}