From 818f9550ec088224a90941d13eb255f8636ca235 Mon Sep 17 00:00:00 2001 From: SamB440 Date: Sat, 8 Jun 2024 14:30:18 +0100 Subject: [PATCH] Update to new build logic --- settings.gradle.kts | 1 + sponge/build.gradle | 53 ------------------- sponge/build.gradle.kts | 45 ++++++++++++++++ .../sponge/InternalSpongeListener.java | 9 ++++ .../sponge/PacketEventsPlugin.java | 3 +- .../factory/SpongePacketEventsBuilder.java | 11 +--- .../handlers/PacketEventsDecoder.java | 31 +++++++++-- .../manager/protocol/ProtocolManagerImpl.java | 4 +- .../manager/server/ServerManagerImpl.java | 12 ++--- .../buffer/ByteBufOperatorModernImpl.java | 13 ++++- .../sponge/util/SpongeConversionUtil.java | 2 +- .../util/viaversion/ViaVersionUtil.java | 1 - 12 files changed, 106 insertions(+), 79 deletions(-) delete mode 100644 sponge/build.gradle create mode 100644 sponge/build.gradle.kts diff --git a/settings.gradle.kts b/settings.gradle.kts index da14ca25db..60cf9969b9 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -32,6 +32,7 @@ include("netty-common") include("spigot") include("bungeecord") include("velocity") +include("sponge") include("fabric") // Patch modules include(":patch:adventure-text-serializer-gson") diff --git a/sponge/build.gradle b/sponge/build.gradle deleted file mode 100644 index 0f64a0b00e..0000000000 --- a/sponge/build.gradle +++ /dev/null @@ -1,53 +0,0 @@ -import org.spongepowered.gradle.plugin.config.PluginLoaders -import org.spongepowered.plugin.metadata.model.PluginDependency - -plugins { - id("org.spongepowered.gradle.plugin") version("2.0.2") -} - -repositories { - maven { url 'https://repo.spongepowered.org/repository/maven-public/' } // Sponge - maven { url 'https://repo.viaversion.com/' } -} - -sponge { - apiVersion("11.0.0-SNAPSHOT") - loader { - name(PluginLoaders.JAVA_PLAIN) - version("2.2.1") - } - - plugin("packetevents") { - displayName("PacketEvents") - entrypoint("io.github.retrooper.packetevents.sponge.PacketEventsPlugin") - license("GPL-3") - dependency("spongeapi") { - loadOrder(PluginDependency.LoadOrder.AFTER) - optional(false) - } - dependency("viaversion") { - version("*") - loadOrder(PluginDependency.LoadOrder.AFTER) - optional(true) - } - } -} - -shadowJar { - relocate 'net.kyori.adventure.text.serializer.gson', 'io.github.retrooper.packetevents.adventure.serializer.gson' - relocate 'net.kyori.adventure.text.serializer.legacy', 'io.github.retrooper.packetevents.adventure.serializer.legacy' - dependencies { - exclude(dependency('com.google.code.gson:gson:2.8.0')) - exclude(dependency('com.google.code.gson:gson:2.8.5')) - exclude(dependency('com.google.code.gson:gson:2.8.8')) - } -} - -dependencies { - compileOnly 'io.netty:netty-all:4.1.75.Final' - api project(':api') - api project(':netty-common') - api(adventureDependencies) - - compileOnly 'com.viaversion:viaversion:4.5.0' -} diff --git a/sponge/build.gradle.kts b/sponge/build.gradle.kts new file mode 100644 index 0000000000..81d1c1d59a --- /dev/null +++ b/sponge/build.gradle.kts @@ -0,0 +1,45 @@ +import org.spongepowered.gradle.plugin.config.PluginLoaders +import org.spongepowered.plugin.metadata.model.PluginDependency + +plugins { + packetevents.`shadow-conventions` + packetevents.`library-conventions` + id("org.spongepowered.gradle.plugin") version("2.0.2") +} + +repositories { + maven("https://repo.spongepowered.org/repository/maven-public/") // Sponge + maven("https://repo.viaversion.com/") +} + +sponge { + apiVersion("11.1.0-SNAPSHOT") + loader { + name(PluginLoaders.JAVA_PLAIN) + version("2.4.1-SNAPSHOT") + } + + plugin("packetevents") { + displayName("PacketEvents") + entrypoint("io.github.retrooper.packetevents.sponge.PacketEventsPlugin") + license("GPL-3") + dependency("spongeapi") { + loadOrder(PluginDependency.LoadOrder.AFTER) + optional(false) + } + dependency("viaversion") { + version("*") + loadOrder(PluginDependency.LoadOrder.AFTER) + optional(true) + } + } +} + +dependencies { + compileOnly(libs.netty) + shadow(libs.adventure.nbt) + shadow(project(":api", "shadow")) + shadow(project(":netty-common")) + + compileOnly(libs.via.version) +} diff --git a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/InternalSpongeListener.java b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/InternalSpongeListener.java index bb38d5f372..690dc20bda 100644 --- a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/InternalSpongeListener.java +++ b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/InternalSpongeListener.java @@ -22,10 +22,12 @@ import com.github.retrooper.packetevents.util.FakeChannelUtil; import io.github.retrooper.packetevents.sponge.injector.SpongeChannelInjector; import net.kyori.adventure.text.Component; +import org.spongepowered.api.Server; import org.spongepowered.api.entity.living.player.server.ServerPlayer; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.Order; import org.spongepowered.api.event.filter.Getter; +import org.spongepowered.api.event.lifecycle.StartedEngineEvent; import org.spongepowered.api.event.network.ServerSideConnectionEvent; public class InternalSpongeListener { @@ -49,4 +51,11 @@ public void onJoin(ServerSideConnectionEvent.Join event, @Getter("player") Serve // Set player object in the injectors injector.updatePlayer(user, player); } + + @Listener(order = Order.EARLY) + public void onStart(StartedEngineEvent event) { + if (PacketEvents.getAPI().getSettings().shouldCheckForUpdates()) { + PacketEvents.getAPI().getUpdateChecker().handleUpdateCheck(); + } + } } diff --git a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/PacketEventsPlugin.java b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/PacketEventsPlugin.java index 72fa962e32..f3ff18bbd4 100644 --- a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/PacketEventsPlugin.java +++ b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/PacketEventsPlugin.java @@ -53,8 +53,7 @@ public void onServerStart(final StartingEngineEvent event) { PacketEvents.getAPI().load(); // Register your listeners - // TODO disable debug - PacketEvents.getAPI().getSettings().debug(true).downsampleColors(false).bStats(true).checkForUpdates(true).timeStampMode(TimeStampMode.MILLIS).reEncodeByDefault(true); + PacketEvents.getAPI().getSettings().debug(true).downsampleColors(false).checkForUpdates(true).timeStampMode(TimeStampMode.MILLIS).reEncodeByDefault(true); PacketEvents.getAPI().init(); SimplePacketListenerAbstract listener = new SimplePacketListenerAbstract(PacketListenerPriority.HIGH) { diff --git a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/factory/SpongePacketEventsBuilder.java b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/factory/SpongePacketEventsBuilder.java index d1442c76c7..e79d9a7603 100644 --- a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/factory/SpongePacketEventsBuilder.java +++ b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/factory/SpongePacketEventsBuilder.java @@ -127,20 +127,11 @@ public boolean isLoaded() { @Override public void init() { - //Load if we haven't loaded already + // Load if we haven't loaded already load(); if (initialized) return; - if (settings.shouldCheckForUpdates()) { - getUpdateChecker().handleUpdateCheck(); - } - - if (settings.isbStatsEnabled()) { - // TODO: how do we do this? bStats wants @Inject but that won't work here. - // https://github.com/Bastian/bstats-metrics/blob/1.x.x/bstats-sponge/src/examples/java/ExamplePlugin.java - } - Sponge.eventManager().registerListeners(plugin, new InternalSpongeListener()); if (lateBind) { diff --git a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/injector/handlers/PacketEventsDecoder.java b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/injector/handlers/PacketEventsDecoder.java index f36c1fb4c9..31ec1c12d9 100644 --- a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/injector/handlers/PacketEventsDecoder.java +++ b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/injector/handlers/PacketEventsDecoder.java @@ -18,17 +18,24 @@ package io.github.retrooper.packetevents.sponge.injector.handlers; +import com.github.retrooper.packetevents.PacketEvents; import com.github.retrooper.packetevents.exception.PacketProcessException; +import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper; import com.github.retrooper.packetevents.protocol.ConnectionState; import com.github.retrooper.packetevents.protocol.player.User; import com.github.retrooper.packetevents.util.ExceptionUtil; import com.github.retrooper.packetevents.util.PacketEventsImplHelper; +import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerDisconnect; import io.github.retrooper.packetevents.sponge.injector.connection.ServerConnectionInitializer; import io.github.retrooper.packetevents.sponge.util.SpongeReflectionUtil; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageDecoder; +import net.kyori.adventure.text.Component; +import org.spongepowered.api.Sponge; import org.spongepowered.api.entity.living.player.server.ServerPlayer; +import org.spongepowered.api.scheduler.Task; +import org.spongepowered.plugin.PluginContainer; import java.util.List; @@ -49,8 +56,8 @@ public PacketEventsDecoder(PacketEventsDecoder decoder) { } public void read(ChannelHandlerContext ctx, ByteBuf input, List out) throws Exception { - PacketEventsImplHelper.handleServerBoundPacket(ctx.channel(), user, player, input, true); - out.add(input.retain()); + Object buffer = PacketEventsImplHelper.handleServerBoundPacket(ctx.channel(), user, player, input, true); + out.add(ByteBufHelper.retain(buffer)); } @Override @@ -68,7 +75,25 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E if (ExceptionUtil.isException(cause, PacketProcessException.class) && !SpongeReflectionUtil.isMinecraftServerInstanceDebugging() && (user != null && user.getDecoderState() != ConnectionState.HANDSHAKING)) { - cause.printStackTrace(); + if (PacketEvents.getAPI().getSettings().isFullStackTraceEnabled()) { + cause.printStackTrace(); + } else { + PacketEvents.getAPI().getLogManager().warn(cause.getMessage()); + } + + if (PacketEvents.getAPI().getSettings().isKickOnPacketExceptionEnabled()) { + try { + user.sendPacket(new WrapperPlayServerDisconnect(Component.text("Invalid packet"))); + } catch (Exception ignored) { // There may (?) be an exception if the player is in the wrong state... + // Do nothing. + } + user.closeConnection(); + if (player != null) { + Sponge.server().scheduler().submit(Task.builder().plugin((PluginContainer) PacketEvents.getAPI().getPlugin()).execute(() -> player.kick(Component.text("Invalid packet"))).build()); + } + + PacketEvents.getAPI().getLogManager().warn("Disconnected " + user.getProfile().getName() + " due to invalid packet!"); + } } } diff --git a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/manager/protocol/ProtocolManagerImpl.java b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/manager/protocol/ProtocolManagerImpl.java index 807d0b2d7d..25cb10b6c1 100644 --- a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/manager/protocol/ProtocolManagerImpl.java +++ b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/manager/protocol/ProtocolManagerImpl.java @@ -90,8 +90,8 @@ public void receivePacket(Object channel, Object byteBuf) { if (ChannelHelper.isOpen(channel)) { List handlerNames = ChannelHelper.pipelineHandlerNames(channel); //Account for ViaVersion - if (handlerNames.contains("encoder")) { - ChannelHelper.fireChannelReadInContext(channel, "decoder", byteBuf); + if (handlerNames.contains("via-encoder")) { + ChannelHelper.fireChannelReadInContext(channel, "via-decoder", byteBuf); } else if (handlerNames.contains("decompress")) { //We will have to just skip through the minecraft server's decompression handler ChannelHelper.fireChannelReadInContext(channel, "decompress", byteBuf); diff --git a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/manager/server/ServerManagerImpl.java b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/manager/server/ServerManagerImpl.java index b852cf8138..cb0c3043fe 100644 --- a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/manager/server/ServerManagerImpl.java +++ b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/manager/server/ServerManagerImpl.java @@ -36,13 +36,13 @@ private ServerVersion resolveVersionNoCache() { // Our PEVersion class can parse this version and detect if it is a newer version than what is currently supported // and account for that properly - PEVersion version = new PEVersion(minecraftRelease); - PEVersion latestVersion = new PEVersion(ServerVersion.getLatest().getReleaseName()); + PEVersion version = PEVersion.fromString(minecraftRelease); + PEVersion latestVersion = PEVersion.fromString(ServerVersion.getLatest().getReleaseName()); if (version.isNewerThan(latestVersion)) { //We do not support this version yet, so let us warn the user - plugin.logger().warn("[packetevents] We currently do not support the minecraft version " - + version + ", so things might break. PacketEvents will behave as if the minecraft version were " - + latestVersion + "!"); + plugin.logger().warn("[packetevents] We currently do not support the minecraft version {}," + + " so things might break. " + + "PacketEvents will behave as if the minecraft version were {}!", version, latestVersion); return ServerVersion.getLatest(); } @@ -53,7 +53,7 @@ private ServerVersion resolveVersionNoCache() { } } - plugin.logger().warn("[packetevents] Your server software is preventing us from checking the server version. This is what we found: " + minecraftRelease + ". We will assume the server version is " + fallbackVersion.name() + "..."); + plugin.logger().warn("[packetevents] Your server software is preventing us from checking the server version. This is what we found: {}. We will assume the server version is {}...", minecraftRelease, fallbackVersion.name()); return fallbackVersion; } diff --git a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/netty/buffer/ByteBufOperatorModernImpl.java b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/netty/buffer/ByteBufOperatorModernImpl.java index c28920973b..aea5060b3e 100644 --- a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/netty/buffer/ByteBufOperatorModernImpl.java +++ b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/netty/buffer/ByteBufOperatorModernImpl.java @@ -79,6 +79,11 @@ public short readShort(Object buffer) { return ((ByteBuf)buffer).readShort(); } + @Override + public int readMedium(Object buffer) { + return ((ByteBuf)buffer).readMedium(); + } + @Override public int readInt(Object buffer) { return ((ByteBuf)buffer).readInt(); @@ -105,6 +110,12 @@ public void writeShort(Object buffer, int value) { ((ByteBuf)buffer).writeShort(value); } + + @Override + public void writeMedium(Object buffer, int value) { + ((ByteBuf)buffer).writeMedium(value); + } + @Override public void writeInt(Object buffer, int value) { ((ByteBuf)buffer).writeInt(value); @@ -192,7 +203,7 @@ public Object writeBytes(Object buffer, byte[] bytes, int offset, int length) { @Override public void readBytes(Object buffer, byte[] bytes) { - ((ByteBuf)buffer).readBytes(bytes); + ((ByteBuf)buffer).readBytes(bytes); } @Override diff --git a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/util/SpongeConversionUtil.java b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/util/SpongeConversionUtil.java index db5f503f24..8dcdeae82b 100644 --- a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/util/SpongeConversionUtil.java +++ b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/util/SpongeConversionUtil.java @@ -23,7 +23,7 @@ import com.github.retrooper.packetevents.protocol.world.states.WrappedBlockState; import org.spongepowered.api.block.BlockState; -public class SpongeConversionUtil { +public final class SpongeConversionUtil { public static ItemStack fromSpongeItemStack(org.spongepowered.api.item.inventory.ItemStack itemStack) { return SpongeReflectionUtil.decodeSpongeItemStack(itemStack); diff --git a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/util/viaversion/ViaVersionUtil.java b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/util/viaversion/ViaVersionUtil.java index e1c1ae091a..1aeb9f4004 100644 --- a/sponge/src/main/java/io/github/retrooper/packetevents/sponge/util/viaversion/ViaVersionUtil.java +++ b/sponge/src/main/java/io/github/retrooper/packetevents/sponge/util/viaversion/ViaVersionUtil.java @@ -42,7 +42,6 @@ private static void load() { public static void checkIfViaIsPresent() { boolean present = Sponge.pluginManager().plugin("viaversion").isPresent(); - System.out.println("via present? " + present); available = present ? ViaState.ENABLED : ViaState.DISABLED; }