Search in sources :

Example 1 with MutableText

use of net.minecraft.text.MutableText in project UniversalGraves by Patbox.

the class GraveListGui method updateIcons.

private void updateIcons() {
    var config = ConfigManager.getConfig();
    for (int x = 0; x < this.size; x++) {
        this.clearSlot(x);
    }
    for (GraveInfo graveInfo : GraveManager.INSTANCE.getByUuid(this.targetUUID)) {
        if (this.getFirstEmptySlot() == -1) {
            return;
        }
        Map<String, Text> placeholders = graveInfo.getPlaceholders();
        List<Text> parsed = new ArrayList<>();
        for (Text text : graveInfo.isProtected() ? ConfigManager.getConfig().guiProtectedText : ConfigManager.getConfig().guiText) {
            MutableText out = (MutableText) PlaceholderAPI.parsePredefinedText(text, PlaceholderAPI.PREDEFINED_PLACEHOLDER_PATTERN, placeholders);
            if (out.getStyle().getColor() == null) {
                out.setStyle(out.getStyle().withColor(Formatting.WHITE));
            }
            parsed.add(out);
        }
        var list = graveInfo.isProtected() ? config.guiProtectedItem : config.guiItem;
        this.addSlot(GuiElementBuilder.from(list[Math.abs(graveInfo.hashCode() % list.length)]).setName((MutableText) parsed.remove(0)).setLore(parsed).setCallback((index, type, action) -> {
            if (Permissions.check(this.player, "universal_graves.teleport", 3)) {
                this.close();
                ServerWorld world = this.player.getServer().getWorld(RegistryKey.of(Registry.WORLD_KEY, graveInfo.getWorld()));
                if (world != null) {
                    var pos = graveInfo.getPosition();
                    this.player.teleport(world, pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 0.5, this.player.getYaw(), this.player.getPitch());
                }
            }
        }));
    }
}
Also used : MutableText(net.minecraft.text.MutableText) LiteralText(net.minecraft.text.LiteralText) GameProfile(com.mojang.authlib.GameProfile) ServerWorld(net.minecraft.server.world.ServerWorld) Permissions(me.lucko.fabric.api.permissions.v0.Permissions) UUID(java.util.UUID) Registry(net.minecraft.util.registry.Registry) GraveManager(eu.pb4.graves.grave.GraveManager) ArrayList(java.util.ArrayList) Formatting(net.minecraft.util.Formatting) List(java.util.List) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) ConfigManager(eu.pb4.graves.config.ConfigManager) Map(java.util.Map) MutableText(net.minecraft.text.MutableText) RegistryKey(net.minecraft.util.registry.RegistryKey) PlaceholderAPI(eu.pb4.placeholders.PlaceholderAPI) GuiElementBuilder(eu.pb4.sgui.api.elements.GuiElementBuilder) Text(net.minecraft.text.Text) GraveInfo(eu.pb4.graves.grave.GraveInfo) SimpleGui(eu.pb4.sgui.api.gui.SimpleGui) ScreenHandlerType(net.minecraft.screen.ScreenHandlerType) ServerWorld(net.minecraft.server.world.ServerWorld) GraveInfo(eu.pb4.graves.grave.GraveInfo) ArrayList(java.util.ArrayList) LiteralText(net.minecraft.text.LiteralText) MutableText(net.minecraft.text.MutableText) Text(net.minecraft.text.Text)

Example 2 with MutableText

use of net.minecraft.text.MutableText in project BleachHack by BleachDrinker420.

the class Texts method forEachWord.

/**
 * Does an operation to each word in a text. *
 */
public static Text forEachWord(Text text, BiFunction<String, Style, Text> operator) {
    MutableText newFullText = new LiteralText("");
    for (Text t : unpack(text)) {
        MutableText newText = new LiteralText("");
        String curString = "";
        String[] split = t.getString().split(" ", -1);
        for (int i = 0; i < split.length; i++) {
            Text word = operator.apply(split[i], t.getStyle());
            if (word != null) {
                if (!curString.isEmpty()) {
                    newText.append(new LiteralText(curString).setStyle(t.getStyle()));
                    curString = "";
                }
                newText.append(word);
            } else {
                curString += split[i];
            }
            if (i != split.length - 1)
                curString += " ";
        }
        if (!curString.isEmpty())
            newText.append(new LiteralText(curString).setStyle(t.getStyle()));
        newFullText.append(newText);
    }
    return newFullText;
}
Also used : MutableText(net.minecraft.text.MutableText) LiteralText(net.minecraft.text.LiteralText) BaseText(net.minecraft.text.BaseText) MutableText(net.minecraft.text.MutableText) Text(net.minecraft.text.Text) LiteralText(net.minecraft.text.LiteralText)

Example 3 with MutableText

use of net.minecraft.text.MutableText in project BleachHack by BleachDrinker420.

the class Texts method replaceAll.

/**
 * Replaces every string in this text that matches the pattern with a text from the provider. *
 */
public static Text replaceAll(Text text, Pattern pattern, BiFunction<String, Style, Text> replacement) {
    if (pattern.pattern().isEmpty())
        return text;
    MutableText newText = new LiteralText("");
    for (Text t : unpack(text)) {
        String string = t.getString();
        Map<Integer, Integer> positions = new HashMap<>();
        Matcher mat = pattern.matcher(string);
        while (mat.find()) {
            positions.put(mat.start(), mat.end());
        }
        String curString = "";
        for (int i = 0; i < string.length(); i++) {
            if (positions.containsKey(i)) {
                if (!curString.isEmpty()) {
                    newText = newText.append(new LiteralText(curString).setStyle(t.getStyle()));
                    curString = "";
                }
                newText.append(replacement.apply(string.substring(i, positions.get(i)), t.getStyle()));
                i = positions.get(i) - 1;
            } else {
                curString += string.charAt(i);
            }
        }
        if (!curString.isEmpty())
            newText.append(new LiteralText(curString).setStyle(t.getStyle()));
    }
    return newText;
}
Also used : MutableText(net.minecraft.text.MutableText) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) LiteralText(net.minecraft.text.LiteralText) BaseText(net.minecraft.text.BaseText) MutableText(net.minecraft.text.MutableText) Text(net.minecraft.text.Text) LiteralText(net.minecraft.text.LiteralText)

Example 4 with MutableText

use of net.minecraft.text.MutableText in project BleachHack by BleachDrinker420.

the class CmdBetterChat method onCommand.

@Override
public void onCommand(String alias, String[] args) throws Exception {
    if (args.length < 2) {
        throw new CmdSyntaxException();
    }
    BetterChat chat = ModuleManager.getModule(BetterChat.class);
    if (args[0].equalsIgnoreCase("filter")) {
        if (args[1].equalsIgnoreCase("list")) {
            MutableText text = new LiteralText("Filter Entries:");
            int i = 1;
            for (Pattern pat : chat.filterPatterns) {
                text = text.append(new LiteralText("\n\u00a76" + i + " > \u00a7f" + pat.pattern()).styled(style -> style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new LiteralText("Click to remove this filter."))).withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, getPrefix() + "betterchat filter remove " + pat.pattern()))));
                i++;
            }
            BleachLogger.info(text);
        } else if (args[1].equalsIgnoreCase("add")) {
            String arg = String.join(" ", ArrayUtils.subarray(args, 2, args.length)).trim();
            chat.filterPatterns.add(Pattern.compile(arg));
            JsonArray jsonFilter = new JsonArray();
            chat.filterPatterns.forEach(p -> jsonFilter.add(p.toString()));
            BleachFileHelper.saveMiscSetting("betterChatFilter", jsonFilter);
            BleachLogger.info("Added \"" + arg + "\" to the filter patterns.");
        } else if (args[1].equalsIgnoreCase("remove")) {
            String arg = String.join(" ", ArrayUtils.subarray(args, 2, args.length)).trim();
            if (chat.filterPatterns.removeIf(p -> p.toString().equals(arg))) {
                JsonArray jsonFilter = new JsonArray();
                chat.filterPatterns.forEach(p -> jsonFilter.add(p.toString()));
                BleachFileHelper.saveMiscSetting("betterChatFilter", jsonFilter);
                BleachLogger.info("Removed \"" + arg + "\" from the filter patterns.");
            } else {
                BleachLogger.info("Could not find \"" + arg + "\" in the pattern list.");
            }
        } else {
            throw new CmdSyntaxException();
        }
    } else if (args[0].equalsIgnoreCase("prefix")) {
        if (args[1].equalsIgnoreCase("current")) {
            BleachLogger.info("Current prefix: \"" + chat.prefix + "\"");
        } else if (args[1].equalsIgnoreCase("reset")) {
            chat.prefix = "";
            BleachFileHelper.saveMiscSetting("betterChatPrefix", new JsonPrimitive(chat.prefix));
            BleachLogger.info("Reset the customchat prefix!");
        } else if (args[1].equalsIgnoreCase("set") && args.length >= 3) {
            chat.prefix = String.join(" ", ArrayUtils.subarray(args, 2, args.length)).trim() + " ";
            BleachFileHelper.saveMiscSetting("betterChatPrefix", new JsonPrimitive(chat.prefix));
            BleachLogger.info("Set prefix to: \"" + chat.prefix + "\"");
        } else {
            throw new CmdSyntaxException();
        }
    } else if (args[0].equalsIgnoreCase("suffix")) {
        if (args[1].equalsIgnoreCase("current")) {
            BleachLogger.info("Current suffix: \"" + chat.suffix + "\"");
        } else if (args[1].equalsIgnoreCase("reset")) {
            chat.suffix = " \u25ba \u0432\u029f\u0454\u03b1c\u043d\u043d\u03b1c\u043a";
            BleachFileHelper.saveMiscSetting("betterChatSuffix", new JsonPrimitive(chat.suffix));
            BleachLogger.info("Reset the customchat suffix!");
        } else if (args[1].equalsIgnoreCase("set") && args.length >= 3) {
            chat.suffix = String.join(" ", ArrayUtils.subarray(args, 2, args.length)).trim() + " ";
            BleachFileHelper.saveMiscSetting("betterChatSuffix", new JsonPrimitive(chat.suffix));
            BleachLogger.info("Set suffix to: \"" + chat.suffix + "\"");
        } else {
            throw new CmdSyntaxException();
        }
    } else {
        throw new CmdSyntaxException();
    }
}
Also used : MutableText(net.minecraft.text.MutableText) JsonArray(com.google.gson.JsonArray) CommandCategory(org.bleachhack.command.CommandCategory) LiteralText(net.minecraft.text.LiteralText) CmdSyntaxException(org.bleachhack.command.exception.CmdSyntaxException) Command(org.bleachhack.command.Command) ArrayUtils(org.apache.commons.lang3.ArrayUtils) BleachFileHelper(org.bleachhack.util.io.BleachFileHelper) HoverEvent(net.minecraft.text.HoverEvent) ModuleManager(org.bleachhack.module.ModuleManager) BetterChat(org.bleachhack.module.mods.BetterChat) JsonArray(com.google.gson.JsonArray) ClickEvent(net.minecraft.text.ClickEvent) BleachLogger(org.bleachhack.util.BleachLogger) MutableText(net.minecraft.text.MutableText) Pattern(java.util.regex.Pattern) JsonPrimitive(com.google.gson.JsonPrimitive) Pattern(java.util.regex.Pattern) HoverEvent(net.minecraft.text.HoverEvent) BetterChat(org.bleachhack.module.mods.BetterChat) CmdSyntaxException(org.bleachhack.command.exception.CmdSyntaxException) JsonPrimitive(com.google.gson.JsonPrimitive) ClickEvent(net.minecraft.text.ClickEvent) LiteralText(net.minecraft.text.LiteralText)

Example 5 with MutableText

use of net.minecraft.text.MutableText in project BleachHack by BleachDrinker420.

the class CmdHelp method onCommand.

@Override
public void onCommand(String alias, String[] args) throws Exception {
    String cmd = args.length == 0 ? "" : args[0];
    if (cmd.isEmpty()) {
        BleachLogger.info("Commands:");
    } else {
        BleachLogger.info("Syntax for " + getPrefix() + cmd.toLowerCase(Locale.ENGLISH) + ":");
    }
    for (Command c : CommandManager.getCommands()) {
        if (!cmd.isEmpty() && Stream.of(c.getAliases()).noneMatch(cmd::equalsIgnoreCase))
            continue;
        MutableText text = new LiteralText(getPrefix() + c.getAliases()[0] + ": \u00a7f" + c.getDescription()).styled(s -> s.withColor(BleachLogger.INFO_COLOR));
        BleachLogger.noPrefix(text.styled(style -> style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, c.getHelpTooltip()))));
    }
}
Also used : MutableText(net.minecraft.text.MutableText) CommandCategory(org.bleachhack.command.CommandCategory) CommandManager(org.bleachhack.command.CommandManager) LiteralText(net.minecraft.text.LiteralText) Stream(java.util.stream.Stream) Locale(java.util.Locale) Command(org.bleachhack.command.Command) BleachLogger(org.bleachhack.util.BleachLogger) MutableText(net.minecraft.text.MutableText) HoverEvent(net.minecraft.text.HoverEvent) HoverEvent(net.minecraft.text.HoverEvent) Command(org.bleachhack.command.Command) LiteralText(net.minecraft.text.LiteralText)

Aggregations

MutableText (net.minecraft.text.MutableText)31 LiteralText (net.minecraft.text.LiteralText)26 Text (net.minecraft.text.Text)7 HoverEvent (net.minecraft.text.HoverEvent)6 List (java.util.List)5 ClickEvent (net.minecraft.text.ClickEvent)5 TranslatableText (net.minecraft.text.TranslatableText)5 Formatting (net.minecraft.util.Formatting)5 CommandSource (net.minecraft.command.CommandSource)3 JsonObject (com.google.gson.JsonObject)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 SINGLE_SUCCESS (com.mojang.brigadier.Command.SINGLE_SUCCESS)2 LiteralArgumentBuilder (com.mojang.brigadier.builder.LiteralArgumentBuilder)2 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Locale (java.util.Locale)2 Collectors (java.util.stream.Collectors)2