mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-27 02:49:15 +00:00
feat(network): 拦截和处理设置分数的网络数据包
This commit is contained in:
@@ -158,6 +158,7 @@ image:
|
||||
system-chat: true
|
||||
tab-list: true # Tab list header and footer
|
||||
player-info: true # User list in tab
|
||||
set-score: true
|
||||
actionbar: true
|
||||
title: true
|
||||
bossbar: true
|
||||
|
||||
@@ -157,6 +157,7 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes
|
||||
registerByteBufPacketConsumer(VersionHelper.isVersionNewerThan1_20_3() ? PacketConsumers.TAB_LIST_1_20_3 : PacketConsumers.TAB_LIST_1_20, this.packetIds.clientboundTabListPacket());
|
||||
registerByteBufPacketConsumer(VersionHelper.isVersionNewerThan1_20_3() ? PacketConsumers.TEAM_1_20_3 : PacketConsumers.TEAM_1_20, this.packetIds.clientboundSetPlayerTeamPacket());
|
||||
registerByteBufPacketConsumer(VersionHelper.isVersionNewerThan1_20_3() ? PacketConsumers.SET_OBJECTIVE_1_20_3 : PacketConsumers.SET_OBJECTIVE_1_20, this.packetIds.clientboundSetObjectivePacket());
|
||||
registerByteBufPacketConsumer(PacketConsumers.SET_SCORE_1_20_3, VersionHelper.isVersionNewerThan1_20_3() ? this.packetIds.clientboundSetScorePacket() : -1);
|
||||
registerByteBufPacketConsumer(PacketConsumers.REMOVE_ENTITY, this.packetIds.clientboundRemoveEntitiesPacket());
|
||||
registerByteBufPacketConsumer(PacketConsumers.ADD_ENTITY_BYTEBUFFER, this.packetIds.clientboundAddEntityPacket());
|
||||
registerByteBufPacketConsumer(PacketConsumers.SOUND, this.packetIds.clientboundSoundPacket());
|
||||
|
||||
@@ -33,6 +33,7 @@ import net.momirealms.craftengine.core.world.chunk.PalettedContainer;
|
||||
import net.momirealms.craftengine.core.world.chunk.packet.MCSection;
|
||||
import net.momirealms.sparrow.nbt.Tag;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@@ -1275,7 +1276,7 @@ public class PacketConsumers {
|
||||
// do ray trace to get current block
|
||||
RayTraceResult result = bukkitPlayer.rayTraceBlocks(interactionRange, FluidCollisionMode.NEVER);
|
||||
if (result == null) return;
|
||||
org.bukkit.block.Block hitBlock = result.getHitBlock();
|
||||
Block hitBlock = result.getHitBlock();
|
||||
if (hitBlock == null) return;
|
||||
ImmutableBlockState state = BukkitBlockManager.instance().getImmutableBlockState(BlockStateUtils.blockDataToId(hitBlock.getBlockData()));
|
||||
// not a custom block
|
||||
@@ -2006,4 +2007,81 @@ public class PacketConsumers {
|
||||
CraftEngine.instance().logger().warn("Failed to handle ClientboundSetEntityDataPacket", e);
|
||||
}
|
||||
};
|
||||
|
||||
public static final BiConsumer<NetWorkUser, ByteBufPacketEvent> SET_SCORE_1_20_3 = (user, event) -> {
|
||||
try {
|
||||
if (!Config.interceptSetScore()) return;
|
||||
boolean isChanged = false;
|
||||
FriendlyByteBuf buf = event.getBuffer();
|
||||
String owner = buf.readUtf();
|
||||
String objectiveName = buf.readUtf();
|
||||
int score = buf.readVarInt();
|
||||
boolean hasDisplay = buf.readBoolean();
|
||||
Tag displayName = null;
|
||||
if (hasDisplay) {
|
||||
displayName = buf.readNbt(false);
|
||||
}
|
||||
outside : if (displayName != null) {
|
||||
Map<String, Component> tokens = CraftEngine.instance().imageManager().matchTags(displayName.getAsString());
|
||||
if (tokens.isEmpty()) break outside;
|
||||
Component component = AdventureHelper.tagToComponent(displayName);
|
||||
for (Map.Entry<String, Component> token : tokens.entrySet()) {
|
||||
component = component.replaceText(b -> b.matchLiteral(token.getKey()).replacement(token.getValue()));
|
||||
}
|
||||
displayName = AdventureHelper.componentToTag(component);
|
||||
isChanged = true;
|
||||
}
|
||||
boolean hasNumberFormat = buf.readBoolean();
|
||||
int format = -1;
|
||||
Tag style = null;
|
||||
Tag fixed = null;
|
||||
if (hasNumberFormat) {
|
||||
format = buf.readVarInt();
|
||||
if (format == 0) {
|
||||
if (displayName == null) return;
|
||||
} else if (format == 1) {
|
||||
if (displayName == null) return;
|
||||
style = buf.readNbt(false);
|
||||
} else if (format == 2) {
|
||||
fixed = buf.readNbt(false);
|
||||
if (fixed == null) return;
|
||||
Map<String, Component> tokens = CraftEngine.instance().imageManager().matchTags(fixed.getAsString());
|
||||
if (tokens.isEmpty() && !isChanged) return;
|
||||
if (!tokens.isEmpty()) {
|
||||
Component component = AdventureHelper.tagToComponent(fixed);
|
||||
for (Map.Entry<String, Component> token : tokens.entrySet()) {
|
||||
component = component.replaceText(b -> b.matchLiteral(token.getKey()).replacement(token.getValue()));
|
||||
}
|
||||
fixed = AdventureHelper.componentToTag(component);
|
||||
isChanged = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isChanged) {
|
||||
event.setChanged(true);
|
||||
buf.clear();
|
||||
buf.writeVarInt(event.packetID());
|
||||
buf.writeUtf(owner);
|
||||
buf.writeUtf(objectiveName);
|
||||
buf.writeVarInt(score);
|
||||
if (hasDisplay) {
|
||||
buf.writeBoolean(true);
|
||||
buf.writeNbt(displayName, false);
|
||||
} else {
|
||||
buf.writeBoolean(false);
|
||||
}
|
||||
if (hasNumberFormat) {
|
||||
buf.writeBoolean(true);
|
||||
buf.writeVarInt(format);
|
||||
if (format == 1) {
|
||||
buf.writeNbt(style, false);
|
||||
} else if (format == 2) {
|
||||
buf.writeNbt(fixed, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
CraftEngine.instance().logger().warn("Failed to handle ClientboundSetScorePacket", e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -39,4 +39,6 @@ public interface PacketIds {
|
||||
int clientboundLevelChunkWithLightPacket();
|
||||
|
||||
int clientboundPlayerInfoUpdatePacket();
|
||||
|
||||
int clientboundSetScorePacket();
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package net.momirealms.craftengine.bukkit.plugin.network.id;
|
||||
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.PacketIds;
|
||||
import net.momirealms.craftengine.bukkit.util.Reflections;
|
||||
import net.momirealms.craftengine.core.util.VersionHelper;
|
||||
|
||||
public class PacketIds1_20 implements PacketIds {
|
||||
|
||||
@@ -99,4 +100,9 @@ public class PacketIds1_20 implements PacketIds {
|
||||
public int clientboundPlayerInfoUpdatePacket() {
|
||||
return PacketIdFinder.clientboundByClazz(Reflections.clazz$ClientboundPlayerInfoUpdatePacket);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int clientboundSetScorePacket() {
|
||||
return PacketIdFinder.clientboundByClazz(Reflections.clazz$ClientboundSetScorePacket);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.momirealms.craftengine.bukkit.plugin.network.id;
|
||||
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.PacketIds;
|
||||
import net.momirealms.craftengine.bukkit.util.Reflections;
|
||||
|
||||
public class PacketIds1_20_5 implements PacketIds {
|
||||
|
||||
@@ -98,4 +99,9 @@ public class PacketIds1_20_5 implements PacketIds {
|
||||
public int clientboundPlayerInfoUpdatePacket() {
|
||||
return PacketIdFinder.clientboundByName("minecraft:player_info_update");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int clientboundSetScorePacket() {
|
||||
return PacketIdFinder.clientboundByName("minecraft:set_score");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6425,4 +6425,11 @@ public class Reflections {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static final Class<?> clazz$ClientboundSetScorePacket = requireNonNull(
|
||||
ReflectionUtils.getClazz(
|
||||
BukkitReflectionUtils.assembleMCClass("network.protocol.game.ClientboundSetScorePacket"),
|
||||
BukkitReflectionUtils.assembleMCClass("network.protocol.game.PacketPlayOutScoreboardScore")
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user