9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-19 15:09:15 +00:00
This commit is contained in:
jhqwqmc
2025-12-11 17:53:23 +08:00
parent 5f09ead4f6
commit 0347125278
11 changed files with 56 additions and 165 deletions

View File

@@ -47,9 +47,9 @@ import net.momirealms.craftengine.bukkit.plugin.network.handler.*;
import net.momirealms.craftengine.bukkit.plugin.network.id.PacketIdHelper;
import net.momirealms.craftengine.bukkit.plugin.network.id.PacketIds1_20;
import net.momirealms.craftengine.bukkit.plugin.network.id.PacketIds1_20_5;
import net.momirealms.craftengine.bukkit.plugin.network.listener.ByteBufferPacketListener;
import net.momirealms.craftengine.bukkit.plugin.network.listener.ByteBufferPacketListenerHolder;
import net.momirealms.craftengine.bukkit.plugin.network.listener.NMSPacketListener;
import net.momirealms.craftengine.core.plugin.network.listener.ByteBufferPacketListener;
import net.momirealms.craftengine.core.plugin.network.listener.ByteBufferPacketListenerHolder;
import net.momirealms.craftengine.core.plugin.network.listener.NMSPacketListener;
import net.momirealms.craftengine.bukkit.plugin.network.payload.DiscardedPayload;
import net.momirealms.craftengine.bukkit.plugin.network.payload.Payload;
import net.momirealms.craftengine.bukkit.plugin.network.payload.PayloadHelper;
@@ -134,7 +134,6 @@ import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Predicate;
@@ -143,16 +142,30 @@ public class BukkitNetworkManager implements NetworkManager, Listener {
private final BukkitCraftEngine plugin;
private final Map<Class<?>, NMSPacketListener> nmsPacketListeners = new IdentityHashMap<>(128);
private final ByteBufferPacketListenerHolder[] s2cHandshakingPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.CLIENTBOUND, ConnectionState.HANDSHAKING)];
private final ByteBufferPacketListenerHolder[] c2sHandshakingPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.SERVERBOUND, ConnectionState.HANDSHAKING)];
private final ByteBufferPacketListenerHolder[] s2cStatusPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.CLIENTBOUND, ConnectionState.STATUS)];
private final ByteBufferPacketListenerHolder[] c2sStatusPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.SERVERBOUND, ConnectionState.STATUS)];
private final ByteBufferPacketListenerHolder[] s2cLoginPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.CLIENTBOUND, ConnectionState.LOGIN)];
private final ByteBufferPacketListenerHolder[] c2sLoginPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.SERVERBOUND, ConnectionState.LOGIN)];
private final ByteBufferPacketListenerHolder[] s2cPlayPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.CLIENTBOUND, ConnectionState.PLAY)];
private final ByteBufferPacketListenerHolder[] c2sPlayPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.SERVERBOUND, ConnectionState.PLAY)];
private final ByteBufferPacketListenerHolder[] s2cConfigurationPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.CLIENTBOUND, ConnectionState.CONFIGURATION)];
private final ByteBufferPacketListenerHolder[] c2sConfigurationPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.SERVERBOUND, ConnectionState.CONFIGURATION)];
private static final ByteBufferPacketListenerHolder[] s2cHandshakingPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.CLIENTBOUND, ConnectionState.HANDSHAKING)];
private static final ByteBufferPacketListenerHolder[] c2sHandshakingPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.SERVERBOUND, ConnectionState.HANDSHAKING)];
private static final ByteBufferPacketListenerHolder[] s2cStatusPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.CLIENTBOUND, ConnectionState.STATUS)];
private static final ByteBufferPacketListenerHolder[] c2sStatusPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.SERVERBOUND, ConnectionState.STATUS)];
private static final ByteBufferPacketListenerHolder[] s2cLoginPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.CLIENTBOUND, ConnectionState.LOGIN)];
private static final ByteBufferPacketListenerHolder[] c2sLoginPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.SERVERBOUND, ConnectionState.LOGIN)];
private static final ByteBufferPacketListenerHolder[] s2cPlayPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.CLIENTBOUND, ConnectionState.PLAY)];
private static final ByteBufferPacketListenerHolder[] c2sPlayPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.SERVERBOUND, ConnectionState.PLAY)];
private static final ByteBufferPacketListenerHolder[] s2cConfigurationPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.CLIENTBOUND, ConnectionState.CONFIGURATION)];
private static final ByteBufferPacketListenerHolder[] c2sConfigurationPacketListeners = new ByteBufferPacketListenerHolder[PacketIdHelper.count(PacketFlow.SERVERBOUND, ConnectionState.CONFIGURATION)];
private final ByteBufferPacketListenerHolder[][] s2cPacketListeners = new ByteBufferPacketListenerHolder[][]{
s2cHandshakingPacketListeners,
s2cStatusPacketListeners,
s2cLoginPacketListeners,
s2cPlayPacketListeners,
s2cConfigurationPacketListeners
};
private final ByteBufferPacketListenerHolder[][] c2sPacketListeners = new ByteBufferPacketListenerHolder[][]{
c2sHandshakingPacketListeners,
c2sStatusPacketListeners,
c2sLoginPacketListeners,
c2sPlayPacketListeners,
c2sConfigurationPacketListeners
};
private final TriConsumer<ChannelHandler, Object, Object> packetConsumer;
private final TriConsumer<ChannelHandler, List<Object>, Object> packetsConsumer;
@@ -233,7 +246,6 @@ public class BukkitNetworkManager implements NetworkManager, Listener {
}
// 对安装了 FreedomChat 的用户告警
if (Bukkit.getPluginManager().getPlugin("FreedomChat") != null) {
for (int i = 0; i < 5; i++) {
plugin.logger().severe("");
if (Locale.getDefault() == Locale.SIMPLIFIED_CHINESE) {
plugin.logger().severe("CraftEngine 与 FreedomChat 不兼容,请立刻卸载 FreedomChat");
@@ -245,7 +257,6 @@ public class BukkitNetworkManager implements NetworkManager, Listener {
plugin.logger().severe("");
}
}
}
public static BukkitNetworkManager instance() {
return instance;
@@ -521,22 +532,14 @@ public class BukkitNetworkManager implements NetworkManager, Listener {
user.sendPacket(TotemAnimationCommand.FIX_TOTEM_SOUND_PACKET, false);
Channel channel = user.nettyChannel();
if (this.hasAntiPopup && Config.disableChatReport() && channel != null) {
removeAntiPopupHandler(channel, 0); // 功能冲突直接移除
if (Locale.getDefault() == Locale.SIMPLIFIED_CHINESE) {
plugin.logger().warn("CraftEngine 的禁用聊天举报功能和 AntiPopup 冲突,可能会导致 Emoji 解析异常,请卸载 AntiPopup 或关闭禁用聊天举报功能");
} else {
plugin.logger().warn("The Disable Chat Report feature conflicts with AntiPopup, potentially causing abnormal emoji parsing.");
plugin.logger().warn("Please uninstall AntiPopup or disable the Disable Chat Report function.");
}
}
}
private void removeAntiPopupHandler(Channel channel, int times) {
if (times > 5) {
Debugger.COMMON.debug(() -> "antipopup_handler Removed Failed");
return;
}
if (channel.pipeline().get("antipopup_handler") == null) {
plugin.scheduler().asyncLater(() -> removeAntiPopupHandler(channel, times + 1), 1, TimeUnit.SECONDS);
return;
}
channel.pipeline().remove("antipopup_handler");
Debugger.COMMON.debug(() -> "antipopup_handler Handler Removed (" + times + ")");
}
@EventHandler(priority = EventPriority.LOWEST)
@@ -923,7 +926,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener {
int packetId = buf.readVarInt();
int preIndex = buf.readerIndex();
try {
ByteBufPacketEvent event = ByteBufPacketEvent.create(packetId, buf, preIndex, this.player.encoderState(), PacketFlow.CLIENTBOUND);
ByteBufPacketEvent event = new ByteBufPacketEvent(packetId, buf, preIndex);
BukkitNetworkManager.this.handleS2CByteBufPacket(this.player, event);
if (event.isCancelled()) {
buf.clear();
@@ -967,7 +970,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener {
int packetId = buf.readVarInt();
int preIndex = buf.readerIndex();
try {
ByteBufPacketEvent event = ByteBufPacketEvent.create(packetId, buf, preIndex, this.player.decoderState(), PacketFlow.SERVERBOUND);
ByteBufPacketEvent event = new ByteBufPacketEvent(packetId, buf, preIndex);
BukkitNetworkManager.this.handleC2SByteBufPacket(this.player, event);
if (event.isCancelled()) {
buf.clear();
@@ -1022,18 +1025,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener {
protected void handleS2CByteBufPacket(NetWorkUser user, ByteBufPacketEvent event) {
int packetID = event.packetID();
ByteBufferPacketListenerHolder holder;
if (event.state() == ConnectionState.PLAY) {
holder = this.s2cPlayPacketListeners[packetID];
} else {
holder = switch (event.state()) {
case HANDSHAKING -> this.s2cHandshakingPacketListeners[packetID];
case STATUS -> this.s2cStatusPacketListeners[packetID];
case LOGIN -> this.s2cLoginPacketListeners[packetID];
case CONFIGURATION -> this.s2cConfigurationPacketListeners[packetID];
default -> null;
};
}
ByteBufferPacketListenerHolder holder = s2cPacketListeners[user.encoderState().ordinal()][packetID];
if (holder != null) {
try {
holder.listener().onPacketSend(user, event);
@@ -1045,18 +1037,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener {
protected void handleC2SByteBufPacket(NetWorkUser user, ByteBufPacketEvent event) {
int packetID = event.packetID();
ByteBufferPacketListenerHolder holder;
if (event.state() == ConnectionState.PLAY) {
holder = this.c2sPlayPacketListeners[packetID];
} else {
holder = switch (event.state()) {
case HANDSHAKING -> this.c2sHandshakingPacketListeners[packetID];
case STATUS -> this.c2sStatusPacketListeners[packetID];
case LOGIN -> this.c2sLoginPacketListeners[packetID];
case CONFIGURATION -> this.c2sConfigurationPacketListeners[packetID];
default -> null;
};
}
ByteBufferPacketListenerHolder holder = c2sPacketListeners[user.decoderState().ordinal()][packetID];
if (holder != null) {
try {
holder.listener().onPacketReceive(user, event);

View File

@@ -4678,17 +4678,11 @@ public final class CoreReflections {
public static final Field field$DedicatedServerProperties$enforceSecureProfile = requireNonNull(
ReflectionUtils.getDeclaredField(
clazz$DedicatedServerProperties,
VersionHelper.isOrAbove1_21_11()
? new String[]{"enforceSecureProfile", "ag"}
: VersionHelper.isOrAbove1_21_9()
? new String[]{"enforceSecureProfile", "af"}
: VersionHelper.isOrAbove1_21_2()
? new String[]{"enforceSecureProfile", "X"}
: VersionHelper.isOrAbove1_21()
? new String[]{"enforceSecureProfile", "Y"}
: VersionHelper.isOrAbove1_20_5()
? new String[]{"enforceSecureProfile", "X"}
clazz$DedicatedServerProperties, VersionHelper.isOrAbove1_21_11() ? new String[]{"enforceSecureProfile", "ag"}
: VersionHelper.isOrAbove1_21_9() ? new String[]{"enforceSecureProfile", "af"}
: VersionHelper.isOrAbove1_21_2() ? new String[]{"enforceSecureProfile", "X"}
: VersionHelper.isOrAbove1_21() ? new String[]{"enforceSecureProfile", "Y"}
: VersionHelper.isOrAbove1_20_5() ? new String[]{"enforceSecureProfile", "X"}
: new String[]{"enforceSecureProfile", "W"}
)
);

View File

@@ -1,25 +1,19 @@
package net.momirealms.craftengine.core.plugin.network.event;
import net.momirealms.craftengine.core.plugin.network.ConnectionState;
import net.momirealms.craftengine.core.plugin.network.PacketFlow;
import net.momirealms.craftengine.core.util.Cancellable;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
public abstract class ByteBufPacketEvent implements Cancellable {
public class ByteBufPacketEvent implements Cancellable {
private boolean cancelled;
private final FriendlyByteBuf buf;
private boolean changed;
private final int packetID;
private final int preIndex;
private final ConnectionState state;
private final PacketFlow direction;
protected ByteBufPacketEvent(int packetID, FriendlyByteBuf buf, int preIndex, ConnectionState state, PacketFlow direction) {
public ByteBufPacketEvent(int packetID, FriendlyByteBuf buf, int preIndex) {
this.buf = buf;
this.packetID = packetID;
this.preIndex = preIndex;
this.state = state;
this.direction = direction;
}
public int packetID() {
@@ -48,22 +42,4 @@ public abstract class ByteBufPacketEvent implements Cancellable {
public void setCancelled(boolean cancel) {
cancelled = cancel;
}
public ConnectionState state() {
return state;
}
public PacketFlow direction() {
return direction;
}
public static ByteBufPacketEvent create(int packetID, FriendlyByteBuf buf, int preIndex, ConnectionState state, PacketFlow direction) {
return switch (state) {
case HANDSHAKING -> new HandshakingByteBufPacketEvent(packetID, buf, preIndex, direction);
case STATUS -> new StatusByteBufPacketEvent(packetID, buf, preIndex, direction);
case LOGIN -> new LoginByteBufPacketEvent(packetID, buf, preIndex, direction);
case PLAY -> new PlayByteBufPacketEvent(packetID, buf, preIndex, direction);
case CONFIGURATION -> new ConfigurationByteBufPacketEvent(packetID, buf, preIndex, direction);
};
}
}

View File

@@ -1,12 +0,0 @@
package net.momirealms.craftengine.core.plugin.network.event;
import net.momirealms.craftengine.core.plugin.network.ConnectionState;
import net.momirealms.craftengine.core.plugin.network.PacketFlow;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
public class ConfigurationByteBufPacketEvent extends ByteBufPacketEvent {
public ConfigurationByteBufPacketEvent(int packetID, FriendlyByteBuf buf, int preIndex, PacketFlow direction) {
super(packetID, buf, preIndex, ConnectionState.CONFIGURATION, direction);
}
}

View File

@@ -1,12 +0,0 @@
package net.momirealms.craftengine.core.plugin.network.event;
import net.momirealms.craftengine.core.plugin.network.ConnectionState;
import net.momirealms.craftengine.core.plugin.network.PacketFlow;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
public class HandshakingByteBufPacketEvent extends ByteBufPacketEvent {
public HandshakingByteBufPacketEvent(int packetID, FriendlyByteBuf buf, int preIndex, PacketFlow direction) {
super(packetID, buf, preIndex, ConnectionState.HANDSHAKING, direction);
}
}

View File

@@ -1,12 +0,0 @@
package net.momirealms.craftengine.core.plugin.network.event;
import net.momirealms.craftengine.core.plugin.network.ConnectionState;
import net.momirealms.craftengine.core.plugin.network.PacketFlow;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
public class LoginByteBufPacketEvent extends ByteBufPacketEvent {
public LoginByteBufPacketEvent(int packetID, FriendlyByteBuf buf, int preIndex, PacketFlow direction) {
super(packetID, buf, preIndex, ConnectionState.LOGIN, direction);
}
}

View File

@@ -1,12 +0,0 @@
package net.momirealms.craftengine.core.plugin.network.event;
import net.momirealms.craftengine.core.plugin.network.ConnectionState;
import net.momirealms.craftengine.core.plugin.network.PacketFlow;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
public class PlayByteBufPacketEvent extends ByteBufPacketEvent {
public PlayByteBufPacketEvent(int packetID, FriendlyByteBuf buf, int preIndex, PacketFlow direction) {
super(packetID, buf, preIndex, ConnectionState.PLAY, direction);
}
}

View File

@@ -1,12 +0,0 @@
package net.momirealms.craftengine.core.plugin.network.event;
import net.momirealms.craftengine.core.plugin.network.ConnectionState;
import net.momirealms.craftengine.core.plugin.network.PacketFlow;
import net.momirealms.craftengine.core.util.FriendlyByteBuf;
public class StatusByteBufPacketEvent extends ByteBufPacketEvent {
public StatusByteBufPacketEvent(int packetID, FriendlyByteBuf buf, int preIndex, PacketFlow direction) {
super(packetID, buf, preIndex, ConnectionState.STATUS, direction);
}
}

View File

@@ -1,4 +1,4 @@
package net.momirealms.craftengine.bukkit.plugin.network.listener;
package net.momirealms.craftengine.core.plugin.network.listener;
import net.momirealms.craftengine.core.plugin.network.event.ByteBufPacketEvent;
import net.momirealms.craftengine.core.plugin.network.NetWorkUser;

View File

@@ -1,4 +1,4 @@
package net.momirealms.craftengine.bukkit.plugin.network.listener;
package net.momirealms.craftengine.core.plugin.network.listener;
public record ByteBufferPacketListenerHolder(String id, ByteBufferPacketListener listener) {
}

View File

@@ -1,4 +1,4 @@
package net.momirealms.craftengine.bukkit.plugin.network.listener;
package net.momirealms.craftengine.core.plugin.network.listener;
import net.momirealms.craftengine.core.plugin.network.event.NMSPacketEvent;
import net.momirealms.craftengine.core.plugin.network.NetWorkUser;