    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.text.DateFormat;
    006    import java.text.SimpleDateFormat;
    007    import java.util.Collections;
    008    import java.util.List;
    009
    010    @SideOnly(Side.CLIENT)
    011    public class GuiSelectWorld extends GuiScreen
    012    {
    013        /** simple date formater */
    014        private final DateFormat dateFormatter = new SimpleDateFormat();
    015
    016        /**
    017         * A reference to the screen object that created this. Used for navigating between screens.
    018         */
    019        protected GuiScreen parentScreen;
    020
    021        /** The title string that is displayed in the top-center of the screen. */
    022        protected String screenTitle = "Select world";
    023
    024        /** True if a world has been selected. */
    025        private boolean selected = false;
    026
    027        /** the currently selected world */
    028        private int selectedWorld;
    029
    030        /** The save list for the world selection screen */
    031        private List saveList;
    032        private GuiWorldSlot worldSlotContainer;
    033
    034        /** E.g. World, Welt, Monde, Mundo */
    035        private String localizedWorldText;
    036        private String localizedMustConvertText;
    037
    038        /**
    039         * The game mode text that is displayed with each world on the world selection list.
    040         */
    041        private String[] localizedGameModeText = new String[3];
    042
    043        /** set to true if you arein the process of deleteing a world/save */
    044        private boolean deleting;
    045
    046        /** the rename button in the world selection gui */
    047        private GuiButton buttonRename;
    048
    049        /** the select button in the world selection gui */
    050        private GuiButton buttonSelect;
    051
    052        /** the delete button in the world selection gui */
    053        private GuiButton buttonDelete;
    054        private GuiButton field_82316_w;
    055
    056        public GuiSelectWorld(GuiScreen par1GuiScreen)
    057        {
    058            this.parentScreen = par1GuiScreen;
    059        }
    060
    061        /**
    062         * Adds the buttons (and other controls) to the screen in question.
    063         */
    064        public void initGui()
    065        {
    066            StringTranslate var1 = StringTranslate.getInstance();
    067            this.screenTitle = var1.translateKey("selectWorld.title");
    068            this.localizedWorldText = var1.translateKey("selectWorld.world");
    069            this.localizedMustConvertText = var1.translateKey("selectWorld.conversion");
    070            this.localizedGameModeText[EnumGameType.SURVIVAL.getID()] = var1.translateKey("gameMode.survival");
    071            this.localizedGameModeText[EnumGameType.CREATIVE.getID()] = var1.translateKey("gameMode.creative");
    072            this.localizedGameModeText[EnumGameType.ADVENTURE.getID()] = var1.translateKey("gameMode.adventure");
    073            this.loadSaves();
    074            this.worldSlotContainer = new GuiWorldSlot(this);
    075            this.worldSlotContainer.registerScrollButtons(this.controlList, 4, 5);
    076            this.initButtons();
    077        }
    078
    079        /**
    080         * loads the saves
    081         */
    082        private void loadSaves()
    083        {
    084            ISaveFormat var1 = this.mc.getSaveLoader();
    085            this.saveList = var1.getSaveList();
    086            Collections.sort(this.saveList);
    087            this.selectedWorld = -1;
    088        }
    089
    090        /**
    091         * returns the file name of the specified save number
    092         */
    093        protected String getSaveFileName(int par1)
    094        {
    095            return ((SaveFormatComparator)this.saveList.get(par1)).getFileName();
    096        }
    097
    098        /**
    099         * returns the name of the saved game
    100         */
    101        protected String getSaveName(int par1)
    102        {
    103            String var2 = ((SaveFormatComparator)this.saveList.get(par1)).getDisplayName();
    104
    105            if (var2 == null || MathHelper.stringNullOrLengthZero(var2))
    106            {
    107                StringTranslate var3 = StringTranslate.getInstance();
    108                var2 = var3.translateKey("selectWorld.world") + " " + (par1 + 1);
    109            }
    110
    111            return var2;
    112        }
    113
    114        /**
    115         * intilize the buttons for this GUI
    116         */
    117        public void initButtons()
    118        {
    119            StringTranslate var1 = StringTranslate.getInstance();
    120            this.controlList.add(this.buttonSelect = new GuiButton(1, this.width / 2 - 154, this.height - 52, 150, 20, var1.translateKey("selectWorld.select")));
    121            this.controlList.add(new GuiButton(3, this.width / 2 + 4, this.height - 52, 150, 20, var1.translateKey("selectWorld.create")));
    122            this.controlList.add(this.buttonDelete = new GuiButton(6, this.width / 2 - 154, this.height - 28, 72, 20, var1.translateKey("selectWorld.rename")));
    123            this.controlList.add(this.buttonRename = new GuiButton(2, this.width / 2 - 76, this.height - 28, 72, 20, var1.translateKey("selectWorld.delete")));
    124            this.controlList.add(this.field_82316_w = new GuiButton(7, this.width / 2 + 4, this.height - 28, 72, 20, var1.translateKey("selectWorld.recreate")));
    125            this.controlList.add(new GuiButton(0, this.width / 2 + 82, this.height - 28, 72, 20, var1.translateKey("gui.cancel")));
    126            this.buttonSelect.enabled = false;
    127            this.buttonRename.enabled = false;
    128            this.buttonDelete.enabled = false;
    129            this.field_82316_w.enabled = false;
    130        }
    131
    132        /**
    133         * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
    134         */
    135        protected void actionPerformed(GuiButton par1GuiButton)
    136        {
    137            if (par1GuiButton.enabled)
    138            {
    139                if (par1GuiButton.id == 2)
    140                {
    141                    String var2 = this.getSaveName(this.selectedWorld);
    142
    143                    if (var2 != null)
    144                    {
    145                        this.deleting = true;
    146                        GuiYesNo var3 = getDeleteWorldScreen(this, var2, this.selectedWorld);
    147                        this.mc.displayGuiScreen(var3);
    148                    }
    149                }
    150                else if (par1GuiButton.id == 1)
    151                {
    152                    this.selectWorld(this.selectedWorld);
    153                }
    154                else if (par1GuiButton.id == 3)
    155                {
    156                    this.mc.displayGuiScreen(new GuiCreateWorld(this));
    157                }
    158                else if (par1GuiButton.id == 6)
    159                {
    160                    this.mc.displayGuiScreen(new GuiRenameWorld(this, this.getSaveFileName(this.selectedWorld)));
    161                }
    162                else if (par1GuiButton.id == 0)
    163                {
    164                    this.mc.displayGuiScreen(this.parentScreen);
    165                }
    166                else if (par1GuiButton.id == 7)
    167                {
    168                    GuiCreateWorld var5 = new GuiCreateWorld(this);
    169                    ISaveHandler var6 = this.mc.getSaveLoader().getSaveLoader(this.getSaveFileName(this.selectedWorld), false);
    170                    WorldInfo var4 = var6.loadWorldInfo();
    171                    var6.flush();
    172                    var5.func_82286_a(var4);
    173                    this.mc.displayGuiScreen(var5);
    174                }
    175                else
    176                {
    177                    this.worldSlotContainer.actionPerformed(par1GuiButton);
    178                }
    179            }
    180        }
    181
    182        /**
    183         * Gets the selected world.
    184         */
    185        public void selectWorld(int par1)
    186        {
    187            this.mc.displayGuiScreen((GuiScreen)null);
    188
    189            if (!this.selected)
    190            {
    191                this.selected = true;
    192                String var2 = this.getSaveFileName(par1);
    193
    194                if (var2 == null)
    195                {
    196                    var2 = "World" + par1;
    197                }
    198
    199                String var3 = this.getSaveName(par1);
    200
    201                if (var3 == null)
    202                {
    203                    var3 = "World" + par1;
    204                }
    205
    206                this.mc.launchIntegratedServer(var2, var3, (WorldSettings)null);
    207            }
    208        }
    209
    210        public void confirmClicked(boolean par1, int par2)
    211        {
    212            if (this.deleting)
    213            {
    214                this.deleting = false;
    215
    216                if (par1)
    217                {
    218                    ISaveFormat var3 = this.mc.getSaveLoader();
    219                    var3.flushCache();
    220                    var3.deleteWorldDirectory(this.getSaveFileName(par2));
    221                    this.loadSaves();
    222                }
    223
    224                this.mc.displayGuiScreen(this);
    225            }
    226        }
    227
    228        /**
    229         * Draws the screen and all the components in it.
    230         */
    231        public void drawScreen(int par1, int par2, float par3)
    232        {
    233            this.worldSlotContainer.drawScreen(par1, par2, par3);
    234            this.drawCenteredString(this.fontRenderer, this.screenTitle, this.width / 2, 20, 16777215);
    235            super.drawScreen(par1, par2, par3);
    236        }
    237
    238        /**
    239         * Gets a GuiYesNo screen with the warning, buttons, etc.
    240         */
    241        public static GuiYesNo getDeleteWorldScreen(GuiScreen par0GuiScreen, String par1Str, int par2)
    242        {
    243            StringTranslate var3 = StringTranslate.getInstance();
    244            String var4 = var3.translateKey("selectWorld.deleteQuestion");
    245            String var5 = "\'" + par1Str + "\' " + var3.translateKey("selectWorld.deleteWarning");
    246            String var6 = var3.translateKey("selectWorld.deleteButton");
    247            String var7 = var3.translateKey("gui.cancel");
    248            GuiYesNo var8 = new GuiYesNo(par0GuiScreen, var4, var5, var6, var7, par2);
    249            return var8;
    250        }
    251
    252        static List getSize(GuiSelectWorld par0GuiSelectWorld)
    253        {
    254            return par0GuiSelectWorld.saveList;
    255        }
    256
    257        /**
    258         * called whenever an element in this gui is selected
    259         */
    260        static int onElementSelected(GuiSelectWorld par0GuiSelectWorld, int par1)
    261        {
    262            return par0GuiSelectWorld.selectedWorld = par1;
    263        }
    264
    265        /**
    266         * returns the world currently selected
    267         */
    268        static int getSelectedWorld(GuiSelectWorld par0GuiSelectWorld)
    269        {
    270            return par0GuiSelectWorld.selectedWorld;
    271        }
    272
    273        /**
    274         * returns the select button
    275         */
    276        static GuiButton getSelectButton(GuiSelectWorld par0GuiSelectWorld)
    277        {
    278            return par0GuiSelectWorld.buttonSelect;
    279        }
    280
    281        /**
    282         * returns the rename button
    283         */
    284        static GuiButton getRenameButton(GuiSelectWorld par0GuiSelectWorld)
    285        {
    286            return par0GuiSelectWorld.buttonRename;
    287        }
    288
    289        /**
    290         * returns the delete button
    291         */
    292        static GuiButton getDeleteButton(GuiSelectWorld par0GuiSelectWorld)
    293        {
    294            return par0GuiSelectWorld.buttonDelete;
    295        }
    296
    297        static GuiButton func_82312_f(GuiSelectWorld par0GuiSelectWorld)
    298        {
    299            return par0GuiSelectWorld.field_82316_w;
    300        }
    301
    302        static String func_82313_g(GuiSelectWorld par0GuiSelectWorld)
    303        {
    304            return par0GuiSelectWorld.localizedWorldText;
    305        }
    306
    307        static DateFormat func_82315_h(GuiSelectWorld par0GuiSelectWorld)
    308        {
    309            return par0GuiSelectWorld.dateFormatter;
    310        }
    311
    312        static String func_82311_i(GuiSelectWorld par0GuiSelectWorld)
    313        {
    314            return par0GuiSelectWorld.localizedMustConvertText;
    315        }
    316
    317        static String[] func_82314_j(GuiSelectWorld par0GuiSelectWorld)
    318        {
    319            return par0GuiSelectWorld.localizedGameModeText;
    320        }
    321    }