From d8a4261a4d941574c70362963967a5efa3103332 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Sun, 19 Mar 2023 11:25:45 -0700 Subject: [PATCH] Use random UUIDs instead of incrementing long for click callback ids (#9007) --- patches/server/Adventure.patch | 21 ++++++++++----------- patches/server/Paper-command.patch | 12 ++++-------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/patches/server/Adventure.patch b/patches/server/Adventure.patch index 2ea9cd3f11..9f37264d52 100644 --- a/patches/server/Adventure.patch +++ b/patches/server/Adventure.patch @@ -1177,8 +1177,9 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 @@ -0,0 +0,0 @@ +package io.papermc.paper.adventure.providers; + -+import it.unimi.dsi.fastutil.longs.Long2ObjectMap; -+import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; ++import java.util.HashMap; ++import java.util.Map; ++import java.util.UUID; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.text.event.ClickCallback; +import net.kyori.adventure.text.event.ClickEvent; @@ -1186,7 +1187,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; -+import java.util.concurrent.atomic.AtomicLong; + +@SuppressWarnings("UnstableApiUsage") // permitted provider +public class ClickCallbackProviderImpl implements ClickCallback.Provider { @@ -1200,15 +1200,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + + public static final class CallbackManager { + -+ private final Long2ObjectMap callbacks = new Long2ObjectOpenHashMap<>(); ++ private final Map callbacks = new HashMap<>(); + private final Queue queue = new ConcurrentLinkedQueue<>(); -+ private final AtomicLong current = new AtomicLong(); + + private CallbackManager() { + } + -+ public long addCallback(final @NotNull ClickCallback callback, final ClickCallback.@NotNull Options options) { -+ final long id = current.getAndIncrement(); ++ public UUID addCallback(final @NotNull ClickCallback callback, final ClickCallback.@NotNull Options options) { ++ final UUID id = UUID.randomUUID(); + this.queue.add(new StoredCallback(callback, options, id)); + return id; + } @@ -1226,7 +1225,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + } + } + -+ 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); + if (callback != null && callback.valid()) { //TODO Message if expired/invalid? + callback.takeUse(); @@ -1239,10 +1238,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + private final long startedAt = System.nanoTime(); + private final ClickCallback callback; + private final long lifetime; -+ private final long id; ++ private final UUID id; + private int remainingUses; + -+ private StoredCallback(final @NotNull ClickCallback callback, final ClickCallback.@NotNull Options options, final long id) { ++ private StoredCallback(final @NotNull ClickCallback callback, final ClickCallback.@NotNull Options options, final UUID id) { + this.callback = callback; + this.lifetime = options.lifetime().toNanos(); + this.remainingUses = options.uses(); @@ -1267,7 +1266,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + return hasRemainingUses() && !expired(); + } + -+ public long id() { ++ public UUID id() { + return this.id; + } + } diff --git a/patches/server/Paper-command.patch b/patches/server/Paper-command.patch index ea409ca871..0cafa9e63b 100644 --- a/patches/server/Paper-command.patch +++ b/patches/server/Paper-command.patch @@ -298,15 +298,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + +import io.papermc.paper.adventure.providers.ClickCallbackProviderImpl; +import io.papermc.paper.command.PaperSubcommand; -+import net.kyori.adventure.text.Component; -+import net.kyori.adventure.text.event.ClickCallback; -+import net.kyori.adventure.text.event.ClickEvent; ++import java.util.UUID; +import org.bukkit.command.CommandSender; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.framework.qual.DefaultQualifier; + -+import java.time.Duration; -+ +@DefaultQualifier(NonNull.class) +public final class CallbackCommand implements PaperSubcommand { + @Override @@ -315,10 +311,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + return false; + } + -+ final long id; ++ final UUID id; + try { -+ id = Long.parseLong(args[0]); -+ } catch (final NumberFormatException ignored) { ++ id = UUID.fromString(args[0]); ++ } catch (final IllegalArgumentException ignored) { + return false; + } +