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