【问题标题】:Marking a location after using item || Minecraft Plugin Spigot Bukkit使用物品后标记位置 || Minecraft 插件 Spigot Bukkit
【发布时间】:2021-05-04 20:54:10
【问题描述】:

我想将玩家点击的方块的位置保存到两个变量中。

我尝试在使用该项目后触发一个事件开始,但该事件仅在我在空中单击时生成。

如果 (p.getItemInHand().getType() == Material.BLAZE_ROD) { System.out.println("测试"); }

我也试过这个设计,但是代码还是不能正常工作:

if ((p.getItemInHand().getType() == Material.BLAZE_ROD) && (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { System.out.println("测试"); }

总而言之,我想将两个指示块的位置写入变量。一种是右键单击给定项目,另一种是左键单击相同项目。

我还没有搜索过,但我会马上问,如何检查给定坐标处的方块是否存在(是否为空,是否为空气)以及如何设置或替换给定坐标处的选定方块与另一个坐标?

【问题讨论】:

    标签: java plugins minecraft bukkit


    【解决方案1】:

    您可以通过PlayerInteractEventActionMaterialLocation 实现此目的。一个例子如下:

    import org.bukkit.Location;
    import org.bukkit.event.EventHandler;
    
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.block.Action;
    
    import org.bukkit.block.Block;
    import org.bukkit.Material;
    
    public class YourListener {
    
        private Location firstLocation;
        private Location secondLocation;
        
        @EventHandler
        public void onPlayerInteractEvent(PlayerInteractEvent event) {
            // Only process when the player has a wooden axe in the hand.
            if (event.getMaterial() == Material.WOODEN_AXE) {
                Action action = event.getAction();
                Block clickedBlock = event.getClickedBlock();
                if (clickedBlock == null) return;
                if (action == Action.LEFT_CLICK_AIR || action == Action.LEFT_CLICK_BLOCK) {
                    // Do one thing (left click)
                    Material type = clickedBlock.getType(); // Block material (Check if it's Material.AIR or another type)
                    firstLocation = clickedBlock.getLocation(); // Save the location
                } else if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK){
                    // Do another thing (right click)
                    Material type = clickedBlock.getType(); // Block material (Check if it's Material.AIR or another type)
                    secondLocation = clickedBlock.getLocation(); // Save location
                    // Let's say you now want to replace the block material to a diamond block:
                    clickedBlock.setType(Material.DIAMOND_BLOCK);
                }
            }
        }
    
    }
    

    PlayerInteractEvent 有方法 getMaterial(),它返回:

    返回此事件所代表的项目的材质

    (玩家手中物品的材质)

    然后getAction() 方法返回以下枚举条目之一

    • Action.LEFT_CLICK_AIR: 左键点击空气
    • Action.LEFT_CLICK_BLOCK: 左键点击方块
    • Action.RIGHT_CLICK_AIR: 右键空气
    • Action.RIGHT_CLICK_BLOCK: 右击方块

    getClickedBlock() 方法返回玩家点击的方块。然后你可以使用getType()setType(Material)方法来获取和设置该块的材质。

    最后来自BlockgetLocation() 方法将返回该块的位置。

    请务必阅读有关此类、枚举和接口的所有文档:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 2017-07-19
      相关资源
      最近更新 更多