13
0
geforkt von Mirrors/Velocity

Run all commands on a separate thread pool.

Dieser Commit ist enthalten in:
Andrew Steinborn 2020-05-25 13:09:04 -04:00
Ursprung ec1fc3944d
Commit 0cb4c02107
2 geänderte Dateien mit 34 neuen und 26 gelöschten Zeilen

Datei anzeigen

@ -11,7 +11,6 @@ import com.velocitypowered.api.plugin.PluginDescription;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.util.ProxyVersion; import com.velocitypowered.api.util.ProxyVersion;
import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.VelocityServer;
import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -20,7 +19,6 @@ import java.util.stream.Collectors;
import net.kyori.text.TextComponent; import net.kyori.text.TextComponent;
import net.kyori.text.event.ClickEvent; import net.kyori.text.event.ClickEvent;
import net.kyori.text.event.HoverEvent; import net.kyori.text.event.HoverEvent;
import net.kyori.text.event.HoverEvent.Action;
import net.kyori.text.format.TextColor; import net.kyori.text.format.TextColor;
import net.kyori.text.format.TextDecoration; import net.kyori.text.format.TextDecoration;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;

Datei anzeigen

@ -4,7 +4,7 @@ import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_13;
import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_8; import static com.velocitypowered.api.network.ProtocolVersion.MINECRAFT_1_8;
import static com.velocitypowered.proxy.protocol.util.PluginMessageUtil.constructChannelsPacket; import static com.velocitypowered.proxy.protocol.util.PluginMessageUtil.constructChannelsPacket;
import com.velocitypowered.api.event.command.CommandExecuteEvent; import com.velocitypowered.api.event.command.CommandExecuteEvent.CommandResult;
import com.velocitypowered.api.event.connection.PluginMessageEvent; import com.velocitypowered.api.event.connection.PluginMessageEvent;
import com.velocitypowered.api.event.player.PlayerChatEvent; import com.velocitypowered.api.event.player.PlayerChatEvent;
import com.velocitypowered.api.event.player.PlayerResourcePackStatusEvent; import com.velocitypowered.api.event.player.PlayerResourcePackStatusEvent;
@ -42,6 +42,7 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Queue; import java.util.Queue;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import net.kyori.text.TextComponent; import net.kyori.text.TextComponent;
import net.kyori.text.format.TextColor; import net.kyori.text.format.TextColor;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
@ -124,30 +125,18 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
String msg = packet.getMessage(); String msg = packet.getMessage();
if (msg.startsWith("/")) { if (msg.startsWith("/")) {
String originalCommand = msg.substring(1);
server.getCommandManager().callCommandEvent(player, msg.substring(1)) server.getCommandManager().callCommandEvent(player, msg.substring(1))
.thenAcceptAsync(event -> { .thenComposeAsync(event -> processCommandExecuteResult(originalCommand,
CommandExecuteEvent.CommandResult commandResult = event.getResult(); event.getResult()))
Optional<String> eventCommand = event.getResult().getCommand(); .exceptionally(e -> {
String command = eventCommand.orElse(event.getCommand()); logger.info("Exception occurred while running command for {}",
if (commandResult.isForwardToServer()) { player.getUsername(), e);
smc.write(Chat.createServerbound("/" + command));
return;
}
if (commandResult.isAllowed()) {
try {
if (!server.getCommandManager().executeImmediately(player, command)) {
smc.write(Chat.createServerbound("/" + command));
}
} catch (Exception e) {
logger.info("Exception occurred while running command for {}", player.getUsername(),
e);
player.sendMessage( player.sendMessage(
TextComponent.of("An error occurred while running this command.", TextComponent.of("An error occurred while running this command.",
TextColor.RED)); TextColor.RED));
} return null;
} });
}, smc.eventLoop());
} else { } else {
PlayerChatEvent event = new PlayerChatEvent(player, msg); PlayerChatEvent event = new PlayerChatEvent(player, msg);
server.getEventManager().fire(event) server.getEventManager().fire(event)
@ -166,6 +155,27 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
return true; return true;
} }
private CompletableFuture<Void> processCommandExecuteResult(String originalCommand,
CommandResult result) {
if (result == CommandResult.denied()) {
return CompletableFuture.completedFuture(null);
}
MinecraftConnection smc = player.ensureAndGetCurrentServer().ensureConnected();
String commandToRun = result.getCommand().orElse(originalCommand);
if (result.isForwardToServer()) {
return CompletableFuture.runAsync(() -> smc.write(Chat.createServerbound("/" +
commandToRun)), smc.eventLoop());
} else {
return server.getCommandManager().executeImmediatelyAsync(player, commandToRun)
.thenAcceptAsync(hasRun -> {
if (!hasRun) {
smc.write(Chat.createServerbound("/" + commandToRun));
}
}, smc.eventLoop());
}
}
@Override @Override
public boolean handle(TabCompleteRequest packet) { public boolean handle(TabCompleteRequest packet) {
boolean isCommand = !packet.isAssumeCommand() && packet.getCommand().startsWith("/"); boolean isCommand = !packet.isAssumeCommand() && packet.getCommand().startsWith("/");