private boolean loadingAsServer;
@Override
@SuppressWarnings("unchecked")
public void initialise() {
final UIText worldName = find("worldName", UIText.class);
if (worldName != null) {
int gameNum = 1;
for (GameInfo info : GameProvider.getSavedGames()) {
if (info.getManifest().getTitle().startsWith(DEFAULT_GAME_NAME_PREFIX)) {
String remainder = info.getManifest().getTitle().substring(DEFAULT_GAME_NAME_PREFIX.length());
try {
gameNum = Math.max(gameNum, Integer.parseInt(remainder) + 1);
} catch (NumberFormatException e) {
logger.trace("Could not parse {} as integer (not an error)", remainder, e);
}
}
}
worldName.setText(DEFAULT_GAME_NAME_PREFIX + gameNum);
}
final UIText seed = find("seed", UIText.class);
if (seed != null) {
seed.setText(new FastRandom().nextString(32));
}
final UIDropdown<Module> gameplay = find("gameplay", UIDropdown.class);
gameplay.setOptions(getGameplayModules());
gameplay.bindSelection(new Binding<Module>() {
Module selected;
@Override
public Module get() {
// try and be smart about auto selecting a gameplay
if (selected == null) {
// get the default gameplay module from the config. This is likely to have a user triggered selection.
Module defaultGameplayModule = moduleManager.getRegistry().getLatestModuleVersion(
new Name(config.getDefaultModSelection().getDefaultGameplayModuleName()));
if (defaultGameplayModule != null) {
set(defaultGameplayModule);
return selected;
}
// find the first gameplay module that is available
for (Name moduleName : config.getDefaultModSelection().listModules()) {
Module module = moduleManager.getRegistry().getLatestModuleVersion(moduleName);
// module is null if it is no longer present
if (module != null && moduleManager.isGameplayModule(module)) {
set(module);
return selected;
}
}
}
return selected;
}
@Override
public void set(Module value) {
setSelectedGameplayModule(selected, value);
selected = value;
}
});
gameplay.setOptionRenderer(new StringTextRenderer<Module>() {
@Override
public String getString(Module value) {
return value.getMetadata().getDisplayName().value();
}
});
UILabel gameplayDescription = find("gameplayDescription", UILabel.class);
gameplayDescription.bindText(new ReadOnlyBinding<String>() {
@Override
public String get() {
Module selectedModule = gameplay.getSelection();
if (selectedModule != null) {
return selectedModule.getMetadata().getDescription().value();
} else {
return "";
}
}
});
final UIDropdown<WorldGeneratorInfo> worldGenerator = find("worldGenerator", UIDropdown.class);
if (worldGenerator != null) {
worldGenerator.bindOptions(new ReadOnlyBinding<List<WorldGeneratorInfo>>() {
@Override
public List<WorldGeneratorInfo> get() {
// grab all the module names and their dependencies
Set<Name> enabledModuleNames = Sets.newHashSet();
for (Name moduleName : getAllEnabledModuleNames()) {
enabledModuleNames.add(moduleName);
}
List<WorldGeneratorInfo> result = Lists.newArrayList();
for (WorldGeneratorInfo option : worldGeneratorManager.getWorldGenerators()) {
if (enabledModuleNames.contains(option.getUri().getModuleName())) {
result.add(option);
}
}
return result;
}
});
worldGenerator.bindSelection(new Binding<WorldGeneratorInfo>() {
@Override
public WorldGeneratorInfo get() {
// get the default generator from the config. This is likely to have a user triggered selection.
WorldGeneratorInfo info = worldGeneratorManager.getWorldGeneratorInfo(config.getWorldGeneration().getDefaultGenerator());
if (info != null && getAllEnabledModuleNames().contains(info.getUri().getModuleName())) {
return info;
}
// get the default generator from the selected gameplay module
Module selectedGameplayModule = gameplay.getSelection();
if (selectedGameplayModule != null) {
String defaultWorldGenerator = selectedGameplayModule.getMetadata().getExtension(ModuleManager.DEFAULT_WORLD_GENERATOR_EXT, String.class);
if (defaultWorldGenerator != null) {
for (WorldGeneratorInfo worldGenInfo : worldGeneratorManager.getWorldGenerators()) {
if (worldGenInfo.getUri().equals(new SimpleUri(defaultWorldGenerator))) {
set(worldGenInfo);
return worldGenInfo;
}
}
}
}
// just use the first available generator
for (WorldGeneratorInfo worldGenInfo : worldGeneratorManager.getWorldGenerators()) {
if (getAllEnabledModuleNames().contains(worldGenInfo.getUri().getModuleName())) {
set(worldGenInfo);
return worldGenInfo;
}
}
return null;
}
@Override
public void set(WorldGeneratorInfo value) {
if (value != null) {
config.getWorldGeneration().setDefaultGenerator(value.getUri());
}
}
});
worldGenerator.setOptionRenderer(new StringTextRenderer<WorldGeneratorInfo>() {
@Override
public String getString(WorldGeneratorInfo value) {
if (value != null) {
return value.getDisplayName();
}
return "";
}
});
}
WidgetUtil.trySubscribe(this, "close", new ActivateEventListener() {
@Override
public void onActivated(UIWidget button) {
getManager().popScreen();
}
});
WidgetUtil.trySubscribe(this, "play", new ActivateEventListener() {
@Override
public void onActivated(UIWidget button) {
if (worldGenerator.getSelection() == null) {
MessagePopup errorMessagePopup = getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class);
if (errorMessagePopup != null) {
errorMessagePopup.setMessage("No World Generator Selected", "Select a world generator (you may need to activate a mod with a generator first).");
}
} else {
GameManifest gameManifest = new GameManifest();
gameManifest.setTitle(worldName.getText());
gameManifest.setSeed(seed.getText());
DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
ResolutionResult result = resolver.resolve(config.getDefaultModSelection().listModules());
if (!result.isSuccess()) {
MessagePopup errorMessagePopup = getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class);
if (errorMessagePopup != null) {