Search in sources :

Example 6 with MessageProvider

use of io.github.nucleuspowered.nucleus.internal.messages.MessageProvider in project Nucleus by NucleusPowered.

the class CommandSpyCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    ModularUserService service = Nucleus.getNucleus().getUserDataManager().getUnchecked(src);
    CommandSpyUserDataModule c = service.get(CommandSpyUserDataModule.class);
    boolean to = args.<Boolean>getOne(truefalse).orElseGet(() -> !c.isCommandSpy());
    c.setCommandSpy(to);
    MessageProvider mp = plugin.getMessageProvider();
    src.sendMessage(mp.getTextMessageWithFormat("command.commandspy.success", mp.getMessageWithFormat(to ? "standard.enabled" : "standard.disabled")));
    return CommandResult.success();
}
Also used : MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) CommandSpyUserDataModule(io.github.nucleuspowered.nucleus.modules.commandspy.datamodules.CommandSpyUserDataModule) ModularUserService(io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService)

Example 7 with MessageProvider

use of io.github.nucleuspowered.nucleus.internal.messages.MessageProvider in project Nucleus by NucleusPowered.

the class WorldCorrector method worldCheck.

public static void worldCheck() throws IOException, ObjectMappingException {
    final Map<String, UUID> m = get();
    // Now get the file out.
    Path path = Nucleus.getNucleus().getDataPath().resolve("worlduuids.json");
    ConfigurationLoader<ConfigurationNode> cl = GsonConfigurationLoader.builder().setPath(path).build();
    Map<String, UUID> ms = cl.load().getValue(new TypeToken<Map<String, UUID>>() {
    }, Maps.newHashMap());
    final Map<UUID, UUID> fromToConverter = Maps.newHashMap();
    for (String r : ms.keySet()) {
        UUID oldUuid = ms.get(r);
        @Nullable UUID newUuid = m.get(r);
        if (newUuid != null && !oldUuid.equals(newUuid)) {
            fromToConverter.put(newUuid, oldUuid);
        }
    }
    cl.save(cl.createEmptyNode().setValue(new TypeToken<Map<String, UUID>>() {
    }, m));
    if (fromToConverter.isEmpty()) {
        return;
    }
    MessageProvider mp = Nucleus.getNucleus().getMessageProvider();
    ConsoleSource cs = Sponge.getServer().getConsole();
    List<Text> msg = Lists.newArrayList();
    Nucleus.getNucleus().addX(msg, 0);
    msg.forEach(cs::sendMessage);
    cs.sendMessage(Text.of(TextColors.RED, "--------------------"));
    cs.sendMessage(mp.getTextMessageWithFormat("worldrepair.detected"));
    for (Map.Entry<UUID, UUID> u : fromToConverter.entrySet()) {
        cs.sendMessage(mp.getTextMessageWithFormat("worldrepair.worldlist", Sponge.getServer().getWorldProperties(u.getKey()).get().getWorldName(), u.getValue().toString(), u.getKey().toString()));
    }
    Method method;
    try {
        method = Sponge.getServer().getDefaultWorld().get().getClass().getDeclaredMethod("setUniqueId", UUID.class);
        method.setAccessible(true);
    } catch (NoSuchMethodException e) {
        cs.sendMessage(mp.getTextMessageWithFormat("worldrepair.whitelist.nocmd"));
        return;
    }
    cs.sendMessage(mp.getTextMessageWithFormat("worldrepair.whitelist.cmd"));
    cs.sendMessage(Text.of(TextColors.RED, "--------------------"));
    cs.sendMessage(mp.getTextMessageWithFormat("worldrepair.whitelist.cmd2"));
    Sponge.getServer().setHasWhitelist(true);
    Sponge.getCommandManager().register(Nucleus.getNucleus(), CommandSpec.builder().executor((s, a) -> {
        MessageProvider mpr = Nucleus.getNucleus().getMessageProvider();
        if (s instanceof ConsoleSource) {
            cs.sendMessage(mpr.getTextMessageWithFormat("worldrepair.repair.start"));
            Sponge.getEventManager().registerListener(Nucleus.getNucleus(), GameStoppedServerEvent.class, event -> {
                for (Map.Entry<UUID, UUID> meuu : fromToConverter.entrySet()) {
                    // Magic!
                    WorldProperties wp = Sponge.getServer().getWorldProperties(meuu.getKey()).orElseThrow(() -> new NoSuchElementException(mpr.getMessageWithFormat("worldrepair.repair.nouuid", meuu.getKey().toString())));
                    final String name = wp.getWorldName();
                    try {
                        cs.sendMessage(mpr.getTextMessageWithFormat("worldrepair.repair.try", name));
                        method.invoke(wp, meuu.getValue());
                        Sponge.getServer().saveWorldProperties(wp);
                        cs.sendMessage(mpr.getTextMessageWithFormat("worldrepair.repair.success", name));
                    } catch (Exception e) {
                        cs.sendMessage(mpr.getTextMessageWithFormat("worldrepair.repair.fail", name));
                        e.printStackTrace();
                    }
                }
                try {
                    cl.save(cl.createEmptyNode().setValue(new TypeToken<Map<String, UUID>>() {
                    }, get()));
                } catch (IOException | ObjectMappingException e) {
                    e.printStackTrace();
                }
            });
            Sponge.getServer().shutdown();
            return CommandResult.success();
        } else {
            s.sendMessage(mpr.getTextMessageWithFormat("command.consoleonly"));
            return CommandResult.empty();
        }
    }).build(), "repairuuids");
}
Also used : UUID(java.util.UUID) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) Path(java.nio.file.Path) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) Text(org.spongepowered.api.text.Text) Method(java.lang.reflect.Method) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) ObjectMappingException(ninja.leaping.configurate.objectmapping.ObjectMappingException) ConfigurationNode(ninja.leaping.configurate.ConfigurationNode) TypeToken(com.google.common.reflect.TypeToken) ConsoleSource(org.spongepowered.api.command.source.ConsoleSource) Map(java.util.Map) Nullable(javax.annotation.Nullable) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) NoSuchElementException(java.util.NoSuchElementException)

Example 8 with MessageProvider

use of io.github.nucleuspowered.nucleus.internal.messages.MessageProvider in project Nucleus by NucleusPowered.

the class NucleusCommandException method getText.

@Nullable
@Override
public Text getText() {
    if (exceptions.isEmpty()) {
        // Unable to get the error.
        return Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.exception.nomoreinfo");
    }
    MessageProvider mp = Nucleus.getNucleus().getMessageProvider();
    // Is it only command permission exceptions?
    if (exceptions.stream().allMatch(x -> CommandPermissionException.class.isInstance(x.getSecond()))) {
        return exceptions.get(0).getSecond().getText();
    }
    if (exceptions.stream().allMatch(x -> {
        CommandException e = x.getSecond();
        return e instanceof NucleusArgumentParseException && ((NucleusArgumentParseException) e).isEnd();
    })) {
        if (exceptions.size() == 1) {
            Tuple<String, CommandException> exceptionTuple = exceptions.get(0);
            return Text.of(mp.getTextMessageWithFormat("command.exception.fromcommand", exceptionTuple.getFirst()), Text.NEW_LINE, TextColors.RED, exceptionTuple.getSecond().getText());
        } else {
            return print(exceptions);
        }
    }
    List<Tuple<String, CommandException>> lce = exceptions.stream().filter(x -> {
        CommandException e = x.getSecond();
        return !(e instanceof NucleusArgumentParseException) || !((NucleusArgumentParseException) e).isEnd();
    }).filter(x -> !CommandPermissionException.class.isInstance(x)).collect(Collectors.toList());
    if (lce.size() == 1) {
        Tuple<String, CommandException> exceptionTuple = exceptions.get(0);
        return Text.of(mp.getTextMessageWithFormat("command.exception.fromcommand", exceptionTuple.getFirst()), Text.NEW_LINE, TextColors.RED, exceptionTuple.getSecond().getText());
    }
    return print(lce);
}
Also used : CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) CommandPermissionException(org.spongepowered.api.command.CommandPermissionException) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) Text(org.spongepowered.api.text.Text) Tuple(org.spongepowered.api.util.Tuple) TextColors(org.spongepowered.api.text.format.TextColors) Collectors(java.util.stream.Collectors) Nullable(javax.annotation.Nullable) CommandPermissionException(org.spongepowered.api.command.CommandPermissionException) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) CommandException(org.spongepowered.api.command.CommandException) Tuple(org.spongepowered.api.util.Tuple) Nullable(javax.annotation.Nullable)

Example 9 with MessageProvider

use of io.github.nucleuspowered.nucleus.internal.messages.MessageProvider in project Nucleus by NucleusPowered.

the class ShowAttributesCommand method executeCommand.

@Override
protected CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    ItemStack itemStack = src.getItemInHand(HandTypes.MAIN_HAND).orElseThrow(() -> ReturnMessageException.fromKey("command.generalerror.handempty"));
    boolean b = args.<Boolean>getOne(flag).orElseGet(() -> itemStack.get(Keys.HIDE_ATTRIBUTES).orElse(false));
    // Command is show, key is hide. We invert.
    itemStack.offer(Keys.HIDE_ATTRIBUTES, !b);
    src.setItemInHand(HandTypes.MAIN_HAND, itemStack);
    MessageProvider mp = plugin.getMessageProvider();
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.showitemattributes.success." + String.valueOf(b), Text.of(itemStack)));
    return CommandResult.success();
}
Also used : MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Example 10 with MessageProvider

use of io.github.nucleuspowered.nucleus.internal.messages.MessageProvider in project Nucleus by NucleusPowered.

the class ItemNameClearCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    MessageProvider provider = Nucleus.getNucleus().getMessageProvider();
    if (!src.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
        throw ReturnMessageException.fromKey("command.itemname.clear.noitem");
    }
    ItemStack stack = src.getItemInHand(HandTypes.MAIN_HAND).get();
    Optional<Text> data = stack.get(Keys.DISPLAY_NAME);
    if (!data.isPresent()) {
        // No display name.
        throw ReturnMessageException.fromKey("command.lore.clear.none");
    }
    if (stack.remove(Keys.DISPLAY_NAME).isSuccessful()) {
        src.setItemInHand(HandTypes.MAIN_HAND, stack);
        src.sendMessage(provider.getTextMessageWithFormat("command.itemname.clear.success"));
        return CommandResult.success();
    }
    throw ReturnMessageException.fromKey("command.itemname.clear.fail");
}
Also used : MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) Text(org.spongepowered.api.text.Text) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Aggregations

MessageProvider (io.github.nucleuspowered.nucleus.internal.messages.MessageProvider)33 Text (org.spongepowered.api.text.Text)18 Nucleus (io.github.nucleuspowered.nucleus.Nucleus)10 List (java.util.List)10 Util (io.github.nucleuspowered.nucleus.Util)8 Collectors (java.util.stream.Collectors)8 CommandSource (org.spongepowered.api.command.CommandSource)8 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)7 Nullable (javax.annotation.Nullable)7 CommandResult (org.spongepowered.api.command.CommandResult)7 CommandContext (org.spongepowered.api.command.args.CommandContext)7 NonnullByDefault (org.spongepowered.api.util.annotation.NonnullByDefault)7 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)6 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)6 Optional (java.util.Optional)6 Sponge (org.spongepowered.api.Sponge)6 User (org.spongepowered.api.entity.living.player.User)6 TextActions (org.spongepowered.api.text.action.TextActions)6 RunAsync (io.github.nucleuspowered.nucleus.internal.annotations.RunAsync)5 ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)5