Search in sources :

Example 26 with WorldProperties

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

the class CommandTeleport method getSuggestions.

@Override
public List<String> getSuggestions(CommandSource source, String arguments, Location<World> targetPosition) throws CommandException {
    List<String> list = new ArrayList<>();
    if (arguments.equalsIgnoreCase("teleport")) {
        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;
    }
    String arg = args[args.length - 1];
    if (arg.equalsIgnoreCase("-c") || arg.equalsIgnoreCase("-coords") || arg.equalsIgnoreCase("-f") || arg.equalsIgnoreCase("-force")) {
        return list;
    } else if (arg.equalsIgnoreCase("-d") || arg.equalsIgnoreCase("-direction")) {
        for (Rotation type : Rotation.values()) {
            list.add(type.getName());
        }
    } else {
        String parent = args[args.length - 2];
        if (parent.equalsIgnoreCase("-c") || parent.equalsIgnoreCase("-coords") || parent.equalsIgnoreCase("-f") || parent.equalsIgnoreCase("-force")) {
            return list;
        } else if (parent.equalsIgnoreCase("-d") || parent.equalsIgnoreCase("-direction")) {
            for (Rotation type : Rotation.values()) {
                if (type.getName().toLowerCase().startsWith(arg.toLowerCase())) {
                    list.add(type.getName());
                }
            }
        }
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) Rotation(com.gmail.trentech.pjw.utils.Rotation) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 27 with WorldProperties

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

the class CommandTeleport method process.

@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
    if (!(source instanceof Player)) {
        throw new CommandException(Text.of(TextColors.RED, "Must be a player"), false);
    }
    Player player = (Player) source;
    if (arguments.equalsIgnoreCase("teleport")) {
        throw new CommandException(getHelp().getUsageText());
    }
    String[] args = arguments.split(" ");
    if (args[args.length - 1].equalsIgnoreCase("--help")) {
        help.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();
    Optional<World> optionalWorld = Sponge.getServer().getWorld(worldName);
    if (!optionalWorld.isPresent()) {
        throw new CommandException(Text.of(TextColors.RED, properties.getWorldName(), " is not loaded"), false);
    }
    World world = optionalWorld.get();
    Location<World> location = world.getSpawnLocation();
    Rotation rotation = Rotation.getClosest(player.getRotation().getFloorY());
    if (args.length > 2) {
        boolean skip = false;
        for (int i = 1; i < args.length - 1; i++) {
            if (skip) {
                skip = false;
                continue;
            }
            String arg = args[i];
            String value;
            try {
                value = args[i + 1];
            } catch (Exception e) {
                throw new CommandException(getHelp().getUsageText());
            }
            if (arg.equalsIgnoreCase("-c") || arg.equalsIgnoreCase("-coords")) {
                if (arg.equalsIgnoreCase("random")) {
                    Optional<Location<World>> optionalLocation = TeleportManager.getRandomLocation(world, 2000);
                    if (!optionalLocation.isPresent()) {
                        throw new CommandException(Text.of(TextColors.RED, "Took to long to find a safe random location. Try again"), false);
                    }
                    location = optionalLocation.get();
                } else {
                    String[] coords = value.split(",");
                    try {
                        int x = Integer.parseInt(coords[0]);
                        int y = Integer.parseInt(coords[1]);
                        int z = Integer.parseInt(coords[2]);
                        location = world.getLocation(x, y, z);
                    } catch (Exception e) {
                        throw new CommandException(Text.of(TextColors.RED, coords.toString(), " is not a valid Coordinate"), true);
                    }
                }
            } else if (arg.equalsIgnoreCase("-d") || arg.equalsIgnoreCase("-direction")) {
                Optional<Rotation> optionalRotation = Rotation.get(value);
                if (!optionalRotation.isPresent()) {
                    throw new CommandException(Text.of(TextColors.RED, "Incorrect direction"));
                }
                rotation = optionalRotation.get();
            } else if (arg.equalsIgnoreCase("-f") || arg.equalsIgnoreCase("-force")) {
                Optional<Location<World>> optionalLocation = TeleportManager.getSafeLocation(location);
                if (!optionalLocation.isPresent()) {
                    throw new CommandException(Text.of(Text.builder().color(TextColors.RED).append(Text.of("Unsafe spawn point detected. ")).onClick(TextActions.executeCallback(TeleportManager.setUnsafeLocation(location))).append(Text.of(TextColors.GOLD, TextStyles.UNDERLINE, "Click Here")).build(), TextColors.RED, " or use the -f flag to force teleport."));
                }
                location = optionalLocation.get();
            } else {
                source.sendMessage(Text.of(TextColors.YELLOW, arg, " is not a valid Flag"));
                throw new CommandException(getHelp().getUsageText());
            }
            skip = true;
        }
    }
    player.setLocationAndRotation(location, rotation.toVector3d());
    player.sendTitle(Title.of(Text.of(TextColors.DARK_GREEN, properties.getWorldName()), Text.of(TextColors.AQUA, "x: ", location.getBlockX(), ", y: ", location.getBlockY(), ", z: ", location.getBlockZ())));
    return CommandResult.success();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Optional(java.util.Optional) CommandException(org.spongepowered.api.command.CommandException) World(org.spongepowered.api.world.World) Rotation(com.gmail.trentech.pjw.utils.Rotation) CommandException(org.spongepowered.api.command.CommandException) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Location(org.spongepowered.api.world.Location)

Example 28 with WorldProperties

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

the class CommandUnload method process.

@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
    if (arguments.equalsIgnoreCase("unload")) {
        throw new CommandException(getHelp().getUsageText());
    }
    if (arguments.equalsIgnoreCase("--help")) {
        help.execute(source);
        return CommandResult.success();
    }
    Optional<WorldProperties> optionalProperties = Sponge.getServer().getWorldProperties(arguments);
    if (!optionalProperties.isPresent()) {
        throw new CommandException(Text.of(TextColors.RED, arguments, " does not exist"), false);
    }
    WorldProperties properties = optionalProperties.get();
    Optional<World> optionalWorld = Sponge.getServer().getWorld(properties.getWorldName());
    if (!optionalWorld.isPresent()) {
        throw new CommandException(Text.of(TextColors.RED, properties.getWorldName(), " is already unloaded"), false);
    }
    World world = optionalWorld.get();
    if (world.getUniqueId().toString().equals(ConfigManager.get(Main.getPlugin()).getConfig().getNode("options", "world_root").getString())) {
        throw new CommandException(Text.of(TextColors.RED, "You cannot unload the default world"), false);
    }
    ConfigurationNode node = ConfigManager.get(Main.getPlugin()).getConfig().getNode("options");
    World defaultWorld = Sponge.getServer().getWorld(Sponge.getServer().getDefaultWorld().get().getWorldName()).get();
    String joinWorldName = node.getNode("first_join", "world").getString();
    optionalWorld = Sponge.getServer().getWorld(joinWorldName);
    if (optionalWorld.isPresent()) {
        defaultWorld = optionalWorld.get();
    }
    for (Entity entity : world.getEntities()) {
        if (entity instanceof Player) {
            Player player = (Player) entity;
            player.setLocationSafely(defaultWorld.getSpawnLocation());
            player.sendMessage(Text.of(TextColors.YELLOW, properties.getWorldName(), " is being unloaded"));
        }
    }
    if (!Sponge.getServer().unloadWorld(world)) {
        throw new CommandException(Text.of(TextColors.RED, "Could not unload ", properties.getWorldName()), false);
    }
    source.sendMessage(Text.of(TextColors.DARK_GREEN, properties.getWorldName(), " unloaded successfully"));
    return CommandResult.success();
}
Also used : Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) CommandException(org.spongepowered.api.command.CommandException) World(org.spongepowered.api.world.World) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 29 with WorldProperties

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

the class CommandUseMapFeatures method getSuggestions.

@Override
public List<String> getSuggestions(CommandSource source, String arguments, Location<World> targetPosition) throws CommandException {
    List<String> list = new ArrayList<>();
    if (arguments.equalsIgnoreCase("usemapfeatures")) {
        return list;
    }
    String[] args = arguments.split(" ");
    if (args.length == 1) {
        for (WorldProperties world : Sponge.getServer().getAllWorldProperties()) {
            if (world.getWorldName().equalsIgnoreCase(args[0])) {
                list.add("true");
                list.add("false");
                return list;
            }
            if (world.getWorldName().toLowerCase().startsWith(args[0].toLowerCase())) {
                list.add(world.getWorldName());
            }
        }
        return list;
    }
    if (args.length == 2) {
        if ("true".equalsIgnoreCase(args[1].toLowerCase()) || "false".equalsIgnoreCase(args[1].toLowerCase())) {
            return list;
        }
        if ("true".startsWith(args[1].toLowerCase())) {
            list.add("true");
        }
        if ("false".startsWith(args[1].toLowerCase())) {
            list.add("false");
        }
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 30 with WorldProperties

use of org.spongepowered.api.world.storage.WorldProperties in project SpongeForge by SpongePowered.

the class MixinDimensionManager method initDimension.

/**
 * @author Zidane - June 2nd, 2016
 * @reason Forge's initDimension is very different from Sponge's multi-world. We basically rig it into our system so mods work.
 * @param dim The dimension to load
 */
@Overwrite
public static void initDimension(int dim) {
    // World is already loaded, bail
    if (WorldManager.getWorldByDimensionId(dim).isPresent()) {
        return;
    }
    if (dim == 0) {
        throw new RuntimeException("Attempt made to initialize overworld!");
    }
    WorldManager.getWorldByDimensionId(0).orElseThrow(() -> new RuntimeException("Attempt made to initialize " + "dimension before overworld is loaded!"));
    DimensionType dimensionType = WorldManager.getDimensionType(dim).orElse(null);
    if (dimensionType == null) {
        SpongeImpl.getLogger().warn("Attempt made to initialize dimension id {} which isn't registered!" + ", falling back to overworld.", dim);
        return;
    }
    final WorldProvider provider = dimensionType.createDimension();
    // make sure to set the dimension id to avoid getting a null save folder
    provider.setDimension(dim);
    String worldFolder = WorldManager.getWorldFolderByDimensionId(dim).orElse(provider.getSaveFolder());
    WorldProperties properties = WorldManager.getWorldProperties(worldFolder).orElse(null);
    final Path worldPath = WorldManager.getCurrentSavesDirectory().get().resolve(worldFolder);
    if (properties == null || !Files.isDirectory(worldPath)) {
        if (properties != null) {
            WorldManager.unregisterWorldProperties(properties, false);
        }
        String modId = StaticMixinForgeHelper.getModIdFromClass(provider.getClass());
        WorldArchetype archetype = Sponge.getRegistry().getType(WorldArchetype.class, modId + ":" + dimensionType.getName().toLowerCase()).orElse(null);
        if (archetype == null) {
            final WorldArchetype.Builder builder = WorldArchetype.builder().dimension((org.spongepowered.api.world.DimensionType) (Object) dimensionType).keepsSpawnLoaded(dimensionType.shouldLoadSpawn());
            archetype = builder.build(modId + ":" + dimensionType.getName().toLowerCase(), dimensionType.getName());
        }
        IMixinWorldSettings worldSettings = (IMixinWorldSettings) archetype;
        worldSettings.setDimensionType((org.spongepowered.api.world.DimensionType) (Object) dimensionType);
        worldSettings.setLoadOnStartup(false);
        properties = WorldManager.createWorldProperties(worldFolder, archetype, dim);
        ((IMixinWorldInfo) properties).setDimensionId(dim);
        ((IMixinWorldInfo) properties).setIsMod(true);
    }
    if (!properties.isEnabled()) {
        return;
    }
    Optional<WorldServer> optWorld = WorldManager.loadWorld(properties);
    if (!optWorld.isPresent()) {
        SpongeImpl.getLogger().error("Could not load world [{}]!", properties.getWorldName());
    }
}
Also used : Path(java.nio.file.Path) DimensionType(net.minecraft.world.DimensionType) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) WorldServer(net.minecraft.world.WorldServer) WorldArchetype(org.spongepowered.api.world.WorldArchetype) WorldProvider(net.minecraft.world.WorldProvider) IMixinWorldSettings(org.spongepowered.common.interfaces.world.IMixinWorldSettings) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Overwrite(org.spongepowered.asm.mixin.Overwrite)

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