Implement click callback provider (#1029)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -31,6 +31,7 @@ import com.velocitypowered.api.proxy.ProxyServer;
|
||||
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||
import com.velocitypowered.api.util.ProxyVersion;
|
||||
import com.velocitypowered.proxy.VelocityServer;
|
||||
import com.velocitypowered.proxy.adventure.ClickCallbackManager;
|
||||
import com.velocitypowered.proxy.util.InformationUtils;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
@@ -49,6 +50,7 @@ import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@@ -93,6 +95,7 @@ public class VelocityCommand implements SimpleCommand {
|
||||
.put("reload", new Reload(server))
|
||||
.put("dump", new Dump(server))
|
||||
.put("heap", new Heap())
|
||||
.put("callback", new Callback())
|
||||
.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user