Skip to content

Commit

Permalink
Merge branch 'fix/fabric-module' into testing/fabric-1-21-2
Browse files Browse the repository at this point in the history
  • Loading branch information
booky10 committed Oct 20, 2024
2 parents 1af49a5 + e54035a commit 7b29b68
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,39 @@
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
import com.github.retrooper.packetevents.event.PacketSendEvent;
import com.github.retrooper.packetevents.event.ProtocolPacketEvent;
import com.github.retrooper.packetevents.event.UserDisconnectEvent;
import com.github.retrooper.packetevents.manager.protocol.ProtocolManager;
import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper;
import com.github.retrooper.packetevents.netty.buffer.UnpooledByteBufAllocationHelper;
import com.github.retrooper.packetevents.protocol.PacketSide;
import com.github.retrooper.packetevents.protocol.player.User;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;

public class PacketEventsImplHelper {

public static PacketSendEvent handleClientBoundPacket(Object channel,
User user,
Object player,
Object buffer,
boolean autoProtocolTranslation) throws Exception {
if (!ByteBufHelper.isReadable(buffer)) return null;
public final class PacketEventsImplHelper {

private PacketEventsImplHelper() {
}

public static @Nullable ProtocolPacketEvent handlePacket(
Object channel, User user, Object player, Object buffer,
boolean autoProtocolTranslation, PacketSide side
) throws Exception {
if (side == PacketSide.SERVER) {
return handleClientBoundPacket(channel, user, player, buffer, autoProtocolTranslation);
} else {
return handleServerBoundPacket(channel, user, player, buffer, autoProtocolTranslation);
}
}

public static @Nullable PacketSendEvent handleClientBoundPacket(
Object channel, User user, Object player, Object buffer,
boolean autoProtocolTranslation
) throws Exception {
if (!ByteBufHelper.isReadable(buffer)) {
return null;
}

int preProcessIndex = ByteBufHelper.readerIndex(buffer);
PacketSendEvent packetSendEvent = EventCreationUtil.createSendEvent(channel, user, player, buffer, autoProtocolTranslation);
Expand Down Expand Up @@ -71,26 +87,25 @@ public static PacketSendEvent handleClientBoundPacket(Object channel,
return packetSendEvent;
}

public static Object handleServerBoundPacket(Object channel, User user,
Object player,
Object buffer,
boolean autoProtocolTranslation) throws Exception {
if (!ByteBufHelper.isReadable(buffer)) return null;
public static @Nullable PacketReceiveEvent handleServerBoundPacket(
Object channel, User user, Object player, Object buffer,
boolean autoProtocolTranslation
) throws Exception {
if (!ByteBufHelper.isReadable(buffer)) {
return null;
}

int preProcessIndex = ByteBufHelper.readerIndex(buffer);
PacketReceiveEvent packetReceiveEvent = EventCreationUtil.createReceiveEvent(channel, user, player, buffer, autoProtocolTranslation);
int processIndex = ByteBufHelper.readerIndex(buffer);
Object finalBuffer = buffer;
PacketEvents.getAPI().getEventManager().callEvent(packetReceiveEvent, () -> {
ByteBufHelper.readerIndex(finalBuffer, processIndex);
ByteBufHelper.readerIndex(buffer, processIndex);
});
if (!packetReceiveEvent.isCancelled()) {
//Did they ever use a wrapper?
if (packetReceiveEvent.getLastUsedWrapper() != null) {
//Rewrite the buffer
ByteBufHelper.clear(buffer);
buffer = UnpooledByteBufAllocationHelper.buffer();
packetReceiveEvent.getLastUsedWrapper().setBuffer(buffer);
packetReceiveEvent.getLastUsedWrapper().writeVarInt(packetReceiveEvent.getPacketId());
packetReceiveEvent.getLastUsedWrapper().write();
} else {
Expand All @@ -107,7 +122,7 @@ public static Object handleServerBoundPacket(Object channel, User user,
task.run();
}
}
return buffer;
return packetReceiveEvent;
}

public static void handleDisconnection(Object channel, @Nullable UUID uuid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,35 @@
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@ApiStatus.Internal
public final class SynchronizedRegistriesHandler {

private static final boolean FORCE_PER_USER_REGISTRIES = Boolean.getBoolean("packetevents.force-per-user-registries");
private static final Map<ResourceLocation, RegistryEntry<?>> REGISTRY_KEYS = Stream.of(
new RegistryEntry<>(Biomes.getRegistry(), Biome::decode),
new RegistryEntry<>(ChatTypes.getRegistry(), ChatType::decode),
new RegistryEntry<>(TrimPatterns.getRegistry(), TrimPattern::decode),
new RegistryEntry<>(TrimMaterials.getRegistry(), TrimMaterial::decode),
new RegistryEntry<>(WolfVariants.getRegistry(), WolfVariant::decode),
new RegistryEntry<>(PaintingVariants.getRegistry(), PaintingVariant::decode),
new RegistryEntry<>(DimensionTypes.getRegistry(), DimensionType::decode),
new RegistryEntry<>(DamageTypes.getRegistry(), DamageType::decode),
new RegistryEntry<>(BannerPatterns.getRegistry(), BannerPattern::decode),
new RegistryEntry<>(EnchantmentTypes.getRegistry(), EnchantmentType::decode),
new RegistryEntry<>(JukeboxSongs.getRegistry(), IJukeboxSong::decode),
new RegistryEntry<>(Instruments.getRegistry(), Instrument::decode)
).collect(Collectors.toMap(RegistryEntry::getRegistryKey, Function.identity()));
private static final Map<ResourceLocation, RegistryEntry<?>> REGISTRY_KEYS = new HashMap<>();

static {
Stream.of(
new RegistryEntry<>(Biomes.getRegistry(), Biome::decode),
new RegistryEntry<>(ChatTypes.getRegistry(), ChatType::decode),
new RegistryEntry<>(TrimPatterns.getRegistry(), TrimPattern::decode),
new RegistryEntry<>(TrimMaterials.getRegistry(), TrimMaterial::decode),
new RegistryEntry<>(WolfVariants.getRegistry(), WolfVariant::decode),
new RegistryEntry<>(PaintingVariants.getRegistry(), PaintingVariant::decode),
new RegistryEntry<>(DimensionTypes.getRegistry(), DimensionType::decode),
new RegistryEntry<>(DamageTypes.getRegistry(), DamageType::decode),
new RegistryEntry<>(BannerPatterns.getRegistry(), BannerPattern::decode),
new RegistryEntry<>(EnchantmentTypes.getRegistry(), EnchantmentType::decode),
new RegistryEntry<>(JukeboxSongs.getRegistry(), IJukeboxSong::decode),
new RegistryEntry<>(Instruments.getRegistry(), Instrument::decode)
).forEach(entry -> REGISTRY_KEYS.put(entry.getRegistryKey(), entry));
}

private SynchronizedRegistriesHandler() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,31 @@
import net.kyori.adventure.text.format.NamedTextColor;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.event.Level;

import java.util.logging.Level;
import java.util.Map;

public class FabricLogger extends LogManager {

private static final Map<java.util.logging.Level, Level> LEVEL_CONVERSION = Map.of(
java.util.logging.Level.FINEST, Level.TRACE,
java.util.logging.Level.FINER, Level.TRACE,
java.util.logging.Level.FINE, Level.DEBUG,
java.util.logging.Level.INFO, Level.INFO,
java.util.logging.Level.WARNING, Level.WARN,
java.util.logging.Level.SEVERE, Level.ERROR
);

private final Logger logger;

public FabricLogger(Logger logger) {
this.logger = logger;
}

@Override
protected void log(Level level, @Nullable NamedTextColor color, String message) {
protected void log(java.util.logging.Level level, @Nullable NamedTextColor color, String message) {
String plainMessage = STRIP_COLOR_PATTERN.matcher(message).replaceAll("");
org.slf4j.event.Level logLevel = org.slf4j.event.Level.intToLevel(level.intValue());
Level logLevel = LEVEL_CONVERSION.getOrDefault(level, Level.INFO);
this.logger.makeLoggingEventBuilder(logLevel).log(plainMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package io.github.retrooper.packetevents.handler;

import com.github.retrooper.packetevents.protocol.PacketSide;
import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.util.PacketEventsImplHelper;
import io.netty.buffer.ByteBuf;
Expand All @@ -31,10 +32,12 @@
@ApiStatus.Internal
public class PacketDecoder extends MessageToMessageDecoder<ByteBuf> {

private final PacketSide side;
public User user;
public Player player;

public PacketDecoder(User user) {
public PacketDecoder(PacketSide side, User user) {
this.side = side.getOpposite();
this.user = user;
}

Expand All @@ -43,8 +46,8 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
if (!msg.isReadable()) {
return;
}
PacketEventsImplHelper.handleClientBoundPacket(ctx.channel(), this.user, this.player,
msg, false);
PacketEventsImplHelper.handlePacket(ctx.channel(), this.user, this.player,
msg, false, this.side);
if (msg.isReadable()) {
out.add(msg.retain());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package io.github.retrooper.packetevents.handler;

import com.github.retrooper.packetevents.protocol.PacketSide;
import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.util.PacketEventsImplHelper;
import io.netty.buffer.ByteBuf;
Expand All @@ -30,10 +31,12 @@
@ApiStatus.Internal
public class PacketEncoder extends ChannelOutboundHandlerAdapter {

private final PacketSide side;
public User user;
public Player player;

public PacketEncoder(User user) {
public PacketEncoder(PacketSide side, User user) {
this.side = side;
this.user = user;
}

Expand All @@ -48,17 +51,10 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
return;
}

ByteBuf out;
try {
out = (ByteBuf) PacketEventsImplHelper.handleServerBoundPacket(ctx.channel(),
this.user, this.player, in, false);
} finally {
in.release();
}
if (out.isReadable()) {
ctx.write(out, promise);
} else {
out.release();
PacketEventsImplHelper.handlePacket(ctx.channel(),
this.user, this.player, in, false, this.side);
if (in.isReadable()) {
ctx.write(in, promise);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.github.retrooper.packetevents.event.UserConnectEvent;
import com.github.retrooper.packetevents.manager.protocol.ProtocolManager;
import com.github.retrooper.packetevents.protocol.ConnectionState;
import com.github.retrooper.packetevents.protocol.PacketSide;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;
import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.protocol.player.UserProfile;
Expand Down Expand Up @@ -67,8 +68,9 @@ private static void configureSerialization(
return;
}

channel.pipeline().addAfter("splitter", PacketEvents.DECODER_NAME, new PacketDecoder(user));
channel.pipeline().addAfter("prepender", PacketEvents.ENCODER_NAME, new PacketEncoder(user));
PacketSide side = PacketEvents.getAPI().getInjector().isServerBound() ? PacketSide.SERVER : PacketSide.CLIENT;
channel.pipeline().addAfter("splitter", PacketEvents.DECODER_NAME, new PacketDecoder(side, user));
channel.pipeline().addAfter("prepender", PacketEvents.ENCODER_NAME, new PacketEncoder(side, user));
channel.closeFuture().addListener((ChannelFutureListener) future ->
PacketEventsImplHelper.handleDisconnection(user.getChannel(), user.getUUID()));
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public PacketEventsDecoder(PacketEventsDecoder decoder) {
}

public void read(ChannelHandlerContext ctx, ByteBuf input, List<Object> out) throws Exception {
Object buffer = PacketEventsImplHelper.handleServerBoundPacket(ctx.channel(), user, player, input, true);
out.add(ByteBufHelper.retain(buffer));
PacketEventsImplHelper.handleServerBoundPacket(ctx.channel(), user, player, input, true);
out.add(ByteBufHelper.retain(input));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
Expand Down Expand Up @@ -83,9 +84,9 @@ protected void encode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> l
list.add(byteBuf.retain());
}

private PacketSendEvent handleClientBoundPacket(Channel channel, User user, Object player, ByteBuf buffer, ChannelPromise promise) throws Exception {
private @Nullable PacketSendEvent handleClientBoundPacket(Channel channel, User user, Object player, ByteBuf buffer, ChannelPromise promise) throws Exception {
PacketSendEvent packetSendEvent = PacketEventsImplHelper.handleClientBoundPacket(channel, user, player, buffer, true);
if (packetSendEvent.hasTasksAfterSend()) {
if (packetSendEvent != null && packetSendEvent.hasTasksAfterSend()) {
promise.addListener((p) -> {
for (Runnable task : packetSendEvent.getTasksAfterSend()) {
task.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public PacketEventsDecoder(PacketEventsDecoder decoder) {
}

public void read(ChannelHandlerContext ctx, ByteBuf input, List<Object> out) throws Exception {
Object buffer = PacketEventsImplHelper.handleServerBoundPacket(ctx.channel(), user, player == null ? null : Sponge.server().player(player).orElse(null), input, true);
out.add(ByteBufHelper.retain(buffer));
PacketEventsImplHelper.handleServerBoundPacket(ctx.channel(), user, player == null ? null : Sponge.server().player(player).orElse(null), input, true);
out.add(ByteBufHelper.retain(input));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.MessageToMessageEncoder;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.api.Sponge;

import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -77,9 +78,9 @@ protected void encode(ChannelHandlerContext ctx, ByteBuf byteBuf, List<Object> l
list.add(byteBuf.retain());
}

private PacketSendEvent handleClientBoundPacket(Channel channel, User user, UUID player, ByteBuf buffer, ChannelPromise promise) throws Exception {
private @Nullable PacketSendEvent handleClientBoundPacket(Channel channel, User user, UUID player, ByteBuf buffer, ChannelPromise promise) throws Exception {
PacketSendEvent packetSendEvent = PacketEventsImplHelper.handleClientBoundPacket(channel, user, player == null ? null : Sponge.server().player(player).orElse(null), buffer, true);
if (packetSendEvent.hasTasksAfterSend()) {
if (packetSendEvent != null && packetSendEvent.hasTasksAfterSend()) {
promise.addListener((p) -> {
for (Runnable task : packetSendEvent.getTasksAfterSend()) {
task.run();
Expand Down

0 comments on commit 7b29b68

Please sign in to comment.