Minecraft Bukkit 和 Spigot 在自动点唱机中获取物品

问题描述 投票:0回答:1

我正在使用 Minecraft 中的 Bukkit 创建一个插件:1.19.4,当玩家右键单击该块但我找不到方法时,我需要查看 JukeBox 中是否有 specific Music Disc这样做。

所以我试过:

public class TestCD implements Listener {

    @EventHandler
    public void onInteract(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        Action action = event.getAction();
        ItemStack it = event.getItem();
        Block block = event.getClickedBlock();
        BlockData blockD = block.getBlockData();


        if (it != null && action == Action.LEFT_CLICK_BLOCK && block.getType().equals(Material.JUKEBOX) && blockD.getMaterial().isRecord() = true) {
            player.sendMessage("Hello World");
        }
    }

然后我尝试了其他一些东西,但我不记得了。

java minecraft bukkit spigot
1个回答
0
投票

要获取记录,如果有的话,在 JukeBox 中播放,您可以使用

JukeBox
块状态方法
getRecord()
像这样:

    ...
    if(!(block.getState() instanceof Jukebox))
        return;
    Jukebox jukebox = (Jukebox) block.getState();
    ItemStack record = jukebox.getRecord();
    ...

在尝试转换之前,您必须确保您的

block
变量不为空。如果自动点唱机中没有光盘,则生成的
ItemStack
将是
Material.AIR
.

编辑:这是一个完整的例子:

@EventHandler(ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent e) {
    // Ensure we want to do something with this event
    if(e.getAction() != Action.LEFT_CLICK_BLOCK
            || e.getClickedBlock() == null
            || e.getClickedBlock().getType() != Material.JUKEBOX
            || !(e.getClickedBlock().getState() instanceof Jukebox)
    ) {
        return;
    }
    Player player = e.getPlayer();
    // Cast the clicked block's state to JukeBox
    Jukebox jukebox = (Jukebox) e.getClickedBlock().getState();
    // Get the record in the JukeBox
    ItemStack record = jukebox.getRecord();
    // If the record's material type is AIR, there is nothing playing
    if(record.getType() == Material.AIR){
        player.sendMessage("There is nothing playing in this JukeBox.");
        return;
    }
    // Do whatever with the record; below is just an example...
    StringBuilder message = new StringBuilder("This JukeBox");
    // Check if it's currently playing
    if(jukebox.isPlaying()){
        message.append(" is currently ");
    } else {
        // If it's not playing, that means a record is in the JukeBox, but it's not playing
        message.append(" was previously ");
    }
    player.sendMessage(message + "playing " + record.getType().name());
}
© www.soinside.com 2019 - 2024. All rights reserved.