mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2026-01-03 22:26:16 +00:00
feat(bukkit): 添加自定义方块交互事件
- 新增 CustomBlockInteractEvent 类用于自定义方块交互事件 - 在 ItemEventListener 中添加自定义方块交互事件的处理逻辑 - 当玩家使用主手右键自定义方块时,触发 CustomBlockInteractEvent - 如果事件被取消,则取消玩家的交互事件
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package net.momirealms.craftengine.bukkit.api.event;
|
||||
|
||||
import net.momirealms.craftengine.core.block.ImmutableBlockState;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class CustomBlockInteractEvent extends CustomBlockEvent {
|
||||
public CustomBlockInteractEvent(ImmutableBlockState state, Location location, @NotNull Player player) {
|
||||
super(state, location, player);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package net.momirealms.craftengine.bukkit.item;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.momirealms.craftengine.bukkit.api.CraftEngineBlocks;
|
||||
import net.momirealms.craftengine.bukkit.api.event.CustomBlockInteractEvent;
|
||||
import net.momirealms.craftengine.bukkit.block.BukkitBlockManager;
|
||||
import net.momirealms.craftengine.bukkit.item.behavior.BlockItemBehavior;
|
||||
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
|
||||
@@ -56,6 +57,24 @@ public class ItemEventListener implements Listener {
|
||||
Location interactionPoint = event.getInteractionPoint();
|
||||
if (interactionPoint == null) return;
|
||||
Player bukkitPlayer = event.getPlayer();
|
||||
// TODO: 自定义方块交互事件
|
||||
Block clickedBlock = Objects.requireNonNull(event.getClickedBlock());
|
||||
if (event.getHand() == EquipmentSlot.HAND) {
|
||||
Key blockKey = BlockStateUtils.getRealBlockId(clickedBlock);
|
||||
if (blockKey.namespace().equals("craftengine")) {
|
||||
int blockId = BlockStateUtils.blockDataToId(clickedBlock.getBlockData());
|
||||
ImmutableBlockState state = BukkitBlockManager.instance().getImmutableBlockState(blockId);
|
||||
CustomBlockInteractEvent customBlockInteractEvent = new CustomBlockInteractEvent(
|
||||
state,
|
||||
clickedBlock.getLocation(),
|
||||
bukkitPlayer
|
||||
);
|
||||
if (EventUtils.fireAndCheckCancel(customBlockInteractEvent)) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
BukkitServerPlayer player = this.plugin.adapt(bukkitPlayer);
|
||||
InteractionHand hand = event.getHand() == EquipmentSlot.HAND ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND;
|
||||
if (hand == InteractionHand.OFF_HAND) {
|
||||
@@ -70,7 +89,6 @@ public class ItemEventListener implements Listener {
|
||||
if (itemInHand == null) return;
|
||||
Optional<CustomItem<ItemStack>> customItem = itemInHand.getCustomItem();
|
||||
|
||||
Block clickedBlock = Objects.requireNonNull(event.getClickedBlock());
|
||||
Material material = itemInHand.getItem().getType();
|
||||
// is custom item
|
||||
if (customItem.isPresent()) {
|
||||
|
||||
Reference in New Issue
Block a user