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    
055        public GuiSelectWorld(GuiScreen par1GuiScreen)
056        {
057            this.parentScreen = par1GuiScreen;
058        }
059    
060        /**
061         * Adds the buttons (and other controls) to the screen in question.
062         */
063        public void initGui()
064        {
065            StringTranslate var1 = StringTranslate.getInstance();
066            this.screenTitle = var1.translateKey("selectWorld.title");
067            this.localizedWorldText = var1.translateKey("selectWorld.world");
068            this.localizedMustConvertText = var1.translateKey("selectWorld.conversion");
069            this.localizedGameModeText[EnumGameType.SURVIVAL.getID()] = var1.translateKey("gameMode.survival");
070            this.localizedGameModeText[EnumGameType.CREATIVE.getID()] = var1.translateKey("gameMode.creative");
071            this.localizedGameModeText[EnumGameType.ADVENTURE.getID()] = var1.translateKey("gameMode.adventure");
072            this.loadSaves();
073            this.worldSlotContainer = new GuiWorldSlot(this);
074            this.worldSlotContainer.registerScrollButtons(this.controlList, 4, 5);
075            this.initButtons();
076        }
077    
078        /**
079         * loads the saves
080         */
081        private void loadSaves()
082        {
083            ISaveFormat var1 = this.mc.getSaveLoader();
084            this.saveList = var1.getSaveList();
085            Collections.sort(this.saveList);
086            this.selectedWorld = -1;
087        }
088    
089        /**
090         * returns the file name of the specified save number
091         */
092        protected String getSaveFileName(int par1)
093        {
094            return ((SaveFormatComparator)this.saveList.get(par1)).getFileName();
095        }
096    
097        /**
098         * returns the name of the saved game
099         */
100        protected String getSaveName(int par1)
101        {
102            String var2 = ((SaveFormatComparator)this.saveList.get(par1)).getDisplayName();
103    
104            if (var2 == null || MathHelper.stringNullOrLengthZero(var2))
105            {
106                StringTranslate var3 = StringTranslate.getInstance();
107                var2 = var3.translateKey("selectWorld.world") + " " + (par1 + 1);
108            }
109    
110            return var2;
111        }
112    
113        /**
114         * intilize the buttons for this GUI
115         */
116        public void initButtons()
117        {
118            StringTranslate var1 = StringTranslate.getInstance();
119            this.controlList.add(this.buttonSelect = new GuiButton(1, this.width / 2 - 154, this.height - 52, 150, 20, var1.translateKey("selectWorld.select")));
120            this.controlList.add(this.buttonDelete = new GuiButton(6, this.width / 2 - 154, this.height - 28, 70, 20, var1.translateKey("selectWorld.rename")));
121            this.controlList.add(this.buttonRename = new GuiButton(2, this.width / 2 - 74, this.height - 28, 70, 20, var1.translateKey("selectWorld.delete")));
122            this.controlList.add(new GuiButton(3, this.width / 2 + 4, this.height - 52, 150, 20, var1.translateKey("selectWorld.create")));
123            this.controlList.add(new GuiButton(0, this.width / 2 + 4, this.height - 28, 150, 20, var1.translateKey("gui.cancel")));
124            this.buttonSelect.enabled = false;
125            this.buttonRename.enabled = false;
126            this.buttonDelete.enabled = false;
127        }
128    
129        /**
130         * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
131         */
132        protected void actionPerformed(GuiButton par1GuiButton)
133        {
134            if (par1GuiButton.enabled)
135            {
136                if (par1GuiButton.id == 2)
137                {
138                    String var2 = this.getSaveName(this.selectedWorld);
139    
140                    if (var2 != null)
141                    {
142                        this.deleting = true;
143                        GuiYesNo var3 = getDeleteWorldScreen(this, var2, this.selectedWorld);
144                        this.mc.displayGuiScreen(var3);
145                    }
146                }
147                else if (par1GuiButton.id == 1)
148                {
149                    this.selectWorld(this.selectedWorld);
150                }
151                else if (par1GuiButton.id == 3)
152                {
153                    this.mc.displayGuiScreen(new GuiCreateWorld(this));
154                }
155                else if (par1GuiButton.id == 6)
156                {
157                    this.mc.displayGuiScreen(new GuiRenameWorld(this, this.getSaveFileName(this.selectedWorld)));
158                }
159                else if (par1GuiButton.id == 0)
160                {
161                    this.mc.displayGuiScreen(this.parentScreen);
162                }
163                else
164                {
165                    this.worldSlotContainer.actionPerformed(par1GuiButton);
166                }
167            }
168        }
169    
170        /**
171         * Gets the selected world.
172         */
173        public void selectWorld(int par1)
174        {
175            this.mc.displayGuiScreen((GuiScreen)null);
176    
177            if (!this.selected)
178            {
179                this.selected = true;
180                String var2 = this.getSaveFileName(par1);
181    
182                if (var2 == null)
183                {
184                    var2 = "World" + par1;
185                }
186    
187                String var3 = this.getSaveName(par1);
188    
189                if (var3 == null)
190                {
191                    var3 = "World" + par1;
192                }
193    
194                this.mc.launchIntegratedServer(var2, var3, (WorldSettings)null);
195            }
196        }
197    
198        public void confirmClicked(boolean par1, int par2)
199        {
200            if (this.deleting)
201            {
202                this.deleting = false;
203    
204                if (par1)
205                {
206                    ISaveFormat var3 = this.mc.getSaveLoader();
207                    var3.flushCache();
208                    var3.deleteWorldDirectory(this.getSaveFileName(par2));
209                    this.loadSaves();
210                }
211    
212                this.mc.displayGuiScreen(this);
213            }
214        }
215    
216        /**
217         * Draws the screen and all the components in it.
218         */
219        public void drawScreen(int par1, int par2, float par3)
220        {
221            this.worldSlotContainer.drawScreen(par1, par2, par3);
222            this.drawCenteredString(this.fontRenderer, this.screenTitle, this.width / 2, 20, 16777215);
223            super.drawScreen(par1, par2, par3);
224        }
225    
226        /**
227         * Gets a GuiYesNo screen with the warning, buttons, etc.
228         */
229        public static GuiYesNo getDeleteWorldScreen(GuiScreen par0GuiScreen, String par1Str, int par2)
230        {
231            StringTranslate var3 = StringTranslate.getInstance();
232            String var4 = var3.translateKey("selectWorld.deleteQuestion");
233            String var5 = "\'" + par1Str + "\' " + var3.translateKey("selectWorld.deleteWarning");
234            String var6 = var3.translateKey("selectWorld.deleteButton");
235            String var7 = var3.translateKey("gui.cancel");
236            GuiYesNo var8 = new GuiYesNo(par0GuiScreen, var4, var5, var6, var7, par2);
237            return var8;
238        }
239    
240        static List getSize(GuiSelectWorld par0GuiSelectWorld)
241        {
242            return par0GuiSelectWorld.saveList;
243        }
244    
245        /**
246         * called whenever an element in this gui is selected
247         */
248        static int onElementSelected(GuiSelectWorld par0GuiSelectWorld, int par1)
249        {
250            return par0GuiSelectWorld.selectedWorld = par1;
251        }
252    
253        /**
254         * returns the world currently selected
255         */
256        static int getSelectedWorld(GuiSelectWorld par0GuiSelectWorld)
257        {
258            return par0GuiSelectWorld.selectedWorld;
259        }
260    
261        /**
262         * returns the select button
263         */
264        static GuiButton getSelectButton(GuiSelectWorld par0GuiSelectWorld)
265        {
266            return par0GuiSelectWorld.buttonSelect;
267        }
268    
269        /**
270         * returns the rename button
271         */
272        static GuiButton getRenameButton(GuiSelectWorld par0GuiSelectWorld)
273        {
274            return par0GuiSelectWorld.buttonRename;
275        }
276    
277        /**
278         * returns the delete button
279         */
280        static GuiButton getDeleteButton(GuiSelectWorld par0GuiSelectWorld)
281        {
282            return par0GuiSelectWorld.buttonDelete;
283        }
284    
285        /**
286         * Gets the localized world name
287         */
288        static String getLocalizedWorldName(GuiSelectWorld par0GuiSelectWorld)
289        {
290            return par0GuiSelectWorld.localizedWorldText;
291        }
292    
293        /**
294         * returns the date formatter for this gui
295         */
296        static DateFormat getDateFormatter(GuiSelectWorld par0GuiSelectWorld)
297        {
298            return par0GuiSelectWorld.dateFormatter;
299        }
300    
301        /**
302         * Gets the localized must convert text
303         */
304        static String getLocalizedMustConvert(GuiSelectWorld par0GuiSelectWorld)
305        {
306            return par0GuiSelectWorld.localizedMustConvertText;
307        }
308    
309        /**
310         * Gets the localized GameMode
311         */
312        static String[] getLocalizedGameMode(GuiSelectWorld par0GuiSelectWorld)
313        {
314            return par0GuiSelectWorld.localizedGameModeText;
315        }
316    }