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