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