Search in sources :

Example 66 with WorldProperties

use of org.spongepowered.api.world.storage.WorldProperties in project ProjectWorlds by trentech.

the class CommandGamemode method process.

@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
    if (arguments.equalsIgnoreCase("gamemode")) {
        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 gm;
    try {
        worldName = args[0];
    } catch (Exception e) {
        throw new CommandException(getHelp().getUsageText());
    }
    try {
        gm = args[1];
    } catch (Exception e) {
        throw new CommandException(getHelp().getUsageText());
    }
    Optional<WorldProperties> optionalWorld = Sponge.getServer().getWorldProperties(worldName);
    if (!optionalWorld.isPresent()) {
        throw new CommandException(Text.of(TextColors.RED, worldName, " does not exist"), false);
    }
    WorldProperties world = optionalWorld.get();
    Optional<GameMode> optionalGameMode = Gamemode.get(gm);
    if (!optionalGameMode.isPresent()) {
        try {
            optionalGameMode = Gamemode.get(Integer.parseInt(gm));
        } catch (Exception e) {
            source.sendMessage(Text.of(TextColors.YELLOW, gm, " is not a valid GameMode"));
            throw new CommandException(getHelp().getUsageText());
        }
        if (!optionalGameMode.isPresent()) {
            source.sendMessage(Text.of(TextColors.YELLOW, gm, " is not a valid GameMode"));
            throw new CommandException(getHelp().getUsageText());
        }
    }
    GameMode gameMode = optionalGameMode.get();
    world.setGameMode(gameMode);
    Sponge.getServer().saveWorldProperties(world);
    source.sendMessage(Text.of(TextColors.DARK_GREEN, "Set gamemode of ", world.getWorldName(), " to ", TextColors.YELLOW, gameMode.getTranslation().get().toUpperCase()));
    return CommandResult.success();
}
Also used : GameMode(org.spongepowered.api.entity.living.player.gamemode.GameMode) CommandException(org.spongepowered.api.command.CommandException) CommandException(org.spongepowered.api.command.CommandException) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 67 with WorldProperties

use of org.spongepowered.api.world.storage.WorldProperties in project ProjectWorlds by trentech.

the class CommandGamerule method process.

@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
    if (arguments.equalsIgnoreCase("gamerule")) {
        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();
    String rule;
    try {
        rule = args[1];
        if (!validGamerule(properties, rule)) {
            source.sendMessage(Text.of(TextColors.YELLOW, rule, " is not a valid Gamerule"));
            throw new CommandException(getHelp().getUsageText());
        }
    } catch (Exception e) {
        List<Text> list = new ArrayList<>();
        for (Entry<String, String> gamerule : properties.getGameRules().entrySet()) {
            list.add(Text.of(TextColors.GREEN, gamerule.getKey(), ": ", TextColors.WHITE, gamerule.getValue()));
        }
        if (source instanceof Player) {
            PaginationList.Builder pages = PaginationList.builder();
            pages.title(Text.builder().color(TextColors.DARK_GREEN).append(Text.of(TextColors.GREEN, properties.getWorldName())).build());
            pages.contents(list);
            pages.sendTo(source);
        } else {
            for (Text text : list) {
                source.sendMessage(text);
            }
        }
        return CommandResult.success();
    }
    String value;
    try {
        value = args[2];
        if (!isValid(rule, value)) {
            source.sendMessage(Text.of(TextColors.YELLOW, value, " is not a valid Value for Gamerule ", rule));
            throw new CommandException(getHelp().getUsageText());
        }
    } catch (Exception e) {
        source.sendMessage(Text.of(TextColors.GREEN, rule, ": ", TextColors.WHITE, properties.getGameRule(rule).get()));
        return CommandResult.success();
    }
    properties.setGameRule(rule, value.toLowerCase());
    Sponge.getServer().saveWorldProperties(properties);
    source.sendMessage(Text.of(TextColors.DARK_GREEN, "Set gamerule ", rule, " to ", value));
    return CommandResult.success();
}
Also used : Entry(java.util.Map.Entry) Player(org.spongepowered.api.entity.living.player.Player) ArrayList(java.util.ArrayList) PaginationList(org.spongepowered.api.service.pagination.PaginationList) List(java.util.List) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException) CommandException(org.spongepowered.api.command.CommandException) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 68 with WorldProperties

use of org.spongepowered.api.world.storage.WorldProperties in project ProjectWorlds by trentech.

the class CommandImport method getSuggestions.

@Override
public List<String> getSuggestions(CommandSource source, String arguments, Location<World> targetPosition) throws CommandException {
    List<String> list = new ArrayList<>();
    if (arguments.equalsIgnoreCase("import")) {
        return list;
    }
    String[] args = arguments.split(" ");
    if (args.length == 1) {
        for (WorldProperties world : Sponge.getServer().getAllWorldProperties()) {
            if (world.getWorldName().equalsIgnoreCase(args[0])) {
                for (DimensionType type : Sponge.getRegistry().getAllOf(DimensionType.class)) {
                    list.add(type.getId());
                }
                return list;
            }
            if (world.getWorldName().toLowerCase().startsWith(args[0].toLowerCase())) {
                list.add(world.getWorldName());
            }
        }
    }
    if (args.length == 2) {
        for (DimensionType type : Sponge.getRegistry().getAllOf(DimensionType.class)) {
            if (type.getId().equalsIgnoreCase(args[1])) {
                for (GeneratorType genType : Sponge.getRegistry().getAllOf(GeneratorType.class)) {
                    list.add(genType.getId());
                }
                return list;
            }
            if (type.getId().toLowerCase().startsWith(args[1].toLowerCase())) {
                list.add(type.getId());
            }
        }
    }
    if (args.length == 3) {
        for (GeneratorType type : Sponge.getRegistry().getAllOf(GeneratorType.class)) {
            if (type.getId().equalsIgnoreCase(args[1])) {
                for (WorldGeneratorModifier modType : Sponge.getRegistry().getAllOf(WorldGeneratorModifier.class)) {
                    list.add(modType.getId());
                }
                return list;
            }
            if (type.getId().toLowerCase().startsWith(args[1].toLowerCase())) {
                list.add(type.getId());
            }
        }
    }
    if (args.length >= 4) {
        for (WorldGeneratorModifier type : Sponge.getRegistry().getAllOf(WorldGeneratorModifier.class)) {
            if (type.getId().equalsIgnoreCase(args[1])) {
                for (WorldGeneratorModifier modType : Sponge.getRegistry().getAllOf(WorldGeneratorModifier.class)) {
                    list.add(modType.getId());
                }
                return list;
            }
            if (type.getId().toLowerCase().startsWith(args[1].toLowerCase())) {
                list.add(type.getId());
            }
        }
    }
    return list;
}
Also used : DimensionType(org.spongepowered.api.world.DimensionType) WorldGeneratorModifier(org.spongepowered.api.world.gen.WorldGeneratorModifier) ArrayList(java.util.ArrayList) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) GeneratorType(org.spongepowered.api.world.GeneratorType)

Example 69 with WorldProperties

use of org.spongepowered.api.world.storage.WorldProperties 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 70 with WorldProperties

use of org.spongepowered.api.world.storage.WorldProperties in project ProjectWorlds by trentech.

the class CommandKeepSpawnLoaded method process.

@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
    if (arguments.equalsIgnoreCase("keepspawnloaded")) {
        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 bool;
    try {
        worldName = args[0];
    } catch (Exception e) {
        throw new CommandException(getHelp().getUsageText());
    }
    try {
        bool = args[1];
    } catch (Exception e) {
        throw new CommandException(getHelp().getUsageText());
    }
    Optional<WorldProperties> optionalWorld = Sponge.getServer().getWorldProperties(worldName);
    if (!optionalWorld.isPresent()) {
        throw new CommandException(Text.of(TextColors.RED, worldName, " does not exist"), false);
    }
    WorldProperties world = optionalWorld.get();
    if (bool.equalsIgnoreCase("true") || bool.equalsIgnoreCase("false")) {
        world.setKeepSpawnLoaded(Boolean.valueOf(bool));
    }
    Sponge.getServer().saveWorldProperties(world);
    source.sendMessage(Text.of(TextColors.DARK_GREEN, "Set keep spawn loaded of ", world.getWorldName(), " to ", TextColors.YELLOW, bool));
    return CommandResult.success();
}
Also used : CommandException(org.spongepowered.api.command.CommandException) CommandException(org.spongepowered.api.command.CommandException) 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