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