Skip to content

Commit

Permalink
fix unregister channel issue and 1.20.2 packet listener
Browse files Browse the repository at this point in the history
  • Loading branch information
hakan-krgn committed Oct 18, 2023
1 parent 56c1975 commit dcfc1a8
Show file tree
Hide file tree
Showing 23 changed files with 41 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,26 @@ public static <T> T newInstance(@Nonnull String path,
@Nullable
public static <T> T getField(@Nonnull Object object,
@Nonnull String fieldName) {
return getField(object, object.getClass(), fieldName);
}

/**
* Gets value of the given field.
*
* @param object Object.
* @param fieldName Field name.
* @param <T> Type of field.
* @return Value of the given field.
*/
@Nullable
public static <T> T getField(@Nonnull Object object,
@Nonnull Class<?> clazz,
@Nonnull String fieldName) {
try {
Validate.notNull(object, "object cannot be null!");
Validate.notNull(fieldName, "fieldName cannot be null!");

Field field = object.getClass().getDeclaredField(fieldName);
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
Object value = field.get(object);
field.setAccessible(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.network.NetworkManager;
import net.minecraft.network.protocol.Packet;
import net.minecraft.server.network.PlayerConnection;
import net.minecraft.server.network.ServerCommonPacketListenerImpl;
import org.bukkit.craftbukkit.v1_20_R2.entity.CraftPlayer;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -49,7 +50,8 @@ public void send(@Nonnull Object... packets) {
@Override
public void register() {
try {
NetworkManager networkManager = ReflectionUtils.getField(this.connection, "h");
NetworkManager networkManager = ReflectionUtils.getField(
this.connection, ServerCommonPacketListenerImpl.class, "c");
if (networkManager == null) return;

super.pipeline = networkManager.n.pipeline().addBefore("packet_handler", CHANNEL + super.player.getUniqueId(), new ChannelDuplexHandler() {
Expand Down Expand Up @@ -81,6 +83,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void write(ChannelHandlerContext channelHandlerContext, Object o, Channel
@Override
public void unregister() {
if (super.pipeline != null && super.pipeline.get(CHANNEL) != null)
super.pipeline.remove(CHANNEL);
super.pipeline.remove(CHANNEL + super.player.getUniqueId());
}
}

0 comments on commit dcfc1a8

Please sign in to comment.