From bf5cbfa17285a1b31ae6c04b682ef7b398bc9879 Mon Sep 17 00:00:00 2001 From: MATRIX-feather Date: Mon, 13 Nov 2023 13:32:58 +0800 Subject: [PATCH] =?UTF-8?q?misc:=20=E4=B8=80=E4=BA=9B=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../events/InteractionMirrorProcessor.java | 100 ++++++++++-------- .../morph/misc/PlayerOperationSimulator.java | 21 +++- 2 files changed, 70 insertions(+), 51 deletions(-) diff --git a/src/main/java/xiamomc/morph/events/InteractionMirrorProcessor.java b/src/main/java/xiamomc/morph/events/InteractionMirrorProcessor.java index 6da66b8e..f6d7f223 100644 --- a/src/main/java/xiamomc/morph/events/InteractionMirrorProcessor.java +++ b/src/main/java/xiamomc/morph/events/InteractionMirrorProcessor.java @@ -202,6 +202,9 @@ public void onPlayerHurtEntity(EntityDamageByEntityEvent e) @Resolved private PlayerTracker tracker; + /** + * todo: SwingEvent应当只相应方块破坏 + */ @EventHandler public void onPlayerSwing(PlayerArmSwingEvent e) { @@ -215,6 +218,7 @@ public void onPlayerSwing(PlayerArmSwingEvent e) var playerInDistance = playerInDistance(player, inf); + //取消一定条件下源玩家的挥手动画 if (targetPlayer.getLocation().getWorld() == player.getLocation().getWorld() && Math.abs(targetPlayer.getLocation().distance(player.getLocation())) <= 6 && playerInDistance) @@ -231,8 +235,15 @@ public void onPlayerSwing(PlayerArmSwingEvent e) if (!playerInDistance || simStack.contains(targetPlayer)) return; - if (tracker.droppingItemThisTick(player)) + //若源玩家正在丢出物品,不要处理 + //若源玩家没有在破坏方块,不要处理 + //检查玩家在此tick内是否存在互动以避免重复镜像 + if (tracker.droppingItemThisTick(player) + || !tracker.isBreakingSuspect(player) + || tracker.interactingThisTick(player)) + { return; + } var lastAction = tracker.getLastInteractAction(player); @@ -249,9 +260,6 @@ public void onPlayerSwing(PlayerArmSwingEvent e) lastAction = PlayerTracker.InteractType.LEFT_CLICK_BLOCK; } - //检查玩家在此tick内是否存在互动以避免重复镜像 - if (tracker.interactingThisTick(player)) return; - simulateOperation(lastAction.toBukkitAction(), targetPlayer, player); logOperation(player, targetPlayer, lastAction.isLeftClick() ? OperationType.LeftClick : OperationType.RightClick); } @@ -318,59 +326,57 @@ private void clearStackIfPossible(Player player, boolean forceClear) return; } - // 清空Stack + // 如果目标实体不是玩家,那么清空simStack var targetedEntity = player.getTargetEntity(5); - if (targetedEntity instanceof Player targetedPlayer) + if (!(targetedEntity instanceof Player targetedPlayer)) + { + simStack.clear(); + return; + } + + //by_name: 直接清空 + //by_sight: 如果下个不是目标,那么清空 + var mode = selectionMode.get(); + if (mode.equalsIgnoreCase(InteractionMirrorSelectionMode.BY_NAME)) + { + simStack.clear(); + } + else { - //by_name: 直接清空 - //by_sight: 如果下个不是目标,那么清空 - var mode = selectionMode.get(); - if (mode.equalsIgnoreCase(InteractionMirrorSelectionMode.BY_NAME)) + //logger.info("Is by sight"); + var nextSelection = getMirrorTarget(player); + //logger.info("Next sel:" + nextSelection); + + // 如果玩家面向的目标不是下一个mirror对象,那么清空栈 + if (!targetedPlayer.equals(nextSelection.target)) { + //logger.info("Target not equal, clear"); simStack.clear(); + return; } - else - { - //logger.info("Is by sight"); - var nextSelection = getMirrorTarget(player); - //logger.info("Next sel:" + nextSelection); - // 如果玩家面向的目标不是下一个mirror对象,那么清空栈 - if (!targetedPlayer.equals(nextSelection.target)) - { - //logger.info("Target not equal, clear"); - simStack.clear(); - return; - } - - var targetPlayerTarget = targetedPlayer.getTargetEntity(5); + var targetPlayerTarget = targetedPlayer.getTargetEntity(5); - //if (debugOutput.get()) - // logger.info("Target player target:" + targetPlayerTarget); - - // 如果面向的玩家没有面向任何东西,那么也清空栈 - if (targetPlayerTarget == null) - { - //logger.info("Null target clear"); - simStack.clear(); - return; - } + //if (debugOutput.get()) + // logger.info("Target player target:" + targetPlayerTarget); - // 如果玩家面向的目标也在面向自己,那么清空栈 - if (player.equals(targetPlayerTarget)) - { - //logger.info("Circular clear"); - simStack.clear(); - return; - } + // 如果面向的玩家没有面向任何东西,那么也清空栈 + if (targetPlayerTarget == null) + { + //logger.info("Null target clear"); + simStack.clear(); + return; + } - //logger.info("All passes failed"); + // 如果玩家面向的目标也在面向自己,那么清空栈 + if (player.equals(targetPlayerTarget)) + { + //logger.info("Circular clear"); + simStack.clear(); + return; } - } - else - { - //logger.info("Outside Null target clear"); - simStack.clear(); + + //logger.info("All passes failed"); } } diff --git a/src/main/java/xiamomc/morph/misc/PlayerOperationSimulator.java b/src/main/java/xiamomc/morph/misc/PlayerOperationSimulator.java index 5b40fb1d..59817b50 100644 --- a/src/main/java/xiamomc/morph/misc/PlayerOperationSimulator.java +++ b/src/main/java/xiamomc/morph/misc/PlayerOperationSimulator.java @@ -32,6 +32,7 @@ import xiamomc.pluginbase.Bindables.Bindable; import java.util.Map; +import java.util.Objects; public class PlayerOperationSimulator extends MorphPluginObject { @@ -114,16 +115,18 @@ public SimulateResult simulateLeftClick(Player player) var targetBlock = traceResult == null ? null : traceResult.getHitBlock(); var targetEntity = traceResult == null ? null : traceResult.getHitEntity(); + // 忽略超过实体reach范围的实体 if (targetEntity != null && targetEntity.getLocation().distance(player.getLocation()) > entityReachDistance) targetEntity = null; //获取方块破坏信息 var destroyHandler = playerHandlerMap.getOrDefault(player, null); - //如果此时没有目标方块,那么移除此破坏信息的目标 + //如果视野内有实体或者不再存在目标方块,那么使destroyHandler重置进度 if ((targetBlock == null || targetEntity != null) && destroyHandler != null) destroyHandler.changeBlock(null); + //优先攻击实体 //如果目标实体不为null,则攻击该实体 if (targetEntity != null) { @@ -135,18 +138,27 @@ public SimulateResult simulateLeftClick(Player player) return SimulateResult.success(EquipmentSlot.HAND); } - //对着空气空挥 + //没有实体也没有方块,视为对着空气空挥 + //此时不要做任何事情,只返回success来允许播放挥手动画 if (targetBlock == null) return SimulateResult.success(EquipmentSlot.HAND); - //冒险模式,并且无法破坏目标方块 -> 操作成功(空挥) + //如果玩家处于冒险模式并且无法破坏目标方块,也不要做任何事情,只返回success来允许播放动画 if (player.getGameMode() == GameMode.ADVENTURE) { - var meta = player.getEquipment().getItemInMainHand().getItemMeta(); + var meta = Objects.requireNonNull(player.getEquipment(), "Null equipment?") + .getItemInMainHand().getItemMeta(); + if (meta == null || !meta.getDestroyableKeys().contains(targetBlock.getBlockData().getMaterial().getKey())) return SimulateResult.success(EquipmentSlot.HAND); } + //=-=-=-=-=-=-=-=-=-=-=-= + // + // 在此开始对方块的处理 + // + //=-=-=-=-=-=-=-=-=-=-=-= + //初始化destoryInfo if (destroyHandler == null) { @@ -218,6 +230,7 @@ public SimulateResult simulateRightClick(Player player) //获取互动位置 hitPos.subtract(targetEntity.getLocation().toVector()); + //轮流尝试左右手 if (this.tryUseItemOnEntity(player, targetEntity, itemInMainHand, InteractionHand.MAIN_HAND, hitPos)) return SimulateResult.success(EquipmentSlot.HAND);