From 6a69d1bcb4c19da7a013078068f228012d86fe69 Mon Sep 17 00:00:00 2001 From: retrooper Date: Mon, 22 Jul 2024 18:50:35 +0200 Subject: [PATCH] Generics usage in ProtocolPacketEvent for platform-dependent player --- .../retrooper/packetevents/event/EventManager.java | 4 ++-- .../packetevents/event/PacketReceiveEvent.java | 3 +-- .../packetevents/event/PacketSendEvent.java | 2 +- .../retrooper/packetevents/event/PlayerEvent.java | 4 ++-- .../packetevents/event/ProtocolPacketEvent.java | 14 +++++++------- .../packetevents/event/UserLoginEvent.java | 6 +++--- .../packetevents/wrapper/PacketWrapper.java | 2 +- .../retrooper/packetevents/PacketEventsMod.java | 10 ---------- 8 files changed, 17 insertions(+), 28 deletions(-) diff --git a/api/src/main/java/com/github/retrooper/packetevents/event/EventManager.java b/api/src/main/java/com/github/retrooper/packetevents/event/EventManager.java index 0a047cbb4f..f7f1f859b9 100644 --- a/api/src/main/java/com/github/retrooper/packetevents/event/EventManager.java +++ b/api/src/main/java/com/github/retrooper/packetevents/event/EventManager.java @@ -93,8 +93,8 @@ public void callEvent(PacketEvent event, @Nullable Runnable postCallListenerActi } } // For performance reasons, we don't want to re-encode the packet if it's not needed. - if (event instanceof ProtocolPacketEvent && !((ProtocolPacketEvent) event).needsReEncode()) { - ((ProtocolPacketEvent) event).setLastUsedWrapper(null); + if (event instanceof ProtocolPacketEvent && !((ProtocolPacketEvent) event).needsReEncode()) { + ((ProtocolPacketEvent) event).setLastUsedWrapper(null); } } diff --git a/api/src/main/java/com/github/retrooper/packetevents/event/PacketReceiveEvent.java b/api/src/main/java/com/github/retrooper/packetevents/event/PacketReceiveEvent.java index 311764723a..7193a66b09 100644 --- a/api/src/main/java/com/github/retrooper/packetevents/event/PacketReceiveEvent.java +++ b/api/src/main/java/com/github/retrooper/packetevents/event/PacketReceiveEvent.java @@ -18,7 +18,6 @@ package com.github.retrooper.packetevents.event; -import com.github.retrooper.packetevents.event.simple.*; import com.github.retrooper.packetevents.exception.PacketProcessException; import com.github.retrooper.packetevents.manager.server.ServerVersion; import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper; @@ -26,7 +25,7 @@ import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon; import com.github.retrooper.packetevents.protocol.player.User; -public class PacketReceiveEvent extends ProtocolPacketEvent { +public class PacketReceiveEvent extends ProtocolPacketEvent { protected PacketReceiveEvent(Object channel, User user, Object player, Object rawByteBuf, boolean autoProtocolTranslation) throws PacketProcessException { super(PacketSide.CLIENT, channel, user, player, rawByteBuf, autoProtocolTranslation); diff --git a/api/src/main/java/com/github/retrooper/packetevents/event/PacketSendEvent.java b/api/src/main/java/com/github/retrooper/packetevents/event/PacketSendEvent.java index ff4b4f1381..fd4c9b1898 100644 --- a/api/src/main/java/com/github/retrooper/packetevents/event/PacketSendEvent.java +++ b/api/src/main/java/com/github/retrooper/packetevents/event/PacketSendEvent.java @@ -29,7 +29,7 @@ import java.util.ArrayList; import java.util.List; -public class PacketSendEvent extends ProtocolPacketEvent { +public class PacketSendEvent extends ProtocolPacketEvent { private List tasksAfterSend = null; protected PacketSendEvent(Object channel, User user, Object player, Object rawByteBuf, diff --git a/api/src/main/java/com/github/retrooper/packetevents/event/PlayerEvent.java b/api/src/main/java/com/github/retrooper/packetevents/event/PlayerEvent.java index c6d0e7f519..7ce64b9df0 100644 --- a/api/src/main/java/com/github/retrooper/packetevents/event/PlayerEvent.java +++ b/api/src/main/java/com/github/retrooper/packetevents/event/PlayerEvent.java @@ -24,11 +24,11 @@ * @author retrooper * @since 1.6.9 */ -public interface PlayerEvent { +public interface PlayerEvent { /** * Associated player. * * @return Player. */ - T getPlayer(); + T getPlayer(); } diff --git a/api/src/main/java/com/github/retrooper/packetevents/event/ProtocolPacketEvent.java b/api/src/main/java/com/github/retrooper/packetevents/event/ProtocolPacketEvent.java index 81df95af0b..b4968b81b3 100644 --- a/api/src/main/java/com/github/retrooper/packetevents/event/ProtocolPacketEvent.java +++ b/api/src/main/java/com/github/retrooper/packetevents/event/ProtocolPacketEvent.java @@ -39,11 +39,11 @@ import java.util.ArrayList; import java.util.List; -public abstract class ProtocolPacketEvent extends PacketEvent implements PlayerEvent, CancellableEvent, UserEvent { +public abstract class ProtocolPacketEvent extends PacketEvent implements PlayerEvent, CancellableEvent, UserEvent { private final Object channel; private final ConnectionState connectionState; private final User user; - private final T player; + private final Object player; private Object byteBuf; private final int packetID; private final PacketTypeCommon packetType; @@ -55,7 +55,7 @@ public abstract class ProtocolPacketEvent extends PacketEvent implements Play private boolean needsReEncode = PacketEvents.getAPI().getSettings().reEncodeByDefault(); public ProtocolPacketEvent(PacketSide packetSide, Object channel, - User user, T player, Object byteBuf, + User user, Object player, Object byteBuf, boolean autoProtocolTranslation) throws PacketProcessException { this.channel = channel; this.user = user; @@ -92,7 +92,7 @@ public ProtocolPacketEvent(PacketSide packetSide, Object channel, } public ProtocolPacketEvent(int packetID, PacketTypeCommon packetType, ServerVersion serverVersion, Object channel, - User user, T player, Object byteBuf) { + User user, Object player, Object byteBuf) { this.channel = channel; this.user = user; this.player = player; @@ -133,8 +133,8 @@ public User getUser() { } @Override - public T getPlayer() { - return player; + public T getPlayer() { + return (T) player; } public ConnectionState getConnectionState() { @@ -213,7 +213,7 @@ public boolean hasPostTasks() { } @Override - public ProtocolPacketEvent clone() { + public ProtocolPacketEvent clone() { return this instanceof PacketReceiveEvent ? ((PacketReceiveEvent) this).clone() : ((PacketSendEvent) this).clone(); } diff --git a/api/src/main/java/com/github/retrooper/packetevents/event/UserLoginEvent.java b/api/src/main/java/com/github/retrooper/packetevents/event/UserLoginEvent.java index b4d574eadf..7d7c9d0bbb 100644 --- a/api/src/main/java/com/github/retrooper/packetevents/event/UserLoginEvent.java +++ b/api/src/main/java/com/github/retrooper/packetevents/event/UserLoginEvent.java @@ -20,7 +20,7 @@ import com.github.retrooper.packetevents.protocol.player.User; -public class UserLoginEvent extends PacketEvent implements CallableEvent, UserEvent, PlayerEvent { +public class UserLoginEvent extends PacketEvent implements CallableEvent, UserEvent, PlayerEvent { private final User user; private final Object player; @@ -35,8 +35,8 @@ public User getUser() { } @Override - public Object getPlayer() { - return player; + public T getPlayer() { + return (T) player; } @Override diff --git a/api/src/main/java/com/github/retrooper/packetevents/wrapper/PacketWrapper.java b/api/src/main/java/com/github/retrooper/packetevents/wrapper/PacketWrapper.java index b95376b55b..8985d365ec 100644 --- a/api/src/main/java/com/github/retrooper/packetevents/wrapper/PacketWrapper.java +++ b/api/src/main/java/com/github/retrooper/packetevents/wrapper/PacketWrapper.java @@ -262,7 +262,7 @@ public void copy(T wrapper) { //TODO public void transform(int protocolVersion) {} //Current idea change server version, but still think more - public final void readEvent(ProtocolPacketEvent event) { + public final void readEvent(ProtocolPacketEvent event) { PacketWrapper last = event.getLastUsedWrapper(); if (last != null) { copy((T) last); diff --git a/fabric/src/main/java/io/github/retrooper/packetevents/PacketEventsMod.java b/fabric/src/main/java/io/github/retrooper/packetevents/PacketEventsMod.java index 90079116e1..d28600295d 100644 --- a/fabric/src/main/java/io/github/retrooper/packetevents/PacketEventsMod.java +++ b/fabric/src/main/java/io/github/retrooper/packetevents/PacketEventsMod.java @@ -1,18 +1,9 @@ package io.github.retrooper.packetevents; import com.github.retrooper.packetevents.PacketEvents; -import com.github.retrooper.packetevents.event.*; -import com.github.retrooper.packetevents.protocol.packettype.PacketType; -import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientChatMessage; -import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerChatMessage; -import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerKeepAlive; import io.github.retrooper.packetevents.factory.fabric.FabricPacketEventsBuilder; import net.fabricmc.api.ModInitializer; import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint; -import net.minecraft.client.Minecraft; -import net.minecraft.client.player.LocalPlayer; -import net.minecraft.network.chat.MutableComponent; -import net.minecraft.network.chat.contents.PlainTextContents; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,7 +16,6 @@ public class PacketEventsMod implements PreLaunchEntrypoint, ModInitializer { @Override public void onPreLaunch() { PacketEvents.setAPI(FabricPacketEventsBuilder.build("packetevents")); - //PacketEvents.getAPI().getSettings().debug(true); PacketEvents.getAPI().load(); }