Solved Creating a chest inventory with a custom title

Discussion in 'Plugin Development' started by Drkmaster83, Jul 24, 2013.

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

    Drkmaster83

    I'm having a bit of trouble creating a chest inventory with a custom title. I see that you can create an inventory with a custom amount of slots with a title, but not one with a chest inventory and a custom title. It's supposed to be a trading plugin, so the player must be able to drag items from their inventory to a part of a second inventory (a chest).

    My code for opening the 27-slotted non-functioning inventory:
    Code:
    //Trading Inventory Setup Begin
    Inventory i = player.getServer().createInventory(null, 27, "Trading with " + Traders.get(player.getName()));
    setInvItem(i, 0, 351, (byte) 10, "§2Accept", null);
    setInvItem(i, 4, 118, (byte) 0, "§r", null);
    setInvItem(i, 8, 351, (byte) 10, "§2Accept", null);
    setInvItem(i, 9, 102, (byte) 0, "§r", null);
    setInvItem(i, 13, 118, (byte) 0, "§r", null);
    setInvItem(i, 17, 102, (byte) 0, "§r", null);
    setInvItem(i, 18, 351, (byte) 1, "§cDecline", null);
    setInvItem(i, 22, 118, (byte) 0, "§r", null);
    setInvItem(i, 26, 351, (byte) 1, "§cDecline", null);
    player.openInventory(i);
    player.getServer().getPlayer(Traders.get(player.getName())).openInventory(i);
    //Trading Inventory Setup End
     
    public void setInvItem(Inventory i, int slot, int itemID, byte data, String name, ArrayList<String> lore)
        {
            ItemStack is = new ItemStack(itemID, 1, data);
            ItemMeta im = is.getItemMeta();
            im.setDisplayName(name);
            im.setLore(lore);
            is.setItemMeta(im);
            i.setItem(slot, is);
        }
    This opens a 27-slot inventory that I can't drag items from my inventory into.

    Bump -- Sorry if it's considered an early bump, pulled an all-nighter and slept, so feels like a new day.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  2. Offline

    Drkmaster83

    Bump, please...
     
  3. Offline

    Pawnguy7

    Not quite sure what the problem here is. Why can you not drag items into it?
     
  4. Offline

    Drkmaster83

    When I first created it, it wouldn't show the player's inventory along with the inventory that I was creating. It seems to be doing this now, though. Either it was different code or I was really tired.

    Anyways, now I'm having a separate issue:
    I can't tell how to distinguish clicks in the inventory that I created from clicks in the player's inventory.
     
  5. Offline

    xxMOxMOxx

    Drkmaster83 Assuming your using an InventoryClickEvent, I think that event.getRawSlot() includes data about which inventory was clicked.
     
  6. Offline

    Pawnguy7

    InventoryClickEvent has getInventory(). Perhaps you can check if it .equals(shopinventory)?
     
  7. Offline

    Drkmaster83

    Well, when I performed a check on InventoryClick for the InventoryType, the only thing that didn't turn up as InventoryType.CHEST was the quickbar (InventoryType.QUICKBAR). I checked for the SlotType, and same result, except in place of CHEST was CONTAINER.

    Edit: I did some googling after doing these checks and found that there's event.getView().getTopInventory() and event.getView().getBottomInventory(), but I don't see how that helps me in InventoryClickEvent. How would I tell if the click was performed in the top or bottom?
     
  8. Offline

    xxMOxMOxx

    Drkmaster83 Did you try this yet? It looks like a good idea.
     
  9. Offline

    Drkmaster83

    Firstly, I think it'd all be the same inventory. Plus, I don't store the inventory with the player's name. I store two players names in the same database.

    All I need is to tell the Player Inventory clicks from the Chest Inventory clicks: [​IMG]
     
  10. Offline

    Drkmaster83

  11. Offline

    xigsag

    All I know is that for getting the top/bottom inventory, you could do
    Code:java
    1. e.getView().getBottomInventory();
    2.  
    3. e.getView().getTopInventory();
    4.  
    5. // e being the InventoryClickEvent


    EDIT: Drkmaster83
     
  12. Offline

    Samthelord1

  13. Offline

    xTrollxDudex

    Drkmaster83
    Use .getSlot() or .getRawSlot() and If it is 0-26 then it is from the top inventory, if it is more, then it is the bottom inventory.

    Also note that this will probably return null if you click outside the inventory window, check to see if the clicktype is not one of the click outside ones
     
  14. Offline

    xigsag


    Or you could just put a try/catch. Works for me.
     
  15. Offline

    Drkmaster83

    I'll post my listener code pretty soon. I tried what you suggested before, TrollDude, and for some reason, the bottom inventory printed out up to slot 26 an the bottom started at 9 and went to 35.

    xTrollxDudex
    xigsag
    Listener Code:
    Code:
    @EventHandler
        public void onInventoryClick(InventoryClickEvent event)
        {
            if(event.getWhoClicked() instanceof Player)
            {
                Player player = (Player) event.getWhoClicked();
                if(plugin.Traders.containsKey(player.getName()) || plugin.Traders.containsValue(player.getName()))
                {
                    if(event.isLeftClick() || event.isRightClick() || event.isShiftClick())
                    {
                        if(event.getSlotType().equals(SlotType.OUTSIDE))
                        {
                            return;
                        }
                        if(event.getInventory().getType().equals(InventoryType.CHEST))
                        {
                            Inventory chestInv = event.getView().getTopInventory();
                            Inventory playerInv = event.getView().getBottomInventory();
                            if(event.getView().equals(event.getView().getTopInventory()))
                            {
                                System.out.println("Chest");
                                return;
                            }
                            else if(event.getView().equals(event.getView().getBottomInventory()))
                            {
                                System.out.println("Player");
                                return;
                            }
                            /*ItemStack clickedItem = event.getInventory().getItem(event.getSlot());
                            if(clickedItem == null || clickedItem.getType().equals(Material.AIR))
                            {
                                return;
                            }
                            else if(clickedItem.getType().equals(Material.INK_SACK) && event.getInventory().getType().equals(InventoryType.CHEST))
                            {
                                addGlow(clickedItem);
                                return;
                            }
                            else
                            {
                                event.setCancelled(true);
                                return;
                            }*/
                        }
                    }
                }
            }
        }
    addGlow(ItemStack i) is a method that makes the enchantment glow on an object, no errors with it, so don't ask about it. :p

    When I click on all three inventories, the quickbar, the chest inventory, and the player inventory, the result is always "Chest." I need to figure out how to make it print out "Player."

    Edit: I was using System.out.println() for mere convenience. Normally, I wouldn't use System methods. Don't get on to me about that, either. :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  16. Offline

    Drkmaster83

    You were correct, sir. In all this testing, I never actually tried using the RawSlot to detect the inventory, only the getSlot() method. I kept thinking that they were the same or that neither could help me, but I have finally figured out that I was looking to use getRawSlot() and if it's more than 26, it's in the player's inventory. Thanks a lot, and sorry all if I was being difficult.
     
  17. Offline

    xTrollxDudex

    Drkmaster83
    Glad to help, you weren't that much of a pain to help :)
     
Thread Status:
Not open for further replies.

Share This Page