001 package net.minecraft.src; 002 003 import cpw.mods.fml.common.Side; 004 import cpw.mods.fml.common.asm.SideOnly; 005 import java.util.Random; 006 import org.lwjgl.input.Keyboard; 007 008 @SideOnly(Side.CLIENT) 009 public class GuiCreateWorld extends GuiScreen 010 { 011 private GuiScreen parentGuiScreen; 012 private GuiTextField textboxWorldName; 013 private GuiTextField textboxSeed; 014 private String folderName; 015 016 /** hardcore', 'creative' or 'survival */ 017 private String gameMode = "survival"; 018 private boolean field_73925_n = true; 019 private boolean field_73926_o = false; 020 private boolean field_73935_p = false; 021 private boolean field_73934_q = false; 022 private boolean field_73933_r = false; 023 private boolean createClicked; 024 025 /** 026 * True if the extra options (Seed box, structure toggle button, world type button, etc.) are being shown 027 */ 028 private boolean moreOptions; 029 030 /** The GUIButton that you click to change game modes. */ 031 private GuiButton gameModeButton; 032 033 /** 034 * The GUIButton that you click to get to options like the seed when creating a world. 035 */ 036 private GuiButton moreWorldOptions; 037 038 /** The GuiButton in the 'More World Options' screen. Toggles ON/OFF */ 039 private GuiButton generateStructuresButton; 040 private GuiButton field_73938_x; 041 042 /** 043 * the GUIButton in the more world options screen. It's currently greyed out and unused in minecraft 1.0.0 044 */ 045 private GuiButton worldTypeButton; 046 private GuiButton field_73936_z; 047 048 /** The first line of text describing the currently selected game mode. */ 049 private String gameModeDescriptionLine1; 050 051 /** The second line of text describing the currently selected game mode. */ 052 private String gameModeDescriptionLine2; 053 054 /** The current textboxSeed text */ 055 private String seed; 056 057 /** E.g. New World, Neue Welt, Nieuwe wereld, Neuvo Mundo */ 058 private String localizedNewWorldText; 059 private int field_73916_E = 0; 060 private static final String[] field_73917_F = new String[] {"CON", "COM", "PRN", "AUX", "CLOCK$", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"}; 061 062 public GuiCreateWorld(GuiScreen par1GuiScreen) 063 { 064 this.parentGuiScreen = par1GuiScreen; 065 this.seed = ""; 066 this.localizedNewWorldText = StatCollector.translateToLocal("selectWorld.newWorld"); 067 } 068 069 /** 070 * Called from the main game loop to update the screen. 071 */ 072 public void updateScreen() 073 { 074 this.textboxWorldName.updateCursorCounter(); 075 this.textboxSeed.updateCursorCounter(); 076 } 077 078 /** 079 * Adds the buttons (and other controls) to the screen in question. 080 */ 081 public void initGui() 082 { 083 StringTranslate var1 = StringTranslate.getInstance(); 084 Keyboard.enableRepeatEvents(true); 085 this.controlList.clear(); 086 this.controlList.add(new GuiButton(0, this.width / 2 - 155, this.height - 28, 150, 20, var1.translateKey("selectWorld.create"))); 087 this.controlList.add(new GuiButton(1, this.width / 2 + 5, this.height - 28, 150, 20, var1.translateKey("gui.cancel"))); 088 this.controlList.add(this.gameModeButton = new GuiButton(2, this.width / 2 - 75, 100, 150, 20, var1.translateKey("selectWorld.gameMode"))); 089 this.controlList.add(this.moreWorldOptions = new GuiButton(3, this.width / 2 - 75, 172, 150, 20, var1.translateKey("selectWorld.moreWorldOptions"))); 090 this.controlList.add(this.generateStructuresButton = new GuiButton(4, this.width / 2 - 155, 100, 150, 20, var1.translateKey("selectWorld.mapFeatures"))); 091 this.generateStructuresButton.drawButton = false; 092 this.controlList.add(this.field_73938_x = new GuiButton(7, this.width / 2 + 5, 136, 150, 20, var1.translateKey("selectWorld.bonusItems"))); 093 this.field_73938_x.drawButton = false; 094 this.controlList.add(this.worldTypeButton = new GuiButton(5, this.width / 2 + 5, 100, 150, 20, var1.translateKey("selectWorld.mapType"))); 095 this.worldTypeButton.drawButton = false; 096 this.controlList.add(this.field_73936_z = new GuiButton(6, this.width / 2 - 155, 136, 150, 20, var1.translateKey("selectWorld.allowCommands"))); 097 this.field_73936_z.drawButton = false; 098 this.textboxWorldName = new GuiTextField(this.fontRenderer, this.width / 2 - 100, 60, 200, 20); 099 this.textboxWorldName.setFocused(true); 100 this.textboxWorldName.setText(this.localizedNewWorldText); 101 this.textboxSeed = new GuiTextField(this.fontRenderer, this.width / 2 - 100, 60, 200, 20); 102 this.textboxSeed.setText(this.seed); 103 this.makeUseableName(); 104 this.func_73914_h(); 105 } 106 107 /** 108 * Makes a the name for a world save folder based on your world name, replacing specific characters for _s and 109 * appending -s to the end until a free name is available. 110 */ 111 private void makeUseableName() 112 { 113 this.folderName = this.textboxWorldName.getText().trim(); 114 char[] var1 = ChatAllowedCharacters.invalidFilenameCharacters; 115 int var2 = var1.length; 116 117 for (int var3 = 0; var3 < var2; ++var3) 118 { 119 char var4 = var1[var3]; 120 this.folderName = this.folderName.replace(var4, '_'); 121 } 122 123 if (MathHelper.stringNullOrLengthZero(this.folderName)) 124 { 125 this.folderName = "World"; 126 } 127 128 this.folderName = func_73913_a(this.mc.getSaveLoader(), this.folderName); 129 } 130 131 private void func_73914_h() 132 { 133 StringTranslate var1 = StringTranslate.getInstance(); 134 this.gameModeButton.displayString = var1.translateKey("selectWorld.gameMode") + " " + var1.translateKey("selectWorld.gameMode." + this.gameMode); 135 this.gameModeDescriptionLine1 = var1.translateKey("selectWorld.gameMode." + this.gameMode + ".line1"); 136 this.gameModeDescriptionLine2 = var1.translateKey("selectWorld.gameMode." + this.gameMode + ".line2"); 137 this.generateStructuresButton.displayString = var1.translateKey("selectWorld.mapFeatures") + " "; 138 139 if (this.field_73925_n) 140 { 141 this.generateStructuresButton.displayString = this.generateStructuresButton.displayString + var1.translateKey("options.on"); 142 } 143 else 144 { 145 this.generateStructuresButton.displayString = this.generateStructuresButton.displayString + var1.translateKey("options.off"); 146 } 147 148 this.field_73938_x.displayString = var1.translateKey("selectWorld.bonusItems") + " "; 149 150 if (this.field_73934_q && !this.field_73933_r) 151 { 152 this.field_73938_x.displayString = this.field_73938_x.displayString + var1.translateKey("options.on"); 153 } 154 else 155 { 156 this.field_73938_x.displayString = this.field_73938_x.displayString + var1.translateKey("options.off"); 157 } 158 159 this.worldTypeButton.displayString = var1.translateKey("selectWorld.mapType") + " " + var1.translateKey(WorldType.worldTypes[this.field_73916_E].getTranslateName()); 160 this.field_73936_z.displayString = var1.translateKey("selectWorld.allowCommands") + " "; 161 162 if (this.field_73926_o && !this.field_73933_r) 163 { 164 this.field_73936_z.displayString = this.field_73936_z.displayString + var1.translateKey("options.on"); 165 } 166 else 167 { 168 this.field_73936_z.displayString = this.field_73936_z.displayString + var1.translateKey("options.off"); 169 } 170 } 171 172 public static String func_73913_a(ISaveFormat par0ISaveFormat, String par1Str) 173 { 174 par1Str = par1Str.replaceAll("[\\./\"]", "_"); 175 String[] var2 = field_73917_F; 176 int var3 = var2.length; 177 178 for (int var4 = 0; var4 < var3; ++var4) 179 { 180 String var5 = var2[var4]; 181 182 if (par1Str.equalsIgnoreCase(var5)) 183 { 184 par1Str = "_" + par1Str + "_"; 185 } 186 } 187 188 while (par0ISaveFormat.getWorldInfo(par1Str) != null) 189 { 190 par1Str = par1Str + "-"; 191 } 192 193 return par1Str; 194 } 195 196 /** 197 * Called when the screen is unloaded. Used to disable keyboard repeat events 198 */ 199 public void onGuiClosed() 200 { 201 Keyboard.enableRepeatEvents(false); 202 } 203 204 /** 205 * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e). 206 */ 207 protected void actionPerformed(GuiButton par1GuiButton) 208 { 209 if (par1GuiButton.enabled) 210 { 211 if (par1GuiButton.id == 1) 212 { 213 this.mc.displayGuiScreen(this.parentGuiScreen); 214 } 215 else if (par1GuiButton.id == 0) 216 { 217 this.mc.displayGuiScreen((GuiScreen)null); 218 219 if (this.createClicked) 220 { 221 return; 222 } 223 224 this.createClicked = true; 225 long var2 = (new Random()).nextLong(); 226 String var4 = this.textboxSeed.getText(); 227 228 if (!MathHelper.stringNullOrLengthZero(var4)) 229 { 230 try 231 { 232 long var5 = Long.parseLong(var4); 233 234 if (var5 != 0L) 235 { 236 var2 = var5; 237 } 238 } 239 catch (NumberFormatException var7) 240 { 241 var2 = (long)var4.hashCode(); 242 } 243 } 244 WorldType.worldTypes[this.field_73916_E].onGUICreateWorldPress(); 245 EnumGameType var9 = EnumGameType.getByName(this.gameMode); 246 WorldSettings var6 = new WorldSettings(var2, var9, this.field_73925_n, this.field_73933_r, WorldType.worldTypes[this.field_73916_E]); 247 248 if (this.field_73934_q && !this.field_73933_r) 249 { 250 var6.enableBonusChest(); 251 } 252 253 if (this.field_73926_o && !this.field_73933_r) 254 { 255 var6.enableCommands(); 256 } 257 258 this.mc.launchIntegratedServer(this.folderName, this.textboxWorldName.getText().trim(), var6); 259 } 260 else if (par1GuiButton.id == 3) 261 { 262 this.moreOptions = !this.moreOptions; 263 this.gameModeButton.drawButton = !this.moreOptions; 264 this.generateStructuresButton.drawButton = this.moreOptions; 265 this.field_73938_x.drawButton = this.moreOptions; 266 this.worldTypeButton.drawButton = this.moreOptions; 267 this.field_73936_z.drawButton = this.moreOptions; 268 StringTranslate var8; 269 270 if (this.moreOptions) 271 { 272 var8 = StringTranslate.getInstance(); 273 this.moreWorldOptions.displayString = var8.translateKey("gui.done"); 274 } 275 else 276 { 277 var8 = StringTranslate.getInstance(); 278 this.moreWorldOptions.displayString = var8.translateKey("selectWorld.moreWorldOptions"); 279 } 280 } 281 else if (par1GuiButton.id == 2) 282 { 283 if (this.gameMode.equals("survival")) 284 { 285 if (!this.field_73935_p) 286 { 287 this.field_73926_o = false; 288 } 289 290 this.field_73933_r = false; 291 this.gameMode = "hardcore"; 292 this.field_73933_r = true; 293 this.field_73936_z.enabled = false; 294 this.field_73938_x.enabled = false; 295 this.func_73914_h(); 296 } 297 else if (this.gameMode.equals("hardcore")) 298 { 299 if (!this.field_73935_p) 300 { 301 this.field_73926_o = true; 302 } 303 304 this.field_73933_r = false; 305 this.gameMode = "creative"; 306 this.func_73914_h(); 307 this.field_73933_r = false; 308 this.field_73936_z.enabled = true; 309 this.field_73938_x.enabled = true; 310 } 311 else 312 { 313 if (!this.field_73935_p) 314 { 315 this.field_73926_o = false; 316 } 317 318 this.gameMode = "survival"; 319 this.func_73914_h(); 320 this.field_73936_z.enabled = true; 321 this.field_73938_x.enabled = true; 322 this.field_73933_r = false; 323 } 324 325 this.func_73914_h(); 326 } 327 else if (par1GuiButton.id == 4) 328 { 329 this.field_73925_n = !this.field_73925_n; 330 this.func_73914_h(); 331 } 332 else if (par1GuiButton.id == 7) 333 { 334 this.field_73934_q = !this.field_73934_q; 335 this.func_73914_h(); 336 } 337 else if (par1GuiButton.id == 5) 338 { 339 ++this.field_73916_E; 340 341 if (this.field_73916_E >= WorldType.worldTypes.length) 342 { 343 this.field_73916_E = 0; 344 } 345 346 while (WorldType.worldTypes[this.field_73916_E] == null || !WorldType.worldTypes[this.field_73916_E].getCanBeCreated()) 347 { 348 ++this.field_73916_E; 349 350 if (this.field_73916_E >= WorldType.worldTypes.length) 351 { 352 this.field_73916_E = 0; 353 } 354 } 355 356 this.func_73914_h(); 357 } 358 else if (par1GuiButton.id == 6) 359 { 360 this.field_73935_p = true; 361 this.field_73926_o = !this.field_73926_o; 362 this.func_73914_h(); 363 } 364 } 365 } 366 367 /** 368 * Fired when a key is typed. This is the equivalent of KeyListener.keyTyped(KeyEvent e). 369 */ 370 protected void keyTyped(char par1, int par2) 371 { 372 if (this.textboxWorldName.isFocused() && !this.moreOptions) 373 { 374 this.textboxWorldName.textboxKeyTyped(par1, par2); 375 this.localizedNewWorldText = this.textboxWorldName.getText(); 376 } 377 else if (this.textboxSeed.isFocused() && this.moreOptions) 378 { 379 this.textboxSeed.textboxKeyTyped(par1, par2); 380 this.seed = this.textboxSeed.getText(); 381 } 382 383 if (par1 == 13) 384 { 385 this.actionPerformed((GuiButton)this.controlList.get(0)); 386 } 387 388 ((GuiButton)this.controlList.get(0)).enabled = this.textboxWorldName.getText().length() > 0; 389 this.makeUseableName(); 390 } 391 392 /** 393 * Called when the mouse is clicked. 394 */ 395 protected void mouseClicked(int par1, int par2, int par3) 396 { 397 super.mouseClicked(par1, par2, par3); 398 399 if (this.moreOptions) 400 { 401 this.textboxSeed.mouseClicked(par1, par2, par3); 402 } 403 else 404 { 405 this.textboxWorldName.mouseClicked(par1, par2, par3); 406 } 407 } 408 409 /** 410 * Draws the screen and all the components in it. 411 */ 412 public void drawScreen(int par1, int par2, float par3) 413 { 414 StringTranslate var4 = StringTranslate.getInstance(); 415 this.drawDefaultBackground(); 416 this.drawCenteredString(this.fontRenderer, var4.translateKey("selectWorld.create"), this.width / 2, 20, 16777215); 417 418 if (this.moreOptions) 419 { 420 this.drawString(this.fontRenderer, var4.translateKey("selectWorld.enterSeed"), this.width / 2 - 100, 47, 10526880); 421 this.drawString(this.fontRenderer, var4.translateKey("selectWorld.seedInfo"), this.width / 2 - 100, 85, 10526880); 422 this.drawString(this.fontRenderer, var4.translateKey("selectWorld.mapFeatures.info"), this.width / 2 - 150, 122, 10526880); 423 this.drawString(this.fontRenderer, var4.translateKey("selectWorld.allowCommands.info"), this.width / 2 - 150, 157, 10526880); 424 this.textboxSeed.drawTextBox(); 425 } 426 else 427 { 428 this.drawString(this.fontRenderer, var4.translateKey("selectWorld.enterName"), this.width / 2 - 100, 47, 10526880); 429 this.drawString(this.fontRenderer, var4.translateKey("selectWorld.resultFolder") + " " + this.folderName, this.width / 2 - 100, 85, 10526880); 430 this.textboxWorldName.drawTextBox(); 431 this.drawString(this.fontRenderer, this.gameModeDescriptionLine1, this.width / 2 - 100, 122, 10526880); 432 this.drawString(this.fontRenderer, this.gameModeDescriptionLine2, this.width / 2 - 100, 134, 10526880); 433 } 434 435 super.drawScreen(par1, par2, par3); 436 } 437 }