From 86d85530bc3cc448aea134690ae415fabec8e891 Mon Sep 17 00:00:00 2001 From: violetc <58360096+s-yh-china@users.noreply.github.com> Date: Fri, 26 Jul 2024 17:48:42 +0800 Subject: [PATCH] Update PCA, fix #280 --- ...006-Leaves-Server-Config-And-Command.patch | 29 ++-- patches/server/0036-PCA-sync-protocol.patch | 131 ++++++++++++------ 2 files changed, 103 insertions(+), 57 deletions(-) diff --git a/patches/server/0006-Leaves-Server-Config-And-Command.patch b/patches/server/0006-Leaves-Server-Config-And-Command.patch index 2b36e7ee..fe19a511 100644 --- a/patches/server/0006-Leaves-Server-Config-And-Command.patch +++ b/patches/server/0006-Leaves-Server-Config-And-Command.patch @@ -85,10 +85,10 @@ index c2c421b1caf76b40542fdc436801accbe97a38cb..29f139fb4d70a9a362ac0a30579eb0b4 .withRequiredArg() diff --git a/src/main/java/org/leavesmc/leaves/LeavesConfig.java b/src/main/java/org/leavesmc/leaves/LeavesConfig.java new file mode 100644 -index 0000000000000000000000000000000000000000..c0afb54a352bec9e311677c2a13e491efead093b +index 0000000000000000000000000000000000000000..403c5f4f374a7e7525f9a81c9e70b6b2791db248 --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/LeavesConfig.java -@@ -0,0 +1,925 @@ +@@ -0,0 +1,930 @@ +package org.leavesmc.leaves; + +import com.destroystokyo.paper.util.SneakyThrow; @@ -789,24 +789,29 @@ index 0000000000000000000000000000000000000000..c0afb54a352bec9e311677c2a13e491e + + // Leaves end - protocol - syncmatica + -+ @GlobalConfig(name = "pca-sync-protocol", category = "protocol") ++ @GlobalConfig(name = "pca-sync-protocol", category = "protocol", verify = PcaVerify.class) + public static boolean pcaSyncProtocol = false; + -+ @GlobalConfig(name = "pca-sync-player-entity", category = "protocol", verify = PcaPlayerEntityVerify.class) -+ public static String pcaSyncPlayerEntity = "OPS"; -+ -+ private static class PcaPlayerEntityVerify extends ConfigVerify.StringConfigVerify { -+ private static final List pcaSyncPlayerEntityList = List.of("NOBODY", "BOT", "OPS", "OPS_AND_SELF", "EVERYONE"); -+ ++ public static class PcaVerify extends ConfigVerify.BooleanConfigVerify { + @Override -+ public String check(String old, String value) { -+ if (!pcaSyncPlayerEntityList.contains(value)) { -+ return "pca-sync-player-entity value error"; ++ public String check(Boolean old, Boolean value) { ++ if (old != null && old != value) { ++ org.leavesmc.leaves.protocol.PcaSyncProtocol.onConfigModify(value); + } + return null; + } + } + ++ @GlobalConfig(name = "pca-sync-player-entity", category = "protocol", verify = PcaPlayerEntityVerify.class) ++ public static PcaPlayerEntityType pcaSyncPlayerEntity = PcaPlayerEntityType.OPS; ++ ++ public enum PcaPlayerEntityType { ++ NOBODY, BOT, OPS, OPS_AND_SELF, EVERYONE ++ } ++ ++ private static class PcaPlayerEntityVerify extends ConfigVerify.EnumConfigVerify { ++ } ++ + @GlobalConfig(name = "bbor-protocol", category = "protocol") + public static boolean bborProtocol = false; + diff --git a/patches/server/0036-PCA-sync-protocol.patch b/patches/server/0036-PCA-sync-protocol.patch index 222e9cb0..66642f31 100644 --- a/patches/server/0036-PCA-sync-protocol.patch +++ b/patches/server/0036-PCA-sync-protocol.patch @@ -295,10 +295,10 @@ index 0d68db20f5fbe5e834f12c1e8fd429099a44e4b6..5b62860cd64b5e6dc02dadb4651824ac return ShulkerBoxBlockEntity.SLOTS; diff --git a/src/main/java/org/leavesmc/leaves/protocol/PcaSyncProtocol.java b/src/main/java/org/leavesmc/leaves/protocol/PcaSyncProtocol.java new file mode 100644 -index 0000000000000000000000000000000000000000..7a86bdc3ddbed0399cdb909cb294d8f8bb5a894f +index 0000000000000000000000000000000000000000..7126ec4b6e0a1bfa16d97fd21d7ae8955a66565c --- /dev/null +++ b/src/main/java/org/leavesmc/leaves/protocol/PcaSyncProtocol.java -@@ -0,0 +1,391 @@ +@@ -0,0 +1,432 @@ +package org.leavesmc.leaves.protocol; + +import net.minecraft.core.BlockPos; @@ -348,8 +348,6 @@ index 0000000000000000000000000000000000000000..7a86bdc3ddbed0399cdb909cb294d8f8 + // send + private static final ResourceLocation ENABLE_PCA_SYNC_PROTOCOL = id("enable_pca_sync_protocol"); + private static final ResourceLocation DISABLE_PCA_SYNC_PROTOCOL = id("disable_pca_sync_protocol"); -+ private static final ResourceLocation UPDATE_ENTITY = id("update_entity"); -+ private static final ResourceLocation UPDATE_BLOCK_ENTITY = id("update_block_entity"); + + private static final Map> playerWatchBlockPos = new HashMap<>(); + private static final Map> playerWatchEntity = new HashMap<>(); @@ -370,15 +368,6 @@ index 0000000000000000000000000000000000000000..7a86bdc3ddbed0399cdb909cb294d8f8 + } + } + -+ @ProtocolHandler.ReloadServer -+ private static void onServerReload() { -+ if (LeavesConfig.pcaSyncProtocol) { -+ enablePcaSyncProtocolGlobal(); -+ } else { -+ disablePcaSyncProtocolGlobal(); -+ } -+ } -+ + @ProtocolHandler.PayloadReceiver(payload = EmptyPayload.class, payloadId = "cancel_sync_block_entity") + private static void cancelSyncBlockEntityHandler(ServerPlayer player, EmptyPayload payload) { + if (!LeavesConfig.pcaSyncProtocol) { @@ -400,6 +389,7 @@ index 0000000000000000000000000000000000000000..7a86bdc3ddbed0399cdb909cb294d8f8 + if (!LeavesConfig.pcaSyncProtocol) { + return; + } ++ + MinecraftServer server = MinecraftServer.getServer(); + BlockPos pos = payload.pos; + ServerLevel world = player.serverLevel(); @@ -411,7 +401,7 @@ index 0000000000000000000000000000000000000000..7a86bdc3ddbed0399cdb909cb294d8f8 + BlockEntity blockEntityAdj = null; + if (blockState.getBlock() instanceof ChestBlock) { + if (blockState.getValue(ChestBlock.TYPE) != ChestType.SINGLE) { -+ BlockPos posAdj = pos.offset(ChestBlock.getConnectedDirection(blockState).getNormal()); ++ BlockPos posAdj = pos.relative(ChestBlock.getConnectedDirection(blockState)); + // The method in World now checks that the caller is from the same thread... + blockEntityAdj = world.getChunk(posAdj).getBlockEntity(posAdj); + } @@ -443,34 +433,39 @@ index 0000000000000000000000000000000000000000..7a86bdc3ddbed0399cdb909cb294d8f8 + if (!LeavesConfig.pcaSyncProtocol) { + return; + } ++ + MinecraftServer server = MinecraftServer.getServer(); + int entityId = payload.entityId; + ServerLevel world = player.serverLevel(); ++ + server.execute(() -> { + Entity entity = world.getEntity(entityId); ++ + if (entity != null) { + clearPlayerWatchData(player); ++ + if (entity instanceof Player) { -+ if (LeavesConfig.pcaSyncPlayerEntity.equals("NOBODY")) { -+ return; -+ } else if (LeavesConfig.pcaSyncPlayerEntity.equals("BOT")) { -+ if (!(entity instanceof ServerBot)) { ++ switch (LeavesConfig.pcaSyncPlayerEntity) { ++ case NOBODY -> { + return; + } -+ } else if (LeavesConfig.pcaSyncPlayerEntity.equals("OPS")) { -+ if (!(entity instanceof ServerBot) && server.getProfilePermissions(player.getGameProfile()) < 2) { -+ return; ++ case BOT -> { ++ if (!(entity instanceof ServerBot)) { ++ return; ++ } + } -+ } else if (LeavesConfig.pcaSyncPlayerEntity.equals("OPS_AND_SELF")) { -+ if (!(entity instanceof ServerBot) && -+ server.getProfilePermissions(player.getGameProfile()) < 2 && -+ entity != player) { -+ return; ++ case OPS -> { ++ if (!(entity instanceof ServerBot) && server.getPlayerList().isOp(player.gameProfile)) { ++ return; ++ } ++ } ++ case OPS_AND_SELF -> { ++ if (!(entity instanceof ServerBot) && server.getPlayerList().isOp(player.gameProfile) && entity != player) { ++ return; ++ } + } -+ } else if (!LeavesConfig.pcaSyncPlayerEntity.equals("EVERYONE")) { -+ // wtf???? -+ LeavesLogger.LOGGER.warning("pcaSyncPlayerEntity wtf???"); -+ return; ++ case EVERYONE -> {} ++ case null -> LeavesLogger.LOGGER.warning("pcaSyncPlayerEntity wtf???"); + } + } + updateEntity(player, entity); @@ -487,8 +482,18 @@ index 0000000000000000000000000000000000000000..7a86bdc3ddbed0399cdb909cb294d8f8 + }); + } + ++ public static void onConfigModify(boolean enable) { ++ if (enable) { ++ enablePcaSyncProtocolGlobal(); ++ } else { ++ disablePcaSyncProtocolGlobal(); ++ } ++ } ++ + public static void enablePcaSyncProtocol(@NotNull ServerPlayer player) { + ProtocolUtils.sendEmptyPayloadPacket(player, ENABLE_PCA_SYNC_PROTOCOL); ++ lock.lock(); ++ lock.unlock(); + } + + public static void disablePcaSyncProtocol(@NotNull ServerPlayer player) { @@ -497,11 +502,7 @@ index 0000000000000000000000000000000000000000..7a86bdc3ddbed0399cdb909cb294d8f8 + + public static void updateEntity(@NotNull ServerPlayer player, @NotNull Entity entity) { + CompoundTag nbt = entity.saveWithoutId(new CompoundTag()); -+ ProtocolUtils.sendPayloadPacket(player, UPDATE_ENTITY, buf -> { -+ buf.writeResourceLocation(entity.level().dimension().location()); -+ buf.writeInt(entity.getId()); -+ buf.writeNbt(nbt); -+ }); ++ ProtocolUtils.sendPayloadPacket(player, new UpdateEntityPayload(entity.level().dimension().location(), entity.getId(), nbt)); + } + + public static void updateBlockEntity(@NotNull ServerPlayer player, @NotNull BlockEntity blockEntity) { @@ -511,11 +512,7 @@ index 0000000000000000000000000000000000000000..7a86bdc3ddbed0399cdb909cb294d8f8 + return; + } + -+ ProtocolUtils.sendPayloadPacket(player, UPDATE_BLOCK_ENTITY, buf -> { -+ buf.writeResourceLocation(world.dimension().location()); -+ buf.writeBlockPos(blockEntity.getBlockPos()); -+ buf.writeNbt(blockEntity.saveWithId(blockEntity.getLevel().registryAccess())); -+ }); ++ ProtocolUtils.sendPayloadPacket(player, new UpdateBlockEntityPayload(world.dimension().location(), blockEntity.getBlockPos(), blockEntity.saveWithoutMetadata(world.registryAccess()))); + } + + private static MutablePair getResourceLocationEntityPair(ResourceLocation ResourceLocation, Entity entity) { @@ -575,7 +572,7 @@ index 0000000000000000000000000000000000000000..7a86bdc3ddbed0399cdb909cb294d8f8 + + if (blockState.getBlock() instanceof ChestBlock) { + if (blockState.getValue(ChestBlock.TYPE) != ChestType.SINGLE) { -+ BlockPos posAdj = pos.offset(ChestBlock.getConnectedDirection(blockState).getNormal()); ++ BlockPos posAdj = pos.relative(ChestBlock.getConnectedDirection(blockState)); + playerListAdj = getWatchPlayerList(world, posAdj); + } + } @@ -650,17 +647,61 @@ index 0000000000000000000000000000000000000000..7a86bdc3ddbed0399cdb909cb294d8f8 + PcaSyncProtocol.clearPlayerWatchEntity(player); + } + ++ public record UpdateEntityPayload(ResourceLocation dimension, int entityId, CompoundTag tag) implements LeavesCustomPayload { ++ ++ public static final ResourceLocation UPDATE_ENTITY = PcaSyncProtocol.id("update_entity"); ++ ++ @New ++ public UpdateEntityPayload(ResourceLocation location, FriendlyByteBuf byteBuf) { ++ this(byteBuf.readResourceLocation(), byteBuf.readInt(), byteBuf.readNbt()); ++ } ++ ++ @Override ++ public void write(@NotNull FriendlyByteBuf buf) { ++ buf.writeResourceLocation(this.dimension); ++ buf.writeInt(this.entityId); ++ buf.writeNbt(this.tag); ++ } ++ ++ @Override ++ public ResourceLocation id() { ++ return UPDATE_ENTITY; ++ } ++ } ++ ++ public record UpdateBlockEntityPayload(ResourceLocation dimension, BlockPos blockPos, CompoundTag tag) implements LeavesCustomPayload { ++ ++ private static final ResourceLocation UPDATE_BLOCK_ENTITY = PcaSyncProtocol.id("update_block_entity"); ++ ++ @New ++ public UpdateBlockEntityPayload(ResourceLocation location, @NotNull FriendlyByteBuf byteBuf) { ++ this(byteBuf.readResourceLocation(), byteBuf.readBlockPos(), byteBuf.readNbt()); ++ } ++ ++ @Override ++ public void write(@NotNull FriendlyByteBuf buf) { ++ buf.writeResourceLocation(this.dimension); ++ buf.writeBlockPos(this.blockPos); ++ buf.writeNbt(this.tag); ++ } ++ ++ @Override ++ public ResourceLocation id() { ++ return UPDATE_BLOCK_ENTITY; ++ } ++ } ++ + public record SyncBlockEntityPayload(BlockPos pos) implements LeavesCustomPayload { + + public static final ResourceLocation SYNC_BLOCK_ENTITY = PcaSyncProtocol.id("sync_block_entity"); + + @New -+ public SyncBlockEntityPayload(ResourceLocation id, FriendlyByteBuf buf) { ++ public SyncBlockEntityPayload(ResourceLocation id, @NotNull FriendlyByteBuf buf) { + this(buf.readBlockPos()); + } + + @Override -+ public void write(FriendlyByteBuf buf) { ++ public void write(@NotNull FriendlyByteBuf buf) { + buf.writeBlockPos(pos); + } + @@ -675,12 +716,12 @@ index 0000000000000000000000000000000000000000..7a86bdc3ddbed0399cdb909cb294d8f8 + public static final ResourceLocation SYNC_ENTITY = PcaSyncProtocol.id("sync_entity"); + + @New -+ public SyncEntityPayload(ResourceLocation id, FriendlyByteBuf buf) { ++ public SyncEntityPayload(ResourceLocation id, @NotNull FriendlyByteBuf buf) { + this(buf.readInt()); + } + + @Override -+ public void write(FriendlyByteBuf buf) { ++ public void write(@NotNull FriendlyByteBuf buf) { + buf.writeInt(entityId); + } +