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