Search in sources :

Example 26 with CommandSource

use of org.spongepowered.api.command.CommandSource 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 27 with CommandSource

use of org.spongepowered.api.command.CommandSource 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)

Example 28 with CommandSource

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

the class AvailableBaseCommand method executeCommand.

@Override
protected CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    MessageProvider mp = Nucleus.getNucleus().getMessageProvider();
    List<Text> types = Sponge.getRegistry().getAllOf(this.catalogType).stream().map(x -> mp.getTextMessageWithFormat("command.world.presets.item", x.getId(), x.getName())).collect(Collectors.toList());
    Util.getPaginationBuilder(src).title(mp.getTextMessageWithTextFormat(this.titleKey)).contents(types).sendTo(src);
    return CommandResult.success();
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) CommandSource(org.spongepowered.api.command.CommandSource) CatalogType(org.spongepowered.api.CatalogType) Sponge(org.spongepowered.api.Sponge) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) Collectors(java.util.stream.Collectors) 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) Util(io.github.nucleuspowered.nucleus.Util) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) Text(org.spongepowered.api.text.Text)

Example 29 with CommandSource

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

the class SetupPermissionsCommand method setupPerms.

private void setupPerms(CommandSource src, Subject group, SuggestedLevel level, boolean reset, boolean inherit) throws Exception {
    if (inherit && level.getLowerLevel() != null) {
        setupPerms(src, group, level.getLowerLevel(), reset, inherit);
    }
    Set<Context> globalContext = Sets.newHashSet();
    SubjectData data = group.getSubjectData();
    Set<String> definedPermissions = data.getPermissions(ImmutableSet.of()).keySet();
    Logger logger = Nucleus.getNucleus().getLogger();
    MessageProvider messageProvider = Nucleus.getNucleus().getMessageProvider();
    // Register all the permissions, but only those that have yet to be assigned.
    permissionRegistry.getPermissions().entrySet().stream().filter(x -> x.getValue().level == level).filter(x -> reset || !definedPermissions.contains(x.getKey())).forEach(x -> {
        logger.info(messageProvider.getMessageWithFormat("command.nucleus.permission.added", x.getKey(), group.getIdentifier()));
        data.setPermission(globalContext, x.getKey(), Tristate.TRUE);
    });
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.permission.complete", level.toString().toLowerCase(), group.getIdentifier()));
}
Also used : CommandContext(org.spongepowered.api.command.args.CommandContext) Context(org.spongepowered.api.service.context.Context) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) GenericArguments(org.spongepowered.api.command.args.GenericArguments) PermissionService(org.spongepowered.api.service.permission.PermissionService) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) PermissionRegistry(io.github.nucleuspowered.nucleus.internal.PermissionRegistry) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) ServiceChangeListener(io.github.nucleuspowered.nucleus.internal.permissions.ServiceChangeListener) Subject(org.spongepowered.api.service.permission.Subject) Nullable(javax.annotation.Nullable) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) SubjectData(org.spongepowered.api.service.permission.SubjectData) CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) CommandSource(org.spongepowered.api.command.CommandSource) Context(org.spongepowered.api.service.context.Context) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Tristate(org.spongepowered.api.util.Tristate) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) Optional(java.util.Optional) Collections(java.util.Collections) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) SubjectData(org.spongepowered.api.service.permission.SubjectData) Logger(org.slf4j.Logger)

Example 30 with CommandSource

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

the class AFKCommandInterceptor method onPostCommand.

@Override
public void onPostCommand(Class<? extends AbstractCommand<?>> commandClass, CommandSource source, CommandContext context, CommandResult result) {
    if (this.send && result.getSuccessCount().orElse(0) > 0 && commandClass.isAnnotationPresent(NotifyIfAFK.class)) {
        NotifyIfAFK annotation = commandClass.getAnnotation(NotifyIfAFK.class);
        Cause cause = CauseStackHelper.createCause(source);
        for (String key : annotation.value()) {
            context.getAll(key).stream().filter(x -> x instanceof User).map(x -> ((User) x).getPlayer().orElse(null)).filter(Objects::nonNull).filter(this.handler::isAFK).forEach(x -> {
                Text messageToSend = this.message == null ? null : message.getForCommandSource(x);
                AFKEvents.Notify event = new AFKEvents.Notify(x, messageToSend, cause);
                Sponge.getEventManager().post(event);
                event.getMessage().ifPresent(source::sendMessage);
            });
        }
    }
}
Also used : NotifyIfAFK(io.github.nucleuspowered.nucleus.internal.annotations.command.NotifyIfAFK) AFKConfig(io.github.nucleuspowered.nucleus.modules.afk.config.AFKConfig) CommandResult(org.spongepowered.api.command.CommandResult) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) User(org.spongepowered.api.entity.living.player.User) CommandSource(org.spongepowered.api.command.CommandSource) CauseStackHelper(io.github.nucleuspowered.nucleus.util.CauseStackHelper) Sponge(org.spongepowered.api.Sponge) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) Objects(java.util.Objects) Cause(org.spongepowered.api.event.cause.Cause) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) NucleusTextTemplate(io.github.nucleuspowered.nucleus.api.text.NucleusTextTemplate) ICommandInterceptor(io.github.nucleuspowered.nucleus.internal.command.ICommandInterceptor) AFKHandler(io.github.nucleuspowered.nucleus.modules.afk.handlers.AFKHandler) AFKEvents(io.github.nucleuspowered.nucleus.modules.afk.events.AFKEvents) AFKConfigAdapter(io.github.nucleuspowered.nucleus.modules.afk.config.AFKConfigAdapter) Nullable(javax.annotation.Nullable) User(org.spongepowered.api.entity.living.player.User) Cause(org.spongepowered.api.event.cause.Cause) Objects(java.util.Objects) Text(org.spongepowered.api.text.Text) AFKEvents(io.github.nucleuspowered.nucleus.modules.afk.events.AFKEvents) NotifyIfAFK(io.github.nucleuspowered.nucleus.internal.annotations.command.NotifyIfAFK)

Aggregations

CommandSource (org.spongepowered.api.command.CommandSource)93 Text (org.spongepowered.api.text.Text)61 CommandResult (org.spongepowered.api.command.CommandResult)49 List (java.util.List)47 CommandContext (org.spongepowered.api.command.args.CommandContext)45 Collectors (java.util.stream.Collectors)39 Optional (java.util.Optional)37 Sponge (org.spongepowered.api.Sponge)37 Player (org.spongepowered.api.entity.living.player.Player)36 TextColors (org.spongepowered.api.text.format.TextColors)30 CommandElement (org.spongepowered.api.command.args.CommandElement)27 TextActions (org.spongepowered.api.text.action.TextActions)27 GenericArguments (org.spongepowered.api.command.args.GenericArguments)25 NonnullByDefault (org.spongepowered.api.util.annotation.NonnullByDefault)25 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)24 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)23 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)23 Util (io.github.nucleuspowered.nucleus.Util)20 Nullable (javax.annotation.Nullable)20 World (org.spongepowered.api.world.World)20