mirror of
https://github.com/PaperMC/Paper.git
synced 2025-08-30 20:03:51 -07:00
Use Adventure for /version
command feedback, add copy to clipboard click event (#5333)
This commit is contained in:
@@ -12,6 +12,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
@@ -0,0 +0,0 @@
|
||||
+package com.destroystokyo.paper.util;
|
||||
+
|
||||
+import net.kyori.adventure.text.Component;
|
||||
+import net.kyori.adventure.text.format.NamedTextColor;
|
||||
+import org.bukkit.Bukkit;
|
||||
+import org.jetbrains.annotations.NotNull;
|
||||
+
|
||||
@@ -26,16 +28,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
+ long getCacheTime();
|
||||
+
|
||||
+ /**
|
||||
+ * Gets the version message to cache and show to command senders. Multiple messages can be sent using newlines (\n)
|
||||
+ * in the string. The string will be split on these newlines and sent as individual messages.
|
||||
+ * <p>
|
||||
+ * NOTE: This is run in a new thread separate from that of the command processing thread
|
||||
+ * Gets the version message to cache and show to command senders.
|
||||
+ *
|
||||
+ * <p>NOTE: This is run in a new thread separate from that of the command processing thread</p>
|
||||
+ *
|
||||
+ * @param serverVersion the current version of the server (will match {@link Bukkit#getVersion()})
|
||||
+ * @return the message to show when requesting a version
|
||||
+ */
|
||||
+ @NotNull
|
||||
+ String getVersionMessage(@NotNull String serverVersion);
|
||||
+ Component getVersionMessage(@NotNull String serverVersion);
|
||||
+
|
||||
+ class DummyVersionFetcher implements VersionFetcher {
|
||||
+
|
||||
@@ -46,11 +47,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
+
|
||||
+ @NotNull
|
||||
+ @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().info("Override the default implementation of org.bukkit.UnsafeValues#getVersionFetcher()");
|
||||
+ 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);
|
||||
+ }
|
||||
+ }
|
||||
+}
|
||||
@@ -82,6 +83,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.io.Resources;
|
||||
@@ -0,0 +0,0 @@ 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;
|
||||
@@ -0,0 +0,0 @@ import org.bukkit.util.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -99,6 +108,23 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
super(name);
|
||||
|
||||
@@ -0,0 +0,0 @@ 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();
|
||||
@@ -0,0 +0,0 @@ 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) {
|
||||
if (hasVersion) {
|
||||
@@ -107,28 +133,25 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
lastCheck = System.currentTimeMillis();
|
||||
hasVersion = false;
|
||||
} else {
|
||||
- sender.sendMessage(versionMessage);
|
||||
+ 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
|
||||
@@ -0,0 +0,0 @@ public class VersionCommand extends BukkitCommand {
|
||||
return;
|
||||
}
|
||||
versionWaiters.add(sender);
|
||||
- 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() {
|
||||
@@ -0,0 +0,0 @@ public class VersionCommand extends BukkitCommand {
|
||||
|
||||
private void obtainVersion() {
|
||||
String version = Bukkit.getVersion();
|
||||
+ // Paper start
|
||||
+ 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;
|
||||
+ }
|
||||
+ setVersionMessage(getVersionFetcher().getVersionMessage(version));
|
||||
+ /*
|
||||
if (version == null) version = "Custom";
|
||||
String[] parts = version.substring(0, version.indexOf(' ')).split("-");
|
||||
@@ -138,39 +161,25 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||
setVersionMessage("Unknown version, custom build?");
|
||||
}
|
||||
+ */
|
||||
+ setVersionMessage(getVersionFetcher().getVersionMessage(version));
|
||||
+ // Paper end
|
||||
}
|
||||
|
||||
private void setVersionMessage(@NotNull String msg) {
|
||||
@@ -0,0 +0,0 @@ public class VersionCommand extends BukkitCommand {
|
||||
- private void setVersionMessage(@NotNull String msg) {
|
||||
+ // 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 {
|
||||
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 {
|
||||
@@ -0,0 +0,0 @@ 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(
|
||||
|
Reference in New Issue
Block a user