Use Adventure for `/version` command feedback, add copy to clipboard click event (#5333)

This commit is contained in:
Jason 2021-03-09 20:56:13 -08:00 committed by GitHub
parent eb11845f87
commit 4d287e31cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 94 additions and 73 deletions

View File

@ -6,12 +6,14 @@ Subject: [PATCH] Version Command 2.0
diff --git a/src/main/java/com/destroystokyo/paper/util/VersionFetcher.java b/src/main/java/com/destroystokyo/paper/util/VersionFetcher.java diff --git a/src/main/java/com/destroystokyo/paper/util/VersionFetcher.java b/src/main/java/com/destroystokyo/paper/util/VersionFetcher.java
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..2a2651299e8dc631938ba4b4078dc694764d784c index 0000000000000000000000000000000000000000..a736d7bcdc5861a01b66ba36158db1c716339346
--- /dev/null --- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/util/VersionFetcher.java +++ b/src/main/java/com/destroystokyo/paper/util/VersionFetcher.java
@@ -0,0 +1,44 @@ @@ -0,0 +1,45 @@
+package com.destroystokyo.paper.util; +package com.destroystokyo.paper.util;
+ +
+import net.kyori.adventure.text.Component;
+import net.kyori.adventure.text.format.NamedTextColor;
+import org.bukkit.Bukkit; +import org.bukkit.Bukkit;
+import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.NotNull;
+ +
@ -26,16 +28,15 @@ index 0000000000000000000000000000000000000000..2a2651299e8dc631938ba4b4078dc694
+ long getCacheTime(); + long getCacheTime();
+ +
+ /** + /**
+ * Gets the version message to cache and show to command senders. Multiple messages can be sent using newlines (\n) + * Gets the version message to cache and show to command senders.
+ * in the string. The string will be split on these newlines and sent as individual messages. + *
+ * <p> + * <p>NOTE: This is run in a new thread separate from that of the command processing thread</p>
+ * NOTE: This is run in a new thread separate from that of the command processing thread
+ * + *
+ * @param serverVersion the current version of the server (will match {@link Bukkit#getVersion()}) + * @param serverVersion the current version of the server (will match {@link Bukkit#getVersion()})
+ * @return the message to show when requesting a version + * @return the message to show when requesting a version
+ */ + */
+ @NotNull + @NotNull
+ String getVersionMessage(@NotNull String serverVersion); + Component getVersionMessage(@NotNull String serverVersion);
+ +
+ class DummyVersionFetcher implements VersionFetcher { + class DummyVersionFetcher implements VersionFetcher {
+ +
@ -46,11 +47,11 @@ index 0000000000000000000000000000000000000000..2a2651299e8dc631938ba4b4078dc694
+ +
+ @NotNull + @NotNull
+ @Override + @Override
+ public String getVersionMessage(@NotNull String serverVersion) { + public Component getVersionMessage(@NotNull String serverVersion) {
+ Bukkit.getLogger().warning("Version provider has not been set, cannot check for updates!"); + Bukkit.getLogger().warning("Version provider has not been set, cannot check for updates!");
+ Bukkit.getLogger().info("Override the default implementation of org.bukkit.UnsafeValues#getVersionFetcher()"); + Bukkit.getLogger().info("Override the default implementation of org.bukkit.UnsafeValues#getVersionFetcher()");
+ new Throwable().printStackTrace(); + new Throwable().printStackTrace();
+ return "Unable to check for updates. No version provider set."; + return Component.text("Unable to check for updates. No version provider set.", NamedTextColor.RED);
+ } + }
+ } + }
+} +}
@ -72,7 +73,7 @@ index dd0b80c268644ac714311501215f45166092b856..bed84606a111648ce39c2e6683c0ca5a
// Paper end // Paper end
} }
diff --git a/src/main/java/org/bukkit/command/defaults/VersionCommand.java b/src/main/java/org/bukkit/command/defaults/VersionCommand.java diff --git a/src/main/java/org/bukkit/command/defaults/VersionCommand.java b/src/main/java/org/bukkit/command/defaults/VersionCommand.java
index 2305eb40832a82159cd89162934870cf57e1aa0e..c1f64d31835bd9cd86f807c77cdb86190f4709b0 100644 index 2305eb40832a82159cd89162934870cf57e1aa0e..5819797b1d6223c4b4ac7a60bdc710ba69cee313 100644
--- a/src/main/java/org/bukkit/command/defaults/VersionCommand.java --- a/src/main/java/org/bukkit/command/defaults/VersionCommand.java
+++ b/src/main/java/org/bukkit/command/defaults/VersionCommand.java +++ b/src/main/java/org/bukkit/command/defaults/VersionCommand.java
@@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
@ -82,7 +83,15 @@ index 2305eb40832a82159cd89162934870cf57e1aa0e..c1f64d31835bd9cd86f807c77cdb8619
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.io.Resources; import com.google.common.io.Resources;
@@ -26,6 +27,15 @@ import org.bukkit.util.StringUtil; @@ -16,6 +17,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
+import net.kyori.adventure.text.Component; // Paper
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@@ -26,6 +28,15 @@ import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class VersionCommand extends BukkitCommand { public class VersionCommand extends BukkitCommand {
@ -98,7 +107,24 @@ index 2305eb40832a82159cd89162934870cf57e1aa0e..c1f64d31835bd9cd86f807c77cdb8619
public VersionCommand(@NotNull String name) { public VersionCommand(@NotNull String name) {
super(name); super(name);
@@ -153,18 +163,18 @@ public class VersionCommand extends BukkitCommand { @@ -40,7 +51,7 @@ public class VersionCommand extends BukkitCommand {
if (!testPermission(sender)) return true;
if (args.length == 0) {
- sender.sendMessage("This server is running " + Bukkit.getName() + " version " + Bukkit.getVersion() + " (Implementing API version " + Bukkit.getBukkitVersion() + ")");
+ //sender.sendMessage("This server is running " + Bukkit.getName() + " version " + Bukkit.getVersion() + " (Implementing API version " + Bukkit.getBukkitVersion() + ")"); // Paper - moved to setVersionMessage
sendVersion(sender);
} else {
StringBuilder name = new StringBuilder();
@@ -146,14 +157,14 @@ public class VersionCommand extends BukkitCommand {
private final ReentrantLock versionLock = new ReentrantLock();
private boolean hasVersion = false;
- private String versionMessage = null;
+ private Component versionMessage = null; // Paper
private final Set<CommandSender> versionWaiters = new HashSet<CommandSender>();
private boolean versionTaskStarted = false;
private long lastCheck = 0;
private void sendVersion(@NotNull CommandSender sender) { private void sendVersion(@NotNull CommandSender sender) {
if (hasVersion) { if (hasVersion) {
@ -107,70 +133,53 @@ index 2305eb40832a82159cd89162934870cf57e1aa0e..c1f64d31835bd9cd86f807c77cdb8619
lastCheck = System.currentTimeMillis(); lastCheck = System.currentTimeMillis();
hasVersion = false; hasVersion = false;
} else { } else {
- sender.sendMessage(versionMessage); @@ -168,7 +179,7 @@ public class VersionCommand extends BukkitCommand {
+ sendMessages(versionMessage, sender); // Paper - allow \n for multiple messages
return;
}
}
versionLock.lock();
try {
if (hasVersion) {
- sender.sendMessage(versionMessage);
+ sendMessages(versionMessage, sender); // Paper - allow \n for multiple messages
return; return;
} }
versionWaiters.add(sender); versionWaiters.add(sender);
@@ -186,6 +196,12 @@ public class VersionCommand extends BukkitCommand { - sender.sendMessage("Checking version, please wait...");
+ sender.sendMessage(Component.text("Checking version, please wait...", net.kyori.adventure.text.format.NamedTextColor.WHITE, net.kyori.adventure.text.format.TextDecoration.ITALIC)); // Paper
if (!versionTaskStarted) {
versionTaskStarted = true;
new Thread(new Runnable() {
@@ -186,6 +197,13 @@ public class VersionCommand extends BukkitCommand {
private void obtainVersion() { private void obtainVersion() {
String version = Bukkit.getVersion(); String version = Bukkit.getVersion();
+ // Paper start + // Paper start
+ if (version.startsWith("null")) { // running from ide? + if (version.startsWith("null")) { // running from ide?
+ setVersionMessage("Unknown version, custom build?"); + setVersionMessage(Component.text("Unknown version, custom build?", net.kyori.adventure.text.format.NamedTextColor.YELLOW));
+ return; + return;
+ } + }
+ setVersionMessage(getVersionFetcher().getVersionMessage(version));
+ /* + /*
if (version == null) version = "Custom"; if (version == null) version = "Custom";
String[] parts = version.substring(0, version.indexOf(' ')).split("-"); String[] parts = version.substring(0, version.indexOf(' ')).split("-");
if (parts.length == 4) { if (parts.length == 4) {
@@ -215,6 +231,9 @@ public class VersionCommand extends BukkitCommand { @@ -215,11 +233,24 @@ public class VersionCommand extends BukkitCommand {
} else { } else {
setVersionMessage("Unknown version, custom build?"); setVersionMessage("Unknown version, custom build?");
} }
+ */ + */
+ setVersionMessage(getVersionFetcher().getVersionMessage(version));
+ // Paper end + // Paper end
} }
private void setVersionMessage(@NotNull String msg) { - private void setVersionMessage(@NotNull String msg) {
@@ -224,8 +243,13 @@ public class VersionCommand extends BukkitCommand { + // Paper start
+ private void setVersionMessage(final @NotNull Component msg) {
lastCheck = System.currentTimeMillis();
- versionMessage = msg;
+ final Component message = net.kyori.adventure.text.TextComponent.ofChildren(
+ Component.text("This server is running " + Bukkit.getName() + " version " + Bukkit.getVersion() + " (Implementing API version " + Bukkit.getBukkitVersion() + ")", net.kyori.adventure.text.format.NamedTextColor.WHITE),
+ Component.newline(),
+ msg
+ );
+ this.versionMessage = Component.text()
+ .append(message)
+ .hoverEvent(Component.text("Click to copy to clipboard", net.kyori.adventure.text.format.NamedTextColor.WHITE))
+ .clickEvent(net.kyori.adventure.text.event.ClickEvent.copyToClipboard(net.kyori.adventure.text.serializer.plain.PlainComponentSerializer.plain().serialize(message)))
+ .build();
+ // Paper end
versionLock.lock();
try { try {
hasVersion = true; hasVersion = true;
versionTaskStarted = false;
+ // Paper - allow \n for multiple messages
+ String[] messages = versionMessage.split("\n");
for (CommandSender sender : versionWaiters) {
- sender.sendMessage(versionMessage);
+ for (String message : messages) {
+ sender.sendMessage(message);
+ }
+ // Paper end
}
versionWaiters.clear();
} finally {
@@ -233,6 +257,15 @@ public class VersionCommand extends BukkitCommand {
}
}
+ // Paper start
+ private void sendMessages(String toSplit, CommandSender target) {
+ String[] messages = toSplit.split("\n");
+ for (String message : messages) {
+ target.sendMessage(message);
+ }
+ }
+ // Paper end
+
private static int getDistance(@NotNull String repo, @NotNull String hash) {
try {
BufferedReader reader = Resources.asCharSource(

View File

@ -6,16 +6,18 @@ Subject: [PATCH] Implement Paper VersionChecker
diff --git a/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java b/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java diff --git a/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java b/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..5deed3e25ff41ab0a4015a5fd0c1e952cdb16402 index 0000000000000000000000000000000000000000..c8b911e5d013525ffc5d2911ee0e421dd916cb00
--- /dev/null --- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java +++ b/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java
@@ -0,0 +1,115 @@ @@ -0,0 +1,117 @@
+package com.destroystokyo.paper; +package com.destroystokyo.paper;
+ +
+import com.destroystokyo.paper.util.VersionFetcher; +import com.destroystokyo.paper.util.VersionFetcher;
+import com.google.common.base.Charsets; +import com.google.common.base.Charsets;
+import com.google.common.io.Resources; +import com.google.common.io.Resources;
+import com.google.gson.*; +import com.google.gson.*;
+import net.kyori.adventure.text.Component;
+import net.kyori.adventure.text.format.NamedTextColor;
+ +
+import javax.annotation.Nonnull; +import javax.annotation.Nonnull;
+import javax.annotation.Nullable; +import javax.annotation.Nullable;
@ -35,7 +37,7 @@ index 0000000000000000000000000000000000000000..5deed3e25ff41ab0a4015a5fd0c1e952
+ +
+ @Nonnull + @Nonnull
+ @Override + @Override
+ public String getVersionMessage(@Nonnull String serverVersion) { + public Component getVersionMessage(@Nonnull String serverVersion) {
+ String[] parts = serverVersion.substring("git-Paper-".length()).split("[-\\s]"); + String[] parts = serverVersion.substring("git-Paper-".length()).split("[-\\s]");
+ return getUpdateStatusMessage("PaperMC/Paper", GITHUB_BRANCH_NAME, parts[0]); + return getUpdateStatusMessage("PaperMC/Paper", GITHUB_BRANCH_NAME, parts[0]);
+ } + }
@ -56,7 +58,7 @@ index 0000000000000000000000000000000000000000..5deed3e25ff41ab0a4015a5fd0c1e952
+ return mcVer; + return mcVer;
+ } + }
+ +
+ private static String getUpdateStatusMessage(@Nonnull String repo, @Nonnull String branch, @Nonnull String versionInfo) { + private static Component getUpdateStatusMessage(@Nonnull String repo, @Nonnull String branch, @Nonnull String versionInfo) {
+ int distance; + int distance;
+ try { + try {
+ int jenkinsBuild = Integer.parseInt(versionInfo); + int jenkinsBuild = Integer.parseInt(versionInfo);
@ -68,13 +70,13 @@ index 0000000000000000000000000000000000000000..5deed3e25ff41ab0a4015a5fd0c1e952
+ +
+ switch (distance) { + switch (distance) {
+ case -1: + case -1:
+ return "Error obtaining version information"; + return Component.text("Error obtaining version information", NamedTextColor.YELLOW);
+ case 0: + case 0:
+ return "You are running the latest version"; + return Component.text("You are running the latest version", NamedTextColor.GREEN);
+ case -2: + case -2:
+ return "Unknown version"; + return Component.text("Unknown version", NamedTextColor.YELLOW);
+ default: + default:
+ return "You are " + distance + " version(s) behind"; + return Component.text("You are " + distance + " version(s) behind", NamedTextColor.YELLOW);
+ } + }
+ } + }
+ +

View File

@ -5,28 +5,38 @@ Subject: [PATCH] Add version history to version command
diff --git a/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java b/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java diff --git a/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java b/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java
index 5deed3e25ff41ab0a4015a5fd0c1e952cdb16402..49a38c6608b652ff48ef4eaca0dd3ccb1ba570e3 100644 index c8b911e5d013525ffc5d2911ee0e421dd916cb00..dc0ea65ab87255fad0d54dfb509300098a0b4864 100644
--- a/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java --- a/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java
+++ b/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java +++ b/src/main/java/com/destroystokyo/paper/PaperVersionFetcher.java
@@ -25,7 +25,10 @@ public class PaperVersionFetcher implements VersionFetcher { @@ -5,7 +5,9 @@ import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import com.google.gson.*;
import net.kyori.adventure.text.Component;
+import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
+import net.kyori.adventure.text.format.TextDecoration;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@@ -27,7 +29,10 @@ public class PaperVersionFetcher implements VersionFetcher {
@Override @Override
public String getVersionMessage(@Nonnull String serverVersion) { public Component getVersionMessage(@Nonnull String serverVersion) {
String[] parts = serverVersion.substring("git-Paper-".length()).split("[-\\s]"); String[] parts = serverVersion.substring("git-Paper-".length()).split("[-\\s]");
- return getUpdateStatusMessage("PaperMC/Paper", GITHUB_BRANCH_NAME, parts[0]); - return getUpdateStatusMessage("PaperMC/Paper", GITHUB_BRANCH_NAME, parts[0]);
+ String updateMessage = getUpdateStatusMessage("PaperMC/Paper", GITHUB_BRANCH_NAME, parts[0]); + final Component updateMessage = getUpdateStatusMessage("PaperMC/Paper", GITHUB_BRANCH_NAME, parts[0]);
+ String history = getHistory(); + final Component history = getHistory();
+ +
+ return history != null ? history + "\n" + updateMessage : updateMessage; + return history != null ? TextComponent.ofChildren(updateMessage, Component.newline(), history) : updateMessage;
} }
private static @Nullable String getMinecraftVersion() { private static @Nullable String getMinecraftVersion() {
@@ -112,4 +115,19 @@ public class PaperVersionFetcher implements VersionFetcher { @@ -114,4 +119,19 @@ public class PaperVersionFetcher implements VersionFetcher {
return -1; return -1;
} }
} }
+ +
+ @Nullable + @Nullable
+ private String getHistory() { + private Component getHistory() {
+ final VersionHistoryManager.VersionData data = VersionHistoryManager.INSTANCE.getVersionData(); + final VersionHistoryManager.VersionData data = VersionHistoryManager.INSTANCE.getVersionData();
+ if (data == null) { + if (data == null) {
+ return null; + return null;
@ -37,7 +47,7 @@ index 5deed3e25ff41ab0a4015a5fd0c1e952cdb16402..49a38c6608b652ff48ef4eaca0dd3ccb
+ return null; + return null;
+ } + }
+ +
+ return "Previous version: " + oldVersion; + return Component.text("Previous version: " + oldVersion, NamedTextColor.GRAY, TextDecoration.ITALIC);
+ } + }
} }
diff --git a/src/main/java/com/destroystokyo/paper/VersionHistoryManager.java b/src/main/java/com/destroystokyo/paper/VersionHistoryManager.java diff --git a/src/main/java/com/destroystokyo/paper/VersionHistoryManager.java b/src/main/java/com/destroystokyo/paper/VersionHistoryManager.java