3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-16 21:10:30 +01:00

Implement click callback provider (#1029)

Dieser Commit ist enthalten in:
Andrew Steinborn 2024-01-20 16:58:00 -05:00 committet von GitHub
Ursprung 46d018c0f2
Commit 953ab6d8e7
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: B5690EEEBB952194
9 geänderte Dateien mit 237 neuen und 1 gelöschten Zeilen

Datei anzeigen

@ -43,6 +43,9 @@ dependencies {
api(libs.brigadier) api(libs.brigadier)
api(libs.bundles.configurate4) api(libs.bundles.configurate4)
api(libs.caffeine) api(libs.caffeine)
compileOnly(libs.auto.service.annotations)
annotationProcessor(libs.auto.service)
} }
tasks { tasks {

Datei anzeigen

@ -7,6 +7,7 @@
package com.velocitypowered.api.plugin.ap; package com.velocitypowered.api.plugin.ap;
import com.google.auto.service.AutoService;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.plugin.Plugin;
import java.io.BufferedWriter; import java.io.BufferedWriter;
@ -16,6 +17,7 @@ import java.util.Objects;
import java.util.Set; import java.util.Set;
import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes; import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion; import javax.lang.model.SourceVersion;
@ -30,6 +32,7 @@ import javax.tools.StandardLocation;
/** /**
* Annotation processor for Velocity. * Annotation processor for Velocity.
*/ */
@AutoService(Processor.class)
@SupportedAnnotationTypes({"com.velocitypowered.api.plugin.Plugin"}) @SupportedAnnotationTypes({"com.velocitypowered.api.plugin.Plugin"})
public class PluginAnnotationProcessor extends AbstractProcessor { public class PluginAnnotationProcessor extends AbstractProcessor {

Datei anzeigen

@ -1 +0,0 @@
com.velocitypowered.api.plugin.ap.PluginAnnotationProcessor

Datei anzeigen

@ -14,6 +14,8 @@ spotless = "com.diffplug.spotless:6.12.0"
adventure-bom = "net.kyori:adventure-bom:4.15.0" adventure-bom = "net.kyori:adventure-bom:4.15.0"
adventure-facet = "net.kyori:adventure-platform-facet:4.3.0" adventure-facet = "net.kyori:adventure-platform-facet:4.3.0"
asm = "org.ow2.asm:asm:9.5" asm = "org.ow2.asm:asm:9.5"
auto-service = "com.google.auto.service:auto-service:1.0.1"
auto-service-annotations = "com.google.auto.service:auto-service-annotations:1.0.1"
brigadier = "com.velocitypowered:velocity-brigadier:1.0.0-SNAPSHOT" brigadier = "com.velocitypowered:velocity-brigadier:1.0.0-SNAPSHOT"
bstats = "org.bstats:bstats-base:3.0.1" bstats = "org.bstats:bstats-base:3.0.1"
caffeine = "com.github.ben-manes.caffeine:caffeine:3.1.5" caffeine = "com.github.ben-manes.caffeine:caffeine:3.1.5"

Datei anzeigen

@ -126,5 +126,8 @@ dependencies {
implementation(libs.asm) implementation(libs.asm)
implementation(libs.bundles.flare) implementation(libs.bundles.flare)
compileOnly(libs.spotbugs.annotations) compileOnly(libs.spotbugs.annotations)
compileOnly(libs.auto.service.annotations)
testImplementation(libs.mockito) testImplementation(libs.mockito)
annotationProcessor(libs.auto.service)
} }

Datei anzeigen

@ -0,0 +1,103 @@
/*
* Copyright (C) 2023 Velocity Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.velocitypowered.proxy.adventure;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Expiry;
import com.github.benmanes.caffeine.cache.Scheduler;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.event.ClickCallback;
import org.checkerframework.checker.index.qual.NonNegative;
/**
* Click callback manager.
*/
public class ClickCallbackManager {
public static final ClickCallbackManager INSTANCE = new ClickCallbackManager();
static final String COMMAND = "/velocity callback ";
private final Cache<UUID, RegisteredCallback> registrations = Caffeine.newBuilder()
.expireAfter(new Expiry<UUID, RegisteredCallback>() {
@Override
public long expireAfterCreate(UUID key, RegisteredCallback value, long currentTime) {
return value.duration().toNanos();
}
@Override
public long expireAfterUpdate(UUID key, RegisteredCallback value, long currentTime,
@NonNegative long currentDuration) {
return currentDuration;
}
@Override
public long expireAfterRead(UUID key, RegisteredCallback value, long currentTime,
@NonNegative long currentDuration) {
final AtomicInteger remainingUses = value.remainingUses();
if (remainingUses != null && remainingUses.get() <= 0) {
return 0;
}
return currentDuration;
}
})
.scheduler(Scheduler.systemScheduler())
.build();
private ClickCallbackManager() {
}
/**
* Run a callback.
*
* @param audience the audience
* @param id the callback's ID
* @return {@code true} if the callback was run, {@code false} if not
*/
public boolean runCallback(final Audience audience, final UUID id) {
final RegisteredCallback callback = this.registrations.getIfPresent(id);
if (callback != null && callback.tryUse()) {
callback.callback().accept(audience);
return true;
}
return false;
}
/**
* Registers a click callback.
*
* @param callback the callback to register
* @param options associated options
* @return the callback ID
*/
public UUID register(
final ClickCallback<Audience> callback,
final ClickCallback.Options options
) {
final UUID id = UUID.randomUUID();
final RegisteredCallback registration = new RegisteredCallback(
options.lifetime(),
options.uses(),
callback
);
this.registrations.put(id, registration);
return id;
}
}

Datei anzeigen

@ -0,0 +1,40 @@
/*
* Copyright (C) 2023 Velocity Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.velocitypowered.proxy.adventure;
import com.google.auto.service.AutoService;
import java.util.UUID;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.event.ClickCallback;
import net.kyori.adventure.text.event.ClickEvent;
/**
* Implementation of {@link ClickCallback.Provider}.
*/
@AutoService(ClickCallback.Provider.class)
@SuppressWarnings("UnstableApiUsage") // permitted provider
public class ClickCallbackProviderImpl implements ClickCallback.Provider {
@Override
public ClickEvent create(
final ClickCallback<Audience> callback,
final ClickCallback.Options options
) {
final UUID id = ClickCallbackManager.INSTANCE.register(callback, options);
return ClickEvent.runCommand(ClickCallbackManager.COMMAND + id);
}
}

Datei anzeigen

@ -0,0 +1,53 @@
/*
* Copyright (C) 2023 Velocity Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.velocitypowered.proxy.adventure;
import java.time.Duration;
import java.util.concurrent.atomic.AtomicInteger;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.event.ClickCallback;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
record RegisteredCallback(
Duration duration,
@Nullable AtomicInteger remainingUses,
ClickCallback<Audience> callback
) {
RegisteredCallback(
final Duration duration,
final int maxUses,
final ClickCallback<Audience> callback
) {
this(
duration,
maxUses == ClickCallback.UNLIMITED_USES
? null
: new AtomicInteger(maxUses),
callback
);
}
boolean tryUse() {
if (this.remainingUses != null) {
return this.remainingUses.decrementAndGet() >= 0;
}
return true;
}
}

Datei anzeigen

@ -31,6 +31,7 @@ import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.util.ProxyVersion; import com.velocitypowered.api.util.ProxyVersion;
import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.VelocityServer;
import com.velocitypowered.proxy.adventure.ClickCallbackManager;
import com.velocitypowered.proxy.util.InformationUtils; import com.velocitypowered.proxy.util.InformationUtils;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.IOException; import java.io.IOException;
@ -49,6 +50,7 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
@ -93,6 +95,7 @@ public class VelocityCommand implements SimpleCommand {
.put("reload", new Reload(server)) .put("reload", new Reload(server))
.put("dump", new Dump(server)) .put("dump", new Dump(server))
.put("heap", new Heap()) .put("heap", new Heap())
.put("callback", new Callback())
.build(); .build();
} }
@ -485,4 +488,31 @@ public class VelocityCommand implements SimpleCommand {
} }
} }
/**
* Callback SubCommand.
*/
public static class Callback implements SubCommand {
@Override
public void execute(final CommandSource source, final String @NonNull [] args) {
if (args.length != 1) {
return;
}
final UUID id;
try {
id = UUID.fromString(args[0]);
} catch (final IllegalArgumentException ignored) {
return;
}
ClickCallbackManager.INSTANCE.runCallback(source, id);
}
@Override
public boolean hasPermission(final CommandSource source, final String @NonNull [] args) {
return true;
}
}
} }