Search in sources :

Example 1 with SpongeData

use of com.gmail.trentech.pjw.io.SpongeData in project ProjectWorlds by trentech.

the class CommandImport method process.

@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
    if (arguments.equalsIgnoreCase("import")) {
        throw new CommandException(getHelp().getUsageText());
    }
    String[] args = arguments.split(" ");
    if (args[args.length - 1].equalsIgnoreCase("--help")) {
        help.execute(source);
        return CommandResult.success();
    }
    String worldName;
    String genType;
    String dimType;
    try {
        worldName = args[0];
    } catch (Exception e) {
        throw new CommandException(getHelp().getUsageText());
    }
    if (Sponge.getServer().getWorld(worldName).isPresent()) {
        throw new CommandException(Text.of(TextColors.RED, worldName, " is already loaded"), false);
    }
    WorldData worldData = new WorldData(worldName);
    if (!worldData.exists()) {
        throw new CommandException(Text.of(TextColors.RED, worldName, " is not a valid world"), false);
    }
    SpongeData spongeData = new SpongeData(worldName);
    if (spongeData.exists()) {
        source.sendMessage(Text.of(TextColors.RED, "Sponge world detected"));
        source.sendMessage(Text.builder().color(TextColors.YELLOW).onHover(TextActions.showText(Text.of("Click command for more information "))).onClick(TextActions.runCommand("/pjw:world load")).append(Text.of(" /world load")).build());
        return CommandResult.success();
    }
    try {
        dimType = args[1];
    } catch (Exception e) {
        throw new CommandException(getHelp().getUsageText());
    }
    Optional<DimensionType> optionalDimensionType = Sponge.getRegistry().getType(DimensionType.class, dimType);
    if (!optionalDimensionType.isPresent()) {
        source.sendMessage(Text.of(TextColors.YELLOW, dimType, " is not a valid DimensionType"));
        throw new CommandException(getHelp().getUsageText());
    }
    DimensionType dimensionType = optionalDimensionType.get();
    try {
        genType = args[2];
    } catch (Exception e) {
        throw new CommandException(getHelp().getUsageText());
    }
    Optional<GeneratorType> optionalGeneratorType = Sponge.getRegistry().getType(GeneratorType.class, genType);
    if (!optionalGeneratorType.isPresent()) {
        source.sendMessage(Text.of(TextColors.YELLOW, genType, " is not a valid GeneratorType"));
        throw new CommandException(getHelp().getUsageText());
    }
    GeneratorType generatorType = optionalGeneratorType.get();
    WorldArchetype.Builder builder = WorldArchetype.builder().dimension(dimensionType).generator(generatorType).enabled(true).keepsSpawnLoaded(true).loadsOnStartup(true);
    Collection<WorldGeneratorModifier> modifiers = Collections.<WorldGeneratorModifier>emptyList();
    if (args.length >= 4) {
        for (int i = 3; i < args.length - 3; i++) {
            Optional<WorldGeneratorModifier> optionalModifier = Sponge.getRegistry().getType(WorldGeneratorModifier.class, args[i]);
            if (!optionalModifier.isPresent()) {
                source.sendMessage(Text.of(TextColors.YELLOW, args[i], " is not a valid WorldGeneratorModifier"));
                throw new CommandException(getHelp().getUsageText());
            }
            modifiers.add(optionalModifier.get());
        }
    }
    WorldArchetype settings = builder.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);
    source.sendMessage(Text.of(TextColors.DARK_GREEN, worldName, " imported successfully"));
    return CommandResult.success();
}
Also used : DimensionType(org.spongepowered.api.world.DimensionType) SpongeData(com.gmail.trentech.pjw.io.SpongeData) CommandException(org.spongepowered.api.command.CommandException) WorldData(com.gmail.trentech.pjw.io.WorldData) IOException(java.io.IOException) IOException(java.io.IOException) CommandException(org.spongepowered.api.command.CommandException) GeneratorType(org.spongepowered.api.world.GeneratorType) WorldGeneratorModifier(org.spongepowered.api.world.gen.WorldGeneratorModifier) WorldArchetype(org.spongepowered.api.world.WorldArchetype) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 2 with SpongeData

use of com.gmail.trentech.pjw.io.SpongeData in project ProjectWorlds by trentech.

the class CommandLoad method process.

@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
    if (arguments.equalsIgnoreCase("load")) {
        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.getUniqueId()).isPresent()) {
        throw new CommandException(Text.of(TextColors.RED, world.getWorldName(), " is already loaded"), false);
    }
    WorldData worldData = new WorldData(world.getWorldName());
    if (!worldData.exists()) {
        throw new CommandException(Text.of(TextColors.RED, world.getWorldName(), " does not exist"), false);
    }
    SpongeData spongeData = new SpongeData(world.getWorldName());
    if (!spongeData.exists()) {
        source.sendMessage(Text.of(TextColors.RED, "Foriegn world detected"));
        source.sendMessage(Text.builder().color(TextColors.YELLOW).onHover(TextActions.showText(Text.of("Click command for more information "))).onClick(TextActions.runCommand("/pjw:world import")).append(Text.of(" /world import")).build());
        return CommandResult.success();
    }
    source.sendMessage(Text.of(TextColors.YELLOW, "Preparing spawn area. This may take a minute."));
    Task.builder().delayTicks(20).execute(c -> {
        Optional<World> load = Sponge.getServer().loadWorld(world);
        if (!load.isPresent()) {
            source.sendMessage(Text.of(TextColors.RED, "Could not load ", world.getWorldName()));
            return;
        }
        if (CommandCreate.worlds.contains(world.getWorldName())) {
            Utils.createPlatform(load.get().getSpawnLocation().getRelative(Direction.DOWN));
            CommandCreate.worlds.remove(world.getWorldName());
        }
        source.sendMessage(Text.of(TextColors.DARK_GREEN, world.getWorldName(), " loaded successfully"));
    }).submit(Main.getPlugin());
    return CommandResult.success();
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) Location(org.spongepowered.api.world.Location) Utils(com.gmail.trentech.pjw.utils.Utils) CommandSource(org.spongepowered.api.command.CommandSource) WorldData(com.gmail.trentech.pjw.io.WorldData) CommandCallable(org.spongepowered.api.command.CommandCallable) Sponge(org.spongepowered.api.Sponge) ArrayList(java.util.ArrayList) CommandException(org.spongepowered.api.command.CommandException) Direction(org.spongepowered.api.util.Direction) List(java.util.List) Text(org.spongepowered.api.text.Text) Main(com.gmail.trentech.pjw.Main) 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) SpongeData(com.gmail.trentech.pjw.io.SpongeData) TextColors(org.spongepowered.api.text.format.TextColors) Optional(java.util.Optional) SpongeData(com.gmail.trentech.pjw.io.SpongeData) CommandException(org.spongepowered.api.command.CommandException) WorldData(com.gmail.trentech.pjw.io.WorldData) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Aggregations

SpongeData (com.gmail.trentech.pjw.io.SpongeData)2 WorldData (com.gmail.trentech.pjw.io.WorldData)2 CommandException (org.spongepowered.api.command.CommandException)2 WorldProperties (org.spongepowered.api.world.storage.WorldProperties)2 Help (com.gmail.trentech.pjc.help.Help)1 Main (com.gmail.trentech.pjw.Main)1 Utils (com.gmail.trentech.pjw.utils.Utils)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 Sponge (org.spongepowered.api.Sponge)1 CommandCallable (org.spongepowered.api.command.CommandCallable)1 CommandResult (org.spongepowered.api.command.CommandResult)1 CommandSource (org.spongepowered.api.command.CommandSource)1 Task (org.spongepowered.api.scheduler.Task)1 Text (org.spongepowered.api.text.Text)1 TextActions (org.spongepowered.api.text.action.TextActions)1 TextColors (org.spongepowered.api.text.format.TextColors)1 Direction (org.spongepowered.api.util.Direction)1