Search in sources :

Example 1 with StaffChatMessageChannel

use of io.github.nucleuspowered.nucleus.modules.staffchat.StaffChatMessageChannel in project Nucleus by NucleusPowered.

the class StaffChatCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    Optional<String> toSend = args.getOne(message);
    if (toSend.isPresent()) {
        try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            frame.addContext(EventContexts.SHOULD_FORMAT_CHANNEL, StaffChatMessageChannel.getInstance().formatMessages());
            if (src instanceof Player) {
                Player pl = (Player) src;
                frame.pushCause(pl);
                frame.addContext(EventContextKeys.PLAYER_SIMULATED, pl.getProfile());
                MessageChannel mc = pl.getMessageChannel();
                pl.setMessageChannel(StaffChatMessageChannel.getInstance());
                pl.simulateChat(TextParsingUtils.addUrls(toSend.get()), Sponge.getCauseStackManager().getCurrentCause());
                pl.setMessageChannel(mc);
            } else {
                StaffChatMessageChannel.getInstance().send(src, TextParsingUtils.addUrls(toSend.get()), ChatTypes.CHAT);
            }
            return CommandResult.success();
        }
    }
    if (!(src instanceof Player)) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.staffchat.consoletoggle"));
    }
    Player player = (Player) src;
    StaffChatTransientModule s = plugin.getUserDataManager().get(player).map(y -> y.getTransient(StaffChatTransientModule.class)).orElseGet(StaffChatTransientModule::new);
    boolean result = !(src.getMessageChannel() instanceof StaffChatMessageChannel);
    if (result) {
        s.setPreviousMessageChannel(player.getMessageChannel());
        src.setMessageChannel(StaffChatMessageChannel.getInstance());
    } else {
        src.setMessageChannel(s.getPreviousMessageChannel().orElse(MessageChannel.TO_ALL));
    }
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.staffchat." + (result ? "on" : "off")));
    return CommandResult.success();
}
Also used : ChatTypes(org.spongepowered.api.text.chat.ChatTypes) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) RemainingStringsArgument(io.github.nucleuspowered.nucleus.argumentparsers.RemainingStringsArgument) TextParsingUtils(io.github.nucleuspowered.nucleus.internal.text.TextParsingUtils) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) EventContexts(io.github.nucleuspowered.nucleus.api.EventContexts) CommandSource(org.spongepowered.api.command.CommandSource) StaffChatMessageChannel(io.github.nucleuspowered.nucleus.modules.staffchat.StaffChatMessageChannel) Sponge(org.spongepowered.api.Sponge) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CommandElement(org.spongepowered.api.command.args.CommandElement) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) StaffChatTransientModule(io.github.nucleuspowered.nucleus.modules.staffchat.datamodules.StaffChatTransientModule) Player(org.spongepowered.api.entity.living.player.Player) StaffChatMessageChannel(io.github.nucleuspowered.nucleus.modules.staffchat.StaffChatMessageChannel) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) StaffChatTransientModule(io.github.nucleuspowered.nucleus.modules.staffchat.datamodules.StaffChatTransientModule) StaffChatMessageChannel(io.github.nucleuspowered.nucleus.modules.staffchat.StaffChatMessageChannel)

Aggregations

EventContexts (io.github.nucleuspowered.nucleus.api.EventContexts)1 RemainingStringsArgument (io.github.nucleuspowered.nucleus.argumentparsers.RemainingStringsArgument)1 NoModifiers (io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers)1 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)1 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)1 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)1 ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)1 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)1 TextParsingUtils (io.github.nucleuspowered.nucleus.internal.text.TextParsingUtils)1 StaffChatMessageChannel (io.github.nucleuspowered.nucleus.modules.staffchat.StaffChatMessageChannel)1 StaffChatTransientModule (io.github.nucleuspowered.nucleus.modules.staffchat.datamodules.StaffChatTransientModule)1 Optional (java.util.Optional)1 Sponge (org.spongepowered.api.Sponge)1 CommandResult (org.spongepowered.api.command.CommandResult)1 CommandSource (org.spongepowered.api.command.CommandSource)1 CommandContext (org.spongepowered.api.command.args.CommandContext)1 CommandElement (org.spongepowered.api.command.args.CommandElement)1 GenericArguments (org.spongepowered.api.command.args.GenericArguments)1 Player (org.spongepowered.api.entity.living.player.Player)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1