Skip to content
Draft
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 @@ -34,12 +34,20 @@
import java.util.logging.Logger;

public abstract class PacketEventsAPI<T> {
private final EventManager eventManager = new EventManager();
private final EventManager eventManager;
private final PacketEventsSettings settings = new PacketEventsSettings();
private final UpdateChecker updateChecker = new UpdateChecker();
private final LogManager logManager = new LogManager();
private static final Logger LOGGER = Logger.getLogger(PacketEventsAPI.class.getName());

public PacketEventsAPI(EventManager eventManager) {
this.eventManager = eventManager;
}

public PacketEventsAPI() {
this(new EventManager());
}

public EventManager getEventManager() {
return eventManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.exception.InvalidHandshakeException;
import com.github.retrooper.packetevents.manager.PreViaInternalListener;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.function.Consumer;
import java.util.logging.Level;

/**
Expand Down Expand Up @@ -52,7 +57,18 @@ public class EventManager {
//Since reads greatly outnumber writes, create an array for the best possible iteration time
//Updated as a whole on writes, no index modifications are allowed
private volatile PacketListenerCommon[] listeners = new PacketListenerCommon[0];
private volatile boolean hasPreViaListeners = false;
private volatile boolean hasPreViaInternalListener = false;
private final Consumer<PacketListenerCommon> onRegisterListener;

public EventManager() {
onRegisterListener = listener -> {
};
}

public EventManager(Consumer<PacketListenerCommon> onRegisterListener) {
this.onRegisterListener = onRegisterListener;
}

/**
* Call the PacketEvent.
Expand All @@ -78,10 +94,11 @@ public void callEvent(PacketEvent event) {
* @param event {@link PacketEvent}
* @param postCallListenerAction The action to be run after all the listeners have finished processing
*/
public void callEvent(PacketEvent event, @Nullable Runnable postCallListenerAction) {
public void callEvent(PacketEvent event, @Nullable Runnable postCallListenerAction, boolean preVia) {
for (PacketListenerCommon listener : listeners) {
try {
event.call(listener);
if (listener.isPreVia() == preVia)
event.call(listener);
} catch (Exception t) {
// ignore handshake exceptions
if (t.getClass() != InvalidHandshakeException.class && (t.getCause() == null || t.getCause().getClass() != InvalidHandshakeException.class)) {
Expand All @@ -98,6 +115,10 @@ public void callEvent(PacketEvent event, @Nullable Runnable postCallListenerActi
}
}

public void callEvent(PacketEvent event, @Nullable Runnable postCallListenerAction) {
callEvent(event, postCallListenerAction, false);
}

/**
* Register the dynamic packet event listener.
*
Expand Down Expand Up @@ -153,6 +174,8 @@ public void unregisterAllListeners() {
this.listenersMap.clear();
synchronized (this) {//like booky10 said, the synchronization is necessary here
this.listeners = new PacketListenerCommon[0];
this.hasPreViaListeners = false;
this.hasPreViaInternalListener = false;
}
}

Expand All @@ -161,18 +184,43 @@ public void unregisterAllListeners() {
private void recalculateListeners() {
synchronized (this) {
List<PacketListenerCommon> list = new ArrayList<>();
boolean hasPreViaListeners = false;
boolean hasPreViaInternalListener = false;
//adds from LOWEST to MONITOR, so in the correct order
for (PacketListenerPriority priority : PacketListenerPriority.values()) {
Set<PacketListenerCommon> set = this.listenersMap.get(priority);
if (set != null) list.addAll(set);
if (set != null) {
list.addAll(set);
for (PacketListenerCommon listener : set) {
if (listener.isPreVia()) {
if (listener instanceof PreViaInternalListener) {
hasPreViaInternalListener = true;
} else {
hasPreViaListeners = true;
}
}
}
}
}
this.listeners = list.toArray(new PacketListenerCommon[0]);
this.hasPreViaListeners = hasPreViaListeners;
this.hasPreViaInternalListener = hasPreViaInternalListener;
}
}

public boolean hasPreViaListeners() {
return this.hasPreViaListeners;
}

public boolean hasPreViaInternalListener() {
return this.hasPreViaInternalListener;
}

//Internal registration methods, specifically separated for lesser overhead when registering an array of Listeners

private void registerListenerNoRecalculation(PacketListenerCommon listener) {
onRegisterListener.accept(listener);

Set<PacketListenerCommon> listenerSet = this.listenersMap.computeIfAbsent(listener.getPriority(), p -> new CopyOnWriteArraySet<>());
listenerSet.add(listener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ void onPacketSend(PacketSendEvent event) {
public void onPacketEventExternal(PacketEvent event) {
}

public boolean isPreVia() {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.github.retrooper.packetevents.exception.PacketProcessException;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper;
import com.github.retrooper.packetevents.protocol.ConnectionState;
import com.github.retrooper.packetevents.protocol.PacketSide;
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon;
import com.github.retrooper.packetevents.protocol.player.User;
Expand All @@ -44,6 +45,14 @@ protected PacketSendEvent(
super(PacketSide.SERVER, channel, user, player, rawByteBuf, autoProtocolTranslation);
}

protected PacketSendEvent(
Object channel, User user,
@UnknownNullability Object player, Object rawByteBuf,
boolean autoProtocolTranslation, ConnectionState connectionState
) throws PacketProcessException {
super(PacketSide.SERVER, channel, user, player, rawByteBuf, autoProtocolTranslation, connectionState);
}

protected PacketSendEvent(
int packetID, PacketTypeCommon packetType, ServerVersion serverVersion,
Object channel, User user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,32 @@ public abstract class ProtocolPacketEvent extends PacketEvent implements PlayerE
private @Nullable List<Runnable> postTasks = null;

private boolean cloned;
private boolean autoProtocolTranslation;
private boolean needsReEncode = PacketEvents.getAPI().getSettings().reEncodeByDefault();

public ProtocolPacketEvent(
PacketSide packetSide, Object channel, User user,
@UnknownNullability Object player, Object byteBuf,
boolean autoProtocolTranslation
) throws PacketProcessException {
this(packetSide, channel, user, player, byteBuf, autoProtocolTranslation,
packetSide == PacketSide.CLIENT ? user.getDecoderState() : user.getEncoderState());
}

protected ProtocolPacketEvent(
PacketSide packetSide, Object channel, User user,
@UnknownNullability Object player, Object byteBuf,
boolean autoProtocolTranslation, ConnectionState connectionState
) throws PacketProcessException {
this.channel = channel;
this.user = user;
this.player = player;
this.autoProtocolTranslation = autoProtocolTranslation;

if (autoProtocolTranslation || user.getClientVersion() == null) {
this.serverVersion = PacketEvents.getAPI().getServerManager().getVersion();
} else {
this.serverVersion = user.getPacketVersion().toServerVersion();
this.serverVersion = user.getClientVersion().toServerVersion();
}

this.byteBuf = byteBuf;
Expand All @@ -90,7 +101,7 @@ public ProtocolPacketEvent(
}

ClientVersion version = serverVersion.toClientVersion();
this.connectionState = packetSide == PacketSide.CLIENT ? user.getDecoderState() : user.getEncoderState();
this.connectionState = connectionState;
PacketTypeCommon packetType = PacketType.getById(packetSide, this.connectionState, version, this.packetID);
if (packetType == null) {
// mojang messed up and keeps sending disconnect packets in the wrong protocol state
Expand Down Expand Up @@ -206,6 +217,10 @@ public PacketTypeCommon getPacketType() {
return packetType;
}

public boolean isAutoProtocolTranslation() {
return autoProtocolTranslation;
}

@Deprecated
public String getPacketName() {
return ((Enum<?>) packetType).name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.github.retrooper.packetevents.exception.PacketProcessException;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper;
import com.github.retrooper.packetevents.protocol.ConnectionState;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon;
import com.github.retrooper.packetevents.protocol.player.User;
Expand All @@ -36,7 +37,15 @@ public PacketConfigSendEvent(
@UnknownNullability Object player, Object rawByteBuf,
boolean autoProtocolTranslation
) throws PacketProcessException {
super(channel, user, player, rawByteBuf, autoProtocolTranslation);
this(channel, user, player, rawByteBuf, autoProtocolTranslation, user.getEncoderState());
}

public PacketConfigSendEvent(
Object channel, User user,
@UnknownNullability Object player, Object rawByteBuf,
boolean autoProtocolTranslation, ConnectionState connectionState
) throws PacketProcessException {
super(channel, user, player, rawByteBuf, autoProtocolTranslation, connectionState);
}

protected PacketConfigSendEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.retrooper.packetevents.exception.PacketProcessException;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper;
import com.github.retrooper.packetevents.protocol.ConnectionState;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon;
import com.github.retrooper.packetevents.protocol.player.User;
Expand All @@ -18,7 +19,15 @@ public PacketHandshakeSendEvent(
@UnknownNullability Object player, Object rawByteBuf,
boolean autoProtocolTranslation
) throws PacketProcessException {
super(channel, user, player, rawByteBuf, autoProtocolTranslation);
this(channel, user, player, rawByteBuf, autoProtocolTranslation, user.getEncoderState());
}

public PacketHandshakeSendEvent(
Object channel, User user,
@UnknownNullability Object player, Object rawByteBuf,
boolean autoProtocolTranslation, ConnectionState connectionState
) throws PacketProcessException {
super(channel, user, player, rawByteBuf, autoProtocolTranslation, connectionState);
}

protected PacketHandshakeSendEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.github.retrooper.packetevents.exception.PacketProcessException;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper;
import com.github.retrooper.packetevents.protocol.ConnectionState;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon;
import com.github.retrooper.packetevents.protocol.player.User;
Expand All @@ -36,7 +37,15 @@ public PacketLoginSendEvent(
@UnknownNullability Object player, Object rawByteBuf,
boolean autoProtocolTranslation
) throws PacketProcessException {
super(channel, user, player, rawByteBuf, autoProtocolTranslation);
this(channel, user, player, rawByteBuf, autoProtocolTranslation, user.getEncoderState());
}

public PacketLoginSendEvent(
Object channel, User user,
@UnknownNullability Object player, Object rawByteBuf,
boolean autoProtocolTranslation, ConnectionState connectionState
) throws PacketProcessException {
super(channel, user, player, rawByteBuf, autoProtocolTranslation, connectionState);
}

protected PacketLoginSendEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.github.retrooper.packetevents.exception.PacketProcessException;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper;
import com.github.retrooper.packetevents.protocol.ConnectionState;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon;
import com.github.retrooper.packetevents.protocol.player.User;
Expand All @@ -36,7 +37,15 @@ public PacketPlaySendEvent(
@UnknownNullability Object player, Object rawByteBuf,
boolean autoProtocolTranslation
) throws PacketProcessException {
super(channel, user, player, rawByteBuf, autoProtocolTranslation);
this(channel, user, player, rawByteBuf, autoProtocolTranslation, user.getEncoderState());
}

public PacketPlaySendEvent(
Object channel, User user,
@UnknownNullability Object player, Object rawByteBuf,
boolean autoProtocolTranslation, ConnectionState connectionState
) throws PacketProcessException {
super(channel, user, player, rawByteBuf, autoProtocolTranslation, connectionState);
}

protected PacketPlaySendEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.github.retrooper.packetevents.exception.PacketProcessException;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.netty.buffer.ByteBufHelper;
import com.github.retrooper.packetevents.protocol.ConnectionState;
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
import com.github.retrooper.packetevents.protocol.packettype.PacketTypeCommon;
import com.github.retrooper.packetevents.protocol.player.User;
Expand All @@ -36,7 +37,15 @@ public PacketStatusSendEvent(
@UnknownNullability Object player, Object rawByteBuf,
boolean autoProtocolTranslation
) throws PacketProcessException {
super(channel, user, player, rawByteBuf, autoProtocolTranslation);
this(channel, user, player, rawByteBuf, autoProtocolTranslation, user.getEncoderState());
}

public PacketStatusSendEvent(
Object channel, User user,
@UnknownNullability Object player, Object rawByteBuf,
boolean autoProtocolTranslation, ConnectionState connectionState
) throws PacketProcessException {
super(channel, user, player, rawByteBuf, autoProtocolTranslation, connectionState);
}

protected PacketStatusSendEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ public void onPacketSend(PacketSendEvent event) {
: event.getServerVersion().isNewerThanOrEquals(ServerVersion.V_1_20_2)) {
user.setEncoderState(ConnectionState.CONFIGURATION);
} else {
user.setConnectionState(ConnectionState.PLAY);
// pre-via with modern client: encoder=PLAY, decoder=LOGIN
// pre-via decoder needs to see LOGIN_SUCCESS_ACK to set
// decoder state CONFIGURATION → PLAY (see PreViaInternalListener)
if (PreViaSupport.shouldUseLegacyLoginTracking(user)) {
user.setEncoderState(ConnectionState.PLAY);
} else {
user.setConnectionState(ConnectionState.PLAY);
}
}
}

Expand Down
Loading
Loading