Solved Inventory Click Event Not Being Triggered

Discussion in 'Plugin Development' started by Mr.Potato, May 21, 2020.

  1. Offline

    Mr.Potato

    I create this inventory:

    Code:
    Inventory inv = createInventory(null, 9, "Cryptic Crafter");
    inv.addItem(excalibur);
    Everything works, but it doesn't trigger the Inventory Click Event when I click in that inventory

    Code:
    @EventHandler
    public void onRecipe(InventoryClickEvent e){
    if(e.getWhoClicked().hasPermission("yabadabado")) System.out.println(e.getInventory());
    if(e.getInventory() != null){
    if(e.getInventory().getName().equals("Cryptic Crafter")){
    ...
    
    e.setCancelled(true);
    }
    }
    }
    I added the output to check if it works on other inventories, and it does, the event doesn't trigger only in this inventory.

    Yes, the event is registered.
    Code:
    getServer().getPluginManager().registerEvents(new BlackSmith(), this);
     
  2. Online

    timtower Moderator Moderator

    @Mr.Potato Add a print statement as first line of the method.
     
  3. Offline

    Mr.Potato

    It is the first line of the method
     
  4. Online

    timtower Moderator Moderator

    Behind an if statement, post a line without the if statement.
     
  5. Offline

    Mr.Potato

    I tried that too, same result
     
  6. Online

    timtower Moderator Moderator

    Full main and listener class please.
     
  7. Offline

    Mr.Potato

    I don't get how it's relevant

    I have a lot of stuff there, I don't wanna get banned for spamming

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2020
  8. Online

    timtower Moderator Moderator

    It is important if your onEnable is good. If your listener class is setup correctly.
    Yet you double post
     
  9. Offline

    Mr.Potato

    Main Class

    Code:
    
    package classes.classes;import classes.classes.ClassInterfaces.BlackSmith;import classes.classes.ClassInterfaces.Warrior;import classes.classes.Commands.Menu;import classes.classes.Commands.SpawnDragon;import classes.classes.Events.DragonEvent;import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.Material;import org.bukkit.configuration.file.FileConfiguration;import org.bukkit.configuration.file.FileConfigurationOptions;import org.bukkit.configuration.file.YamlConfiguration;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.entity.PlayerDeathEvent;import org.bukkit.event.inventory.InventoryClickEvent;import org.bukkit.event.player.AsyncPlayerChatEvent;import org.bukkit.event.player.PlayerJoinEvent;import org.bukkit.inventory.ItemStack;import org.bukkit.inventory.ShapedRecipe;import org.bukkit.inventory.meta.ItemMeta;import org.bukkit.plugin.java.JavaPlugin;import java.io.File;import java.io.IOException;import java.util.ArrayList;public final class Classes extends JavaPlugin implements Listener {
    public static Classes instance; public static void setInstance(Classes instance){
    Classes.instance = instance;}
    public static Classes getInstance(){
    return instance;}
    
    @Overridepublic void onEnable() {
    setInstance(this);getServer().getPluginManager().registerEvents(new Menu(), this);getServer().getPluginManager().registerEvents(this, this);getServer().getPluginManager().registerEvents(new Warrior(), this);getServer().getPluginManager().registerEvents(new SpawnDragon(), this);getServer().getPluginManager().registerEvents(new DragonEvent(), this);getServer().getPluginManager().registerEvents(new BlackSmith(), this);getCommand("getartifacts").setExecutor(new Artifacts());getCommand("classes").setExecutor(new Menu());getCommand("spawndragon").setExecutor(new SpawnDragon());File specialcrafting = new File(getDataFolder() + File.separator + "specialcraftingtables.yml"); if(!specialcrafting.exists()){
    try {
    specialcrafting.createNewFile();FileConfiguration special = YamlConfiguration.loadConfiguration(specialcrafting);special.set("PlacedCraftings", new ArrayList<String>());special.save(specialcrafting);} catch (IOException e) {
    e.printStackTrace();}
    
    }
    // Plugin startup logic}
    
    @EventHandlerpublic void onPlayerJoin(PlayerJoinEvent e) throws IOException {
    File file = new File(getDataFolder() + File.separator + e.getPlayer().getUniqueId() + ".yml"); if(!file.exists()){
    file.createNewFile();FileConfiguration edit = YamlConfiguration.loadConfiguration(file);edit.set("HasWarrior", false);edit.set("HasMage", false);edit.set("HasFisher", false);edit.set("HasGuardian", false);edit.set("HasSales", false);edit.set("HasLumber", false);edit.set("HasArcher", false);edit.set("HasScientist", false);edit.set("CurrentClass", "none");edit.save(file);}
    }
    
    public static File getFile(Player player){
    File file = new File("plugins/Classes/" + File.separator + player.getUniqueId() + ".yml"); return file;}
    
    @EventHandlerpublic void onTag(AsyncPlayerChatEvent e){
    if(e.getPlayer() != null){
    Player player = e.getPlayer();File file = getFile(player);FileConfiguration edit = YamlConfiguration.loadConfiguration(file); if(edit.get("CurrentClass") != "none"){
    if(edit.get("CurrentClass").equals("Warrior")){
    player.setDisplayName(player.getDisplayName() + ChatColor.translateAlternateColorCodes('&', " &7[&3Warrior&7]&f"));}
    }
    }
    }
    
    @EventHandlerpublic void onDeath(PlayerDeathEvent e){
    e.setDeathMessage(null);}
    
    
    
    
    @Overridepublic void onDisable() {
    // Plugin shutdown logic}
    }
    

    Listener Class
    Code:
    package classes.classes.ClassInterfaces;import classes.classes.Commands.Menu;import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.Location;import org.bukkit.Material;import org.bukkit.configuration.file.FileConfiguration;import org.bukkit.configuration.file.YamlConfiguration;import org.bukkit.enchantments.Enchantment;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.block.Action;import org.bukkit.event.block.BlockBreakEvent;import org.bukkit.event.block.BlockPlaceEvent;import org.bukkit.event.inventory.InventoryClickEvent;import org.bukkit.event.inventory.InventoryOpenEvent;import org.bukkit.event.inventory.InventoryType;import org.bukkit.event.player.PlayerInteractEvent;import org.bukkit.inventory.Inventory;import org.bukkit.inventory.ItemFlag;import org.bukkit.inventory.ItemStack;import org.bukkit.inventory.meta.ItemMeta;import org.yaml.snakeyaml.Yaml;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.List;import static org.bukkit.Bukkit.createInventory;public class BlackSmith implements Listener {
    File specialcraftingfile = new File("plugins/Classes/" + File.separator + "specialcraftingtables.yml");FileConfiguration cryptic = YamlConfiguration.loadConfiguration(specialcraftingfile);ItemStack specialcrafting = Menu.createItem(ChatColor.AQUA + "Cryptic Crafter", "Using much power, it can craft what the others can't", Material.BEACON); public static ItemStack excalibur = new ItemStack(Material.GOLD_SWORD); public static ItemMeta em = excalibur.getItemMeta();@EventHandlerpublic void onSpecialCrafting(PlayerInteractEvent e){
    if(e.getPlayer() != null){
    if(e.getClickedBlock() != null){
    if(e.getClickedBlock().getType().equals(Material.BEACON)){
    Location loc = e.getClickedBlock().getLocation(); if(cryptic.getStringList("PlacedCraftings").contains(loc.getX() + "," + loc.getY() + "," + loc.getZ())){
    if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    em.setDisplayName(ChatColor.GOLD + "Excalibur");em.spigot().setUnbreakable(true);em.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);em.addItemFlags(ItemFlag.HIDE_ENCHANTS);em.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);em.addEnchant(Enchantment.DAMAGE_ALL, 45, true);List<String> lore = new ArrayList<>();lore.add(ChatColor.RED + "> Warrior Rage");lore.add(ChatColor.GREEN + "> Boss Killer 2");lore.add(ChatColor.WHITE + "> Sharpness 45");lore.add(ChatColor.WHITE + "> Unbreakable");lore.add("Strong, but is it the strongest?");em.setLore(lore);excalibur.setItemMeta(em);Inventory inv = createInventory(null, 9, "Cryptic Crafter");inv.addItem(excalibur);e.getPlayer().openInventory(inv);}
    
    }
    }
    }
    
    }
    }
    @EventHandlerpublic void onBlock(BlockPlaceEvent e) throws IOException {
    if(e.getPlayer() != null){
    if(e.getBlock().getType().equals(Material.BEACON)){
    if(e.getPlayer().getInventory().getItemInHand().getItemMeta().equals(specialcrafting.getItemMeta())){
    Location loc = e.getBlock().getLocation();List<String> list = cryptic.getStringList("PlacedCraftings");list.add(loc.getX() + "," + loc.getY() + "," + loc.getZ());cryptic.set("PlacedCraftings", list);cryptic.save(specialcraftingfile);}
    }
    }
    }
    @EventHandlerpublic void onBreak(BlockBreakEvent e) throws IOException {
    if(e.getPlayer() != null){
    if(e.getBlock().getType().equals(Material.BEACON)){
    Location loc = e.getBlock().getLocation(); if(cryptic.getStringList("PlacedCraftings").contains(loc.getX() + "," + loc.getY() + "," + loc.getZ())){
    List<String> list = cryptic.getStringList("PlacedCraftings");list.remove(loc.getX() + "," + loc.getY() + "," + loc.getZ());cryptic.set("PlacedCraftings", list);cryptic.save(specialcraftingfile);e.getBlock().getDrops().clear();e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), specialcrafting);}
    
    }
    }
    }
    @EventHandlerpublic void onOpenInv(InventoryOpenEvent e){
    if(e.getPlayer() != null){
    if(e.getInventory().getType().equals(InventoryType.BEACON)){
    e.setCancelled(true);}
    }
    }
    public static Inventory addItemInMenu(ItemStack item1, ItemStack item2, ItemStack item3, ItemStack item4, ItemStack item5, ItemStack item6, ItemStack item7, ItemStack item8, ItemStack item9, ItemStack result, String itemname){
    Inventory inv = createInventory(null, 45, "Recipe for " + itemname);ItemStack no = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 7);ItemMeta nom = no.getItemMeta();nom.setDisplayName(" ");no.setItemMeta(nom);ItemStack back = new ItemStack(Material.BARRIER);ItemMeta bm = back.getItemMeta();bm.setDisplayName(ChatColor.RED + "Back");ArrayList<String> lor = new ArrayList<>();lor.add("Go back to recipes");bm.setLore(lor);back.setItemMeta(bm);ItemStack crafted = new ItemStack(Material.ARROW);ItemMeta cm = crafted.getItemMeta();cm.setDisplayName(ChatColor.GOLD + "Crafted Item");crafted.setItemMeta(cm);ItemStack craft = new ItemStack(Material.ANVIL);ItemMeta crm = craft.getItemMeta();crm.setDisplayName(ChatColor.GRAY + "Craft This Item");craft.setItemMeta(crm);ItemStack[] contens = {no,no,no,no,no,no,no,no,no,no,item1,item2,item3,no,no,no,no,no,no,item4,item5,item6,no,crafted,no,result,no,no,item7,item8,item9,no,no,no,no,no,no,no,no,back,no,craft,no,no,no};inv.setContents(contens); return inv;}
    
    @EventHandlerpublic void onRecipe(InventoryClickEvent e){
    if(e.getWhoClicked().hasPermission("yabadabado")) System.out.println(e.getInventory()); if(e.getInventory() != null){
    if(e.getInventory().getName().equals("Cryptic Crafter")){
    if(e.getCurrentItem().getType().equals(Material.GOLD_SWORD)){
    if(e.getCurrentItem().getItemMeta() != null){
    if(e.getCurrentItem().getItemMeta().equals(em)){
    ItemStack air = new ItemStack(Material.AIR);ItemStack bigblade = new ItemStack(Material.GOLD_INGOT);ItemMeta bm = bigblade.getItemMeta();bm.setDisplayName(ChatColor.GOLD + "Big Blade");ArrayList<String> lo = new ArrayList<>();lo.add("This sure is big!");bm.setLore(lo);bigblade.setItemMeta(bm);ItemStack branch = new ItemStack(Material.BLAZE_ROD);ItemMeta bbb = branch.getItemMeta();bbb.setDisplayName(ChatColor.DARK_GREEN + "Branch");ArrayList<String> lore = new ArrayList<>();lore.add("Who knows why this branch is so thick");bbb.setLore(lore);branch.setItemMeta(bbb);e.getWhoClicked().openInventory(addItemInMenu(air,air,bigblade,air,bigblade,air,branch,air,air, excalibur, "Excalibur"));}
    }
    }
    e.setCancelled(true);}
    }
    }
    
    
    }
    
    Sorry for double posting, I am new to this forum
     
  10. Online

    timtower Moderator Moderator

  11. Offline

    Mr.Potato

  12. Online

    timtower Moderator Moderator

    @Mr.Potato So a printline on line 140 does not work?
     
  13. Offline

    Mr.Potato

    It does, but only when I click in that inventory it doesn't, I tried every other inventory and it prints


    Edit: I tried putting the inventory click event in another plugin I am 100% sure it will work and it didn't trigger. Is the inventory the problem?
     
    Last edited: May 21, 2020
  14. Offline

    Mr.Potato

  15. Offline

    Mr.Potato

  16. Offline

    caderapee

    @Mr.Potato try to not import static Inventory. Use Bukkit.createInventory if that make a diff
     
  17. Offline

    Mr.Potato

    Sorry for late response. I still have the issue, but I found something. I can't keep the item if i take it out.

    I have figured it out. I needed to do the left click action and not cancel the beacon inventory.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 19, 2020
  18. Offline

    Machine Maker

    If your problem/question has been resolved/answered, please mark this thread as Solved.
     

Share This Page