Skip to content

Some updates to networking #642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: dev/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public abstract class CommandAPIConfig<Impl
boolean usePluginNamespace = false;
String namespace = null;

boolean errorOnFailedPacketSends;

/**
* Sets verbose output logging for the CommandAPI if true.
*
Expand Down Expand Up @@ -191,4 +193,15 @@ public Impl setNamespace(String namespace) {
*/
public abstract Impl usePluginNamespace();

/**
* Sets whether the CommandAPI should throw an exception if it cannot send a packet.
* If false, the problem will be logged as a warning instead.
*
* @param value Whether an exception should be thrown when a packet cannot be sent
* @return this CommandAPIConfig
*/
public Impl errorOnFailedPacketSends(boolean value) {
this.errorOnFailedPacketSends = value;
return instance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public class InternalConfig {
// The default command namespace
private final String namespace;

// Whether we should throw an exception when a packet cannot be sent
private final boolean errorOnFailedPacketSends;

/**
* Creates an {@link InternalConfig} from a {@link CommandAPIConfig}
*
Expand All @@ -74,6 +77,7 @@ public InternalConfig(CommandAPIConfig<?> config) {
this.nbtContainerClass = config.nbtContainerClass;
this.nbtContainerConstructor = config.nbtContainerConstructor;
this.namespace = config.namespace;
this.errorOnFailedPacketSends = config.errorOnFailedPacketSends;
}

/**
Expand Down Expand Up @@ -154,4 +158,11 @@ public void lateInitializeNBT(Class<?> nbtContainerClass, Function<Object, ?> nb
public String getNamespace() {
return namespace;
}

/**
* @return Whether an exception is thrown when a packet cannot be sent
*/
public boolean shouldErrorOnFailedPacketSends() {
return this.errorOnFailedPacketSends;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ else throw new IllegalStateException("Tried to handle " + packet + " with Handsh
*/
void handleSetVersionPacket(InputChannel sender, SetVersionPacket packet);


/**
* Handles a {@link ProtocolVersionTooOldPacket}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static UpdateRequirementsPacket deserialize(FriendlyByteBuffer ignored) {
public void write(FriendlyByteBuffer buffer, Object target, int protocolVersion) throws ProtocolVersionTooOldException {
if (protocolVersion == 0) {
throw ProtocolVersionTooOldException.whileSending(target, protocolVersion,
"CommandAPI version 10.0.0 or greater is required to receive UpdateRequirementsPacket"
"CommandAPI version 10.0.0 or greater is required to receive UpdateRequirementsPacket."
);
}
// Nothing to write
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ private void unregisterInternal(String commandName, boolean unregisterNamespaces

@Override
public BukkitCommandAPIMessenger setupMessenger() {
messenger = new BukkitCommandAPIMessenger(getConfiguration().getPlugin());
messenger = new BukkitCommandAPIMessenger(config.getPlugin());
return messenger;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package dev.jorel.commandapi.network;

import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPIBukkit;
import dev.jorel.commandapi.exceptions.ProtocolVersionTooOldException;
import dev.jorel.commandapi.network.packets.ProtocolVersionTooOldPacket;
import dev.jorel.commandapi.network.packets.SetVersionPacket;
import org.bukkit.entity.Player;

Expand All @@ -12,4 +15,17 @@ public class BukkitHandshakePacketHandler implements HandshakePacketHandler<Play
public void handleSetVersionPacket(Player sender, SetVersionPacket packet) {
CommandAPIBukkit.get().getMessenger().setProtocolVersion(sender, packet.protocolVersion());
}

@Override
public void handleProtocolVersionTooOldPacket(Player sender, ProtocolVersionTooOldPacket packet) {
try {
HandshakePacketHandler.super.handleProtocolVersionTooOldPacket(sender, packet);
} catch (ProtocolVersionTooOldException exception) {
if (CommandAPI.getConfiguration().shouldErrorOnFailedPacketSends()) {
throw exception;
} else {
CommandAPI.logWarning(exception.getMessage());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.jorel.commandapi.network;

import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPINetworkingMain;
import dev.jorel.commandapi.exceptions.ProtocolVersionTooOldException;
import dev.jorel.commandapi.network.packets.SetVersionPacket;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -99,6 +101,19 @@ public void onPluginMessageReceived(String channel, Player player, byte[] messag
messageReceived(protocol, player, message);
}

@Override
public void sendPacket(Player target, CommandAPIPacket packet) {
try {
super.sendPacket(target, packet);
} catch (ProtocolVersionTooOldException exception) {
if (CommandAPI.getConfiguration().shouldErrorOnFailedPacketSends()) {
throw exception;
} else {
CommandAPI.logWarning(exception.getMessage());
}
}
}

@Override
public void sendRawBytes(CommandAPIProtocol protocol, Player target, byte[] bytes) {
target.sendPluginMessage(this.plugin, protocol.getChannelIdentifier(), bytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public void onLoad() {
.dispatcherFile(fileConfig.getBoolean("create-dispatcher-json") ? new File(getDataFolder(), "command_registration.json") : null)
.shouldHookPaperReload(fileConfig.getBoolean("hook-paper-reload"))
.skipReloadDatapacks(fileConfig.getBoolean("skip-initial-datapack-reload"))
.beLenientForMinorVersions(fileConfig.getBoolean("be-lenient-for-minor-versions"));
.beLenientForMinorVersions(fileConfig.getBoolean("be-lenient-for-minor-versions"))
.errorOnFailedPacketSends(fileConfig.getBoolean("error-on-failed-packet-sends"));

for (String pluginName : fileConfig.getStringList("skip-sender-proxy")) {
if (Bukkit.getPluginManager().getPlugin(pluginName) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public static DefaultBukkitConfig createDefault() {
options.put("be-lenient-for-minor-versions", BE_LENIENT_FOR_MINOR_VERSIONS);
options.put("hook-paper-reload", SHOULD_HOOK_PAPER_RELOAD);
options.put("skip-initial-datapack-reload", SKIP_RELOAD_DATAPACKS);
options.put("error-on-failed-packet-sends", ERROR_ON_FAILED_PACKET_SENDS);
options.put("plugins-to-convert", PLUGINS_TO_CONVERT);
options.put("other-commands-to-convert", OTHER_COMMANDS_TO_CONVERT);
options.put("skip-sender-proxy", SKIP_SENDER_PROXY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ authors:
website: https://www.jorel.dev/CommandAPI/
softdepend:
- NBTAPI
provides:
- CommandAPINetworking
api-version: 1.13
folia-supported: true
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ authors:
website: https://www.jorel.dev/CommandAPI/
softdepend:
- NBTAPI
provides:
- CommandAPINetworking
api-version: 1.13
folia-supported: true
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ public void unregister(String commandName, boolean unregisterNamespaces) {
@Override
public VelocityCommandAPIMessenger setupMessenger() {
messenger = new VelocityCommandAPIMessenger(
getConfiguration().getPlugin(),
getConfiguration().getServer()
config.getPlugin(),
config.getServer()
);
return messenger;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.messages.*;
import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.exceptions.ProtocolVersionTooOldException;
import dev.jorel.commandapi.network.packets.SetVersionPacket;

import java.util.HashMap;
Expand Down Expand Up @@ -143,6 +145,19 @@ public void onPluginMessageEvent(PluginMessageEvent event) {
messageReceived(protocol, event.getSource(), event.getData());
}

@Override
public void sendPacket(ChannelMessageSink target, CommandAPIPacket packet) {
try {
super.sendPacket(target, packet);
} catch (ProtocolVersionTooOldException exception) {
if (CommandAPI.getConfiguration().shouldErrorOnFailedPacketSends()) {
throw exception;
} else {
CommandAPI.logWarning(exception.getMessage());
}
}
}

@Override
public void sendRawBytes(CommandAPIProtocol protocol, ChannelMessageSink target, byte[] bytes) {
target.sendPluginMessage(MinecraftChannelIdentifier.from(protocol.getChannelIdentifier()), bytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ServerConnection;
import com.velocitypowered.api.proxy.messages.ChannelMessageSource;
import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPIVelocity;
import dev.jorel.commandapi.exceptions.ProtocolVersionTooOldException;
import dev.jorel.commandapi.network.packets.ProtocolVersionTooOldPacket;
import dev.jorel.commandapi.network.packets.SetVersionPacket;

/**
Expand All @@ -20,9 +23,22 @@ public void handleSetVersionPacket(ChannelMessageSource sender, SetVersionPacket
VelocityCommandAPIMessenger messenger = CommandAPIVelocity.get().getMessenger();

// Incoming messages are from ChannelMessageSource, while outgoing messages are ChannelMessageSink
// We actually want to set the version of a ChannelMessageSink. These istanceofs safely down-cast,
// We actually want to set the version of a ChannelMessageSink. These instanceofs safely down-cast,
// then the method call up-casts.
if (sender instanceof ServerConnection server) messenger.setServerProtocolVersion(server, protocolVersion);
if (sender instanceof Player player) messenger.setPlayerProtocolVersion(player, protocolVersion);
}

@Override
public void handleProtocolVersionTooOldPacket(ChannelMessageSource sender, ProtocolVersionTooOldPacket packet) {
try {
HandshakePacketHandler.super.handleProtocolVersionTooOldPacket(sender, packet);
} catch (ProtocolVersionTooOldException exception) {
if (CommandAPI.getConfiguration().shouldErrorOnFailedPacketSends()) {
throw exception;
} else {
CommandAPI.logWarning(exception.getMessage());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public CommandAPIMain(ProxyServer server, Logger logger, @DataDirectory Path dat
.verboseOutput(configYAML.node("verbose-outputs").getBoolean())
.silentLogs(configYAML.node("silent-logs").getBoolean())
.missingExecutorImplementationMessage(configYAML.node("messages", "missing-executor-implementation").getString())
.dispatcherFile(configYAML.node("create-dispatcher-json").getBoolean() ? new File(dataDirectory.toFile(), "command_registration.json") : null);
.dispatcherFile(configYAML.node("create-dispatcher-json").getBoolean() ? new File(dataDirectory.toFile(), "command_registration.json") : null)
.errorOnFailedPacketSends(configYAML.node("error-on-failed-packet-sends").getBoolean());

// Load
CommandAPI.setLogger(CommandAPILogger.fromJavaLogger(logger));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static DefaultVelocityConfig createDefault() {
options.put("silent-logs", SILENT_LOGS);
options.put("messages.missing-executor-implementation", MISSING_EXECUTOR_IMPLEMENTATION);
options.put("create-dispatcher-json", CREATE_DISPATCHER_JSON);
options.put("error-on-failed-packet-sends", ERROR_ON_FAILED_PACKET_SENDS);

Map<String, CommentedSection> sections = new LinkedHashMap<>();
sections.put("messages", SECTION_MESSAGE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public abstract class DefaultConfig {
}, false
);

public static final CommentedConfigOption<Boolean> ERROR_ON_FAILED_PACKET_SENDS = new CommentedConfigOption<>(
new String[]{
"Throw an error when a packet fails to send (default: true)",
"If \"true\", the CommandAPI will throw an exception if it tries to send a packet but cannot",
"(likely due to the receiver not having a new enough CommandAPI version to receive it). If",
"\"false\", failed attempts to send a packet will be logged as a warning."
}, true
);

public static final CommentedSection SECTION_MESSAGE = new CommentedSection(
new String[]{
"Messages",
Expand Down
Loading