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                if (this.mc.getSaveLoader().func_90033_f(var2))
207                {
208                    this.mc.launchIntegratedServer(var2, var3, (WorldSettings)null);
209                }
210            }
211        }
212    
213        public void confirmClicked(boolean par1, int par2)
214        {
215            if (this.deleting)
216            {
217                this.deleting = false;
218    
219                if (par1)
220                {
221                    ISaveFormat var3 = this.mc.getSaveLoader();
222                    var3.flushCache();
223                    var3.deleteWorldDirectory(this.getSaveFileName(par2));
224                    this.loadSaves();
225                }
226    
227                this.mc.displayGuiScreen(this);
228            }
229        }
230    
231        /**
232         * Draws the screen and all the components in it.
233         */
234        public void drawScreen(int par1, int par2, float par3)
235        {
236            this.worldSlotContainer.drawScreen(par1, par2, par3);
237            this.drawCenteredString(this.fontRenderer, this.screenTitle, this.width / 2, 20, 16777215);
238            super.drawScreen(par1, par2, par3);
239        }
240    
241        /**
242         * Gets a GuiYesNo screen with the warning, buttons, etc.
243         */
244        public static GuiYesNo getDeleteWorldScreen(GuiScreen par0GuiScreen, String par1Str, int par2)
245        {
246            StringTranslate var3 = StringTranslate.getInstance();
247            String var4 = var3.translateKey("selectWorld.deleteQuestion");
248            String var5 = "\'" + par1Str + "\' " + var3.translateKey("selectWorld.deleteWarning");
249            String var6 = var3.translateKey("selectWorld.deleteButton");
250            String var7 = var3.translateKey("gui.cancel");
251            GuiYesNo var8 = new GuiYesNo(par0GuiScreen, var4, var5, var6, var7, par2);
252            return var8;
253        }
254    
255        static List getSize(GuiSelectWorld par0GuiSelectWorld)
256        {
257            return par0GuiSelectWorld.saveList;
258        }
259    
260        /**
261         * called whenever an element in this gui is selected
262         */
263        static int onElementSelected(GuiSelectWorld par0GuiSelectWorld, int par1)
264        {
265            return par0GuiSelectWorld.selectedWorld = par1;
266        }
267    
268        /**
269         * returns the world currently selected
270         */
271        static int getSelectedWorld(GuiSelectWorld par0GuiSelectWorld)
272        {
273            return par0GuiSelectWorld.selectedWorld;
274        }
275    
276        /**
277         * returns the select button
278         */
279        static GuiButton getSelectButton(GuiSelectWorld par0GuiSelectWorld)
280        {
281            return par0GuiSelectWorld.buttonSelect;
282        }
283    
284        /**
285         * returns the rename button
286         */
287        static GuiButton getRenameButton(GuiSelectWorld par0GuiSelectWorld)
288        {
289            return par0GuiSelectWorld.buttonRename;
290        }
291    
292        /**
293         * returns the delete button
294         */
295        static GuiButton getDeleteButton(GuiSelectWorld par0GuiSelectWorld)
296        {
297            return par0GuiSelectWorld.buttonDelete;
298        }
299    
300        static GuiButton func_82312_f(GuiSelectWorld par0GuiSelectWorld)
301        {
302            return par0GuiSelectWorld.field_82316_w;
303        }
304    
305        static String func_82313_g(GuiSelectWorld par0GuiSelectWorld)
306        {
307            return par0GuiSelectWorld.localizedWorldText;
308        }
309    
310        static DateFormat func_82315_h(GuiSelectWorld par0GuiSelectWorld)
311        {
312            return par0GuiSelectWorld.dateFormatter;
313        }
314    
315        static String func_82311_i(GuiSelectWorld par0GuiSelectWorld)
316        {
317            return par0GuiSelectWorld.localizedMustConvertText;
318        }
319    
320        static String[] func_82314_j(GuiSelectWorld par0GuiSelectWorld)
321        {
322            return par0GuiSelectWorld.localizedGameModeText;
323        }
324    }