如果当前行包含太多文本,请在字符串列表中新建一行



所以我目前有一个字符串列表,用户可以在其中编辑放入的内容,只要它是一个字符串。因此,对于较短的文本,这很好,但当文本开始变得越来越长时,我使用字符串列表的工具提示会占用太多屏幕,让事情看起来很难看。所以我想为每一个特定数量的字符的字符串列表制作一个新行,这样就不会变得混乱。这是我目前一直在运行的测试:

'Salamander specializes in disguising in the green woods, mainly in oak forests salamanders will hide and lure to wait for your loot, they will then jump out and do a sneak attack. Be prepared!'

我通过以下方式获得该信息:

lore.add(kitConfig.getString("kits." + kitName + ".description"));

知识库ArrayList<String>中的每个String都显示在新行上。您可以编写一个方法,将String拆分为多个片段,并返回一个包含这些部分的ArrayList,然后您可以将这些部分设置为新的知识。下面是一个例子:

public static ArrayList<String> splitLore(String text, int characters) {
    ArrayList<String> lore = new ArrayList<String>(); // Create the ArrayList that will contain the lore lines
    if (text.length() <= characters) { // If the line of text is short enough (doesn't need to be split)...
        lore.add(text); // Add the entire line to the list and return it
    } else { // If the line is longer and needs to be split into at least two lines...
        int beginIndex = 0; // A "begin index" where each substring or segment will begin
        while (beginIndex <= text.length() - 1) { // If the index is not larger than the last character index in the line of text...
            lore.add(text.substring(beginIndex, Math.min(beginIndex + characters, text.length()))); // Add the segment
            beginIndex += characters;
            // This will also add any trailing segments at the end of the line that are shorter than the character limit
        }
    }
    return lore;
}

示例用法:

String lore = "This is a long description of an item's lore that needs to be broken down"
ItemMeta meta = // ... Get the ItemMeta object of an ItemStack
meta.setLore(splitLore(lore, 10)); // Splits the single line into multiple ones containing 10 or less characters

这种方法不是很"聪明",会剪掉单词,创建看起来不太漂亮或难以阅读的文本行。

为了让故事读起来更愉快,你可以把一行文字分成"单词",然后试着把它们分组,这样就不会有一行超过"软"字符的限制。下面的示例方法不会分割单词,尽管它也不会强制分割占据整行以上的超长单词。

public static ArrayList<String> splitLoreNicely(String text, int characters) {
    ArrayList<String> lore = new ArrayList<>();
    String[] words = text.split(" "); // Get the "words" in the line of text by splitting the space characters
    int wordsUsed = 0; // A counter for how many words have been placed in lines so far
    while (wordsUsed < words.length) { // Repeat this process until all words have been placed into separate lines
        String line = ""; // The line that will be added to the lore list
        for (int i = wordsUsed; i < words.length; i++) { // For each remaining word in the array
            if (line.length() + words[i].length() >= characters) { // If adding the next word exceeds or matches the character limit...
                line += words[i]; // Add the last word in the line without a space character
                wordsUsed++;
                break; // Break out of this inner loop, since we have reached/exceeded the character limit for this line
            } else { // If adding this word does not exceed or match the character limit... 
                line += words[i] + " "; // Add the word with a space character, continue for loop
                wordsUsed++;
            }
        }
        lore.add(line); // Add the line of text to the list
    }
    return lore;
}

最新更新