use of org.spongepowered.api.world.storage.WorldProperties in project ProjectWorlds by trentech.
the class CommandRegen method process.
@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
if (arguments.equalsIgnoreCase("regen")) {
throw new CommandException(getHelp().getUsageText());
}
String[] args = arguments.split(" ");
if (args[args.length - 1].equalsIgnoreCase("--help")) {
getHelp().execute(source);
return CommandResult.success();
}
String worldName;
try {
worldName = args[0];
} catch (Exception e) {
throw new CommandException(getHelp().getUsageText());
}
Optional<WorldProperties> optionalProperties = Sponge.getServer().getWorldProperties(worldName);
if (!optionalProperties.isPresent()) {
throw new CommandException(Text.of(TextColors.RED, worldName, " does not exist"), false);
}
WorldProperties properties = optionalProperties.get();
if (Sponge.getServer().getWorld(properties.getWorldName()).isPresent()) {
throw new CommandException(Text.of(TextColors.RED, properties.getWorldName(), " must be unloaded before you can regenerate"), false);
}
WorldArchetype.Builder builder = WorldArchetype.builder().from(properties);
String bool;
try {
bool = args[1];
if (!bool.equalsIgnoreCase("true") && !bool.equalsIgnoreCase("false")) {
source.sendMessage(Text.of(TextColors.YELLOW, bool, " is not a valid Boolean"));
throw new CommandException(getHelp().getUsageText());
}
String seed;
try {
seed = args[2];
try {
Long s = Long.parseLong(seed);
builder.seed(s);
} catch (Exception e) {
builder.seed(seed.hashCode());
}
} catch (Exception e) {
builder.randomSeed();
}
} catch (Exception e) {
}
try {
CompletableFuture<Boolean> delete = Sponge.getServer().deleteWorld(properties);
while (!delete.isDone()) {
}
if (!delete.get()) {
throw new CommandException(Text.of(TextColors.RED, "Could not delete ", properties.getWorldName()), false);
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
source.sendMessage(Text.of(TextColors.DARK_GREEN, "Regenerating world.."));
WorldArchetype settings = builder.enabled(true).loadsOnStartup(true).build(properties.getWorldName() + "_" + (ThreadLocalRandom.current().nextInt(100) + 1), properties.getWorldName());
WorldProperties newProperties;
try {
newProperties = Sponge.getServer().createWorldProperties(properties.getWorldName(), settings);
} catch (IOException e) {
e.printStackTrace();
throw new CommandException(Text.of(TextColors.RED, "Something went wrong. Check server log for details"), false);
}
Task.builder().delayTicks(20).execute(c -> {
Optional<World> load = Sponge.getServer().loadWorld(newProperties);
if (!load.isPresent()) {
source.sendMessage(Text.of(TextColors.RED, "Could not load ", properties.getWorldName()));
return;
}
Utils.createPlatform(load.get().getSpawnLocation().getRelative(Direction.DOWN));
source.sendMessage(Text.of(TextColors.DARK_GREEN, properties.getWorldName(), " regenerated successfully"));
}).submit(Main.getPlugin());
return CommandResult.success();
}
use of org.spongepowered.api.world.storage.WorldProperties in project ProjectWorlds by trentech.
the class CommandRemove method process.
@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
if (arguments.equalsIgnoreCase("remove")) {
throw new CommandException(getHelp().getUsageText());
}
if (arguments.equalsIgnoreCase("--help")) {
help.execute(source);
return CommandResult.success();
}
Optional<WorldProperties> optionalWorld = Sponge.getServer().getWorldProperties(arguments);
if (!optionalWorld.isPresent()) {
throw new CommandException(Text.of(TextColors.RED, arguments, " does not exist"), false);
}
WorldProperties world = optionalWorld.get();
if (Sponge.getServer().getWorld(world.getWorldName()).isPresent()) {
throw new CommandException(Text.of(TextColors.RED, world.getWorldName(), " must be unloaded before you can delete"), false);
}
new Zip(world.getWorldName()).save();
try {
if (Sponge.getServer().deleteWorld(world).get()) {
source.sendMessage(Text.of(TextColors.DARK_GREEN, world.getWorldName(), " deleted successfully"));
return CommandResult.success();
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
throw new CommandException(Text.of(TextColors.RED, "Something went wrong. Check server log for details"), false);
}
source.sendMessage(Text.of(TextColors.RED, "Could not delete ", world.getWorldName()));
return CommandResult.empty();
}
use of org.spongepowered.api.world.storage.WorldProperties in project ProjectWorlds by trentech.
the class CommandRename method process.
@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
if (arguments.equalsIgnoreCase("rename")) {
throw new CommandException(getHelp().getUsageText());
}
String[] args = arguments.split(" ");
if (args[args.length - 1].equalsIgnoreCase("--help")) {
help.execute(source);
return CommandResult.success();
}
String srcWorldName;
String newWorldName;
try {
srcWorldName = args[0];
} catch (Exception e) {
throw new CommandException(getHelp().getUsageText());
}
try {
newWorldName = args[1];
} catch (Exception e) {
throw new CommandException(getHelp().getUsageText());
}
Optional<WorldProperties> optionalWorld = Sponge.getServer().getWorldProperties(srcWorldName);
if (!optionalWorld.isPresent()) {
throw new CommandException(Text.of(TextColors.RED, srcWorldName, " does not exist"), false);
}
WorldProperties world = optionalWorld.get();
if (Sponge.getServer().getWorld(world.getWorldName()).isPresent()) {
throw new CommandException(Text.of(TextColors.RED, world.getWorldName(), " must be unloaded before you can rename"), false);
}
if (Sponge.getServer().getWorldProperties(newWorldName).isPresent()) {
throw new CommandException(Text.of(TextColors.RED, newWorldName, " already exists"), false);
}
Optional<WorldProperties> rename = Sponge.getServer().renameWorld(world, newWorldName);
if (!rename.isPresent()) {
throw new CommandException(Text.of(TextColors.RED, "Could not rename ", world.getWorldName()), false);
}
source.sendMessage(Text.of(TextColors.DARK_GREEN, world.getWorldName(), " renamed to ", newWorldName, " successfully"));
return CommandResult.success();
}
use of org.spongepowered.api.world.storage.WorldProperties in project ProjectWorlds by trentech.
the class CommandTime method process.
@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
if (arguments.equalsIgnoreCase("time")) {
throw new CommandException(getHelp().getUsageText());
}
String[] args = arguments.split(" ");
if (args[args.length - 1].equalsIgnoreCase("--help")) {
getHelp().execute(source);
return CommandResult.success();
}
String worldName;
String value;
long time = 0;
try {
worldName = args[0];
} catch (Exception e) {
throw new CommandException(getHelp().getUsageText());
}
Optional<WorldProperties> optionalProperties = Sponge.getServer().getWorldProperties(worldName);
if (!optionalProperties.isPresent()) {
throw new CommandException(Text.of(TextColors.RED, worldName, " does not exist"), false);
}
WorldProperties world = optionalProperties.get();
try {
value = args[1];
} catch (Exception e) {
source.sendMessage(Text.of(TextColors.GREEN, "Time: ", TextColors.WHITE, Utils.getTime(world.getWorldTime()), TextColors.GREEN, " Ticks: ", TextColors.WHITE, world.getWorldTime() % 24000));
return CommandResult.success();
}
try {
time = Long.parseLong(value);
} catch (Exception e) {
throw new CommandException(getHelp().getUsageText());
}
if (time < 0 || time > 24000) {
throw new CommandException(Text.of(TextColors.RED, "Time value must be between 0 - 24000"), false);
}
world.setWorldTime(time);
Sponge.getServer().saveWorldProperties(world);
source.sendMessage(Text.of(TextColors.DARK_GREEN, "Set time of ", world.getWorldName(), " to ", TextColors.YELLOW, Utils.getTime(world.getWorldTime())));
return CommandResult.success();
}
use of org.spongepowered.api.world.storage.WorldProperties in project ProjectWorlds by trentech.
the class CommandTime method getSuggestions.
@Override
public List<String> getSuggestions(CommandSource source, String arguments, Location<World> targetPosition) throws CommandException {
List<String> list = new ArrayList<>();
if (arguments.equalsIgnoreCase("time")) {
return list;
}
String[] args = arguments.split(" ");
if (args.length == 1) {
for (WorldProperties world : Sponge.getServer().getAllWorldProperties()) {
if (world.getWorldName().equalsIgnoreCase(args[0])) {
return list;
}
if (world.getWorldName().toLowerCase().startsWith(args[0].toLowerCase())) {
list.add(world.getWorldName());
}
}
}
return list;
}
Aggregations