点击的CustomInventory无法使用Bukkit

时间:2018-11-09 04:43:26

标签: minecraft bukkit

我从不喜欢诉诸于此,因为我试图自己解决该代码并弄清为什么它不起作用,这一次我真的不明白为什么它不起作用。 这是我制作的清单:

package io.github.bxnie.gui;

import java.util.ArrayList;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import net.minecraft.server.v1_12_R1.CommandExecute;

public class build extends CommandExecute implements Listener, CommandExecutor {

    public String build = "build";

    //open main GUI for building /build
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        Player p = (Player) sender;
        if (!(sender instanceof Player)) {
            sender.sendMessage("Only players may execute this command!");
            return true;
        }
        if (cmd.getName().equalsIgnoreCase(build) && sender instanceof Player) {
            if (p.hasPermission("fp.build")) {
                //Creates the Inventory
                Inventory gui = Bukkit.createInventory(null, 27, ChatColor.BLUE + "Build Menu");

                //Where the Items and Meta are made
                ItemStack creative = new ItemStack(Material.CONCRETE, 1, (short) 3);
                ItemMeta creativemeta = creative.getItemMeta();
                creativemeta.setDisplayName(ChatColor.BLUE + "Creative Mode");
                creativemeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
                ArrayList<String> creativelore = new ArrayList<String>();
                creativelore.add(ChatColor.GRAY + "Click this to set your gamemode to Creative!");
                creativemeta.setLore(creativelore);
                creative.setItemMeta(creativemeta);

                //Positioning
                gui.setItem(18, creative);

                p.openInventory(gui);

            } else {
                p.sendMessage(ChatColor.RED + "Insufficient Permission!");
                return false;
            }
        }

        return false;
    }

}

以下是广告资源的onclick事件:

package io.github.bxnie.events;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

public class buildmenu implements Listener {

    @SuppressWarnings("unlikely-arg-type")
    @EventHandler
    public void InventoryOnClick(InventoryClickEvent event) { 
        Player player = (Player) event.getWhoClicked();
        Inventory open = event.getInventory();
        ItemStack item = event.getCurrentItem();

        if(open == null) {
            return;
        }

        if(open.getName().equals(ChatColor.BLUE + "Build Menu")) {

            event.setCancelled(true);

            if(item == null || !item.hasItemMeta()) {
                return;
            }
            if(item.getItemMeta().getDisplayName().equals(ChatColor.BLUE + "Creative Mode")) {
                Bukkit.broadcastMessage("Test");
                return;
            }
        }
    }
}

当我在游戏中使用命令/ build时,清单中会显示正确的物品,但是,当我单击该物品时,它不会运行我设置的测试广播,此外,玩家可以在清单中移动物品。 我敢肯定这只是一个简单的错误,如果您在浪费时间,很抱歉,但是我已经尝试修复一个小时了。

感谢-本

1 个答案:

答案 0 :(得分:0)

我会将其发布为评论,但感谢您stackoverflow不允许这样做。

如Squiddie所建议,这可能值得调试。点击广告资源后,也许会向玩家发送一条消息。

Player.updateInventory();

对于防止项目在您取消活动后徘徊很有用,因为这可能会导致重复问题。

在bukkit上打开了类似的线程,该线程是我在搜索事件时获得的-> https://bukkit.org/threads/how-can-i-cancel-inventory-click-event.144874/