Skip to content

Commit

Permalink
Add ability to set the width that the hover text will have
Browse files Browse the repository at this point in the history
This is set to 60 characters by default (same as Minecraft) but will not apply to hover messages that already contain new line characters to allow manual modification. Also it will try to split between words but might cut very long words if they don't fit. This is done in a way that is a bit more intelligent about trying to keep words together than the Vanilla Minecraft algorithm
  • Loading branch information
Phoenix616 committed Jul 12, 2019
1 parent b847e6e commit c511daa
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/main/java/de/themoep/minedown/MineDown.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,19 @@ public MineDown urlHoverText(String text) {
return this;
}

/**
* Set the max width the hover text should have.
* Minecraft itself will wrap after 60 characters.
* Won't apply if the text already includes new lines.
* @param hoverTextWidth The url hover text length
* @return The MineDown instance
*/
public MineDown hoverTextWidth(int hoverTextWidth) {
reset();
parser().hoverTextWidth(hoverTextWidth);
return this;
}

/**
* Enable an option. Unfilter it if you filtered it before.
* @param option The option to enable
Expand Down
36 changes: 34 additions & 2 deletions src/main/java/de/themoep/minedown/MineDownParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ public class MineDownParser {
*/
private String urlHoverText = "Click to open url";

/**
* The max width the hover text should have.
* Minecraft itself will wrap after 60 characters.
* Won't apply if the text already includes new lines.
*/
private int hoverTextWidth = 60;

public static final Pattern URL_PATTERN = Pattern.compile("^(?:(https?)://)?([-\\w_\\.]{2,}\\.[a-z]{2,4})(/\\S*)?$");

public static final String COLOR_PREFIX = "color=";
Expand Down Expand Up @@ -443,7 +450,10 @@ public ComponentBuilder parseEvent(String text, String definitions) {
hoverAction = HoverEvent.Action.SHOW_TEXT;
}
if (hoverAction != null) {
hoverEvent = new HoverEvent(hoverAction, copy(false).urlDetection(false).parse(value.toString()).create());
String valueStr = value.toString();
hoverEvent = new HoverEvent(hoverAction, copy(false).urlDetection(false).parse(
hoverAction == HoverEvent.Action.SHOW_TEXT ? Util.wrap(valueStr, hoverTextWidth()) : valueStr
).create());
}
}

Expand Down Expand Up @@ -579,6 +589,7 @@ public MineDownParser copy(MineDownParser from, boolean formatting) {
lenient(from.lenient());
urlDetection(from.urlDetection());
urlHoverText(from.urlHoverText());
hoverTextWidth(from.hoverTextWidth());
enabledOptions(from.enabledOptions());
filteredOptions(from.filteredOptions());
colorChar(from.colorChar());
Expand Down Expand Up @@ -828,12 +839,33 @@ public String urlHoverText() {

/**
* Set the text to display when hovering over an URL. Has a %url% placeholder.
* @param urlHoverText The url over text
* @param urlHoverText The url hover text
* @return The MineDownParser instance
*/
public MineDownParser urlHoverText(String urlHoverText) {
this.urlHoverText = urlHoverText;
return this;
}

/**
* Get the max width the hover text should have.
* Minecraft itself will wrap after 60 characters.
* Won't apply if the text already includes new lines.
*/
public int hoverTextWidth() {
return this.hoverTextWidth;
}

/**
* Set the max width the hover text should have.
* Minecraft itself will wrap after 60 characters.
* Won't apply if the text already includes new lines.
* @param hoverTextWidth The url hover text length
* @return The MineDownParser instance
*/
public MineDownParser hoverTextWidth(int hoverTextWidth) {
this.hoverTextWidth = hoverTextWidth;
return this;
}

}
48 changes: 48 additions & 0 deletions src/main/java/de/themoep/minedown/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentBuilder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

public class Util {

private static final Pattern WRAP_PATTERN = Pattern.compile(" ", Pattern.LITERAL);

/**
* Utility method to throw an IllegalArgumentException if the value is false
* @param value The value to validate
Expand Down Expand Up @@ -224,4 +230,46 @@ public static boolean isEscaped(String string, int index) {
}
return e % 2 != 0;
}

/**
* Wrap a string if it is longer than the line length and contains no new line.
* Will try to wrap at spaces between words.
* @param string The string to wrap
* @param lineLength The max length of a line
* @return The wrapped string
*/
public static String wrap(String string, int lineLength) {
if (string.length() <= lineLength || string.contains("\n")) {
return string;
}

List<String> lines = new ArrayList<>();
StringBuilder currentLine = new StringBuilder();
for (String s : WRAP_PATTERN.split(string)) {
if (currentLine.length() + s.length() + 1 > lineLength) {
int rest = lineLength - currentLine.length() - 1;
if (rest > lineLength / 4 && s.length() > Math.min(rest * 2, lineLength / 4)) {
currentLine.append(" ").append(s, 0, rest);
} else {
rest = 0;
}
lines.add(currentLine.toString());
String restString = s.substring(rest);
while (restString.length() >= lineLength) {
lines.add(restString.substring(0, lineLength));
restString = restString.substring(lineLength);
}
currentLine = new StringBuilder(restString);
} else {
if (currentLine.length() > 0) {
currentLine.append(" ");
}
currentLine.append(s);
}
}
if (currentLine.length() > 0) {
lines.add(currentLine.toString());
}
return String.join("\n", lines);
}
}

0 comments on commit c511daa

Please sign in to comment.