Skip to content

Commit

Permalink
Add clickable version on version command (#9347)
Browse files Browse the repository at this point in the history
Co-authored-by: Yannick Lamprecht <[email protected]>
  • Loading branch information
TheMeinerLP and yannicklamprecht authored Aug 6, 2023
1 parent 31358d5 commit 8fe8ca6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 32 deletions.
73 changes: 48 additions & 25 deletions patches/api/0014-Version-Command-2.0.patch
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,24 @@ index f68ef89f37057cf677a767026ab395f7a839a2f9..a6aa33b9574d0278e10927007a62290e
// Paper end
}
diff --git a/src/main/java/org/bukkit/command/defaults/VersionCommand.java b/src/main/java/org/bukkit/command/defaults/VersionCommand.java
index 04b4fb6859df0221f8f9f92c5a7ac2dda1073355..b50f614806f4634960d383e8a33f094c2f46935f 100644
index 04b4fb6859df0221f8f9f92c5a7ac2dda1073355..b437cf212a63aa96a9492db8d01d5d37061aee23 100644
--- a/src/main/java/org/bukkit/command/defaults/VersionCommand.java
+++ b/src/main/java/org/bukkit/command/defaults/VersionCommand.java
@@ -1,5 +1,6 @@
package org.bukkit.command.defaults;

+import com.destroystokyo.paper.util.VersionFetcher; // Paper - version supplier
import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
@@ -26,6 +27,15 @@ import org.bukkit.util.StringUtil;
@@ -24,8 +24,25 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.NotNull;
+// Paper start - version command 2.0
+import com.destroystokyo.paper.util.VersionFetcher;
+import net.kyori.adventure.text.Component;
+import net.kyori.adventure.text.format.NamedTextColor;
+import net.kyori.adventure.text.event.ClickEvent;
+import net.kyori.adventure.text.format.TextDecoration;
+import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
+// Paper end - version command 2.0

public class VersionCommand extends BukkitCommand {
+ private VersionFetcher versionFetcher;
+ private VersionFetcher versionFetcher; // Paper - version command 2.0
+ private VersionFetcher getVersionFetcher() { // lazy load because unsafe isn't available at command registration
+ if (versionFetcher == null) {
+ versionFetcher = Bukkit.getUnsafe().getVersionFetcher();
Expand All @@ -99,7 +102,7 @@ index 04b4fb6859df0221f8f9f92c5a7ac2dda1073355..b50f614806f4634960d383e8a33f094c
public VersionCommand(@NotNull String name) {
super(name);

@@ -40,7 +50,7 @@ public class VersionCommand extends BukkitCommand {
@@ -40,7 +57,7 @@ public class VersionCommand extends BukkitCommand {
if (!testPermission(sender)) return true;

if (args.length == 0) {
Expand All @@ -108,12 +111,32 @@ index 04b4fb6859df0221f8f9f92c5a7ac2dda1073355..b50f614806f4634960d383e8a33f094c
sendVersion(sender);
} else {
StringBuilder name = new StringBuilder();
@@ -146,14 +156,14 @@ public class VersionCommand extends BukkitCommand {
@@ -79,8 +96,17 @@ public class VersionCommand extends BukkitCommand {

private void describeToSender(@NotNull Plugin plugin, @NotNull CommandSender sender) {
PluginDescriptionFile desc = plugin.getDescription();
- sender.sendMessage(ChatColor.GREEN + desc.getName() + ChatColor.WHITE + " version " + ChatColor.GREEN + desc.getVersion());
-
+ // Paper start - version command 2.0
+ sender.sendMessage(
+ Component.text()
+ .append(Component.text(desc.getName(), NamedTextColor.GREEN))
+ .append(Component.text(" version "))
+ .append(Component.text(desc.getVersion(), NamedTextColor.GREEN)
+ .hoverEvent(Component.text("Click to copy to clipboard", NamedTextColor.WHITE))
+ .clickEvent(ClickEvent.copyToClipboard(desc.getVersion()))
+ )
+ );
+ // Paper end - version command 2.0
if (desc.getDescription() != null) {
sender.sendMessage(desc.getDescription());
}
@@ -146,14 +172,14 @@ public class VersionCommand extends BukkitCommand {

private final ReentrantLock versionLock = new ReentrantLock();
private boolean hasVersion = false;
- private String versionMessage = null;
+ private net.kyori.adventure.text.Component versionMessage = null; // Paper
+ private Component versionMessage = null; // Paper
private final Set<CommandSender> versionWaiters = new HashSet<CommandSender>();
private boolean versionTaskStarted = false;
private long lastCheck = 0;
Expand All @@ -125,30 +148,30 @@ index 04b4fb6859df0221f8f9f92c5a7ac2dda1073355..b50f614806f4634960d383e8a33f094c
lastCheck = System.currentTimeMillis();
hasVersion = false;
} else {
@@ -168,7 +178,7 @@ public class VersionCommand extends BukkitCommand {
@@ -168,7 +194,7 @@ public class VersionCommand extends BukkitCommand {
return;
}
versionWaiters.add(sender);
- sender.sendMessage("Checking version, please wait...");
+ sender.sendMessage(net.kyori.adventure.text.Component.text("Checking version, please wait...", net.kyori.adventure.text.format.NamedTextColor.WHITE, net.kyori.adventure.text.format.TextDecoration.ITALIC)); // Paper
+ sender.sendMessage(Component.text("Checking version, please wait...", NamedTextColor.WHITE, TextDecoration.ITALIC)); // Paper
if (!versionTaskStarted) {
versionTaskStarted = true;
new Thread(new Runnable() {
@@ -186,6 +196,13 @@ public class VersionCommand extends BukkitCommand {
@@ -186,6 +212,13 @@ public class VersionCommand extends BukkitCommand {

private void obtainVersion() {
String version = Bukkit.getVersion();
+ // Paper start
+ if (version.startsWith("null")) { // running from ide?
+ setVersionMessage(net.kyori.adventure.text.Component.text("Unknown version, custom build?", net.kyori.adventure.text.format.NamedTextColor.YELLOW));
+ setVersionMessage(Component.text("Unknown version, custom build?", NamedTextColor.YELLOW));
+ return;
+ }
+ setVersionMessage(getVersionFetcher().getVersionMessage(version));
+ /*
if (version == null) version = "Custom";
String[] parts = version.substring(0, version.indexOf(' ')).split("-");
if (parts.length == 4) {
@@ -215,11 +232,24 @@ public class VersionCommand extends BukkitCommand {
@@ -215,11 +248,24 @@ public class VersionCommand extends BukkitCommand {
} else {
setVersionMessage("Unknown version, custom build?");
}
Expand All @@ -158,18 +181,18 @@ index 04b4fb6859df0221f8f9f92c5a7ac2dda1073355..b50f614806f4634960d383e8a33f094c

- private void setVersionMessage(@NotNull String msg) {
+ // Paper start
+ private void setVersionMessage(final @NotNull net.kyori.adventure.text.Component msg) {
+ private void setVersionMessage(final @NotNull Component msg) {
lastCheck = System.currentTimeMillis();
- versionMessage = msg;
+ final net.kyori.adventure.text.Component message = net.kyori.adventure.text.TextComponent.ofChildren(
+ net.kyori.adventure.text.Component.text("This server is running " + Bukkit.getName() + " version " + Bukkit.getVersion() + " (Implementing API version " + Bukkit.getBukkitVersion() + ")", net.kyori.adventure.text.format.NamedTextColor.WHITE),
+ net.kyori.adventure.text.Component.newline(),
+ final Component message = Component.textOfChildren(
+ Component.text("This server is running " + Bukkit.getName() + " version " + Bukkit.getVersion() + " (Implementing API version " + Bukkit.getBukkitVersion() + ")", NamedTextColor.WHITE),
+ Component.newline(),
+ msg
+ );
+ this.versionMessage = net.kyori.adventure.text.Component.text()
+ this.versionMessage = Component.text()
+ .append(message)
+ .hoverEvent(net.kyori.adventure.text.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)))
+ .hoverEvent(Component.text("Click to copy to clipboard", NamedTextColor.WHITE))
+ .clickEvent(ClickEvent.copyToClipboard(PlainTextComponentSerializer.plainText().serialize(message)))
+ .build();
+ // Paper end
versionLock.lock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ index 67c92e10b38af2ac45c1d9b6374695247bf1e7c3..627c04f5e71ad032760296092a65596b

/**
diff --git a/src/main/java/org/bukkit/command/defaults/VersionCommand.java b/src/main/java/org/bukkit/command/defaults/VersionCommand.java
index b50f614806f4634960d383e8a33f094c2f46935f..e40f017f87d6b6b4770501b106c76dc69ec69abb 100644
index b437cf212a63aa96a9492db8d01d5d37061aee23..fd5d9881abfd930bb883120f018f76dc78b62b14 100644
--- a/src/main/java/org/bukkit/command/defaults/VersionCommand.java
+++ b/src/main/java/org/bukkit/command/defaults/VersionCommand.java
@@ -240,7 +240,7 @@ public class VersionCommand extends BukkitCommand {
private void setVersionMessage(final @NotNull net.kyori.adventure.text.Component msg) {
@@ -256,7 +256,7 @@ public class VersionCommand extends BukkitCommand {
private void setVersionMessage(final @NotNull Component msg) {
lastCheck = System.currentTimeMillis();
final net.kyori.adventure.text.Component message = net.kyori.adventure.text.TextComponent.ofChildren(
- net.kyori.adventure.text.Component.text("This server is running " + Bukkit.getName() + " version " + Bukkit.getVersion() + " (Implementing API version " + Bukkit.getBukkitVersion() + ")", net.kyori.adventure.text.format.NamedTextColor.WHITE),
+ net.kyori.adventure.text.Component.text(Bukkit.getVersionMessage(), net.kyori.adventure.text.format.NamedTextColor.WHITE),
net.kyori.adventure.text.Component.newline(),
final Component message = Component.textOfChildren(
- Component.text("This server is running " + Bukkit.getName() + " version " + Bukkit.getVersion() + " (Implementing API version " + Bukkit.getBukkitVersion() + ")", NamedTextColor.WHITE),
+ Component.text(Bukkit.getVersionMessage(), NamedTextColor.WHITE),
Component.newline(),
msg
);

0 comments on commit 8fe8ca6

Please sign in to comment.