My VirtualInventoryPlugin

Discussion in 'Spigot Plugin Development' started by CodingDuck, May 24, 2015.

  1. Hi I made a plugin and when I reload my server I don't get it in my plugins list and no errors are showing up. I think I made a pretty dumb mistake, but I am unable to find it.
    The main:
    Code (Text):
    package me.codingduck.main;

    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.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    public class VirtualInventory extends JavaPlugin implements Listener{
           
        private Inventory inv;
        private int lines = 3;
       
        public void onEnable(){
           
        }
       
        public void onDisable(){
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
           
            if(label.equalsIgnoreCase("chest")){
                Player p = (Player) sender;
                if(p.hasPermission("chest.open")){
                    inv = p.getServer().createInventory(null, lines * 9, "&b" + p.getName() + "&e" + "Inventory");
                   
                    ItemStack item = new ItemStack(Material.BAKED_POTATO);
                    ItemMeta meta = item.getItemMeta();
                    meta.setDisplayName("&aFOOD");
                    item.setItemMeta(meta);
                   
                    inv.setItem(13, item);
                   
                    p.openInventory(inv);
            }
           
          }
            return true;
        }
       
        @EventHandler
        public void onInvClick(InventoryClickEvent e){
            Player p = (Player) e.getWhoClicked();
            if(e.getInventory().getName().equalsIgnoreCase("&b" + p.getName() + "&e" + "Inventory")){
                e.setCancelled(true);
                    if(e.getCurrentItem().getType() == Material.BAKED_POTATO){
                        p.sendMessage("Hier bekommst du Essen!");
                        p.getInventory().addItem(new ItemStack(Material.BAKED_POTATO, 9));
                    }
            }
        }
    }
     
    And my plugin.yml:

    Code (Text):
    name: VirtualInventory
    version: 1.0
    author: CodingDuck
    main: VirtualInventory.me.codingduck.main.VirtualInventory
    commands:
      chest:
        description:  Creates a chest
    permissions:
      chest.open:
        description: Open a chest
        default: op
    It would be nice if you would help me! :)
     
  2. Just a good idea, is to put a message in console saying like [VirtualInventory] Version 1.0 Loaded.
    and on disable
    [VirtualInventory] Version 1.0 Unloaded.
    or something like that.
     
  3. Your package is incorrect in the plugin.yml, it should be:
    Code (Text):
    main: me.codingduck.main.VirtualInventory
     
    #4 Arman, May 24, 2015
    Last edited: May 24, 2015
  4. Code (Text):

    package me.codingduck.main;
     
     
  5. @CodingDuck
    You have some odd naming, (codingduck.main for a package? - assuming only one package in plugin) But you need to change your Plugin.yml main reference, as Arman said. Change it to something like

    Code (Text):
    main: me.codingduck.main,VirtualInventory
    Hope that helps.
     
  6. Thanks You guys that was very helpful. So now I have coded a bit more but when I enter /chest it opens the inventory, but I can take out the potatoes so something is wrong with e.setCancelled(true); I think. Here the code:

    Code (Text):
    package me.codingduck.main;

    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.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    public class VirtualInventory extends JavaPlugin implements Listener{
           
        private Inventory inv;
        private int lines = 3;
       
        public void onEnable(){
           
        }
       
        public void onDisable(){
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
           
            if(label.equalsIgnoreCase("chest")){
                Player p = (Player) sender;
                if(p.hasPermission("chest.open")){
                    inv = p.getServer().createInventory(null, lines * 9, "§b" + p.getName() + "§e " + "Inventory");
                   
                    ItemStack item = new ItemStack(Material.BAKED_POTATO);
                    ItemMeta meta = item.getItemMeta();
                    meta.setDisplayName("§aFOOD");
                    item.setItemMeta(meta);
                   
                    inv.setItem(13, item);
                   
                    p.openInventory(inv);
            }
           
          }
            return true;
        }
       
        @EventHandler
        public void onInvClick(InventoryClickEvent e){
            Player p = (Player) e.getWhoClicked();
            if(e.getInventory().getName().equalsIgnoreCase("§b" + p.getName() + "§e " + "Inventory")){
                    e.setCancelled(true);
                    if(e.getCurrentItem().getType() == Material.BAKED_POTATO){
                        p.sendMessage("Hier bekommst du Essen!");
                        p.getInventory().addItem(new ItemStack(Material.BAKED_POTATO, 9));
                        e.setCancelled(true);
                    }
            }
        }
    }
     
  7. You seem to be checking for the name of the inventory, and then entering the title.

    This is how you would do it:
    Code (Java):
        @EventHandler
        public void onInvClick(InventoryClickEvent e){
            if (e.getWhoClicked() instanceof Player) {
            Player p = (Player) e.getWhoClicked();
            if (e.getInventory() != null) {
            if(e.getInventory().getTitle().equalsIgnoreCase("§b" + p.getName() + "§e " + "Inventory")){
                    e.setCancelled(true);
                    }
                    if (e.getCurrentItem() != null) {
                    if(e.getCurrentItem().getType() == Material.BAKED_POTATO){
                        p.sendMessage("Hier bekommst du Essen!");
                        p.getInventory().addItem(new ItemStack(Material.BAKED_POTATO, 9));
                   
            }}
        }}
    }}
    Added a couple of checks in there to prevent NPEs, and removed the extra cancellation of the event.
     
  8. You're cancelling the event twice. Perhaps you mean to close the inventory? Also, I believe that it is trying to add the potatoes to the chest inventory, not the player inventory (not 100% on that). A solution would be closing the inventory then adding the potatoes, or waiting for the player to close the inventory.
     
  9. You have "e.setCancelled(true);" twice in there. Take out the second one. (I'm not 100% sure if that will fix it, but this is how I wrote my SuperGamble plugin).

    Edit: Wow. Two people beat me q.q
     
  10. Hello it still doesn't work, and I think the error is that I dont register the event in the onEnable() part but I dont know how do to that. Can anyone help me? The plugin code can be found in the last post of me.
     
  11. Code (Java):
    Bukkit.getPluginManager().registerEvents(this, this);
     
    • Agree Agree x 1
  12. But is this enough? Shouldn't be the first 'this' a listener? If it is enough thank you. If not thank you too. I will test it tomorrow
     
  13. When this refers to a class that implements the Listener interface (as this one does), that's the case.
     
    • Agree Agree x 1
  14. So it is enough? Or it isn't?
     
    • Creative Creative x 1
  15. That will register your VirtualInventory instance as Listener, yes.
     
  16. Ok thank you I will try it out tomorrow