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