mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-30 20:39:10 +00:00
feat(resource-pack): 解决全版本拒绝资源包处理
This commit is contained in:
@@ -21,7 +21,6 @@ import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerResourcePackStatusEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -55,16 +54,6 @@ public class BukkitPackManager extends AbstractPackManager implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOW)
|
||||
public void onResourcePackStatus(PlayerResourcePackStatusEvent event) {
|
||||
// for 1.20.1 servers, not recommended to use
|
||||
if (Config.sendPackOnJoin() && Config.kickOnDeclined() && !VersionHelper.isVersionNewerThan1_20_2()) {
|
||||
if (event.getStatus() == PlayerResourcePackStatusEvent.Status.DECLINED || event.getStatus() == PlayerResourcePackStatusEvent.Status.FAILED_DOWNLOAD) {
|
||||
event.getPlayer().kick();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
if (ReloadCommand.RELOAD_PACK_FLAG || CraftEngine.instance().isInitializing()) {
|
||||
|
||||
@@ -151,6 +151,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
|
||||
registerNMSPacketConsumer(PacketConsumers.RESOURCE_PACK_PUSH, Reflections.clazz$ClientboundResourcePackPushPacket);
|
||||
registerNMSPacketConsumer(PacketConsumers.HANDSHAKE_C2S, Reflections.clazz$ClientIntentionPacket);
|
||||
registerNMSPacketConsumer(PacketConsumers.LOGIN_ACKNOWLEDGED, Reflections.clazz$ServerboundLoginAcknowledgedPacket);
|
||||
registerNMSPacketConsumer(PacketConsumers.RESOURCE_PACK_RESPONSE, Reflections.clazz$ServerboundResourcePackPacket);
|
||||
registerByteBufPacketConsumer(PacketConsumers.SECTION_BLOCK_UPDATE, this.packetIds.clientboundSectionBlocksUpdatePacket());
|
||||
registerByteBufPacketConsumer(PacketConsumers.BLOCK_UPDATE, this.packetIds.clientboundBlockUpdatePacket());
|
||||
registerByteBufPacketConsumer(VersionHelper.isVersionNewerThan1_21_3() ? PacketConsumers.LEVEL_PARTICLE_1_21_3 : (VersionHelper.isVersionNewerThan1_20_5() ? PacketConsumers.LEVEL_PARTICLE_1_20_5 : PacketConsumers.LEVEL_PARTICLE_1_20), this.packetIds.clientboundLevelParticlesPacket());
|
||||
|
||||
@@ -2204,4 +2204,25 @@ public class PacketConsumers {
|
||||
CraftEngine.instance().logger().warn("Failed to handle ServerboundLoginAcknowledgedPacket", e);
|
||||
}
|
||||
};
|
||||
|
||||
public static final TriConsumer<NetWorkUser, NMSPacketEvent, Object> RESOURCE_PACK_RESPONSE = (user, event, packet) -> {
|
||||
try {
|
||||
if (user.sentResourcePack() || !Config.sendPackOnJoin() || !Config.kickOnDeclined()) return;
|
||||
Object action = Reflections.field$ServerboundResourcePackPacket$action.get(packet);
|
||||
if (action == null) return;
|
||||
if (action == Reflections.instance$ServerboundResourcePackPacket$Action$DECLINED
|
||||
|| action == Reflections.instance$ServerboundResourcePackPacket$Action$FAILED_DOWNLOAD) {
|
||||
Object kickPacket = Reflections.constructor$ClientboundDisconnectPacket.newInstance(
|
||||
ComponentUtils.adventureToMinecraft(Config.resourcePackPrompt()));
|
||||
user.nettyChannel().writeAndFlush(kickPacket);
|
||||
user.nettyChannel().disconnect();
|
||||
return;
|
||||
}
|
||||
if (action == Reflections.instance$ServerboundResourcePackPacket$Action$SUCCESSFULLY_LOADED) {
|
||||
user.setSentResourcePack(true);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
CraftEngine.instance().logger().warn("Failed to handle ServerboundResourcePackPacket", e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ public class BukkitServerPlayer extends Player {
|
||||
private ConnectionState decoderState;
|
||||
private ConnectionState encoderState;
|
||||
private final Set<UUID> resourcePackUUID = Collections.synchronizedSet(new HashSet<>());
|
||||
private boolean sentResourcePack = !Config.sendPackOnJoin();
|
||||
// some references
|
||||
private Reference<org.bukkit.entity.Player> playerRef;
|
||||
private Reference<Object> serverPlayerRef;
|
||||
@@ -771,6 +772,16 @@ public class BukkitServerPlayer extends Player {
|
||||
this.protocolVersion = ProtocolVersion.getById(protocolVersion);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sentResourcePack() {
|
||||
return this.sentResourcePack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSentResourcePack(boolean sentResourcePack) {
|
||||
this.sentResourcePack = sentResourcePack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearView() {
|
||||
this.entityTypeView.clear();
|
||||
|
||||
@@ -6499,12 +6499,33 @@ public class Reflections {
|
||||
)
|
||||
);
|
||||
|
||||
public static final Object instance$ServerboundResourcePackPacket$Action$SUCCESSFULLY_LOADED;
|
||||
public static final Object instance$ServerboundResourcePackPacket$Action$DECLINED;
|
||||
public static final Object instance$ServerboundResourcePackPacket$Action$FAILED_DOWNLOAD;
|
||||
public static final Object instance$ServerboundResourcePackPacket$Action$ACCEPTED;
|
||||
public static final Object instance$ServerboundResourcePackPacket$Action$DOWNLOADED;
|
||||
public static final Object instance$ServerboundResourcePackPacket$Action$INVALID_URL;
|
||||
public static final Object instance$ServerboundResourcePackPacket$Action$FAILED_RELOAD;
|
||||
public static final Object instance$ServerboundResourcePackPacket$Action$DISCARDED;
|
||||
|
||||
static {
|
||||
try {
|
||||
Object[] values = (Object[]) method$ServerboundResourcePackPacket$Action$values.invoke(null);
|
||||
instance$ServerboundResourcePackPacket$Action$SUCCESSFULLY_LOADED = values[0];
|
||||
instance$ServerboundResourcePackPacket$Action$DECLINED = values[1];
|
||||
instance$ServerboundResourcePackPacket$Action$FAILED_DOWNLOAD = values[2];
|
||||
instance$ServerboundResourcePackPacket$Action$ACCEPTED = values[3];
|
||||
if (VersionHelper.isVersionNewerThan1_20_3()) {
|
||||
instance$ServerboundResourcePackPacket$Action$DOWNLOADED = values[4];
|
||||
instance$ServerboundResourcePackPacket$Action$INVALID_URL = values[5];
|
||||
instance$ServerboundResourcePackPacket$Action$FAILED_RELOAD = values[6];
|
||||
instance$ServerboundResourcePackPacket$Action$DISCARDED = values[7];
|
||||
} else {
|
||||
instance$ServerboundResourcePackPacket$Action$DOWNLOADED = null;
|
||||
instance$ServerboundResourcePackPacket$Action$INVALID_URL = null;
|
||||
instance$ServerboundResourcePackPacket$Action$FAILED_RELOAD = null;
|
||||
instance$ServerboundResourcePackPacket$Action$DISCARDED = null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@@ -6540,4 +6561,10 @@ public class Reflections {
|
||||
ReflectionUtils.getClazz(
|
||||
BukkitReflectionUtils.assembleMCClass("network.protocol.login.ServerboundLoginAcknowledgedPacket")
|
||||
);
|
||||
|
||||
public static final Field field$ServerboundResourcePackPacket$action = requireNonNull(
|
||||
ReflectionUtils.getDeclaredField(
|
||||
clazz$ServerboundResourcePackPacket, clazz$ServerboundResourcePackPacket$Action, 0
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user