use of org.spongepowered.api.world.storage.WorldProperties in project SpongeCommon by SpongePowered.
the class WorldManager method unloadWorld.
// TODO Result
public static boolean unloadWorld(WorldServer worldServer, boolean checkConfig) {
checkNotNull(worldServer);
final MinecraftServer server = SpongeImpl.getServer();
// Likely leaked, don't want to drop leaked world data
if (!worldByDimensionId.containsValue(worldServer)) {
return false;
}
// Vanilla sometimes doesn't remove player entities from world first
if (server.isServerRunning()) {
if (!worldServer.playerEntities.isEmpty()) {
return false;
}
// We only check config if base game wants to unload world. If mods/plugins say unload, we unload
if (checkConfig) {
if (((WorldProperties) worldServer.getWorldInfo()).doesKeepSpawnLoaded()) {
return false;
}
}
}
try (PhaseContext<?> context = GeneralPhase.State.WORLD_UNLOAD.createPhaseContext().source(worldServer).buildAndSwitch()) {
if (SpongeImpl.postEvent(SpongeEventFactory.createUnloadWorldEvent(Sponge.getCauseStackManager().getCurrentCause(), (org.spongepowered.api.world.World) worldServer))) {
return false;
}
final IMixinWorldServer mixinWorldServer = (IMixinWorldServer) worldServer;
final int dimensionId = mixinWorldServer.getDimensionId();
try {
// Don't save if server is stopping to avoid duplicate saving.
if (server.isServerRunning()) {
saveWorld(worldServer, true);
}
mixinWorldServer.getActiveConfig().save();
} catch (MinecraftException e) {
e.printStackTrace();
} finally {
worldByDimensionId.remove(dimensionId);
weakWorldByWorld.remove(worldServer);
((IMixinMinecraftServer) server).removeWorldTickTimes(dimensionId);
SpongeImpl.getLogger().info("Unloading world [{}] (DIM{})", worldServer.getWorldInfo().getWorldName(), dimensionId);
reorderWorldsVanillaFirst();
}
if (!server.isServerRunning()) {
unregisterDimension(dimensionId);
}
}
return true;
}
use of org.spongepowered.api.world.storage.WorldProperties in project ProjectWorlds by trentech.
the class CommandCopy method getSuggestions.
@Override
public List<String> getSuggestions(CommandSource source, String arguments, Location<World> targetPosition) throws CommandException {
List<String> list = new ArrayList<>();
if (arguments.equalsIgnoreCase("copy")) {
return list;
}
String[] args = arguments.split(" ");
if (args.length != 1) {
return list;
}
for (WorldProperties world : Sponge.getServer().getAllWorldProperties()) {
if (world.getWorldName().equalsIgnoreCase(args[args.length - 1])) {
return list;
}
if (world.getWorldName().toLowerCase().startsWith(args[args.length - 1].toLowerCase())) {
list.add(world.getWorldName());
}
}
return list;
}
use of org.spongepowered.api.world.storage.WorldProperties in project ProjectWorlds by trentech.
the class CommandCreate method process.
@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
if (arguments.equalsIgnoreCase("create")) {
throw new CommandException(getHelp().getUsageText());
}
String[] args = arguments.split(" ");
if (args[args.length - 1].equalsIgnoreCase("--help")) {
help.execute(source);
return CommandResult.success();
}
String worldName;
try {
worldName = args[0];
} catch (Exception e) {
throw new CommandException(getHelp().getUsageText());
}
if (Sponge.getServer().getWorldProperties(worldName).isPresent()) {
throw new CommandException(Text.of(TextColors.RED, worldName, " already exists"), false);
}
WorldArchetype.Builder builder = WorldArchetype.builder();
List<WorldGeneratorModifier> modifiers = new ArrayList<>();
if (args.length > 2) {
boolean skip = false;
for (int i = 1; i < args.length - 1; i++) {
if (skip) {
skip = false;
continue;
}
String arg = args[i];
String value;
try {
value = args[i + 1];
} catch (Exception e) {
throw new CommandException(getHelp().getUsageText());
}
if (arg.equalsIgnoreCase("-d") || arg.equalsIgnoreCase("-dimension")) {
Optional<DimensionType> optionalDimension = Sponge.getRegistry().getType(DimensionType.class, value);
if (!optionalDimension.isPresent()) {
source.sendMessage(Text.of(TextColors.YELLOW, value, " is not a valid DimensionType"));
throw new CommandException(getHelp().getUsageText());
}
builder.dimension(optionalDimension.get());
} else if (arg.equalsIgnoreCase("-g") || arg.equalsIgnoreCase("-generator")) {
String[] split = value.split("\\{");
Optional<GeneratorType> optionalGenerator = Sponge.getRegistry().getType(GeneratorType.class, split[0]);
if (!optionalGenerator.isPresent()) {
source.sendMessage(Text.of(TextColors.YELLOW, value, " is not a valid GeneratorType"));
throw new CommandException(getHelp().getUsageText());
}
builder.generator(optionalGenerator.get());
if (split.length == 2) {
builder.generatorSettings(DataContainer.createNew().set(DataQuery.of("customSettings"), split[1].replace("\\}", "")));
}
} else if (arg.equalsIgnoreCase("-gm") || arg.equalsIgnoreCase("-gamemode")) {
Optional<GameMode> optionalGamemode = Optional.empty();
try {
optionalGamemode = Gamemode.get(Integer.parseInt(value));
} catch (Exception e) {
optionalGamemode = Gamemode.get(value);
}
if (!optionalGamemode.isPresent()) {
source.sendMessage(Text.of(TextColors.YELLOW, value, " is not a valid GameMode"));
throw new CommandException(getHelp().getUsageText());
}
builder.gameMode(optionalGamemode.get());
} else if (arg.equalsIgnoreCase("-m") || arg.equalsIgnoreCase("-modifier")) {
Optional<WorldGeneratorModifier> optionalModifier = Sponge.getRegistry().getType(WorldGeneratorModifier.class, value);
if (!optionalModifier.isPresent()) {
source.sendMessage(Text.of(TextColors.YELLOW, value, " is not a valid WorldGeneratorModifier"));
throw new CommandException(getHelp().getUsageText());
}
modifiers.add(optionalModifier.get());
} else if (arg.equalsIgnoreCase("-s") || arg.equalsIgnoreCase("-seed")) {
try {
Long s = Long.parseLong(value);
builder.seed(s);
} catch (Exception e) {
builder.seed(value.hashCode());
}
} else if (arg.equalsIgnoreCase("-df") || arg.equalsIgnoreCase("-difficulty")) {
Optional<Difficulty> optionalDifficulty = Sponge.getRegistry().getType(Difficulty.class, value);
if (!optionalDifficulty.isPresent()) {
source.sendMessage(Text.of(TextColors.YELLOW, value, " is not a valid Difficulty"));
throw new CommandException(getHelp().getUsageText());
}
builder.difficulty(optionalDifficulty.get());
} else if (arg.equalsIgnoreCase("-l") || arg.equalsIgnoreCase("-loadonstartup")) {
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
builder.loadsOnStartup(Boolean.valueOf(value));
} else {
source.sendMessage(Text.of(TextColors.YELLOW, value, " is not a valid Boolean"));
throw new CommandException(getHelp().getUsageText());
}
} else if (arg.equalsIgnoreCase("-k") || arg.equalsIgnoreCase("-keepspawnloaded")) {
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
builder.keepsSpawnLoaded(Boolean.valueOf(value));
} else {
source.sendMessage(Text.of(TextColors.YELLOW, value, " is not a valid Boolean"));
throw new CommandException(getHelp().getUsageText());
}
} else if (arg.equalsIgnoreCase("-c") || arg.equalsIgnoreCase("-allowcommands")) {
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
builder.commandsAllowed(Boolean.valueOf(value));
} else {
source.sendMessage(Text.of(TextColors.YELLOW, value, " is not a valid Boolean"));
throw new CommandException(getHelp().getUsageText());
}
} else if (arg.equalsIgnoreCase("-b") || arg.equalsIgnoreCase("-bonuschest")) {
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
builder.generateBonusChest(Boolean.valueOf(value));
} else {
source.sendMessage(Text.of(TextColors.YELLOW, value, " is not a valid Boolean"));
throw new CommandException(getHelp().getUsageText());
}
} else if (arg.equalsIgnoreCase("-f") || arg.equalsIgnoreCase("-mapfeatures")) {
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
builder.usesMapFeatures(Boolean.valueOf(value));
} else {
source.sendMessage(Text.of(TextColors.YELLOW, value, " is not a valid Boolean"));
throw new CommandException(getHelp().getUsageText());
}
} else {
source.sendMessage(Text.of(TextColors.YELLOW, value, " is not a valid Flag"));
throw new CommandException(getHelp().getUsageText());
}
skip = true;
}
}
WorldArchetype settings = builder.enabled(true).keepsSpawnLoaded(true).loadsOnStartup(true).build(worldName, worldName);
WorldProperties properties;
try {
properties = Sponge.getServer().createWorldProperties(worldName, settings);
properties.setGeneratorModifiers(modifiers);
} catch (IOException e) {
e.printStackTrace();
throw new CommandException(Text.of(TextColors.RED, "Something went wrong. Check server log for details"), false);
}
Sponge.getServer().saveWorldProperties(properties);
worlds.add(worldName);
source.sendMessage(Text.of(TextColors.DARK_GREEN, worldName, " created successfully"));
return CommandResult.success();
}
use of org.spongepowered.api.world.storage.WorldProperties in project ProjectWorlds by trentech.
the class CommandEnable method getSuggestions.
@Override
public List<String> getSuggestions(CommandSource source, String arguments, Location<World> targetPosition) throws CommandException {
List<String> list = new ArrayList<>();
if (arguments.equalsIgnoreCase("enable")) {
return list;
}
String[] args = arguments.split(" ");
if (args.length == 1) {
for (WorldProperties world : Sponge.getServer().getAllWorldProperties()) {
if (world.getWorldName().equalsIgnoreCase(args[0])) {
list.add("true");
list.add("false");
return list;
}
if (world.getWorldName().toLowerCase().startsWith(args[0].toLowerCase())) {
list.add(world.getWorldName());
}
}
return list;
}
if (args.length == 2) {
if ("true".equalsIgnoreCase(args[1].toLowerCase()) || "false".equalsIgnoreCase(args[1].toLowerCase())) {
return list;
}
if ("true".startsWith(args[1].toLowerCase())) {
list.add("true");
}
if ("false".startsWith(args[1].toLowerCase())) {
list.add("false");
}
}
return list;
}
use of org.spongepowered.api.world.storage.WorldProperties in project ProjectWorlds by trentech.
the class CommandGamemode method getSuggestions.
@Override
public List<String> getSuggestions(CommandSource source, String arguments, Location<World> targetPosition) throws CommandException {
List<String> list = new ArrayList<>();
if (arguments.equalsIgnoreCase("gamemode")) {
return list;
}
String[] args = arguments.split(" ");
if (args.length == 1) {
for (WorldProperties world : Sponge.getServer().getAllWorldProperties()) {
if (world.getWorldName().equalsIgnoreCase(args[0])) {
for (Gamemode gamemode : Gamemode.values()) {
list.add(gamemode.getGameMode().getName());
list.add(Integer.toString(gamemode.getIndex()));
}
return list;
}
if (world.getWorldName().toLowerCase().startsWith(args[0].toLowerCase())) {
list.add(world.getWorldName());
}
}
}
if (args.length == 2) {
for (Gamemode gamemode : Gamemode.values()) {
if (gamemode.getGameMode().getName().equalsIgnoreCase(args[1])) {
return list;
}
try {
Integer.parseInt(args[1]);
} catch (Exception e) {
if (gamemode.getGameMode().getName().toLowerCase().startsWith(args[1].toLowerCase())) {
list.add(gamemode.getGameMode().getName());
}
}
}
}
return list;
}
Aggregations