Solved Add inventory permanently to player

Discussion in 'Plugin Development' started by Shmobi, Mar 15, 2015.

Thread Status:
Not open for further replies.
  1. Heyho,
    i want to create an inventory, a player opens every time he opens his own inventory via pressing the inventory button (default e). creating the inventory etc is done but idk how i cann show it to the player, every time the player opens his own inventory. i tried so far with
    Code:
      @EventHandler
       public void onPlayerOpenInventory(InventoryOpenEvent pIoe) {
         System.out.println("Inventory opened");
         InventoryHolder holder = pIoe.getInventory().getHolder();
         if (holder instanceof Player) {
           System.out.println("Holder is player");
           ((Player) holder).openInventory(i);
         }
       }
    
    but a) this event isnt called if a player opens his own inventory. it is only called if he opens a craftingtable or something like this and b) it didnt show the inventory, event when the event was called.

    Thanks, Shmobi
     
  2. @Shmobi
    InventoryHolder will never be an instance of a Player. Perhaps try PlayerInventory instead?
     
  3. @Assist you're wrong. the inventory holder is always the person who owns the inventory. for example, if you open an craftingtable you are the inventory holder, because only you can access the inventory of the craftingtable you opened. in this case you are the inventory holder, so it is an instance of a player. if you open a chest then inventory is public, so you arent the holder. but the problem is something else. InventoryOpenedEvent isnt triggered if the player opens his own inventory by pressing e (or which key you use for it). it is only triggered if the inventory of a chest or a craftingtable or something is opened
     
  4. @Shmobi
    Oh oops, I was thinking of something else :oops:
     
  5. Offline

    Kilorbine

  6. @Kilorbine
    You should read this:
    "Deprecated. Use InventoryClickEvent or InventoryOpenEvent instead, or one of the other inventory events in org.bukkit.event.inventory."

    So InventoryOpenEvent should work fine.
     
  7. @Assist @Kilorbine PlayerInventoryEvent is deprecated and wont be called when a Player open his own inventory. InventoryOpenEvent or another inventoryevent is also not called if a player open his own inventory. it is only called if a player opens then inventory of something else, like a chest, craftingtable or a furnance
     
  8. @Shmobi
    Yeah apparently the client doesn't tell the server when they open the inventory. The best thing I can think of is to create a repeating task to check if their current open inventory is their own inventory.
     
  9. @Assist
    tried something like:
    @EventHandler
    public void onSelfInventory(InventoryOpenEvent e) {
    if (e.getInventory().equals(e.getPlayer().getInventory()) {
    //DO Something
    }
    }
     
  10. @Juancomaster1998 have you even read what i typed more than 2 times yet?! InventoryOpenEvent is not called if a player opens his own inventory....
     
  11. @Shmobi
    well, then you can just edit the player`s inventory instead of a new inventory?
     
  12. Offline

    NathanWolf

    If you want them to see a certain inventory when they open their inventory, why not just put items directly in their inventory?

    EDIT: Ninja'd by Junancomaster1998
     
  13. @Juancomaster1998 @NathanWolf i want to add something like a backpack. as long as you got the backpack item in your inventory, you can see the inventory of the backpack.
     
  14. Offline

    Signatured

    There's a way around this issue. When a player opens their inventory for the first time, they get an "Opened Inventory" achievement. We can utilize this to do what you need. Create a listener that listens for PlayerAcheivementAwardedEvent. Check to see if the acheivement is Acheivement.OPEN_INVENTORY. Since this acheivement is called when a player opens his own inventory, once this happens get the player who earned the achievement and open your custom inventory. Once they close the inventory (InventoryCloseEvent), remove the acheivement from the player. That way, once their inventory is reopened, they will earn the acheivement again and the custom inventory will be opened.

    This is the most efficient way I know how to do this. I suggest creating a login listener, and checking to see if they have the open inventory acheivement. If they do, remove it. This should help prevent issues in the future. You should also prevent achievements from being broadcasted.

    Remember to like if this helped you ;)
     
    dlange likes this.
  15. Offline

    caderape

    @Shmobi You can create a scheduler every second for replace the inventory, but this is not a good idea.

    if the inventory when a player press 'e' is the only one not called in the PlayerOpenInventory you can add player in an arraylist when they open an inventory. (PlayerOpenInventory) and check if the player is in the arraylist when they click an item. (InventoryClickEvent)
    If he's not, then you cancel the event and you replace with your created inventory. But i dun know if this can work.
     
  16. Offline

    NathanWolf

    I'm still a little confused about exactly what you want to do!

    Why not open a new inventory when they click on the backpack in their survival inventory?
     
  17. InventoryOpenEvent should work fine
     
  18. Offline

    Jacc734

    @Shmobi I agree with @FisheyLP just keep using the InventoryOpenEvent, but I have an idea that should help you out... make the first line of code in the event listener to cancel the action, then use the event data to open your custom inventory. I think the reason you might not be seeing it is because the players inventory opens... if that initial action is canceled perhaps the custom one will show up. Worth a shot.
     
  19. Offline

    NathanWolf

    It's been said in here several times already- but, just to be clear. The InventoryOpenEvent does not fire for a player opening their survival inventory.
     
    MexMaster and CodePlaysMinecraft like this.
  20. @NathanWolf i think you can give up on that xD for some reason they dont read what others type i guess :O

    I am gonna create my custom item and make its inventory open on right clicking it. done, problem solved. not as nice as i wanted it to be but fine, too. does anybody know how i can give my itemstack a own id or something to recognize that it is my custom item? i know that i can set the display name, lore and enchantments of an itemstack via its itemmeta, but this can always be changed via enchantment table and anvil. and i dont wanna do this via puting an id in the lore of an itemstack. thats ugly. is there a way to give an item you included via a shapedrecipe an identifier so i can recognice if it is my item or not?

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Mar 28, 2015
  21. Offline

    1Rogue

    How about this: You can create bags, and when a player right-clicks a bag in their inventory, it will open the backpack.
     
    NathanWolf likes this.
  22. Offline

    MexMaster

  23. Offline

    Signatured

    @NathanWolf but my solution does.
     
  24. Offline

    NathanWolf

    While I don't disagree with that... I can't exactly condone the approach either :D

    Seems kinda super hacky- I mean, kudos for the cleverness of it!

    But what if he wants to actually use achievements or has them turned on?

    In any case, I think making a clickable item would be a far easier way to go.
     
  25. Offline

    Signatured

    I agree, it is somewhat hacky, and achievements would have to be disabled, but I believe it's the most efficient way to detect if you open your own inventory. But if you plan on making a backpack type system, that sounds good too :)
     
  26. @Signatured well your way would work but i got achivements enabled so it would spam the serverchat every time somebody opens his inventory :p but ye it would be a possibility but also something i dont want to use

    in time i created my own class extending form itemstack which has some additional methods and infos to it. but now i got another problem. i want every player to have only one backpack. otherwise you could put backpack in a backpack in a backpack... and extend your inventory this way. so i have a java.util.Map<UUID, BackPackItemStack> which stores the backpack of every player. so you cant craft a backpack when you already got one and you can remove it by command. the thing is, how to find out which backpackitemstack belongs to which player after a reload or a restart? safing inventory of a backpack is not problem.
    how can i create a connection between a player and an item?
     
  27. Offline

    NathanWolf

    Not to be a Debbie Downer, but extending ItemStack is not going to work out. It might appear to work at first, but your custom ItemStack won't survive a server restart, or creative mode, or the InventoryDragEvent, or a variety of other cases where Bukkit will make a copy of your item internally, turning it back into a standard CraftItemStack.

    There is no reliable way to tag items with extra data, unfortunately. You could stick the player's UUID in the Backpack lore - if you use ChatColor.MAGIC it won't be immediately readable.
     
Thread Status:
Not open for further replies.

Share This Page