Skip to content

Commit

Permalink
Update to new build logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SamB440 committed Aug 22, 2024
1 parent 4d6d430 commit 818f955
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 79 deletions.
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
53 changes: 0 additions & 53 deletions sponge/build.gradle

This file was deleted.

45 changes: 45 additions & 0 deletions sponge/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Server> event) {
if (PacketEvents.getAPI().getSettings().shouldCheckForUpdates()) {
PacketEvents.getAPI().getUpdateChecker().handleUpdateCheck();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public void onServerStart(final StartingEngineEvent<Server> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -49,8 +56,8 @@ public PacketEventsDecoder(PacketEventsDecoder decoder) {
}

public void read(ChannelHandlerContext ctx, ByteBuf input, List<Object> 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
Expand All @@ -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!");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public void receivePacket(Object channel, Object byteBuf) {
if (ChannelHelper.isOpen(channel)) {
List<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 818f955

Please sign in to comment.