Use Paper command forwarding API (#4794)

Uses API added in PaperMC/Paper@9940bca, when available.
This commit is contained in:
Josh Roy 2022-02-06 12:56:13 -05:00 committed by GitHub
parent c6fe160b47
commit 41f5cc3175
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 15 deletions

View file

@ -6,11 +6,12 @@ import net.ess3.provider.providers.BukkitSenderProvider;
import net.ess3.provider.providers.PaperCommandSender;
import net.essentialsx.discord.JDADiscordService;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.scheduler.BukkitTask;
public class DiscordCommandSender {
private final BukkitSenderProvider sender;
private final CommandSender sender;
private BukkitTask task;
private String responseBuffer = "";
private long lastTime = System.currentTimeMillis();
@ -20,7 +21,7 @@ public class DiscordCommandSender {
responseBuffer = responseBuffer + (responseBuffer.isEmpty() ? "" : "\n") + MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripFormat(message));
lastTime = System.currentTimeMillis();
};
this.sender = (VersionUtil.isPaper() && VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_16_5_R01)) ? new PaperCommandSender(sender, hook) : new BukkitSenderProvider(sender, hook);
this.sender = getCustomSender(sender, hook);
task = Bukkit.getScheduler().runTaskTimerAsynchronously(jda.getPlugin(), () -> {
if (!responseBuffer.isEmpty() && System.currentTimeMillis() - lastTime >= 1000) {
@ -36,11 +37,21 @@ public class DiscordCommandSender {
}, 0, 20);
}
private CommandSender getCustomSender(final ConsoleCommandSender consoleSender, final BukkitSenderProvider.MessageHook hook) {
if (VersionUtil.isPaper() && VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_16_5_R01)) {
if (PaperCommandSender.forwardingSenderAvailable()) {
return PaperCommandSender.createCommandSender(hook::sendMessage);
}
return new PaperCommandSender(consoleSender, hook);
}
return new BukkitSenderProvider(consoleSender, hook);
}
public interface CmdCallback {
void onMessage(String message);
}
public BukkitSenderProvider getSender() {
public CommandSender getSender() {
return sender;
}
}

View file

@ -1,21 +1,49 @@
package net.ess3.provider.providers;
import io.papermc.paper.text.PaperComponents;
import net.kyori.adventure.audience.MessageType;
import net.kyori.adventure.identity.Identified;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.jetbrains.annotations.NotNull;
import java.util.function.Consumer;
public class PaperCommandSender extends BukkitSenderProvider {
private static final boolean FORWARDING_SENDER_AVAILABLE;
static {
boolean jmpHappy;
try {
Bukkit.createCommandSender(component -> {});
jmpHappy = true;
} catch (NoSuchMethodError ignored) {
jmpHappy = false;
}
FORWARDING_SENDER_AVAILABLE = jmpHappy;
}
public PaperCommandSender(ConsoleCommandSender base, MessageHook hook) {
super(base, hook);
}
public static boolean forwardingSenderAvailable() {
return FORWARDING_SENDER_AVAILABLE;
}
public static CommandSender createCommandSender(Consumer<String> consumer) {
if (!forwardingSenderAvailable()) {
return null;
}
return Bukkit.createCommandSender(component -> consumer.accept(PaperComponents.legacySectionSerializer().serialize(component)));
}
@Override
public void sendMessage(Identity identity, Component message, MessageType type) {
public void sendMessage(@NotNull Identity identity, @NotNull Component message, @NotNull MessageType type) {
sendDumbComponent(message);
}
@ -25,57 +53,57 @@ public class PaperCommandSender extends BukkitSenderProvider {
}
@Override
public void sendMessage(Identified source, ComponentLike message) {
public void sendMessage(@NotNull Identified source, ComponentLike message) {
sendDumbComponent(message.asComponent());
}
@Override
public void sendMessage(Identity source, ComponentLike message) {
public void sendMessage(@NotNull Identity source, ComponentLike message) {
sendDumbComponent(message.asComponent());
}
@Override
public void sendMessage(Component message) {
public void sendMessage(@NotNull Component message) {
sendDumbComponent(message);
}
@Override
public void sendMessage(Identified source, Component message) {
public void sendMessage(@NotNull Identified source, @NotNull Component message) {
sendDumbComponent(message);
}
@Override
public void sendMessage(Identity source, Component message) {
public void sendMessage(@NotNull Identity source, @NotNull Component message) {
sendDumbComponent(message);
}
@Override
public void sendMessage(ComponentLike message, MessageType type) {
public void sendMessage(ComponentLike message, @NotNull MessageType type) {
sendDumbComponent(message.asComponent());
}
@Override
public void sendMessage(Identified source, ComponentLike message, MessageType type) {
public void sendMessage(@NotNull Identified source, ComponentLike message, @NotNull MessageType type) {
sendDumbComponent(message.asComponent());
}
@Override
public void sendMessage(Identity source, ComponentLike message, MessageType type) {
public void sendMessage(@NotNull Identity source, ComponentLike message, @NotNull MessageType type) {
sendDumbComponent(message.asComponent());
}
@Override
public void sendMessage(Component message, MessageType type) {
public void sendMessage(@NotNull Component message, @NotNull MessageType type) {
sendDumbComponent(message);
}
@Override
public void sendMessage(Identified source, Component message, MessageType type) {
public void sendMessage(@NotNull Identified source, @NotNull Component message, @NotNull MessageType type) {
sendDumbComponent(message);
}
public void sendDumbComponent(Component message) {
sendMessage(Bukkit.getUnsafe().legacyComponentSerializer().serialize(message));
sendMessage(PaperComponents.legacySectionSerializer().serialize(message));
}
@Override