Search in sources :

Example 11 with CommandContext

use of org.spongepowered.api.command.args.CommandContext in project CatClearLag by Time6628.

the class EntitiesCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    List<Chunk> chunksToSort = (List<Chunk>) ((Player) src).getWorld().getLoadedChunks();
    TreeMap<Chunk, Integer> sortedChunks = new TreeMap<>((o1, o2) -> Integer.compare(o2.getEntities().size(), o1.getEntities().size()));
    for (Chunk chunk : chunksToSort) {
        sortedChunks.put(chunk, chunk.getEntities().size());
    }
    List<Text> texts = new ArrayList<>();
    sortedChunks.forEach(((chunk, integer) -> texts.add(Text.builder().append(Text.of(chunk.getPosition().getX() + "," + chunk.getPosition().getZ() + " contains " + integer + " entities.")).onClick(callback(chunk)).build())));
    plugin.getPaginationService().builder().contents((texts)).title(Text.builder().color(TextColors.LIGHT_PURPLE).append(Text.of("Laggy Chunks")).build()).sendTo(src);
    return CommandResult.success();
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) CommandSource(org.spongepowered.api.command.CommandSource) Chunk(org.spongepowered.api.world.Chunk) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) ArrayList(java.util.ArrayList) CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) TreeMap(java.util.TreeMap) LaggyChunksCommand(me.time6628.clag.sponge.commands.LaggyChunksCommand) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) CatClearLag(me.time6628.clag.sponge.CatClearLag) Player(org.spongepowered.api.entity.living.player.Player) TextColors(org.spongepowered.api.text.format.TextColors) Player(org.spongepowered.api.entity.living.player.Player) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Text(org.spongepowered.api.text.Text) Chunk(org.spongepowered.api.world.Chunk) TreeMap(java.util.TreeMap)

Example 12 with CommandContext

use of org.spongepowered.api.command.args.CommandContext in project CatClearLag by Time6628.

the class TilesCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    List<Chunk> chunksToSort = (List<Chunk>) ((Player) src).getWorld().getLoadedChunks();
    TreeMap<Chunk, Integer> sortedChunks = new TreeMap<>((o1, o2) -> Integer.compare(o2.getTileEntities().size(), o1.getTileEntities().size()));
    for (Chunk chunk : chunksToSort) {
        sortedChunks.put(chunk, chunk.getTileEntities().size());
    }
    List<Text> texts = new ArrayList<>();
    sortedChunks.forEach(((chunk, integer) -> texts.add(Text.builder().append(Text.of(chunk.getPosition().getX() + "," + chunk.getPosition().getZ() + " contains " + integer + " tiles.")).onClick(callback(chunk)).build())));
    plugin.getPaginationService().builder().contents((texts)).title(Text.builder().color(TextColors.LIGHT_PURPLE).append(Text.of("Laggy Chunks")).build()).sendTo(src);
    return CommandResult.success();
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) CommandSource(org.spongepowered.api.command.CommandSource) Chunk(org.spongepowered.api.world.Chunk) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) ArrayList(java.util.ArrayList) CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) TreeMap(java.util.TreeMap) LaggyChunksCommand(me.time6628.clag.sponge.commands.LaggyChunksCommand) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) CatClearLag(me.time6628.clag.sponge.CatClearLag) Player(org.spongepowered.api.entity.living.player.Player) TextColors(org.spongepowered.api.text.format.TextColors) Player(org.spongepowered.api.entity.living.player.Player) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Text(org.spongepowered.api.text.Text) Chunk(org.spongepowered.api.world.Chunk) TreeMap(java.util.TreeMap)

Example 13 with CommandContext

use of org.spongepowered.api.command.args.CommandContext in project SpongeAPI by SpongePowered.

the class CommandSpec method getSuggestions.

@Override
public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPos) throws CommandException {
    CommandArgs args = new CommandArgs(arguments, getInputTokenizer().tokenize(arguments, true));
    CommandContext ctx = new CommandContext();
    if (targetPos != null) {
        ctx.putArg(CommandContext.TARGET_BLOCK_ARG, targetPos);
    }
    ctx.putArg(CommandContext.TAB_COMPLETION, true);
    return complete(source, args, ctx);
}
Also used : CommandArgs(org.spongepowered.api.command.args.CommandArgs) CommandContext(org.spongepowered.api.command.args.CommandContext)

Example 14 with CommandContext

use of org.spongepowered.api.command.args.CommandContext in project Nucleus by NucleusPowered.

the class TeleportWorldCommand method executeCommand.

@Override
public CommandResult executeCommand(final CommandSource src, CommandContext args) throws Exception {
    Player player = getUserFromArgs(Player.class, src, playerKey, args, "command.world.player");
    WorldProperties worldProperties = args.<WorldProperties>getOne(world).get();
    if (!worldProperties.isEnabled()) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.world.teleport.notenabled", worldProperties.getWorldName()));
    }
    World world = Sponge.getServer().loadWorld(worldProperties.getUniqueId()).orElseThrow(() -> ReturnMessageException.fromKey("command.world.teleport.failed", worldProperties.getWorldName()));
    Vector3d pos = worldProperties.getSpawnPosition().toDouble();
    if (!player.transferToWorld(world, pos)) {
        throw ReturnMessageException.fromKey("command.world.teleport.failed", worldProperties.getWorldName());
    }
    // Rotate.
    Nucleus.getNucleus().getWorldDataManager().getWorld(worldProperties.getUniqueId()).ifPresent(x -> x.get(SpawnWorldDataModule.class).getSpawnRotation().ifPresent(y -> new Transform<World>(world, pos, y)));
    if (src instanceof Player && ((Player) src).getUniqueId().equals(player.getUniqueId())) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.world.teleport.success", worldProperties.getWorldName()));
    } else {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.world.teleport.successplayer", plugin.getNameUtil().getSerialisedName(player), worldProperties.getWorldName()));
        player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.world.teleport.success", worldProperties.getWorldName()));
    }
    return CommandResult.success();
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) Vector3d(com.flowpowered.math.vector.Vector3d) HashMap(java.util.HashMap) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) SpawnWorldDataModule(io.github.nucleuspowered.nucleus.modules.spawn.datamodules.SpawnWorldDataModule) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) NucleusWorldPropertiesArgument(io.github.nucleuspowered.nucleus.argumentparsers.NucleusWorldPropertiesArgument) CommandResult(org.spongepowered.api.command.CommandResult) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) CommandSource(org.spongepowered.api.command.CommandSource) Sponge(org.spongepowered.api.Sponge) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CommandElement(org.spongepowered.api.command.args.CommandElement) Entity(org.spongepowered.api.entity.Entity) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) Transform(org.spongepowered.api.entity.Transform) World(org.spongepowered.api.world.World) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) Player(org.spongepowered.api.entity.living.player.Player) Player(org.spongepowered.api.entity.living.player.Player) SpawnWorldDataModule(io.github.nucleuspowered.nucleus.modules.spawn.datamodules.SpawnWorldDataModule) Vector3d(com.flowpowered.math.vector.Vector3d) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) World(org.spongepowered.api.world.World) Transform(org.spongepowered.api.entity.Transform) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Example 15 with CommandContext

use of org.spongepowered.api.command.args.CommandContext in project Nucleus by NucleusPowered.

the class UnloadWorldCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    WorldProperties worldProperties = args.<WorldProperties>getOne(worldKey).get();
    Optional<WorldProperties> transferWorld = args.getOne(transferWorldKey);
    boolean disable = args.hasAny("d");
    Optional<World> worldOptional = Sponge.getServer().getWorld(worldProperties.getUniqueId());
    if (!worldOptional.isPresent()) {
        // Not loaded.
        if (disable) {
            disable(worldProperties, src, plugin.getMessageProvider(), false);
        }
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.world.unload.alreadyunloaded", worldProperties.getWorldName()));
    }
    World world = worldOptional.get();
    List<Player> playerCollection = Sponge.getServer().getOnlinePlayers().stream().filter(x -> x.getWorld().equals(world)).collect(Collectors.toList());
    if (playerCollection.isEmpty() || (transferWorld.isPresent() && transferWorld.get().isEnabled())) {
        if (!playerCollection.isEmpty()) {
            // Transfer World is present and enabled.
            playerCollection.forEach(x -> x.transferToWorld(transferWorld.get().getUniqueId(), transferWorld.get().getSpawnPosition().toDouble()));
            Sponge.getScheduler().createSyncExecutor(plugin).schedule(() -> unloadWorld(src, world, plugin.getMessageProvider(), disable), 40, TimeUnit.MILLISECONDS);
            // Well, this bit succeeded, at least.
            return CommandResult.success();
        } else if (unloadWorld(src, world, plugin.getMessageProvider(), disable)) {
            return CommandResult.success();
        } else {
            return CommandResult.empty();
        }
    }
    throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.world.unload.players", worldProperties.getWorldName()));
}
Also used : NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) CommandSource(org.spongepowered.api.command.CommandSource) Sponge(org.spongepowered.api.Sponge) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CommandElement(org.spongepowered.api.command.args.CommandElement) GenericArguments(org.spongepowered.api.command.args.GenericArguments) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) World(org.spongepowered.api.world.World) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) NucleusWorldPropertiesArgument(io.github.nucleuspowered.nucleus.argumentparsers.NucleusWorldPropertiesArgument) Player(org.spongepowered.api.entity.living.player.Player) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) World(org.spongepowered.api.world.World) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Aggregations

CommandContext (org.spongepowered.api.command.args.CommandContext)55 CommandResult (org.spongepowered.api.command.CommandResult)46 Text (org.spongepowered.api.text.Text)45 CommandSource (org.spongepowered.api.command.CommandSource)40 List (java.util.List)36 Collectors (java.util.stream.Collectors)30 CommandElement (org.spongepowered.api.command.args.CommandElement)30 Player (org.spongepowered.api.entity.living.player.Player)29 Optional (java.util.Optional)28 Sponge (org.spongepowered.api.Sponge)28 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)27 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)27 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)27 NonnullByDefault (org.spongepowered.api.util.annotation.NonnullByDefault)27 GenericArguments (org.spongepowered.api.command.args.GenericArguments)26 TextColors (org.spongepowered.api.text.format.TextColors)24 Util (io.github.nucleuspowered.nucleus.Util)19 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)19 NoModifiers (io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers)18 CommandException (org.spongepowered.api.command.CommandException)18