From d3ae85f9cb50d1aa7ae6efe613d796bf7eed438b Mon Sep 17 00:00:00 2001 From: XiaoMoMi Date: Sat, 24 May 2025 03:34:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=81=E4=B9=85=E5=8C=96=E5=AD=98=E5=82=A8?= =?UTF-8?q?=E5=86=B7=E5=8D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bukkit/loader/src/main/resources/commands.yml | 7 ++++ .../src/main/resources/translations/en.yml | 2 +- .../src/main/resources/translations/zh_cn.yml | 2 +- .../plugin/command/BukkitCommandManager.java | 1 + .../feature/DebugClearCooldownCommand.java | 33 +++++++++++++++++ .../plugin/network/BukkitNetworkManager.java | 35 +++++++++---------- .../core/plugin/context/CooldownData.java | 9 ++++- .../context/condition/CommonConditions.java | 2 +- ...ondition.java => OnCooldownCondition.java} | 10 +++--- .../plugin/context/event/EventConditions.java | 2 +- 10 files changed, 74 insertions(+), 29 deletions(-) create mode 100644 bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/command/feature/DebugClearCooldownCommand.java rename core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/{CooldownCondition.java => OnCooldownCondition.java} (79%) diff --git a/bukkit/loader/src/main/resources/commands.yml b/bukkit/loader/src/main/resources/commands.yml index fdf4e1613..d600a8e7c 100644 --- a/bukkit/loader/src/main/resources/commands.yml +++ b/bukkit/loader/src/main/resources/commands.yml @@ -175,6 +175,13 @@ debug_is_section_injected: - /craftengine debug is-section-injected - /ce debug is-section-injected +debug_clear_cooldown: + enable: true + permission: ce.command.debug.clear_cooldown + usage: + - /craftengine debug clear-cooldown + - /ce debug clear-cooldown + debug_test: enable: true permission: ce.command.debug.test diff --git a/bukkit/loader/src/main/resources/translations/en.yml b/bukkit/loader/src/main/resources/translations/en.yml index 3fb06992d..0ee61b98d 100644 --- a/bukkit/loader/src/main/resources/translations/en.yml +++ b/bukkit/loader/src/main/resources/translations/en.yml @@ -101,7 +101,7 @@ warning.config.condition.expression.missing_expression: "Issue found in warning.config.condition.is_null.missing_argument: "Issue found in file - The config '' is missing the required 'argument' argument for 'is_null' condition." warning.config.condition.hand.missing_hand: "Issue found in file - The config '' is missing the required 'hand' argument for 'hand' condition." warning.config.condition.hand.invalid_hand: "Issue found in file - The config '' is using an invalid 'hand' argument '' for 'hand' condition. Allowed hand types: []" -warning.config.condition.cooldown.missing_id: "Issue found in file - The config '' is missing the required 'id' argument for 'cooldown' condition." +warning.config.condition.on_cooldown.missing_id: "Issue found in file - The config '' is missing the required 'id' argument for 'on_cooldown' condition." warning.config.structure.not_section: "Issue found in file - The config '' is expected to be a config section while it's actually a(n) ''." warning.config.image.duplicate: "Issue found in file - Duplicated image ''. Please check if there is the same configuration in other files." warning.config.image.missing_height: "Issue found in file - The image '' is missing the required 'height' argument." diff --git a/bukkit/loader/src/main/resources/translations/zh_cn.yml b/bukkit/loader/src/main/resources/translations/zh_cn.yml index 804f0414b..1b87ce60b 100644 --- a/bukkit/loader/src/main/resources/translations/zh_cn.yml +++ b/bukkit/loader/src/main/resources/translations/zh_cn.yml @@ -102,7 +102,7 @@ warning.config.condition.is_null.missing_argument: "在文件 warning.config.structure.not_section: "在文件 发现问题 - 配置项 '' 应为配置段落 但实际类型为 ''" warning.config.condition.hand.missing_hand: "在文件 发现问题 - 配置项 '' 缺少 'hand' 条件必需的 'hand' 参数" warning.config.condition.hand.invalid_hand: "在文件 发现问题 - 配置项 '' 使用了无效的 'hand' 参数 ''('hand' 条件)。允许的手部类型: []" -warning.config.condition.cooldown.missing_id: "在文件 发现问题 - 配置项 '' 缺少 'cooldown' 条件必需的 'id' 参数" +warning.config.condition.on_cooldown.missing_id: "在文件 发现问题 - 配置项 '' 缺少 'on_cooldown' 条件必需的 'id' 参数" warning.config.image.duplicate: "在文件 发现问题 - 重复的图片配置 '' 请检查其他文件中是否存在相同配置" warning.config.image.missing_height: "在文件 发现问题 - 图片 '' 缺少必需的 'height' 参数" warning.config.image.height_ascent_conflict: "在文件 发现问题 - 图片 '' 违反位图规则: 'height' 参数 '' 必须不小于 'ascent' 参数 ''" diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/command/BukkitCommandManager.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/command/BukkitCommandManager.java index c1f1c3f39..0eee9c0aa 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/command/BukkitCommandManager.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/command/BukkitCommandManager.java @@ -41,6 +41,7 @@ public class BukkitCommandManager extends AbstractCommandManager new DebugGetBlockStateRegistryIdCommand(this, plugin), new DebugGetBlockInternalIdCommand(this, plugin), new DebugAppearanceStateUsageCommand(this, plugin), + new DebugClearCooldownCommand(this, plugin), new DebugRealStateUsageCommand(this, plugin), new DebugItemDataCommand(this, plugin), new DebugSetBlockCommand(this, plugin), diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/command/feature/DebugClearCooldownCommand.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/command/feature/DebugClearCooldownCommand.java new file mode 100644 index 000000000..757627d11 --- /dev/null +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/command/feature/DebugClearCooldownCommand.java @@ -0,0 +1,33 @@ +package net.momirealms.craftengine.bukkit.plugin.command.feature; + +import net.kyori.adventure.text.Component; +import net.momirealms.craftengine.bukkit.plugin.command.BukkitCommandFeature; +import net.momirealms.craftengine.bukkit.plugin.user.BukkitServerPlayer; +import net.momirealms.craftengine.core.plugin.CraftEngine; +import net.momirealms.craftengine.core.plugin.command.CraftEngineCommandManager; +import org.bukkit.command.CommandSender; +import org.incendo.cloud.Command; +import org.incendo.cloud.bukkit.parser.PlayerParser; + +public class DebugClearCooldownCommand extends BukkitCommandFeature { + + public DebugClearCooldownCommand(CraftEngineCommandManager commandManager, CraftEngine plugin) { + super(commandManager, plugin); + } + + @Override + public Command.Builder assembleCommand(org.incendo.cloud.CommandManager manager, Command.Builder builder) { + return builder + .required("player", PlayerParser.playerParser()) + .handler(context -> { + BukkitServerPlayer serverPlayer = plugin().adapt(context.get("player")); + serverPlayer.cooldown().clearCooldowns(); + plugin().senderFactory().wrap(context.sender()).sendMessage(Component.text("Done clearing cooldowns!")); + }); + } + + @Override + public String getFeatureID() { + return "debug_clear_cooldown"; + } +} diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/BukkitNetworkManager.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/BukkitNetworkManager.java index 82b8ea92a..df14f8079 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/BukkitNetworkManager.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/BukkitNetworkManager.java @@ -198,26 +198,21 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes @EventHandler(priority = EventPriority.LOWEST) public void onPlayerQuit(PlayerQuitEvent event) { Player player = event.getPlayer(); - Channel channel = getChannel(player); - NetWorkUser user = removeUser(channel); - if (user == null) return; - saveCooldown(player, user); - handleDisconnection(channel); - this.onlineUsers.remove(player.getUniqueId()); - this.resetUserArray(); + BukkitServerPlayer serverPlayer = this.onlineUsers.remove(player.getUniqueId()); + if (serverPlayer != null) { + this.resetUserArray(); + this.saveCooldown(player, serverPlayer.cooldown()); + } } - private void saveCooldown(Player player, NetWorkUser user) { - if (user instanceof BukkitServerPlayer serverPlayer) { - CooldownData cd = serverPlayer.cooldown(); - if (cd != null) { - try { - byte[] data = CooldownData.toBytes(cd); - player.getPersistentDataContainer().set(KeyUtils.toNamespacedKey(CooldownData.COOLDOWN_KEY), PersistentDataType.BYTE_ARRAY, data); - } catch (IOException e) { - player.getPersistentDataContainer().remove(KeyUtils.toNamespacedKey(CooldownData.COOLDOWN_KEY)); - this.plugin.logger().warn("Failed to save cooldown for player " + player.getName(), e); - } + private void saveCooldown(Player player, CooldownData cd) { + if (cd != null && player != null) { + try { + byte[] data = CooldownData.toBytes(cd); + player.getPersistentDataContainer().set(KeyUtils.toNamespacedKey(CooldownData.COOLDOWN_KEY), PersistentDataType.BYTE_ARRAY, data); + } catch (IOException e) { + player.getPersistentDataContainer().remove(KeyUtils.toNamespacedKey(CooldownData.COOLDOWN_KEY)); + this.plugin.logger().warn("Failed to save cooldown for player " + player.getName(), e); } } } @@ -465,7 +460,9 @@ public class BukkitNetworkManager implements NetworkManager, Listener, PluginMes String encoderName = pipeline.names().contains("outbound_config") ? "outbound_config" : "encoder"; pipeline.addBefore(encoderName, PACKET_ENCODER, new PluginChannelEncoder(user)); - channel.closeFuture().addListener((ChannelFutureListener) future -> handleDisconnection(user.nettyChannel())); + channel.closeFuture().addListener((ChannelFutureListener) future -> { + handleDisconnection(user.nettyChannel()); + }); setUser(channel, user); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/CooldownData.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/CooldownData.java index f693e573f..235888530 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/CooldownData.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/CooldownData.java @@ -65,11 +65,18 @@ public class CooldownData { if (entry.getValue() instanceof LongTag longTag) { long expire = longTag.getAsLong(); if (currentTime < expire) { - cd.setCooldown(entry.getKey(), expire); + cd.cooldownMap.put(entry.getKey(), expire); } } } } return cd; } + + @Override + public String toString() { + return "CooldownData{" + + "cooldownMap=" + cooldownMap + + '}'; + } } \ No newline at end of file diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/CommonConditions.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/CommonConditions.java index 43ac2cb0d..a5521169c 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/CommonConditions.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/CommonConditions.java @@ -18,7 +18,7 @@ public final class CommonConditions { public static final Key FALLING_BLOCK = Key.from("craftengine:falling_block"); public static final Key DISTANCE = Key.from("craftengine:distance"); public static final Key PERMISSION = Key.from("craftengine:permission"); - public static final Key COOLDOWN = Key.from("craftengine:cooldown"); + public static final Key ON_COOLDOWN = Key.from("craftengine:on_cooldown"); public static final Key EQUALS = Key.from("craftengine:equals"); public static final Key STRING_EQUALS = Key.from("craftengine:string_equals"); public static final Key STRING_CONTAINS = Key.from("craftengine:string_contains"); diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/CooldownCondition.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/OnCooldownCondition.java similarity index 79% rename from core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/CooldownCondition.java rename to core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/OnCooldownCondition.java index 785866de7..fd47802b5 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/CooldownCondition.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/condition/OnCooldownCondition.java @@ -10,16 +10,16 @@ import net.momirealms.craftengine.core.util.ResourceConfigUtils; import java.util.Map; import java.util.Optional; -public class CooldownCondition implements Condition { +public class OnCooldownCondition implements Condition { private final String key; - public CooldownCondition(String key) { + public OnCooldownCondition(String key) { this.key = key; } @Override public Key type() { - return CommonConditions.COOLDOWN; + return CommonConditions.ON_COOLDOWN; } @Override @@ -36,8 +36,8 @@ public class CooldownCondition implements Condition { @Override public Condition create(Map arguments) { - String id = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("id"), "warning.config.condition.cooldown.missing_id"); - return new CooldownCondition<>(id); + String id = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("id"), "warning.config.condition.on_cooldown.missing_id"); + return new OnCooldownCondition<>(id); } } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/event/EventConditions.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/event/EventConditions.java index a46d7a09b..5e6a110fa 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/event/EventConditions.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/event/EventConditions.java @@ -36,7 +36,7 @@ public class EventConditions { register(CommonConditions.EXPRESSION, new ExpressionCondition.FactoryImpl<>()); register(CommonConditions.IS_NULL, new IsNullCondition.FactoryImpl<>()); register(CommonConditions.HAND, new HandCondition.FactoryImpl<>()); - register(CommonConditions.COOLDOWN, new CooldownCondition.FactoryImpl<>()); + register(CommonConditions.ON_COOLDOWN, new OnCooldownCondition.FactoryImpl<>()); } public static void register(Key key, ConditionFactory factory) {