Event InventoryClickEvent 不起作用 [Minecraft Bukkit 插件]
Event InventoryClickEvent don't work [Minecraft Bukkit Plugin]
我想创建一个使用物品栏的 Minecraft GUI。我已经用里面的项目制作了所有的 GUI。但是当我尝试与 GUI 交互时(比如点击一个项目),它什么也做不了。所以我很确定是 InventoryClickEvent 不起作用。
这是我的代码:
package can.evilestvirus.randomtp;
import java.util.Arrays;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
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 org.bukkit.plugin.PluginManager;
public class CommandRtp implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
Inventory inv = Bukkit.createInventory(null, 9, "Menu de tp aléatoire");
ItemStack goldBlock = new ItemStack(Material.GOLD_BLOCK, 1);
ItemMeta goldBlockM = goldBlock.getItemMeta();
goldBlockM.setDisplayName("§8[§a+§8] §aRandomTP Normal §8[§a+§8]");
goldBlockM.setLore(Arrays.asList("§b- §6Distance de TP: 2500 Blocks", "§b- §6Prix: [=10=]", "§b- §6Cooldown de 60 secondes", "§a§lCliquez ici pour utiliser"));
goldBlockM.addEnchant(Enchantment.DURABILITY, 10, true);
goldBlockM.addItemFlags(ItemFlag.HIDE_ENCHANTS);
goldBlock.setItemMeta(goldBlockM);
inv.setItem(3, goldBlock);
ItemStack diamondBlock = new ItemStack(Material.DIAMOND_BLOCK, 1);
ItemMeta diamondBlockM = diamondBlock.getItemMeta();
diamondBlockM.setDisplayName("§8[§a+§8] §aRandomTP §6§lVIP §8[§a+§8]");
diamondBlockM.setLore(Arrays.asList("§b- §6Distance de TP: 5000 Blocks", "§b- §6Prix: 00", "§b- §6Cooldown de 5 minutes", "§a§lCliquez ici pour utiliser"));
diamondBlockM.addEnchant(Enchantment.DURABILITY, 10, true);
diamondBlockM.addItemFlags(ItemFlag.HIDE_ENCHANTS);
diamondBlock.setItemMeta(diamondBlockM);
inv.setItem(5, diamondBlock);
player.openInventory(inv);
}
return true;
}
@EventHandler
public void onClick(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
ItemStack current = event.getCurrentItem();
if(current == null) return;
if(event.getInventory().getTitle().equals("Menu de tp aléatoire")) {
event.setCancelled(true);
if(current.getType() == Material.GOLD_BLOCK) {
player.closeInventory();
player.chat("/rtpPlayer");
}
if(current.getType() == Material.DIAMOND_BLOCK) {
player.closeInventory();
player.setFlying(true);
}
}
}
}
我的主要class代码是这样的:
package can.evilestvirus.randomtp;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import can.evilestvirus.randomtp.CommandRtpPlayer;
public class Main extends JavaPlugin {
@Override
public void onEnable() {
System.out.println("HELLO PEOPLE!");
this.getCommand("rtpplayer").setExecutor(new CommandRtpPlayer());
this.getCommand("rtp").setExecutor(new CommandRtp());
Bukkit.getPluginManager().registerEvents((Listener) this, Bukkit.getPluginManager().getPlugins()[0]);
Bukkit.getPluginManager().registerEvents((Listener) new CommandRtp(), this);
}
}
您需要在 CommandRtp
class 中实施 Listener
。一旦你这样做它应该工作。
因此您的 class 将如下所示:
public class CommandRtp implements Listener, CommandExecutor {
// Code
}
好的,所以我发现了我的错误。我创建了新的 class 并且正确注册了新的 class。在我更改 IF 后检查我的菜单是否与命令菜单相同,因为我使用的功能不是功能性的。
所以这是我的新代码,适用于那些和我有同样问题的人:
package can.evilestvirus.randomtp;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
public class MenuHandler implements Listener {
@EventHandler
public void onMenuClick(InventoryClickEvent e){
Player player = (Player) e.getWhoClicked();
if(e.getView().getTitle().equalsIgnoreCase("Menu de tp aleatoire")){
e.setCancelled(true);
if (e.getCurrentItem().getType() == Material.GOLD_BLOCK) {
player.closeInventory();
player.chat("/rtpPlayer");
}
if (e.getCurrentItem().getType() == Material.DIAMOND_BLOCK) {
player.closeInventory();
player.setFlying(true);
}
}
}
}
这是主要的class:
package can.evilestvirus.randomtp;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin{
@Override
public void onEnable() {
getCommand("rtpplayer").setExecutor(new CommandRtpPlayer());
getCommand("rtp").setExecutor(new CommandRtp());
Bukkit.getPluginManager().registerEvents(new MenuHandler(), this);
}
}
您可以使用此活动:
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
}
对我来说很管用
我想创建一个使用物品栏的 Minecraft GUI。我已经用里面的项目制作了所有的 GUI。但是当我尝试与 GUI 交互时(比如点击一个项目),它什么也做不了。所以我很确定是 InventoryClickEvent 不起作用。
这是我的代码:
package can.evilestvirus.randomtp;
import java.util.Arrays;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
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 org.bukkit.plugin.PluginManager;
public class CommandRtp implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
Inventory inv = Bukkit.createInventory(null, 9, "Menu de tp aléatoire");
ItemStack goldBlock = new ItemStack(Material.GOLD_BLOCK, 1);
ItemMeta goldBlockM = goldBlock.getItemMeta();
goldBlockM.setDisplayName("§8[§a+§8] §aRandomTP Normal §8[§a+§8]");
goldBlockM.setLore(Arrays.asList("§b- §6Distance de TP: 2500 Blocks", "§b- §6Prix: [=10=]", "§b- §6Cooldown de 60 secondes", "§a§lCliquez ici pour utiliser"));
goldBlockM.addEnchant(Enchantment.DURABILITY, 10, true);
goldBlockM.addItemFlags(ItemFlag.HIDE_ENCHANTS);
goldBlock.setItemMeta(goldBlockM);
inv.setItem(3, goldBlock);
ItemStack diamondBlock = new ItemStack(Material.DIAMOND_BLOCK, 1);
ItemMeta diamondBlockM = diamondBlock.getItemMeta();
diamondBlockM.setDisplayName("§8[§a+§8] §aRandomTP §6§lVIP §8[§a+§8]");
diamondBlockM.setLore(Arrays.asList("§b- §6Distance de TP: 5000 Blocks", "§b- §6Prix: 00", "§b- §6Cooldown de 5 minutes", "§a§lCliquez ici pour utiliser"));
diamondBlockM.addEnchant(Enchantment.DURABILITY, 10, true);
diamondBlockM.addItemFlags(ItemFlag.HIDE_ENCHANTS);
diamondBlock.setItemMeta(diamondBlockM);
inv.setItem(5, diamondBlock);
player.openInventory(inv);
}
return true;
}
@EventHandler
public void onClick(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
ItemStack current = event.getCurrentItem();
if(current == null) return;
if(event.getInventory().getTitle().equals("Menu de tp aléatoire")) {
event.setCancelled(true);
if(current.getType() == Material.GOLD_BLOCK) {
player.closeInventory();
player.chat("/rtpPlayer");
}
if(current.getType() == Material.DIAMOND_BLOCK) {
player.closeInventory();
player.setFlying(true);
}
}
}
}
我的主要class代码是这样的:
package can.evilestvirus.randomtp;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import can.evilestvirus.randomtp.CommandRtpPlayer;
public class Main extends JavaPlugin {
@Override
public void onEnable() {
System.out.println("HELLO PEOPLE!");
this.getCommand("rtpplayer").setExecutor(new CommandRtpPlayer());
this.getCommand("rtp").setExecutor(new CommandRtp());
Bukkit.getPluginManager().registerEvents((Listener) this, Bukkit.getPluginManager().getPlugins()[0]);
Bukkit.getPluginManager().registerEvents((Listener) new CommandRtp(), this);
}
}
您需要在 CommandRtp
class 中实施 Listener
。一旦你这样做它应该工作。
因此您的 class 将如下所示:
public class CommandRtp implements Listener, CommandExecutor {
// Code
}
好的,所以我发现了我的错误。我创建了新的 class 并且正确注册了新的 class。在我更改 IF 后检查我的菜单是否与命令菜单相同,因为我使用的功能不是功能性的。
所以这是我的新代码,适用于那些和我有同样问题的人:
package can.evilestvirus.randomtp;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
public class MenuHandler implements Listener {
@EventHandler
public void onMenuClick(InventoryClickEvent e){
Player player = (Player) e.getWhoClicked();
if(e.getView().getTitle().equalsIgnoreCase("Menu de tp aleatoire")){
e.setCancelled(true);
if (e.getCurrentItem().getType() == Material.GOLD_BLOCK) {
player.closeInventory();
player.chat("/rtpPlayer");
}
if (e.getCurrentItem().getType() == Material.DIAMOND_BLOCK) {
player.closeInventory();
player.setFlying(true);
}
}
}
}
这是主要的class:
package can.evilestvirus.randomtp;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin{
@Override
public void onEnable() {
getCommand("rtpplayer").setExecutor(new CommandRtpPlayer());
getCommand("rtp").setExecutor(new CommandRtp());
Bukkit.getPluginManager().registerEvents(new MenuHandler(), this);
}
}
您可以使用此活动:
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
}
对我来说很管用