mirror of
https://github.com/EssentialsX/Essentials.git
synced 2024-12-22 09:08:01 +01:00
Use Paper command forwarding API (#4794)
Uses API added in PaperMC/Paper@9940bca, when available.
This commit is contained in:
parent
c6fe160b47
commit
41f5cc3175
@ -6,11 +6,12 @@ import net.ess3.provider.providers.BukkitSenderProvider;
|
|||||||
import net.ess3.provider.providers.PaperCommandSender;
|
import net.ess3.provider.providers.PaperCommandSender;
|
||||||
import net.essentialsx.discord.JDADiscordService;
|
import net.essentialsx.discord.JDADiscordService;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.ConsoleCommandSender;
|
import org.bukkit.command.ConsoleCommandSender;
|
||||||
import org.bukkit.scheduler.BukkitTask;
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
|
|
||||||
public class DiscordCommandSender {
|
public class DiscordCommandSender {
|
||||||
private final BukkitSenderProvider sender;
|
private final CommandSender sender;
|
||||||
private BukkitTask task;
|
private BukkitTask task;
|
||||||
private String responseBuffer = "";
|
private String responseBuffer = "";
|
||||||
private long lastTime = System.currentTimeMillis();
|
private long lastTime = System.currentTimeMillis();
|
||||||
@ -20,7 +21,7 @@ public class DiscordCommandSender {
|
|||||||
responseBuffer = responseBuffer + (responseBuffer.isEmpty() ? "" : "\n") + MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripFormat(message));
|
responseBuffer = responseBuffer + (responseBuffer.isEmpty() ? "" : "\n") + MessageUtil.sanitizeDiscordMarkdown(FormatUtil.stripFormat(message));
|
||||||
lastTime = System.currentTimeMillis();
|
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(), () -> {
|
task = Bukkit.getScheduler().runTaskTimerAsynchronously(jda.getPlugin(), () -> {
|
||||||
if (!responseBuffer.isEmpty() && System.currentTimeMillis() - lastTime >= 1000) {
|
if (!responseBuffer.isEmpty() && System.currentTimeMillis() - lastTime >= 1000) {
|
||||||
@ -36,11 +37,21 @@ public class DiscordCommandSender {
|
|||||||
}, 0, 20);
|
}, 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 {
|
public interface CmdCallback {
|
||||||
void onMessage(String message);
|
void onMessage(String message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BukkitSenderProvider getSender() {
|
public CommandSender getSender() {
|
||||||
return sender;
|
return sender;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,49 @@
|
|||||||
package net.ess3.provider.providers;
|
package net.ess3.provider.providers;
|
||||||
|
|
||||||
|
import io.papermc.paper.text.PaperComponents;
|
||||||
import net.kyori.adventure.audience.MessageType;
|
import net.kyori.adventure.audience.MessageType;
|
||||||
import net.kyori.adventure.identity.Identified;
|
import net.kyori.adventure.identity.Identified;
|
||||||
import net.kyori.adventure.identity.Identity;
|
import net.kyori.adventure.identity.Identity;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.ComponentLike;
|
import net.kyori.adventure.text.ComponentLike;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.ConsoleCommandSender;
|
import org.bukkit.command.ConsoleCommandSender;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class PaperCommandSender extends BukkitSenderProvider {
|
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) {
|
public PaperCommandSender(ConsoleCommandSender base, MessageHook hook) {
|
||||||
super(base, 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
|
@Override
|
||||||
public void sendMessage(Identity identity, Component message, MessageType type) {
|
public void sendMessage(@NotNull Identity identity, @NotNull Component message, @NotNull MessageType type) {
|
||||||
sendDumbComponent(message);
|
sendDumbComponent(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,57 +53,57 @@ public class PaperCommandSender extends BukkitSenderProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendMessage(Identified source, ComponentLike message) {
|
public void sendMessage(@NotNull Identified source, ComponentLike message) {
|
||||||
sendDumbComponent(message.asComponent());
|
sendDumbComponent(message.asComponent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendMessage(Identity source, ComponentLike message) {
|
public void sendMessage(@NotNull Identity source, ComponentLike message) {
|
||||||
sendDumbComponent(message.asComponent());
|
sendDumbComponent(message.asComponent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendMessage(Component message) {
|
public void sendMessage(@NotNull Component message) {
|
||||||
sendDumbComponent(message);
|
sendDumbComponent(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendMessage(Identified source, Component message) {
|
public void sendMessage(@NotNull Identified source, @NotNull Component message) {
|
||||||
sendDumbComponent(message);
|
sendDumbComponent(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendMessage(Identity source, Component message) {
|
public void sendMessage(@NotNull Identity source, @NotNull Component message) {
|
||||||
sendDumbComponent(message);
|
sendDumbComponent(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendMessage(ComponentLike message, MessageType type) {
|
public void sendMessage(ComponentLike message, @NotNull MessageType type) {
|
||||||
sendDumbComponent(message.asComponent());
|
sendDumbComponent(message.asComponent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendMessage(Identified source, ComponentLike message, MessageType type) {
|
public void sendMessage(@NotNull Identified source, ComponentLike message, @NotNull MessageType type) {
|
||||||
sendDumbComponent(message.asComponent());
|
sendDumbComponent(message.asComponent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendMessage(Identity source, ComponentLike message, MessageType type) {
|
public void sendMessage(@NotNull Identity source, ComponentLike message, @NotNull MessageType type) {
|
||||||
sendDumbComponent(message.asComponent());
|
sendDumbComponent(message.asComponent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendMessage(Component message, MessageType type) {
|
public void sendMessage(@NotNull Component message, @NotNull MessageType type) {
|
||||||
sendDumbComponent(message);
|
sendDumbComponent(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendMessage(Identified source, Component message, MessageType type) {
|
public void sendMessage(@NotNull Identified source, @NotNull Component message, @NotNull MessageType type) {
|
||||||
sendDumbComponent(message);
|
sendDumbComponent(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendDumbComponent(Component message) {
|
public void sendDumbComponent(Component message) {
|
||||||
sendMessage(Bukkit.getUnsafe().legacyComponentSerializer().serialize(message));
|
sendMessage(PaperComponents.legacySectionSerializer().serialize(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user