@EventHandler does not open the menu

Discussion in 'Plugin Development' started by migcabreraes, Mar 1, 2015.

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

    migcabreraes

    I have a problem and I have done an onJoin which gives the player one Item all this correct the problem I have is that that item has to open a menu: the code making the menu opens this is:
    Code:
    @EventHandler
                public void onPlayerInteract(PlayerInteractEvent e) {
                  if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
                    menu.show(e.getPlayer());
    but I can not make the item to run the
    Code:
     menu.show (e.getPlayer ());
    And I have to add the menu.show in the code below but I can't do it.

    Code:
       public static ItemStack EQUIPOS() {
            ItemStack item = new ItemStack(Material.BEACON, 1);
            ItemMeta im = item.getItemMeta();
            im.setDisplayName(ChatColor.GREEN + "Selector de Equipos");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GRAY + "Elige un equipo y lucha!");
            im.setLore(lore);
            item.setItemMeta(im);
            return item;
          
        }
       @EventHandler
        public void onJoin(final PlayerJoinEvent e) {
            final Player p = e.getPlayer();
            getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                public void run() {
                    p.getInventory().addItem(EQUIPOS());
                }
            }, 5L);
        }
    
     
  2. Offline

    ChipDev

    Debug..
     
  3. Offline

    migcabreraes

    that doesn't help much
     
  4. Offline

    SuperOriginal

    Stacktraces?
     
  5. Offline

    migcabreraes

    what?
     
  6. Offline

    Zombie_Striker

    Did you implement listener? Did you registerEvents();? Does your plugin even get run?

    These are the first things you have to look at when an Event isn't firing.
     
  7. Offline

    Skionz

  8. Offline

    BurnerDiamond

  9. Offline

    LetsTalkTnTHere

    Be sure you have this in your main class
    Code:
    public void onEnable() {
           Bukkit.getServer().getPluginManager().registerEvents(ClasswhereEventIs, this);
    
    }
     
  10. Offline

    Skionz

    I disagree.
     
  11. Offline

    BurnerDiamond

    Skionz likes this.
  12. Offline

    migcabreraes

    I have the class registered as runs the menu using this:
    Code:
    @EventHandler
                public void onPlayerInteract(PlayerInteractEvent e) {
                  if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
                    menu.show(e.getPlayer());
            }
    but I want to work with the item but I cannot make the item to run the:
    Code:
    menu.show(e.getPlayer());
    Item public static:
    Code:
    public static ItemStack EQUIPOS() {
            ItemStack item = new ItemStack(Material.BEACON, 1);
            ItemMeta im = item.getItemMeta();
         
            //Lore del item del inventario.
         
          im.setDisplayName(ChatColor.GREEN + "Selector de Equipos");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GRAY + "Elige un equipo y lucha!");
            im.setLore(lore);
         
            item.setItemMeta(im);
            return item;
         
        }
       @EventHandler
        public void onJoin(final PlayerJoinEvent e) {
            final Player p = e.getPlayer();
            getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                public void run() {
                    p.getInventory().addItem(EQUIPOS());
                }
            }, 5L);
        }
    
     
  13. Offline

    LetsTalkTnTHere

    Try
    Code:
    p.openInventory(inv);
    
    Even better create a void where you create the items etc. and the player opens the inventory
    Code:
    Inventory inv = Bukkit.createInventory(null, 9, "name");
    public void openMyInventory(Player p) {
          ItemStack i = new ItemStack(Material.MATERIAL);
        
         inv.setItem(0,i);
       
         p.openInventory(p); //Opens inventory
    }
    I use this all the time just type
    Code:
    @Override
    public boolean onCommand(CommandSender cs, Command cmd ,String label, String args[]){
         if(label.equalsIgnorecase("cmd") {
              if(cs instanceof Player) {
                   openMyInventory(cs);
         }
    }
     
    Last edited by a moderator: Mar 2, 2015
  14. Offline

    migcabreraes

    I don't understand your idea, I need the item to open the menu that opens with.
    Code:
       menu.show(e.getPlayer());
    I need the item that gives you to log in to the server to open my menu now opens the menu with this
    Code:
     @EventHandler
                public void onPlayerInteract(PlayerInteractEvent e) {
                  if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
                    menu.show(e.getPlayer());
            }
     
  15. Offline

    guitargun

    @migcabreraes
    can we see your class menu with the method show? you only show us this small code snippet we can't see what this menu and what this show is. also
    Code:
      if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK))
    can be simplified to
    Code:
      if (e.getAction() != Action.RIGHT_CLICK_BLOCK)
     
  16. Offline

    migcabreraes

    Yes Menu class:
    Code:
    package com.topodits.Annihilation.listeners;
    
    import java.util.Arrays;
    import org.bukkit.Bukkit;
    import static org.bukkit.Bukkit.getServer;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.GameMode;
    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;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.material.Wool;
    import org.bukkit.plugin.Plugin;
    public class SeleccionarTeam implements Listener {
            private Inventory inv;
            private ItemStack ROJO, VERDE, AMARILLO, AZUL;
      
            public SeleccionarTeam(Plugin p) {
                    inv = Bukkit.getServer().createInventory(null, 9, ChatColor.AQUA + "§lSelecciona Equipo");
              
                    ROJO = createItem(DyeColor.RED, ChatColor.RED + "Equipo Rojo");
               
                    VERDE = createItem(DyeColor.GREEN, ChatColor.GREEN + "Equipo Verde");
               
                    AMARILLO = createItem(DyeColor.YELLOW, ChatColor.YELLOW + "Equipo Amarillo");
               
                    AZUL = createItem(DyeColor.BLUE, ChatColor.BLUE + "Equipo Azul");
              
                    inv.setItem(0, ROJO);
                    inv.setItem(1, VERDE);
                    inv.setItem(2, AMARILLO);
                    inv.setItem(3, AZUL);
               
                    Bukkit.getServer().getPluginManager().registerEvents(this, p);
            }
      
            private ItemStack createItem(DyeColor dc, String name) {
                    ItemStack i = new Wool(dc).toItemStack(1);
                    ItemMeta im = i.getItemMeta();
                    im.setDisplayName(name);
                    im.setLore(Arrays.asList("Elige el " + name.toLowerCase()));
                    i.setItemMeta(im);
                    return i;
            }
      
            public void show(Player p) {
                    p.openInventory(inv);
            }
      
            @EventHandler
            public void onInventoryClick(InventoryClickEvent e) {
                    if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
                    if (e.getCurrentItem().getItemMeta() == null) return;
                    //Equipo Rojo
                    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Rojo")) {
                            e.setCancelled(true);
                            getServer().dispatchCommand(getServer().getConsoleSender(), "comando");
                            e.getWhoClicked().closeInventory();
                    }
                    //Equipo Verde
                    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Verde")) {
                            e.setCancelled(true);
                       
                            e.getWhoClicked().closeInventory();
                    }
                    //Equipo Amarillo
                    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Amarillo")) {
                            e.setCancelled(true);
                         
                            e.getWhoClicked().closeInventory();
                    }
                    //Equipo Azul
                    if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Azul")) {
                            e.setCancelled(true);
                       
                            e.getWhoClicked().closeInventory();
                    }
                    //Añadir Equipo Random usando Math.random();
         
    
            }
    }
     
  17. Offline

    migcabreraes

  18. Offline

    LetsTalkTnTHere

    try ItemStack i = new ItemStack(Material.WOOL);
     
    Last edited: Mar 3, 2015
  19. Offline

    migcabreraes

    seriously?
    The menu works and everything just need help with this: do not open the menu and I need to incorporate the
    Code:
    menu.show(e.getPlayer());
    to:
    Code:
    public static ItemStack EQUIPOS() {
            ItemStack item = new ItemStack(Material.BEACON, 1);
            ItemMeta im = item.getItemMeta();
            im.setDisplayName(ChatColor.GREEN + "Selector de Equipos");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GRAY + "Elige un equipo y lucha!");
            im.setLore(lore);
          
            item.setItemMeta(im);
            return item;
          
        }
    
       @EventHandler
        public void onJoin(final PlayerJoinEvent e) {
            final Player p = e.getPlayer();
            getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                public void run() {
                    p.getInventory().addItem(EQUIPOS());
                }
            }, 5L);
        }
    For the item
    Code:
    im.setDisplayName(ChatColor.GREEN + "Selector de Equipos");
    Open this:
    Code:
    menu.show(e.getPlayer());
    I need insert:
    Code:
     menu.show(e.getPlayer());
    in:
    Code:
      public static ItemStack EQUIPOS() {
            ItemStack item = new ItemStack(Material.BEACON, 1);
            ItemMeta im = item.getItemMeta();
           
            im.setDisplayName(ChatColor.GREEN + "Selector de Equipos");
            List<String> lore = new ArrayList<String>();  
            lore.add(ChatColor.GRAY + "Elige un equipo y lucha!");
           
            im.setLore(lore);
            item.setItemMeta(im);
            return item;
           
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  20. If you want help on spanish send me a pm .
     
Thread Status:
Not open for further replies.

Share This Page