Use random UUIDs instead of incrementing long for click callback ids (#9007)

This commit is contained in:
Jason 2023-03-19 11:25:45 -07:00 committed by GitHub
parent 510082222b
commit 662b85e00e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 25 deletions

View File

@ -1171,14 +1171,15 @@ index 0000000000000000000000000000000000000000..2a08e0461db4e699b7e6a1558a4419c8
+} +}
diff --git a/src/main/java/io/papermc/paper/adventure/providers/ClickCallbackProviderImpl.java b/src/main/java/io/papermc/paper/adventure/providers/ClickCallbackProviderImpl.java diff --git a/src/main/java/io/papermc/paper/adventure/providers/ClickCallbackProviderImpl.java b/src/main/java/io/papermc/paper/adventure/providers/ClickCallbackProviderImpl.java
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..04bc65019b7abf331feb1b6c5475466b9d7ad075 index 0000000000000000000000000000000000000000..3c17001bcd3862a76a22df488bff80a0ff4d1b83
--- /dev/null --- /dev/null
+++ b/src/main/java/io/papermc/paper/adventure/providers/ClickCallbackProviderImpl.java +++ b/src/main/java/io/papermc/paper/adventure/providers/ClickCallbackProviderImpl.java
@@ -0,0 +1,97 @@ @@ -0,0 +1,96 @@
+package io.papermc.paper.adventure.providers; +package io.papermc.paper.adventure.providers;
+ +
+import it.unimi.dsi.fastutil.longs.Long2ObjectMap; +import java.util.HashMap;
+import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; +import java.util.Map;
+import java.util.UUID;
+import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.audience.Audience;
+import net.kyori.adventure.text.event.ClickCallback; +import net.kyori.adventure.text.event.ClickCallback;
+import net.kyori.adventure.text.event.ClickEvent; +import net.kyori.adventure.text.event.ClickEvent;
@ -1186,7 +1187,6 @@ index 0000000000000000000000000000000000000000..04bc65019b7abf331feb1b6c5475466b
+ +
+import java.util.Queue; +import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicLong;
+ +
+@SuppressWarnings("UnstableApiUsage") // permitted provider +@SuppressWarnings("UnstableApiUsage") // permitted provider
+public class ClickCallbackProviderImpl implements ClickCallback.Provider { +public class ClickCallbackProviderImpl implements ClickCallback.Provider {
@ -1200,15 +1200,14 @@ index 0000000000000000000000000000000000000000..04bc65019b7abf331feb1b6c5475466b
+ +
+ public static final class CallbackManager { + public static final class CallbackManager {
+ +
+ private final Long2ObjectMap<StoredCallback> callbacks = new Long2ObjectOpenHashMap<>(); + private final Map<UUID, StoredCallback> callbacks = new HashMap<>();
+ private final Queue<StoredCallback> queue = new ConcurrentLinkedQueue<>(); + private final Queue<StoredCallback> queue = new ConcurrentLinkedQueue<>();
+ private final AtomicLong current = new AtomicLong();
+ +
+ private CallbackManager() { + private CallbackManager() {
+ } + }
+ +
+ public long addCallback(final @NotNull ClickCallback<Audience> callback, final ClickCallback.@NotNull Options options) { + public UUID addCallback(final @NotNull ClickCallback<Audience> callback, final ClickCallback.@NotNull Options options) {
+ final long id = current.getAndIncrement(); + final UUID id = UUID.randomUUID();
+ this.queue.add(new StoredCallback(callback, options, id)); + this.queue.add(new StoredCallback(callback, options, id));
+ return id; + return id;
+ } + }
@ -1226,7 +1225,7 @@ index 0000000000000000000000000000000000000000..04bc65019b7abf331feb1b6c5475466b
+ } + }
+ } + }
+ +
+ public void runCallback(final @NotNull Audience audience, final long id) { + public void runCallback(final @NotNull Audience audience, final UUID id) {
+ final StoredCallback callback = this.callbacks.get(id); + final StoredCallback callback = this.callbacks.get(id);
+ if (callback != null && callback.valid()) { //TODO Message if expired/invalid? + if (callback != null && callback.valid()) { //TODO Message if expired/invalid?
+ callback.takeUse(); + callback.takeUse();
@ -1239,10 +1238,10 @@ index 0000000000000000000000000000000000000000..04bc65019b7abf331feb1b6c5475466b
+ private final long startedAt = System.nanoTime(); + private final long startedAt = System.nanoTime();
+ private final ClickCallback<Audience> callback; + private final ClickCallback<Audience> callback;
+ private final long lifetime; + private final long lifetime;
+ private final long id; + private final UUID id;
+ private int remainingUses; + private int remainingUses;
+ +
+ private StoredCallback(final @NotNull ClickCallback<Audience> callback, final ClickCallback.@NotNull Options options, final long id) { + private StoredCallback(final @NotNull ClickCallback<Audience> callback, final ClickCallback.@NotNull Options options, final UUID id) {
+ this.callback = callback; + this.callback = callback;
+ this.lifetime = options.lifetime().toNanos(); + this.lifetime = options.lifetime().toNanos();
+ this.remainingUses = options.uses(); + this.remainingUses = options.uses();
@ -1267,7 +1266,7 @@ index 0000000000000000000000000000000000000000..04bc65019b7abf331feb1b6c5475466b
+ return hasRemainingUses() && !expired(); + return hasRemainingUses() && !expired();
+ } + }
+ +
+ public long id() { + public UUID id() {
+ return this.id; + return this.id;
+ } + }
+ } + }
@ -3928,7 +3927,7 @@ index 4d341db0daecb5f6ff1f0a1a9238f9dedb4b50d1..27cf140f8a7715caec5637d7b487720c
private final Player.Spigot spigot = new Player.Spigot() private final Player.Spigot spigot = new Player.Spigot()
{ {
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftTextDisplay.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftTextDisplay.java diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftTextDisplay.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftTextDisplay.java
index 1bed95ee1af2354b710361f737bffa2ff9ec4f97..1b4277655bded7e560ef4ba8477dd0d701a50cc0 100644 index b7f54a7d90a2a34e641f152eb8f5c3c62033e511..2d04443a9efd6e4b5eb1e360a727d8492323724b 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftTextDisplay.java --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftTextDisplay.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftTextDisplay.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftTextDisplay.java
@@ -38,6 +38,17 @@ public class CraftTextDisplay extends CraftDisplay implements TextDisplay { @@ -38,6 +38,17 @@ public class CraftTextDisplay extends CraftDisplay implements TextDisplay {
@ -3950,7 +3949,7 @@ index 1bed95ee1af2354b710361f737bffa2ff9ec4f97..1b4277655bded7e560ef4ba8477dd0d7
@Override @Override
public int getLineWidth() { public int getLineWidth() {
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
index 131be7a463e74178622482a74bd42917d775495c..15f16844bf151f8244d7333faec215f1092e2147 100644 index f9499aa8fc5443b54617c8e84011af5d0325bd45..2c3518ea974be5fce424093e9b710c853fd52a7a 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java --- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -820,9 +820,9 @@ public class CraftEventFactory { @@ -820,9 +820,9 @@ public class CraftEventFactory {

View File

@ -290,23 +290,19 @@ index 0000000000000000000000000000000000000000..7e9e0ff8639be135bf8575e375cbada5
+} +}
diff --git a/src/main/java/io/papermc/paper/command/subcommands/CallbackCommand.java b/src/main/java/io/papermc/paper/command/subcommands/CallbackCommand.java diff --git a/src/main/java/io/papermc/paper/command/subcommands/CallbackCommand.java b/src/main/java/io/papermc/paper/command/subcommands/CallbackCommand.java
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..62e121e703716b38162020d34467855a02443d88 index 0000000000000000000000000000000000000000..c5111eef4ba85ed96f7496b7db6954106fa05053
--- /dev/null --- /dev/null
+++ b/src/main/java/io/papermc/paper/command/subcommands/CallbackCommand.java +++ b/src/main/java/io/papermc/paper/command/subcommands/CallbackCommand.java
@@ -0,0 +1,37 @@ @@ -0,0 +1,33 @@
+package io.papermc.paper.command.subcommands; +package io.papermc.paper.command.subcommands;
+ +
+import io.papermc.paper.adventure.providers.ClickCallbackProviderImpl; +import io.papermc.paper.adventure.providers.ClickCallbackProviderImpl;
+import io.papermc.paper.command.PaperSubcommand; +import io.papermc.paper.command.PaperSubcommand;
+import net.kyori.adventure.text.Component; +import java.util.UUID;
+import net.kyori.adventure.text.event.ClickCallback;
+import net.kyori.adventure.text.event.ClickEvent;
+import org.bukkit.command.CommandSender; +import org.bukkit.command.CommandSender;
+import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.framework.qual.DefaultQualifier; +import org.checkerframework.framework.qual.DefaultQualifier;
+ +
+import java.time.Duration;
+
+@DefaultQualifier(NonNull.class) +@DefaultQualifier(NonNull.class)
+public final class CallbackCommand implements PaperSubcommand { +public final class CallbackCommand implements PaperSubcommand {
+ @Override + @Override
@ -315,10 +311,10 @@ index 0000000000000000000000000000000000000000..62e121e703716b38162020d34467855a
+ return false; + return false;
+ } + }
+ +
+ final long id; + final UUID id;
+ try { + try {
+ id = Long.parseLong(args[0]); + id = UUID.fromString(args[0]);
+ } catch (final NumberFormatException ignored) { + } catch (final IllegalArgumentException ignored) {
+ return false; + return false;
+ } + }
+ +