Search in sources :

Example 1 with Weather

use of org.spongepowered.api.world.weather.Weather in project Skree by Skelril.

the class WeatherCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    // World resolution
    Optional<WorldProperties> optWorldProps = args.getOne("world");
    Optional<World> optWorld;
    if (!optWorldProps.isPresent()) {
        if (!(src instanceof Player)) {
            src.sendMessage(Text.of(TextColors.RED, "You are not a player and need to specify a world!"));
            return CommandResult.empty();
        }
        optWorld = Optional.of(((Player) src).getWorld());
    } else {
        optWorld = Sponge.getServer().getWorld(optWorldProps.get().getUniqueId());
    }
    // Handled by command spec, so always provided
    Weather weather = args.<Weather>getOne("type").get();
    Optional<Integer> duration = args.getOne("duration");
    if (!duration.isPresent()) {
        // Between 5 and 15 minutes
        duration = Optional.of(Probability.getRangedRandom(5 * 60, 15 * 60));
    }
    if (duration.get() < 1) {
        src.sendMessage(Text.of(TextColors.RED, "Weather duration must be at least 1 second!"));
        return CommandResult.empty();
    }
    World world = optWorld.get();
    if (!world.isLoaded()) {
        src.sendMessage(Text.of(TextColors.RED, "The specified world was not loaded!"));
        return CommandResult.empty();
    }
    world.setWeather(weather, duration.get() * 20);
    src.sendMessage(Text.of(TextColors.YELLOW, "Changed weather state in " + world.getName() + " to: " + weather.getName() + '.'));
    return CommandResult.success();
}
Also used : Weather(org.spongepowered.api.world.weather.Weather) Player(org.spongepowered.api.entity.living.player.Player) World(org.spongepowered.api.world.World) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Aggregations

Player (org.spongepowered.api.entity.living.player.Player)1 World (org.spongepowered.api.world.World)1 WorldProperties (org.spongepowered.api.world.storage.WorldProperties)1 Weather (org.spongepowered.api.world.weather.Weather)1