PlayerInteractEvent doesn't work

Discussion in 'Plugin Development' started by advancedjus, Aug 26, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    advancedjus

    Code:
    package io.github.nonsensicality.gamehub;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
            System.out.println("(!) GameHub Plugin Ready");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
        @EventHandler
        public void onPlayerJoin (PlayerJoinEvent e) {
            Player p = e.getPlayer();
           
            p.getInventory().clear();
           
            ItemStack itemStack = new ItemStack(Material.COMPASS, 1);
            ItemMeta meta = itemStack.getItemMeta();
            meta.setDisplayName(ChatColor.GREEN + "Game Menu");
           
            List<String> lore = new ArrayList<>();
            lore.add(ChatColor.AQUA + "Use this compass to select a game!");
           
            meta.setLore(lore);
            itemStack.setItemMeta(meta);
            p.getInventory().setItem(0, itemStack);
        }
       
       
        public class GameMenu {
            public final Inventory gameMenu = Bukkit.createInventory(null, 54, ChatColor.GREEN + "Game Menu");
           
            public void Head() {
               
                ItemStack tntrun = new ItemStack(Material.TNT, 1);
                ItemMeta meta = tntrun.getItemMeta();
                meta.setDisplayName(ChatColor.RED + "TNT Run");
                gameMenu.setItem(29, tntrun);
               
                ItemStack playerhead = new ItemStack(Material.PLAYER_HEAD, 1);
                ItemMeta meta1 = playerhead.getItemMeta();
                meta1.setDisplayName(ChatColor.translateAlternateColorCodes('&', ChatColor.BLUE + "&LYour Games"));
                gameMenu.setItem(13, playerhead);
            }
            
             @EventHandler
             public void playerInteractEvent (PlayerInteractEvent e) {
                 Player p = e.getPlayer();
                     if (p.getItemInHand().getType() == Material.COMPASS) {
                         if (e.getAction().equals(Action.LEFT_CLICK_AIR) || e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
                             p.openInventory(gameMenu);
                         }
                     }
             }
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked(); // The player that clicked the item
            ItemStack clicked = e.getCurrentItem(); // The item that was clicked
            Inventory inventory = e.getInventory(); // The inventory that was clicked in
           
            
            if (inventory.getName().equals(gameMenu.getName())) {
                if (clicked.getType() == Material.TNT) { // The item that the player clicked it dirt
                    if (clicked.getItemMeta().getDisplayName().equals(ChatColor.RED + "TNT Run"));
                e.setCancelled(true); // Make it so the dirt is back in its original spot
                p.closeInventory(); // Closes their inventory
                p.teleport(new Location(Bukkit.getWorld("TNT Run"), 0, 69, 0));
                p.sendMessage(ChatColor.BLUE + "Game> " + ChatColor.GRAY + "You have been sent to " + ChatColor.YELLOW + "TNT Run" + ChatColor.GRAY + ".");;
                    }
                }
            }
        }
    }
    
    When I click on the compass, the custom inventory doesn't seem to up. Anyone have a solution?
     
  2. Offline

    CraftCreeper6

    @advancedjus
    You have registered the events in the Main class but not in the GameMenu class, and GameMenu doesn't implement Listener.
     
    advancedjus likes this.
  3. Offline

    advancedjus

    @CraftCreeper6

    It still doesn't work. Here is the new code by the way:

    Code:
    package io.github.nonsensicality.gamehub;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
            System.out.println("(!) GameHub Plugin Ready");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
    
        }
       
        @EventHandler
        public void onPlayerJoin (PlayerJoinEvent e) {
            Player p = e.getPlayer();
           
            p.getInventory().clear();
           
            ItemStack itemStack = new ItemStack(Material.COMPASS, 1);
            ItemMeta meta = itemStack.getItemMeta();
            meta.setDisplayName(ChatColor.GREEN + "Game Menu");
           
            List<String> lore = new ArrayList<>();
            lore.add(ChatColor.AQUA + "Use this compass to select a game!");
           
            meta.setLore(lore);
            itemStack.setItemMeta(meta);
            p.getInventory().setItem(0, itemStack);
        }
       
        public class GameMenu implements Listener {
           
             public GameMenu (Main plugin) {
                    plugin.getServer().getPluginManager().registerEvents(this, plugin);
                }
    
            public final Inventory gameMenu = Bukkit.createInventory(null, 54, ChatColor.GREEN + "Game Menu");
           
            public void Head() {
               
                ItemStack tntrun = new ItemStack(Material.TNT, 1);
                ItemMeta meta = tntrun.getItemMeta();
                meta.setDisplayName(ChatColor.RED + "TNT Run");
                gameMenu.setItem(29, tntrun);
               
                ItemStack playerhead = new ItemStack(Material.PLAYER_HEAD, 1);
                ItemMeta meta1 = playerhead.getItemMeta();
                meta1.setDisplayName(ChatColor.translateAlternateColorCodes('&', ChatColor.BLUE + "&LYour Games"));
                gameMenu.setItem(13, playerhead);
            }
            
             @EventHandler
             public void playerInteractEvent (PlayerInteractEvent e) {
                 Player p = e.getPlayer();
                     if (p.getItemInHand().getType() == Material.COMPASS) {
                         if (e.getAction().equals(Action.LEFT_CLICK_AIR) || e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
                             p.openInventory(gameMenu);
                         }
                     }
             }
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked(); // The player that clicked the item
            ItemStack clicked = e.getCurrentItem(); // The item that was clicked
            Inventory inventory = e.getInventory(); // The inventory that was clicked in
           
            
            if (inventory.getName().equals(gameMenu.getName())) {
                if (clicked.getType() == Material.TNT) { // The item that the player clicked it dirt
                    if (clicked.getItemMeta().getDisplayName().equals(ChatColor.RED + "TNT Run"));
                e.setCancelled(true); // Make it so the dirt is back in its original spot
                p.closeInventory(); // Closes their inventory
                p.teleport(new Location(Bukkit.getWorld("TNT Run"), 0, 69, 0));
                p.sendMessage(ChatColor.BLUE + "Game> " + ChatColor.GRAY + "You have been sent to " + ChatColor.YELLOW + "TNT Run" + ChatColor.GRAY + ".");;
                    }
                }
            }
        }
    }
    
     
  4. Offline

    Strahan

    You are putting the listener registration for the GameMenu class in itself, yet never creating an instance of that class. Just add the listener to your main in the onEnable, i.e.

    Code:
    @Override
    public void onEnable() {
        Bukkit.getServer().getPluginManager().registerEvents(this, this);
        Bukkit.getServer().getPluginManager().registerEvents(new GameMenu(this), this);
    }
    Also you can remove that pointless plugin ready msg, the system will automatically send enable/disable messages. Plus even if you wanted to make a redundant message, you're better off using the logger like getLogger().info("blah");

    PS when you make an ItemStack, if you want just one of the item that is default. You don't need to do (Material, 1). Also why are you mixing use of the ChatColor enum with translateAlt? Just do ChatColor.BOLD instead of &l.

    PPS if you want to save on indentation, cuz IMO it's annoying as hell, consider negative checks instead of positive. For example:

    Code:
    @EventHandler
    public void playerInteractEvent (PlayerInteractEvent e) {
       Player p = e.getPlayer();
           if (p.getItemInHand().getType() == Material.COMPASS) {
               if (e.getAction().equals(Action.LEFT_CLICK_AIR) || e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
                   p.openInventory(gameMenu);
               }
           }
    }
    ...could be:

    Code:
    @EventHandler
    public void playerInteractEvent (PlayerInteractEvent e) {
       Player p = e.getPlayer();
       if (p.getItemInHand().getType() != Material.COMPASS) return;
       if (!e.getAction().equals(Action.LEFT_CLICK_AIR) && !e.getAction().equals(Action.LEFT_CLICK_BLOCK)) return;
       p.openInventory(gameMenu);
    }
     
    Last edited: Aug 26, 2019
    advancedjus likes this.
  5. Offline

    advancedjus

    @Strahan
    Thanks, the compass works now. However, the items do not seem to show up on the menu and is blank.
    Code:
    package io.github.nonsensicality.gamehub;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
             Bukkit.getServer().getPluginManager().registerEvents(new GameMenu(this), this);
        }
     
        @EventHandler
        public void onPlayerJoin (PlayerJoinEvent e) {
            Player p = e.getPlayer();
         
            p.getInventory().clear();
         
            ItemStack itemStack = new ItemStack(Material.COMPASS, 1);
            ItemMeta meta = itemStack.getItemMeta();
            meta.setDisplayName(ChatColor.GREEN + "Game Menu");
         
            List<String> lore = new ArrayList<>();
            lore.add(ChatColor.AQUA + "Use this compass to select a game!");
         
            meta.setLore(lore);
            itemStack.setItemMeta(meta);
            p.getInventory().setItem(0, itemStack);
        }
     
        public class GameMenu implements Listener {
         
             public GameMenu (Main plugin) {
             plugin.getServer().getPluginManager().registerEvents(this, plugin);
                }
    
            public final Inventory gameMenu = Bukkit.createInventory(null, 54, ChatColor.GREEN + "Game Menu");
         
            public void Head() {
             
                ItemStack tntrun = new ItemStack(Material.TNT, 1);
                ItemMeta meta = tntrun.getItemMeta();
                meta.setDisplayName(ChatColor.RED + "TNT Run");
                gameMenu.setItem(29, tntrun);
             
                ItemStack playerhead = new ItemStack(Material.PLAYER_HEAD, 1);
                ItemMeta meta1 = playerhead.getItemMeta();
                meta1.setDisplayName(ChatColor.BLUE + "Your Games" + ChatColor.BOLD);
                gameMenu.setItem(13, playerhead);
            }
          
             @EventHandler
             public void playerInteractEvent (PlayerInteractEvent e) {
                 Player p = e.getPlayer();
                 if (!(p.getItemInHand().getType() == Material.COMPASS)) return; {
                 if (!e.getAction().equals(Action.LEFT_CLICK_AIR) && !e.getAction().equals(Action.LEFT_CLICK_BLOCK) || !e.getAction().equals(Action.RIGHT_CLICK_AIR) && !e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                 p.openInventory(gameMenu);
                         }
                     }
             }
     
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked(); // The player that clicked the item
            ItemStack clicked = e.getCurrentItem(); // The item that was clicked
            Inventory inventory = e.getInventory(); // The inventory that was clicked in
         
          
            if (inventory.getName().equals(gameMenu.getName())) {
                if (clicked.getType() == Material.TNT) { // The item that the player clicked it TNT
                if (clicked.getItemMeta().getDisplayName().equals(ChatColor.RED + "TNT Run"));
                e.setCancelled(true); // Make it so the TNT is back in its original spot
                p.closeInventory(); // Closes their inventory
                p.teleport(new Location(Bukkit.getWorld("TNT Run"), 0, 69, 0));
                p.sendMessage(ChatColor.BLUE + "Game> " + ChatColor.GRAY + "You have been sent to " + ChatColor.YELLOW + "TNT Run" + ChatColor.GRAY + ".");;
                    }
                }
            }
        }
    }
    
     
  6. Offline

    timtower Moderator Moderator

    advancedjus likes this.
  7. Offline

    advancedjus

    @timtower
    I tried that, the items still do not show up in the menu.
     
  8. Offline

    timtower Moderator Moderator

    @advancedjus Do you call the method Head somewhere then?
     
  9. Offline

    advancedjus

  10. Offline

    timtower Moderator Moderator

    You never fill the inventory with something.
     
  11. Offline

    advancedjus

    @timtower
    What would I need to do to make the items show up upon clicking on the compass?
     
  12. Offline

    timtower Moderator Moderator

    @advancedjus You can put them in the inventory before hand already.
    In the onEnable for example.
     
  13. Offline

    KarimAKL

  14. Offline

    Strahan

    Also remove the listener registration from the GameMenu class; you don't need to register it twice.
     
Thread Status:
Not open for further replies.

Share This Page