如果当前行包含太多文本,请在Stringlist中创建一个新行?

时间:2016-04-15 18:28:19

标签: java api minecraft bukkit

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

'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"));

1 个答案:

答案 0 :(得分:0)

传说String内的每个ArrayList<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;
}