Trouble Calling "openGUI(player);"

Discussion in 'Spigot Plugin Development' started by Karatechimp29, May 7, 2015.

  1. Hey guys, so i'm currently creating a Bukkit plugin and i'm really dropped for what to do here. I wanted my plugin to open the GUI upon command but i cant seem to get it to go, here is my code:

    package me.karatechimp29.bukkit;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    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()
    {
    this.getServer().getPluginManager().registerEvents(this, this);
    }

    private void openGUI(Player player)
    {
    Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GOLD + "Voting Menu");

    ItemStack Link1 = new ItemStack(Material.DIAMOND);
    ItemMeta Link1Meta = Link1.getItemMeta();
    ItemStack Link2 = new ItemStack(Material.DIAMOND);
    ItemMeta Link2Meta = Link1.getItemMeta();

    Link1Meta.setDisplayName(ChatColor.YELLOW + "Vote #1");
    Link1.setItemMeta(Link1Meta);
    Link2Meta.setDisplayName(ChatColor.YELLOW + "Vote #2");
    Link2.setItemMeta(Link2Meta);

    inv.setItem(1, Link1);
    inv.setItem(2, Link1);

    player.openInventory(inv);
    }

    @EventHandler
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    {
    if(cmd.getName().equalsIgnoreCase("vote")){
    if(sender instanceof Player)
    {
    Inventory.open(player);
    }
    }

    return false;
    }

    }
     
  2. Wrong section + pastebin please
     
  3. jflory7

    jflory7 Retired Moderator
    Retired Benefactor

    Thread moved => Spigot Plugin Development
     
  4. Inkzzz

    Resource Staff

    A command isn't an event
     

  5. Code (Text):
    if(cmd.getName().equalsIgnoreCase("vote")){
    if(sender instanceof Player)
    {
    this.openGUI(<player>);
    }
    }
     
  6. What he said ^

    Also, onCommand doesn't need the EventHandler annotation because it is not an event. Make sure you register your command in your plugin.yml and also add a getCommand#setExecutor(this).
     
  7. I would add that you also need to declare "player" or cast "sender" to a "Player."
     
  8. ok now im recieving an incorrect syntax error with "this.openGUI(<player>);"

    http://pastebin.com/k3QgvhpQ
    as requested

    thanks for the support, im new to this :D
     
  9. Remove "< >" :p
     
  10. Almost right.

    As per my previous post, you haven't declared what "<player>" is.

    When you used...
    Code (Text):

    if(sender instanceof Player)
                {

                }
     
    ...you were checking to see if the 'CommandSender' object, named "sender" was an instance of a 'Player' object. Once you know this, you can safely treat it as one. However, you need to tell the plugin to treat it that way. We do this by casting (which you should read up on) the 'CommandSender' to a 'Player' and we can then use this object for your GUI method, which requires a 'Player'
    Example 1:
    Code (Text):

    Player player = (Player) sender;
    openGUI(player);
     
    Example 2:
    Code (Text):

    openGUI((Player) sender);
     
    Now, just put them back together.
     
  11. This is probably getting really annoying guys, but i feel while i have you here i might aswell get some help :D

    So i have separated the GUI from my main class and was wondering how to make each of my diamond "buttons" display a vote link in chat or even launch a web browser to a link that has been set in a config (no idea how to make a config).

    (Maybe call them by their meta names as they are all diamonds?)

    http://pastebin.com/Y93R6Q8C
    here is my gui class ^^

    Thanks for staying by me, it means a lot. I feel im gradually learning something, sometimes i need to ask "my peers"
    :D
     
  12. Only your main class can extend JavaPlugin. If I'm not mistaken that GUI class doesn't look like your main.
     
  13. ok i have fixed that, removed the "extends JavaPlugin".
     
  14. COmmandPrePRocess event :p
     
  15. I could really use a hand please
     
  16. He isn't calling this event...

    @Karatechimp29 Also, you should learn Java before you learn Spigot. I can tell by your lack of java naming conventions and the static abuse.
     
  17. Doing the best i can, i figured that making a plugin i would be able to see what each thing does in code relevant to in-game. but your right :D Know of any good resources for java.

    (also, i would love for my question above to be answered, cant find this info anywhere)
     
    • Agree Agree x 1
  18. Thanks man, I'll be sure to check that out. But does anyone know? Having several diamonds as my gui "buttons" would I need to call them by their meta name or something as they would all have the same ID, for adding actions to them that is