Skip to content

Commit

Permalink
chore: replace minimal injector with netty injector
Browse files Browse the repository at this point in the history
fix: netty injector not working for inbound packets if the have built-in listeners
  • Loading branch information
Ingrim4 committed Jun 8, 2024
1 parent fb20a6a commit 17fdc6c
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum ListenerOptions {
* Disable the automatic game phase detection that will normally force {@link GamePhase#LOGIN} when a packet ID is
* known to be transmitted during login.
*/
@Deprecated
DISABLE_GAMEPHASE_DETECTION,

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@

package com.comphenix.protocol.events;

import java.util.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.injector.GamePhase;
Expand Down Expand Up @@ -90,20 +95,6 @@ public static Builder newBuilder(ListeningWhitelist template) {
return new Builder(template);
}

/**
* Construct a copy of a given enum.
*
* @param options - the options to copy, or NULL to indicate the empty set.
* @return A copy of the enum set.
*/
private static <T extends Enum<T>> EnumSet<T> safeEnumSet(Collection<T> options, Class<T> enumClass) {
if (options != null && !options.isEmpty()) {
return EnumSet.copyOf(options);
} else {
return EnumSet.noneOf(enumClass);
}
}

/**
* Construct a copy of a given set.
*
Expand Down Expand Up @@ -311,6 +302,7 @@ public Builder types(Collection<PacketType> types) {
* @param gamePhase - the gamephase.
* @return This builder, for chaining.
*/
@Deprecated
public Builder gamePhase(GamePhase gamePhase) {
this.gamePhase = gamePhase;
return this;
Expand All @@ -321,6 +313,7 @@ public Builder gamePhase(GamePhase gamePhase) {
*
* @return This builder, for chaining.
*/
@Deprecated
public Builder gamePhaseBoth() {
return gamePhase(GamePhase.BOTH);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*
* @author Kristian
*/
@Deprecated
public enum GamePhase {
/**
* Only listen for packets sent or received before a player has logged in.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ public interface Injector {
* Inject the current channel.
* <p>
* Note that only active channels can be injected.
*
* @return TRUE if we injected the channel, false if we could not inject or it was already injected.
*/
boolean inject();
void inject();

/**
* Close the current injector.
Expand All @@ -49,17 +47,6 @@ public interface Injector {

void sendWirePacket(WirePacket packet);

/**
* Retrieve the current protocol state.
*
* @return The current protocol.
* @deprecated use {@link #getCurrentProtocol(PacketType.Sender)} instead.
*/
@Deprecated
default Protocol getCurrentProtocol() {
return this.getCurrentProtocol(PacketType.Sender.SERVER);
}

/**
* Retrieve the current protocol state. Note that since 1.20.2 the client and server direction can be in different
* protocol states.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public int getProtocolVersion() {
}

@Override
public boolean inject() {
return false;
public void inject() {
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.comphenix.protocol.error.ErrorReporter;
import com.comphenix.protocol.injector.netty.ChannelListener;
import com.comphenix.protocol.injector.netty.Injector;
import com.comphenix.protocol.injector.temporary.MinimalInjector;
import com.comphenix.protocol.injector.temporary.TemporaryPlayerFactory;
import com.comphenix.protocol.reflect.FuzzyReflection;
import com.comphenix.protocol.utility.MinecraftFields;
Expand Down Expand Up @@ -175,10 +174,9 @@ public Injector fromChannel(Channel channel) {
this.channelListener,
this,
this.errorReporter);
MinimalInjector minimalInjector = new NettyChannelMinimalInjector(injector);

// Initialize temporary player
TemporaryPlayerFactory.setInjectorInPlayer(temporaryPlayer, minimalInjector);
TemporaryPlayerFactory.setInjectorForPlayer(temporaryPlayer, injector);
return injector;
}

Expand Down Expand Up @@ -234,9 +232,9 @@ public Injector cacheInjector(String name, Injector injector) {
* @return The associated injector, or NULL if this is a Bukkit player.
*/
private NettyChannelInjector getTemporaryInjector(Player player) {
MinimalInjector injector = TemporaryPlayerFactory.getInjectorFromPlayer(player);
if (injector instanceof NettyChannelMinimalInjector) {
return ((NettyChannelMinimalInjector) injector).getInjector();
Injector injector = TemporaryPlayerFactory.getInjectorFromPlayer(player);
if (injector instanceof NettyChannelInjector) {
return (NettyChannelInjector) injector;
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,17 @@ public int getProtocolVersion() {
}

@Override
public boolean inject() {
public void inject() {
// ensure we are in the channel's event loop to be "thread-safe" within netty
// and our own code
if (!this.channel.eventLoop().inEventLoop()) {
channel.eventLoop().execute(this::inject);
return false;
return;
}

// don't need to inject if injector or channel got closed
if (this.closed.get() || !this.channel.isActive()) {
return false;
return;
}

// wrap channel inside the NetworkMananger to proxy write calls to our
Expand Down Expand Up @@ -214,14 +214,13 @@ public boolean inject() {

// mark injector as injected
this.injected = true;
return true;
}

private void uninject() {
// ensure we are in the channel's event loop to be "thread-safe" within netty
// and our own code
if (!this.channel.eventLoop().inEventLoop()) {
channel.eventLoop().execute(this::inject);
channel.eventLoop().execute(this::uninject);
return;
}

Expand Down Expand Up @@ -446,6 +445,8 @@ void processInbound(ChannelHandlerContext ctx, PacketContainer packet) {
// invoke plugin listeners
if (this.channelListener.hasInboundListener(packet.getType())) {
this.processInboundInternal(ctx, packet);
} else {
ctx.fireChannelRead(packet.getHandle());
}
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.comphenix.protocol.injector.temporary;

import java.util.Objects;

import com.comphenix.protocol.injector.netty.Injector;

/**
* A temporary player created by ProtocolLib when a true player instance does not exist.
* <p>
Expand All @@ -8,15 +12,17 @@
*/
public class TemporaryPlayer {

private volatile MinimalInjector injector;
private volatile Injector injector;

MinimalInjector getInjector() {
public Injector getInjector() {
return this.injector;
}

void setInjector(MinimalInjector injector) {
if (injector == null) {
throw new IllegalArgumentException("Injector cannot be NULL.");
void setInjector(Injector injector) {
Objects.requireNonNull(injector, "injector can't be null");

if (this.injector != null) {
throw new IllegalStateException("Can't redefine injector for temporary player");
}

this.injector = injector;
Expand Down
Loading

0 comments on commit 17fdc6c

Please sign in to comment.