9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-24 17:39:30 +00:00

优化交互

This commit is contained in:
XiaoMoMi
2025-07-26 17:54:58 +08:00
parent 3b5a2855ab
commit 422c091bfa
3 changed files with 35 additions and 1 deletions

View File

@@ -71,7 +71,11 @@ public class ItemEventListener implements Listener {
Entity entity = event.getRightClicked();
BukkitServerPlayer serverPlayer = this.plugin.adapt(player);
if (serverPlayer == null) return;
InteractionHand hand = event.getHand() == EquipmentSlot.HAND ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND;
// prevent duplicated interact air events
serverPlayer.updateLastInteractEntityTick(hand);
Item<ItemStack> itemInHand = serverPlayer.getItemInHand(hand);
if (ItemUtils.isEmpty(itemInHand)) return;
@@ -350,11 +354,16 @@ public class ItemEventListener implements Listener {
return;
// Gets the item in hand
InteractionHand hand = event.getHand() == EquipmentSlot.HAND ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND;
// prevents duplicated events
if (serverPlayer.lastInteractEntityCheck(hand)) {
return;
}
Item<ItemStack> itemInHand = serverPlayer.getItemInHand(hand);
// should never be null
if (ItemUtils.isEmpty(itemInHand)) return;
// todo 真的需要这个吗
// TODO 有必要存在吗?
if (cancelEventIfHasInteraction(event, serverPlayer, hand)) {
return;
}

View File

@@ -76,6 +76,9 @@ public class BukkitServerPlayer extends Player {
private Key clientSideDimension;
// check main hand/offhand interaction
private int lastSuccessfulInteraction;
// to prevent duplicated events
private int lastInteractEntityWithMainHand;
private int lastInteractEntityWithOffHand;
// re-sync attribute timely to prevent some bugs
private long lastAttributeSyncTime;
// for breaking blocks
@@ -234,6 +237,24 @@ public class BukkitServerPlayer extends Player {
return this.lastSuccessfulInteraction;
}
@Override
public void updateLastInteractEntityTick(@NotNull InteractionHand hand) {
if (hand == InteractionHand.MAIN_HAND) {
this.lastInteractEntityWithMainHand = gameTicks();
} else {
this.lastInteractEntityWithOffHand = gameTicks();
}
}
@Override
public boolean lastInteractEntityCheck(@NotNull InteractionHand hand) {
if (hand == InteractionHand.MAIN_HAND) {
return gameTicks() == this.lastInteractEntityWithMainHand;
} else {
return gameTicks() == this.lastInteractEntityWithOffHand;
}
}
@Override
public int gameTicks() {
return this.gameTicks;

View File

@@ -68,6 +68,10 @@ public abstract class Player extends AbstractEntity implements NetWorkUser {
public abstract int lastSuccessfulInteractionTick();
public abstract void updateLastInteractEntityTick(@NotNull InteractionHand hand);
public abstract boolean lastInteractEntityCheck(@NotNull InteractionHand hand);
public abstract int gameTicks();
public abstract void swingHand(InteractionHand hand);