3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-12-24 15:20:35 +01:00

Merge branch 'dev/1.1.0' into decode-multiple

# Conflicts:
#	proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java
Dieser Commit ist enthalten in:
Andrew Steinborn 2020-05-25 13:27:44 -04:00
Commit 62d3f61a77
3 geänderte Dateien mit 83 neuen und 64 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)); player.sendMessage(
return; TextComponent.of("An error occurred while running this command.",
} TextColor.RED));
if (commandResult.isAllowed()) { return null;
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(
TextComponent.of("An error occurred while running this command.",
TextColor.RED));
}
}
}, smc.eventLoop());
} else { } else {
PlayerChatEvent event = new PlayerChatEvent(player, msg); PlayerChatEvent event = new PlayerChatEvent(player, msg);
server.getEventManager().fire(event) server.getEventManager().fire(event)
@ -481,6 +470,27 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler {
}, player.getConnection().eventLoop()); }, player.getConnection().eventLoop());
} }
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());
}
}
/** /**
* Immediately send any queued messages to the server. * Immediately send any queued messages to the server.
*/ */

Datei anzeigen

@ -1,5 +1,8 @@
package com.velocitypowered.proxy.connection.client; package com.velocitypowered.proxy.connection.client;
import static com.velocitypowered.proxy.connection.util.ConnectionRequestResults.plainResult;
import static java.util.concurrent.CompletableFuture.completedFuture;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.velocitypowered.api.event.connection.DisconnectEvent; import com.velocitypowered.api.event.connection.DisconnectEvent;
@ -33,7 +36,6 @@ import com.velocitypowered.proxy.connection.MinecraftConnectionAssociation;
import com.velocitypowered.proxy.connection.backend.VelocityServerConnection; import com.velocitypowered.proxy.connection.backend.VelocityServerConnection;
import com.velocitypowered.proxy.connection.forge.legacy.LegacyForgeConstants; import com.velocitypowered.proxy.connection.forge.legacy.LegacyForgeConstants;
import com.velocitypowered.proxy.connection.util.ConnectionMessages; import com.velocitypowered.proxy.connection.util.ConnectionMessages;
import com.velocitypowered.proxy.connection.util.ConnectionRequestResults;
import com.velocitypowered.proxy.connection.util.ConnectionRequestResults.Impl; import com.velocitypowered.proxy.connection.util.ConnectionRequestResults.Impl;
import com.velocitypowered.proxy.protocol.StateRegistry; import com.velocitypowered.proxy.protocol.StateRegistry;
import com.velocitypowered.proxy.protocol.packet.Chat; import com.velocitypowered.proxy.protocol.packet.Chat;
@ -711,8 +713,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
} }
private Optional<ConnectionRequestBuilder.Status> checkServer(RegisteredServer server) { private Optional<ConnectionRequestBuilder.Status> checkServer(RegisteredServer server) {
Preconditions Preconditions.checkArgument(server instanceof VelocityRegisteredServer,
.checkState(server instanceof VelocityRegisteredServer, "Not a valid Velocity server."); "Not a valid Velocity server.");
if (connectionInFlight != null || (connectedServer != null if (connectionInFlight != null || (connectedServer != null
&& !connectedServer.hasCompletedJoin())) { && !connectedServer.hasCompletedJoin())) {
return Optional.of(ConnectionRequestBuilder.Status.CONNECTION_IN_PROGRESS); return Optional.of(ConnectionRequestBuilder.Status.CONNECTION_IN_PROGRESS);
@ -723,49 +725,58 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player {
return Optional.empty(); return Optional.empty();
} }
private CompletableFuture<Optional<Status>> getInitialStatus() {
return CompletableFuture.supplyAsync(() -> checkServer(toConnect), connection.eventLoop());
}
private CompletableFuture<Impl> internalConnect() { private CompletableFuture<Impl> internalConnect() {
Optional<ConnectionRequestBuilder.Status> initialCheck = checkServer(toConnect); return this.getInitialStatus()
if (initialCheck.isPresent()) { .thenCompose(initialCheck -> {
return CompletableFuture if (initialCheck.isPresent()) {
.completedFuture(ConnectionRequestResults.plainResult(initialCheck.get(), toConnect)); return completedFuture(plainResult(initialCheck.get(), toConnect));
}
ServerPreConnectEvent event = new ServerPreConnectEvent(ConnectedPlayer.this,
toConnect);
return server.getEventManager().fire(event)
.thenComposeAsync(newEvent -> {
Optional<RegisteredServer> newDest = newEvent.getResult().getServer();
if (!newDest.isPresent()) {
return completedFuture(
plainResult(ConnectionRequestBuilder.Status.CONNECTION_CANCELLED, toConnect)
);
}
RegisteredServer realDestination = newDest.get();
Optional<ConnectionRequestBuilder.Status> check = checkServer(realDestination);
if (check.isPresent()) {
return completedFuture(plainResult(check.get(), realDestination));
}
VelocityRegisteredServer vrs = (VelocityRegisteredServer) realDestination;
VelocityServerConnection con = new VelocityServerConnection(vrs,
ConnectedPlayer.this, server);
connectionInFlight = con;
return con.connect().whenCompleteAsync((result, throwable) ->
this.cleanupIfRequired(con), connection.eventLoop());
}, connection.eventLoop());
});
}
private void cleanupIfRequired(VelocityServerConnection establishedConnection) {
if (establishedConnection == connectionInFlight) {
resetInFlightConnection();
} }
// Otherwise, initiate the connection.
ServerPreConnectEvent event = new ServerPreConnectEvent(ConnectedPlayer.this, toConnect);
return server.getEventManager().fire(event)
.thenComposeAsync(newEvent -> {
Optional<RegisteredServer> connectTo = newEvent.getResult().getServer();
if (!connectTo.isPresent()) {
return CompletableFuture.completedFuture(
ConnectionRequestResults
.plainResult(ConnectionRequestBuilder.Status.CONNECTION_CANCELLED, toConnect)
);
}
RegisteredServer rs = connectTo.get();
Optional<ConnectionRequestBuilder.Status> lastCheck = checkServer(rs);
if (lastCheck.isPresent()) {
return CompletableFuture
.completedFuture(ConnectionRequestResults.plainResult(lastCheck.get(), rs));
}
VelocityRegisteredServer vrs = (VelocityRegisteredServer) rs;
VelocityServerConnection con = new VelocityServerConnection(vrs, ConnectedPlayer.this,
server);
connectionInFlight = con;
return con.connect();
}, connection.eventLoop());
} }
@Override @Override
public CompletableFuture<Result> connect() { public CompletableFuture<Result> connect() {
return this.internalConnect() return this.internalConnect()
.whenCompleteAsync((status, throwable) -> { .whenCompleteAsync((status, throwable) -> {
if (status != null && !status.isSafe()) { if (status != null && !status.isSuccessful()) {
// If it's not safe to continue the connection we need to shut it down. if (!status.isSafe()) {
handleConnectionException(status.getAttemptedConnection(), throwable, true); handleConnectionException(status.getAttemptedConnection(), throwable, false);
} else if ((status != null && !status.isSuccessful())) { }
resetInFlightConnection();
} }
}, connection.eventLoop()) }, connection.eventLoop())
.thenApply(x -> x); .thenApply(x -> x);