mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-25 09:59:20 +00:00
Merge pull request #105 from jhqwqmc/dev
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,83 @@ 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);
|
||||
}
|
||||
} else {
|
||||
buf.writeBoolean(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")
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -135,6 +135,7 @@ public class Config {
|
||||
protected boolean image$intercept_packets$text_display;
|
||||
protected boolean image$intercept_packets$armor_stand;
|
||||
protected boolean image$intercept_packets$player_info;
|
||||
protected boolean image$intercept_packets$set_score;
|
||||
|
||||
public Config(CraftEngine plugin) {
|
||||
this.plugin = plugin;
|
||||
@@ -304,6 +305,7 @@ public class Config {
|
||||
image$intercept_packets$text_display = config.getBoolean("image.intercept-packets.text-display", true);
|
||||
image$intercept_packets$armor_stand = config.getBoolean("image.intercept-packets.armor-stand", true);
|
||||
image$intercept_packets$player_info = config.getBoolean("image.intercept-packets.player-info", true);
|
||||
image$intercept_packets$set_score = config.getBoolean("image.intercept-packets.set-score", true);
|
||||
|
||||
Class<?> modClazz = ReflectionUtils.getClazz(CraftEngine.MOD_CLASS);
|
||||
if (modClazz != null) {
|
||||
@@ -656,6 +658,10 @@ public class Config {
|
||||
return instance.image$intercept_packets$player_info;
|
||||
}
|
||||
|
||||
public static boolean interceptSetScore() {
|
||||
return instance.image$intercept_packets$set_score;
|
||||
}
|
||||
|
||||
public YamlDocument loadOrCreateYamlData(String fileName) {
|
||||
File file = new File(this.plugin.dataFolderFile(), fileName);
|
||||
if (!file.exists()) {
|
||||
|
||||
@@ -3,7 +3,7 @@ org.gradle.jvmargs=-Xmx1G
|
||||
# Project settings
|
||||
# Rule: [major update].[feature update].[bug fix]
|
||||
project_version=1.0.0
|
||||
config_version=24
|
||||
config_version=25
|
||||
lang_version=4
|
||||
project_group=net.momirealms
|
||||
latest_supported_version=1.21.5
|
||||
|
||||
Reference in New Issue
Block a user