Custom Inventory Won't Open

Discussion in 'Plugin Development' started by Simon76800, Jan 4, 2015.

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

    Simon76800

    I've tried really hard to find a solution to my problem, but I couldn't find one anywhere. I'm trying to make it so that when you click a merchant, they open up a custom inventory with 54 slots instead of the default inventory. I registered all my events properly, since the other events are working. It's also my first time posting on the Bukkit Forums, so I'm sorry if I posted it on the wrong thread. Here's part of my code:

    Declaring variables:
    Code:
    public class EventListener implements Listener {
        private Inventory pricesInventory;
        private Inventory productsInventory;
        private String testMerchantName = ChatColor.LIGHT_PURPLE + "Test Merchant";
    PlayerInteractEntityEvent:
    Code:
    @EventHandler(priority = EventPriority.NORMAL)
        public void onInteract(PlayerInteractEntityEvent event) {
            if (event.getRightClicked() instanceof Villager) {
                event.setCancelled(true);
                if (Merchant.isMerchant((Villager) event.getRightClicked())) {
                    Merchant merchant = Merchant.merchants.get((Villager) event
                            .getRightClicked());
                    Player player = event.getPlayer();
                    if (Merchant.getIdentifier(merchant).equals(
                            "spawnCity_testMerchant")) {
                        pricesInventory = Bukkit.createInventory(null, 54);
                        productsInventory = Bukkit.createInventory(null, 54, testMerchantName);
                        pricesInventory.setItem(0, new ItemStack(Material.EMERALD,
                                3));
                        productsInventory.setItem(0, new ItemStack(
                                Material.DIAMOND_AXE, 1));
                        player.openInventory(productsInventory);
                        event.setCancelled(true);
                    }
                }
            }
        }
    Oh and BTW, I know the code is being reached since I can send the player a message right before or after I try to open the inventory.
     
    Last edited: Jan 5, 2015
  2. Offline

    _Cookie_

    Why create the inventory? Just make a custom one outside of the event and use it? could make your code look a lot neater and help you find the problem.
     
  3. Offline

    Tecno_Wizard

    @Simon76800, did you register the listener in the main class?
     
  4. Offline

    xMakerx

    @Simon76800

    Try printing out the Merchant's identifier and seeing if it's set correctly and you should add a way to check if the Merchant is null so you don't get a NullPointerException.
     
  5. Offline

    Simon76800

    Yeah... since the other events are working

    I've added a way to check if a merchant is null, and the identifier of the merchant is set correctly... but it still doesn't work :(

    I moved my inventories outside the event, and the inventory still won't open. I also tried making the inventories static... but it didn't do anything.
    Code:
    public class EventListener implements Listener {
        private Inventory pricesInventory;
        private Inventory productsInventory;
        private String testMerchantName = ChatColor.LIGHT_PURPLE + "Test Merchant";
    
        {
            pricesInventory = Bukkit.createInventory(null, 54);
            productsInventory = Bukkit.createInventory(null, 54, testMerchantName);
            pricesInventory.setItem(0, new ItemStack(Material.EMERALD, 3));
            productsInventory.setItem(0, new ItemStack(Material.DIAMOND_AXE, 1));
        }
     
    Last edited by a moderator: Jan 5, 2015
  6. Offline

    Tecno_Wizard

  7. Offline

    VG.Developments

    @Tecno_Wizard Set item will work but he has too make sure he does different slots, not all the same like he did.
     
  8. Offline

    Simon76800

    I didn't put two items in the same inventory... I put them in the same slot of different inventories
     
  9. Offline

    Tecno_Wizard

    @VG.Developments @Simon76800, I know, but I don't see anything else that could cause it.

    EDIT: wait, is pos 0 nonexistant? Try 1, things aren't always 0 oriented.
    EDIT2: Yes, pos 0 does not exist. I'm surprised that it doesn't throw an illegal argument exception.
     
  10. Offline

    Simon76800

    ?
     
  11. Offline

    Tecno_Wizard

    @Simon76800
    there is no position 0
    pricesInventory.setItem(0, new ItemStack(Material.EMERALD, 3));
     
  12. Offline

    Simon76800

    I thought there was a 0 for setItem, but I changed it to addItem... and it still doesn't work :(

    Should I just rewrite the plugin but copy paste a few things?
     
  13. Offline

    Tecno_Wizard

    @Simon76800, no, that won't get you anywhere.
    add a sysout to that code with gibberish in it and see if the code is even running first.
     
  14. Offline

    Simon76800

    I tried doing player.sendMessage() and I saw it on the screen so the code is running
     
  15. Offline

    Tecno_Wizard

    @Simon76800, now sysout the inventory itself and make sure it isn't null. if it isn't, you'll see a class summary. if it is, you'll see null.
     
  16. Offline

    Simon76800

    Uh... how do I exactly do that?
     
  17. Offline

    Tecno_Wizard

    @Simon76800, System.out.println(inventory);
    if that's true, loop through every inv slot with this again.
     
    Last edited: Jan 5, 2015
  18. Offline

    Simon76800

    It returned org.bukkit.craftbukkit.v1_7_R4.inventory.CraftInventoryCustom@245f0656
     
  19. Offline

    Tecno_Wizard

    @Simon76800, so the inventory exists correctly. Get each slot of the inventory and sysout it. you should see a different result this time I believe.
     
  20. Offline

    Simon76800

    I tried it, and the first slot (which is the only one I set) is one diamond axe, and the rest is null
     
  21. Offline

    Tecno_Wizard

    @Simon76800, so then what should you look at as the culprit? (I'm trying to teach you how to debug btw, and I have known what has been causing it for almost 5 posts now (Well, I think) )
     
  22. Offline

    Simon76800

    lol... honestly i think it has something to do with openInventory()
     
  23. Offline

    Tecno_Wizard

    @Simon76800, so canceling the event doesn't have an effect?
    If removing that doesn't fix it, tell me and I'll continue assisting tomorrow. I have some sleeping to do.
     
  24. Offline

    Simon76800

    I tried not cancelling it, but then the default villager inventory shows up
     
  25. Offline

    Tecno_Wizard

  26. Offline

    VG.Developments

    @Tecno_Wizard 0 does exist, it's the first slot in the inventory. Remember the numbers start with 0, 1, 2, not 1, 2 etc....
     
  27. Offline

    Tecno_Wizard

    VG.Developments likes this.
  28. Offline

    Simon76800

    Last edited: Jan 6, 2015
  29. Offline

    bobthefish

    Code:
    new BukkitRunnable(){
    
       public void run(){
          //do stuff
       }
    }.runTaskLater(plugin, 1);
     
  30. Offline

    Simon76800

    thx
     
Thread Status:
Not open for further replies.

Share This Page