Search in sources :

Example 6 with ConsoleSource

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

the class SingleKit method redeemKitCommands.

@Override
public void redeemKitCommands(Player player) {
    ConsoleSource source = Sponge.getServer().getConsole();
    String playerName = player.getName();
    getCommands().forEach(x -> Sponge.getCommandManager().process(source, x.replace("{{player}}", playerName)));
}
Also used : ConsoleSource(org.spongepowered.api.command.source.ConsoleSource)

Example 7 with ConsoleSource

use of org.spongepowered.api.command.source.ConsoleSource in project SpongeCommon by SpongePowered.

the class TimingsExport method run.

@Override
public void run() {
    this.sender.sendMessage(Text.of(TextColors.GREEN, "Preparing Timings Report..."));
    this.out.add("data", JSONUtil.mapArray(this.history, TimingHistory::export));
    String response = null;
    try {
        String hostname = "localhost";
        try {
            hostname = InetAddress.getLocalHost().getHostName();
        } catch (IOException e) {
            SpongeImpl.getLogger().warn("Could not get own server hostname when uploading timings - falling back to 'localhost'", e);
        }
        HttpURLConnection con = (HttpURLConnection) new URL("http://timings.aikar.co/post").openConnection();
        con.setDoOutput(true);
        con.setRequestProperty("User-Agent", "Sponge/" + getServerName() + "/" + hostname);
        con.setRequestMethod("POST");
        con.setInstanceFollowRedirects(false);
        OutputStream request = new GZIPOutputStream(con.getOutputStream()) {

            {
                this.def.setLevel(7);
            }
        };
        request.write(JSONUtil.toString(this.out).getBytes("UTF-8"));
        request.close();
        response = getResponse(con);
        if (con.getResponseCode() != 302) {
            this.sender.sendMessage(Text.of(TextColors.RED, "Upload Error: " + con.getResponseCode() + ": " + con.getResponseMessage()));
            this.sender.sendMessage(Text.of(TextColors.RED, "Check your logs for more information"));
            if (response != null) {
                SpongeImpl.getLogger().fatal(response);
            }
            return;
        }
        String location = con.getHeaderField("Location");
        this.sender.sendMessage(Text.of(TextColors.GREEN, "View Timings Report: ", TextActions.openUrl(new URL(location)), location));
        if (!(this.sender instanceof ConsoleSource)) {
            SpongeImpl.getLogger().info("View Timings Report: " + location);
        }
        if (response != null && !response.isEmpty()) {
            SpongeImpl.getLogger().info("Timing Response: " + response);
        }
    } catch (IOException ex) {
        this.sender.sendMessage(Text.of(TextColors.RED, "Error uploading timings, check your logs for more information"));
        if (response != null) {
            SpongeImpl.getLogger().fatal(response);
        }
        SpongeImpl.getLogger().fatal("Could not paste timings", ex);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) IOException(java.io.IOException) ConsoleSource(org.spongepowered.api.command.source.ConsoleSource) URL(java.net.URL)

Example 8 with ConsoleSource

use of org.spongepowered.api.command.source.ConsoleSource in project LanternServer by LanternPowered.

the class CommandOp method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    specBuilder.arguments(new CommandElement(Text.of("player")) {

        @Nullable
        @Override
        protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
            return args.next();
        }

        @Override
        public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
            final String prefix = args.nextIfPresent().orElse("");
            final UserConfig<OpsEntry> config = Lantern.getGame().getOpsConfig();
            return Lantern.getGame().getGameProfileManager().getCache().getProfiles().stream().filter(p -> p.getName().isPresent() && !config.getEntryByUUID(p.getUniqueId()).isPresent()).map(p -> p.getName().get()).filter(new StartsWithPredicate(prefix)).collect(ImmutableList.toImmutableList());
        }
    }, GenericArguments.optional(GenericArguments.integer(Text.of("level")))).executor((src, args) -> {
        String playerName = args.<String>getOne("player").get();
        UserConfig<OpsEntry> config = Lantern.getGame().getOpsConfig();
        if (!(src instanceof ConsoleSource) && args.hasAny("level")) {
            throw new CommandException(Text.of("Only the console may specify the op level."));
        }
        int opLevel = args.<Integer>getOne("level").orElse(Lantern.getGame().getGlobalConfig().getDefaultOpPermissionLevel());
        Lantern.getGame().getGameProfileManager().get(playerName).whenComplete((profile, error) -> {
            if (error != null) {
                src.sendMessage(t("commands.op.failed", playerName));
            } else {
                src.sendMessage(t("commands.op.success", playerName));
                config.addEntry(new OpsEntry(((LanternGameProfile) profile).withoutProperties(), opLevel));
            }
        });
        return CommandResult.success();
    });
}
Also used : CommandArgs(org.spongepowered.api.command.args.CommandArgs) CommandResult(org.spongepowered.api.command.CommandResult) ConsoleSource(org.spongepowered.api.command.source.ConsoleSource) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) CommandSource(org.spongepowered.api.command.CommandSource) OpsEntry(org.lanternpowered.server.config.user.OpsEntry) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) CommandElement(org.spongepowered.api.command.args.CommandElement) GenericArguments(org.spongepowered.api.command.args.GenericArguments) LanternGameProfile(org.lanternpowered.server.profile.LanternGameProfile) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) UserConfig(org.lanternpowered.server.config.user.UserConfig) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Lantern(org.lanternpowered.server.game.Lantern) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Nullable(javax.annotation.Nullable) CommandContext(org.spongepowered.api.command.args.CommandContext) LanternGameProfile(org.lanternpowered.server.profile.LanternGameProfile) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) CommandException(org.spongepowered.api.command.CommandException) CommandSource(org.spongepowered.api.command.CommandSource) OpsEntry(org.lanternpowered.server.config.user.OpsEntry) CommandElement(org.spongepowered.api.command.args.CommandElement) ConsoleSource(org.spongepowered.api.command.source.ConsoleSource)

Example 9 with ConsoleSource

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

the class MessageHandler method sendMessage.

@Override
public boolean sendMessage(CommandSource sender, CommandSource receiver, String message) {
    // Message is about to be sent. Send the event out. If canceled, then that's that.
    boolean isBlocked = false;
    boolean isCancelled = Sponge.getEventManager().post(new InternalNucleusMessageEvent(sender, receiver, message));
    if (isCancelled) {
        sender.sendMessage(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("message.cancel"));
        // Only continue to show Social Spy messages if the subject is muted.
        if (!messageConfig.isShowMessagesInSocialSpyWhileMuted()) {
            return false;
        }
    }
    // What about msgtoggle?
    if (receiver instanceof Player && !sender.hasPermission(this.msgToggleBypass) && ucl.get((Player) receiver).map(x -> !x.get(MessageUserDataModule.class).isMsgToggle()).orElse(false)) {
        isCancelled = true;
        isBlocked = true;
        sender.sendMessage(Nucleus.getNucleus().getMessageProvider().getTextMessageWithTextFormat("message.blocked", Nucleus.getNucleus().getNameUtil().getName((Player) receiver)));
        if (!messageConfig.isShowMessagesInSocialSpyWhileMuted()) {
            return false;
        }
    }
    // Social Spies.
    final UUID uuidSender = getUUID(sender);
    final UUID uuidReceiver = getUUID(receiver);
    final Map<String, Object> variables = Maps.newHashMap();
    variables.put("from", sender);
    variables.put("to", receiver);
    // Create the tokens.
    Map<String, Function<CommandSource, Optional<Text>>> tokens = Maps.newHashMap();
    tokens.put("from", cs -> getNameFromCommandSource(sender, textParsingUtils::addCommandToName));
    tokens.put("to", cs -> getNameFromCommandSource(receiver, textParsingUtils::addCommandToName));
    tokens.put("fromdisplay", cs -> getNameFromCommandSource(sender, textParsingUtils::addCommandToDisplayName));
    tokens.put("todisplay", cs -> getNameFromCommandSource(receiver, textParsingUtils::addCommandToDisplayName));
    Text tm = useMessage(sender, message);
    if (!isCancelled) {
        sender.sendMessage(constructMessage(sender, tm, messageConfig.getMessageSenderPrefix(), tokens, variables));
        receiver.sendMessage(constructMessage(sender, tm, messageConfig.getMessageReceiverPrefix(), tokens, variables));
    }
    NucleusTextTemplateImpl prefix = messageConfig.getMessageSocialSpyPrefix();
    if (isBlocked) {
        prefix = NucleusTextTemplateFactory.createFromAmpersandString(messageConfig.getBlockedTag() + prefix.getRepresentation());
    }
    if (isCancelled) {
        prefix = NucleusTextTemplateFactory.createFromAmpersandString(messageConfig.getMutedTag() + prefix.getRepresentation());
    }
    MessageConfig.Targets targets = messageConfig.spyOn();
    if (sender instanceof Player && targets.isPlayer() || sender instanceof ConsoleSource && targets.isCustom() || targets.isCustom()) {
        Set<CommandSource> lm = onlinePlayersCanSpyOn(!uuidSender.equals(Util.consoleFakeUUID) && !uuidReceiver.equals(Util.consoleFakeUUID), sender, receiver);
        MessageChannel mc = MessageChannel.fixed(lm);
        if (!mc.getMembers().isEmpty()) {
            mc.send(constructMessage(sender, tm, prefix, tokens, variables));
        }
    }
    // Add the UUIDs to the reply list - the receiver will now reply to the sender.
    if (!isCancelled) {
        messagesReceived.put(uuidReceiver, uuidSender);
    }
    return !isCancelled;
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) NucleusTextTemplateImpl(io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateImpl) Text(org.spongepowered.api.text.Text) CommandSource(org.spongepowered.api.command.CommandSource) Function(java.util.function.Function) MessageConfig(io.github.nucleuspowered.nucleus.modules.message.config.MessageConfig) MessageChannel(org.spongepowered.api.text.channel.MessageChannel) MessageUserDataModule(io.github.nucleuspowered.nucleus.modules.message.datamodules.MessageUserDataModule) ConsoleSource(org.spongepowered.api.command.source.ConsoleSource) UUID(java.util.UUID) InternalNucleusMessageEvent(io.github.nucleuspowered.nucleus.modules.message.events.InternalNucleusMessageEvent)

Example 10 with ConsoleSource

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

the class BorderCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    WorldProperties wp = getWorldFromUserOrArgs(src, worldKey, args);
    List<Text> worldBorderInfo = Lists.newArrayList();
    Vector3d centre = wp.getWorldBorderCenter();
    int currentDiameter = (int) wp.getWorldBorderDiameter();
    int targetDiameter = (int) wp.getWorldBorderTargetDiameter();
    // Border centre
    worldBorderInfo.add(plugin.getMessageProvider().getTextMessageWithFormat("command.world.border.centre", String.valueOf(centre.getFloorX()), String.valueOf(centre.getFloorZ())));
    worldBorderInfo.add(plugin.getMessageProvider().getTextMessageWithFormat("command.world.border.currentdiameter", String.valueOf(wp.getWorldBorderDiameter())));
    if (currentDiameter != targetDiameter) {
        worldBorderInfo.add(plugin.getMessageProvider().getTextMessageWithFormat("command.world.border.targetdiameter", String.valueOf(targetDiameter), String.valueOf(wp.getWorldBorderTimeRemaining() / 1000)));
    }
    PaginationList.Builder pb = Sponge.getServiceManager().provideUnchecked(PaginationService.class).builder().contents(worldBorderInfo).title(plugin.getMessageProvider().getTextMessageWithFormat("command.world.border.title", wp.getWorldName())).padding(Text.of(TextColors.GREEN, "="));
    if (src instanceof ConsoleSource) {
        pb.linesPerPage(-1);
    }
    pb.sendTo(src);
    return CommandResult.success();
}
Also used : PaginationList(org.spongepowered.api.service.pagination.PaginationList) Vector3d(com.flowpowered.math.vector.Vector3d) Text(org.spongepowered.api.text.Text) ConsoleSource(org.spongepowered.api.command.source.ConsoleSource) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Aggregations

ConsoleSource (org.spongepowered.api.command.source.ConsoleSource)10 Text (org.spongepowered.api.text.Text)8 UUID (java.util.UUID)3 Nullable (javax.annotation.Nullable)3 CommandSource (org.spongepowered.api.command.CommandSource)3 Player (org.spongepowered.api.entity.living.player.Player)3 WorldProperties (org.spongepowered.api.world.storage.WorldProperties)3 IOException (java.io.IOException)2 List (java.util.List)2 Lantern (org.lanternpowered.server.game.Lantern)2 TranslationHelper.t (org.lanternpowered.server.text.translation.TranslationHelper.t)2 CommandException (org.spongepowered.api.command.CommandException)2 CommandResult (org.spongepowered.api.command.CommandResult)2 ArgumentParseException (org.spongepowered.api.command.args.ArgumentParseException)2 CommandArgs (org.spongepowered.api.command.args.CommandArgs)2 CommandContext (org.spongepowered.api.command.args.CommandContext)2 CommandElement (org.spongepowered.api.command.args.CommandElement)2 GenericArguments (org.spongepowered.api.command.args.GenericArguments)2 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)2 Vector3d (com.flowpowered.math.vector.Vector3d)1