簡體   English   中英

使用項目后標記位置 || Minecraft 插件 Spigot Bukkit

[英]Marking a location after using item || Minecraft Plugin Spigot Bukkit

我想將玩家點擊的方塊的位置保存到兩個變量中。

我嘗試在使用該項目后觸發一個事件開始,但該事件僅在我單擊空中時生成。

if (p.getItemInHand().getType() == Material.BLAZE_ROD) { System.out.println("TEST"); }

我也嘗試過這種設計,但代碼仍然無法正常工作:

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

總之,我想將兩個指示塊的位置寫入變量。 一種是右鍵單擊給定項目,另一種是左鍵單擊相同項目。

我還沒有搜索過,但我會馬上問,如何檢查給定坐標處的塊是否存在(是否為空,是否為空氣)以及如何設置或替換給定坐標處的選定塊一?

您可以通過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()方法將返回該塊的位置。

確保閱讀有關此類、枚舉和接口的所有文檔:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM