Skip to content

Commit

Permalink
Can join server without command packet from server
Browse files Browse the repository at this point in the history
  • Loading branch information
adde0109 committed Mar 30, 2023
1 parent 51c887b commit 7613b68
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
30 changes: 26 additions & 4 deletions src/main/java/org/adde0109/ambassador/AmbassadorConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ public class AmbassadorConfig {
@Expose
private int serverSwitchCancellationTime = 120;

@Expose
private boolean silenceWarnings = false;

private net.kyori.adventure.text.@MonotonicNonNull Component messageAsAsComponent;

private AmbassadorConfig(int resetTimeout, String kickResetMessage, int serverSwitchCancellationTime) {
private AmbassadorConfig(int resetTimeout, String kickResetMessage, int serverSwitchCancellationTime, boolean silenceWarnings) {
this.resetTimeout = resetTimeout;
this.disconnectResetMessage = kickResetMessage;
this.serverSwitchCancellationTime = serverSwitchCancellationTime;
this.silenceWarnings = silenceWarnings;
};

public void validate() {
final int connectionTimeout = Ambassador.getInstance().server.getConfiguration().getConnectTimeout();
if (resetTimeout >= connectionTimeout) {
throw new InvalidValueException("'reset-timeout' can't be more than nor equal to 'connection-timeout': reset-timeout=" + resetTimeout + " connection-timeout=" + connectionTimeout);
if (resetTimeout > connectionTimeout) {
throw new InvalidValueException("'reset-timeout' can't be more than to 'connection-timeout': reset-timeout=" + resetTimeout + " connection-timeout=" + connectionTimeout);
}
if (resetTimeout <= 0) {
throw new InvalidValueException("'reset-timeout' can't be less than nor equal to zero: reset-timeout=" + resetTimeout);
Expand All @@ -57,11 +61,25 @@ public static AmbassadorConfig read(Path path) {
.build();
config.load();

double configVersion;
try {
configVersion = Double.parseDouble(config.getOrElse("config-version", "1.0"));
} catch (NumberFormatException e) {
configVersion = 1.0;
}

if (configVersion < 1.1) {
config.set("silence-warnings", false);
config.set("config-version", "1.1");
}

int resetTimeout = config.getIntOrElse("reset-timeout", 3000);
String kickResetMessage = config.getOrElse("disconnect-reset-message", "Please reconnect");
int serverSwitchCancellationTime = config.getIntOrElse("server-switch-cancellation-time", 120000);

return new AmbassadorConfig(resetTimeout, kickResetMessage, serverSwitchCancellationTime);
boolean silenceWarnings = config.getOrElse("silence-warnings", false);

return new AmbassadorConfig(resetTimeout, kickResetMessage, serverSwitchCancellationTime, silenceWarnings);
}

public int getResetTimeout() {
Expand All @@ -82,4 +100,8 @@ public net.kyori.adventure.text.Component getDisconnectResetMessage() {
public int getServerSwitchCancellationTime() {
return serverSwitchCancellationTime;
}

public boolean isSilenceWarnings() {
return silenceWarnings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@
import com.velocitypowered.proxy.protocol.StateRegistry;
import com.velocitypowered.proxy.protocol.netty.MinecraftDecoder;
import com.velocitypowered.proxy.protocol.packet.AvailableCommands;
import com.velocitypowered.proxy.protocol.packet.Disconnect;
import com.velocitypowered.proxy.util.except.QuietRuntimeException;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.CorruptedFrameException;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.adde0109.ambassador.Ambassador;
import org.jetbrains.annotations.NotNull;

public class CommandDecoderErrorCatcher extends ChannelInboundHandlerAdapter {

private final StateRegistry.PacketRegistry.ProtocolRegistry registry;

private final ConnectedPlayer player;
private boolean sentWarning = false;

public CommandDecoderErrorCatcher(ProtocolVersion protocolVersion, ConnectedPlayer player) {
this.registry = StateRegistry.PLAY.getProtocolRegistry(ProtocolUtils.Direction.CLIENTBOUND, protocolVersion);
Expand All @@ -46,10 +48,13 @@ public void channelRead(@NotNull ChannelHandlerContext ctx, @NotNull Object msg)
((MinecraftDecoder) ctx.pipeline().get(Connections.MINECRAFT_DECODER)).channelRead(ctx, msg);
} catch (QuietRuntimeException | CorruptedFrameException e) {
RegisteredServer server = player.getConnectedServer().getServer();
player.handleConnectionException(server,
Disconnect.create(Component.text("Ambassador: Unsupported command argument type detected! " +
"Please install Proxy-Compatible-Forge mod on this backend server."),
player.getProtocolVersion()),true);
if (!Ambassador.getInstance().config.isSilenceWarnings() && !sentWarning) {
player.sendMessage(Component.text("[Ambassador Warning]: Unsupported command argument type detected! " +
"Please install Proxy-Compatible-Forge mod on this backend server to have access to commands. " +
"This message can be silenced in the ambassador.toml config file.", NamedTextColor.YELLOW));
sentWarning = true;
}

}

} else {
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/default-ambassador.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Do not change this
config-version = "1.0"
config-version = "1.1"

# How long to wait for the client to reset before disconnecting (In milliseconds)
reset-timeout = 1000
Expand All @@ -8,3 +8,4 @@ reset-timeout = 1000
disconnect-reset-message = "&6Please reconnect"
# How much time the player has to reconnect before canceling the server switch. (In seconds)
server-switch-cancellation-time = 120
silence-warnings = false

0 comments on commit 7613b68

Please sign in to comment.