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