Support more actions in Messenger (and therefore format()) (#1242)

* Support more actions in Messenger (and therefore `format()`)

* Add the new actions to docs

* New `format()` docs fixes
This commit is contained in:
altrisi authored and GitHub committed 2022-01-08 23:16:06 +01:00
1 parent 8f265f4e52
commit 763abe68c9
2 files changed
+39 -57

No files matched your search

+2
View File
@@ -252,6 +252,8 @@ Decorators (listed as extra argument after the component they would affect):
* `'^<format> <text>'` - hover over tooltip text, appearing when hovering with your mouse over the text below. * `'^<format> <text>'` - hover over tooltip text, appearing when hovering with your mouse over the text below.
* `'?<suggestion>` - command suggestion - a message that will be pasted to chat when text below it is clicked. * `'?<suggestion>` - command suggestion - a message that will be pasted to chat when text below it is clicked.
* `'!<message>'` - a chat message that will be executed when the text below it is clicked. * `'!<message>'` - a chat message that will be executed when the text below it is clicked.
* `'@<url>'` - a URL that will be opened when the text below it is clicked.
* `'&<text>'` - a text that will be copied to clipboard when the text below it is clicked.
Both suggestions and messages can contain a command, which will be executed as a player that clicks it. Both suggestions and messages can contain a command, which will be executed as a player that clicks it.
+37 -57
View File
@@ -103,23 +103,18 @@ public class Messenger
} }
public static String creatureTypeColor(SpawnGroup type) public static String creatureTypeColor(SpawnGroup type)
{ {
switch (type) return switch (type)
{ {
case MONSTER: case MONSTER -> "n";
return "n"; case CREATURE -> "e";
case CREATURE: case AMBIENT -> "f";
return "e"; case WATER_CREATURE -> "v";
case AMBIENT: case WATER_AMBIENT -> "q";
return "f"; default -> "w"; // missing MISC and UNDERGROUND_WATER_CREATURE
case WATER_CREATURE: };
return "v";
case WATER_AMBIENT:
return "q";
}
return "w";
} }
private static BaseText _getChatComponentFromDesc(String message, BaseText previous_message) private static BaseText getChatComponentFromDesc(String message, BaseText previousMessage)
{ {
if (message.equalsIgnoreCase("")) if (message.equalsIgnoreCase(""))
{ {
@@ -127,7 +122,7 @@ public class Messenger
} }
if (Character.isWhitespace(message.charAt(0))) if (Character.isWhitespace(message.charAt(0)))
{ {
message = "w"+message; message = "w" + message;
} }
int limit = message.indexOf(' '); int limit = message.indexOf(' ');
String desc = message; String desc = message;
@@ -137,52 +132,37 @@ public class Messenger
desc = message.substring(0, limit); desc = message.substring(0, limit);
str = message.substring(limit+1); str = message.substring(limit+1);
} }
if (desc.charAt(0) == '/') // deprecated if (previousMessage == null) {
{ BaseText text = new LiteralText(str);
if (previous_message != null) text.setStyle(parseStyle(desc));
previous_message.setStyle( return text;
previous_message.getStyle().withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message))
);
return previous_message;
} }
if (desc.charAt(0) == '?') Style previousStyle = previousMessage.getStyle();
{ BaseText ret = previousMessage;
if (previous_message != null) previousMessage.setStyle(switch (desc.charAt(0)) {
previous_message.setStyle( case '?' -> previousStyle.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message.substring(1)));
previous_message.getStyle().withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, message.substring(1))) case '!' -> previousStyle.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, message.substring(1)));
); case '^' -> previousStyle.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, c(message.substring(1))));
return previous_message; case '@' -> previousStyle.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, message.substring(1)));
} case '&' -> previousStyle.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, message.substring(1)));
if (desc.charAt(0) == '!') default -> { // Create a new component
{ ret = new LiteralText(str);
if (previous_message != null) ret.setStyle(parseStyle(desc));
previous_message.setStyle( yield previousStyle; // no op for the previous style
previous_message.getStyle().withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, message.substring(1))) }
); });
return previous_message; return ret;
}
if (desc.charAt(0) == '^')
{
if (previous_message != null)
previous_message.setStyle(
previous_message.getStyle().withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, c(message.substring(1))))
);
return previous_message;
}
BaseText txt = new LiteralText(str);
txt.setStyle(parseStyle(desc));
return txt;
} }
public static BaseText tp(String desc, Vec3d pos) { return tp(desc, pos.x, pos.y, pos.z); } public static BaseText tp(String desc, Vec3d pos) { return tp(desc, pos.x, pos.y, pos.z); }
public static BaseText tp(String desc, BlockPos pos) { return tp(desc, pos.getX(), pos.getY(), pos.getZ()); } public static BaseText tp(String desc, BlockPos pos) { return tp(desc, pos.getX(), pos.getY(), pos.getZ()); }
public static BaseText tp(String desc, double x, double y, double z) { return tp(desc, (float)x, (float)y, (float)z);} public static BaseText tp(String desc, double x, double y, double z) { return tp(desc, (float)x, (float)y, (float)z);}
public static BaseText tp(String desc, float x, float y, float z) public static BaseText tp(String desc, float x, float y, float z)
{ {
return _getCoordsTextComponent(desc, x, y, z, false); return getCoordsTextComponent(desc, x, y, z, false);
} }
public static BaseText tp(String desc, int x, int y, int z) public static BaseText tp(String desc, int x, int y, int z)
{ {
return _getCoordsTextComponent(desc, (float)x, (float)y, (float)z, true); return getCoordsTextComponent(desc, (float)x, (float)y, (float)z, true);
} }
/// to be continued /// to be continued
@@ -232,7 +212,7 @@ public class Messenger
return c(components.toArray(new Object[0])); return c(components.toArray(new Object[0]));
} }
private static BaseText _getCoordsTextComponent(String style, float x, float y, float z, boolean isInt) private static BaseText getCoordsTextComponent(String style, float x, float y, float z, boolean isInt)
{ {
String text; String text;
String command; String command;
@@ -266,19 +246,19 @@ public class Messenger
public static BaseText c(Object ... fields) public static BaseText c(Object ... fields)
{ {
BaseText message = new LiteralText(""); BaseText message = new LiteralText("");
BaseText previous_component = null; BaseText previousComponent = null;
for (Object o: fields) for (Object o: fields)
{ {
if (o instanceof BaseText) if (o instanceof BaseText)
{ {
message.append((BaseText)o); message.append((BaseText)o);
previous_component = (BaseText)o; previousComponent = (BaseText)o;
continue; continue;
} }
String txt = o.toString(); String txt = o.toString();
BaseText comp = _getChatComponentFromDesc(txt,previous_component); BaseText comp = getChatComponentFromDesc(txt, previousComponent);
if (comp != previous_component) message.append(comp); if (comp != previousComponent) message.append(comp);
previous_component = comp; previousComponent = comp;
} }
return message; return message;
} }