Search in sources :

Example 76 with WorldProperties

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();
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) Location(org.spongepowered.api.world.Location) Utils(com.gmail.trentech.pjw.utils.Utils) CommandSource(org.spongepowered.api.command.CommandSource) CommandCallable(org.spongepowered.api.command.CommandCallable) Sponge(org.spongepowered.api.Sponge) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) CommandException(org.spongepowered.api.command.CommandException) Direction(org.spongepowered.api.util.Direction) WorldArchetype(org.spongepowered.api.world.WorldArchetype) List(java.util.List) Text(org.spongepowered.api.text.Text) Main(com.gmail.trentech.pjw.Main) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) World(org.spongepowered.api.world.World) Task(org.spongepowered.api.scheduler.Task) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Help(com.gmail.trentech.pjc.help.Help) Optional(java.util.Optional) TextColors(org.spongepowered.api.text.format.TextColors) Optional(java.util.Optional) CommandException(org.spongepowered.api.command.CommandException) IOException(java.io.IOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) CommandException(org.spongepowered.api.command.CommandException) WorldArchetype(org.spongepowered.api.world.WorldArchetype) ExecutionException(java.util.concurrent.ExecutionException) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 77 with WorldProperties

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();
}
Also used : Zip(com.gmail.trentech.pjw.utils.Zip) CommandException(org.spongepowered.api.command.CommandException) ExecutionException(java.util.concurrent.ExecutionException) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 78 with WorldProperties

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();
}
Also used : CommandException(org.spongepowered.api.command.CommandException) CommandException(org.spongepowered.api.command.CommandException) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 79 with WorldProperties

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();
}
Also used : CommandException(org.spongepowered.api.command.CommandException) CommandException(org.spongepowered.api.command.CommandException) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 80 with WorldProperties

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;
}
Also used : ArrayList(java.util.ArrayList) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Aggregations

WorldProperties (org.spongepowered.api.world.storage.WorldProperties)109 World (org.spongepowered.api.world.World)37 CommandException (org.spongepowered.api.command.CommandException)27 ArrayList (java.util.ArrayList)26 Player (org.spongepowered.api.entity.living.player.Player)19 Text (org.spongepowered.api.text.Text)16 Vector3d (com.flowpowered.math.vector.Vector3d)9 ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)9 Listener (org.spongepowered.api.event.Listener)9 WorldGeneratorModifier (org.spongepowered.api.world.gen.WorldGeneratorModifier)9 List (java.util.List)8 Optional (java.util.Optional)8 CommandResult (org.spongepowered.api.command.CommandResult)8 Vector3i (com.flowpowered.math.vector.Vector3i)7 IOException (java.io.IOException)7 Sponge (org.spongepowered.api.Sponge)7 CommandSource (org.spongepowered.api.command.CommandSource)7 IMixinWorldInfo (org.spongepowered.common.interfaces.world.IMixinWorldInfo)7 GenericArguments (org.spongepowered.api.command.args.GenericArguments)6 Location (org.spongepowered.api.world.Location)6