inventory questions

Discussion in 'Plugin Development' started by paxi, Jan 21, 2014.

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

    paxi

    Hello everbody,

    at the moment i write a plugin which should allow me to use a inventory as menu, so every itemslot for as menu option! How i can open an inventory and how i can read out from a special slot i can do but i have some questions:
    - How can i set only the inventory to "read-only"? So nobody can take items of this inventory away
    - Which Event Listener should i use? At the moment i use the inventoryClickevent...
    And how can i only read from the opened inventory and not from the normal inventory?

    I hope somebody have some nice tipps or ideas and sorry for my question and my bad english :)!
     
  2. Offline

    Warreo

    paxi InventoryClickEvent is the event you're most definitely going to want to use, to make it so you can't actually pull or put items in the inventory you just cancel the event. Here is how you can tell which inventory is clicked:
    Code:java
    1. if (event.getRawSlot() == event.getSlot()) {
    2. //Click happened in the chest inventory
    3. } else {
    4. //Click happened in player's inventory
    5. }
     
  3. Offline

    Niknea

    paxi InventoryClickEvent ;). Make sure you make a if statement at the start, where it checks the inventory, else if you click any item anywhere, chests, in your own inventory, it will act like it's in the inventory, and perform the event. So be careful ;)
     
  4. Offline

    paxi

    Thanks for your answers but they doens't solve my problem: here my code:
    Code:java
    1. public void oninventoryclick(InventoryClickEvent e){
    2.  
    3. if(e.getRawSlot()==e.getSlot()){
    4. e.setCancelled(true);
    5.  
    6. //code where i read out the item and do something other


    I can move the items and the funktion which readout the items starts also in the normal player inventory :(
     
  5. Offline

    Warreo

    paxi I don't understand what you're trying to say. What do you mean by the readout starts in the normal player inventory; as in the slots?
     
  6. Offline

    paxi

    Okay sorry. I mean following: With one command I open a small chest inventory which i want to use as a menu. So when i klick on slot 1 of this menu/inventory for example my plugin prints in the config.yml something. This read out works good! But I also want that i can't take this items of this menu away and this doens't work.... On this position O hope someone of you can help me :)! So maybe you can compare it like the "read only" status of a folder in windows....!
    I hope it is a better explanation :D!

    With this code i try it:
    Code:java
    1. public void oninventoryclick(InventoryClickEvent e){
    2.  
    3. if(e.getRawSlot()==e.getSlot()){
    4. e.setCancelled(true);


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

    Warreo

    paxi Ok now I understand, what's really happening, or at least should be, is it is giving you a ghost item. The best way I've found to get rid of this error is by updating the inventory. :)
     
  8. Offline

    paxi

    How do you mean this? :O
    At the moment it is so:
    When i take a item of this inventory i replace it thats not the problem but how can i set the item in the "cursor==air"?
     
  9. Offline

    Warreo

    paxi So wherever you click the item is just removed all together? Or get the ItemStack?
     
  10. Offline

    paxi

    I only want that in the cursor is no item when I click on a special slot
     
  11. Offline

    Warreo

    Code:java
    1. ItemStack i = event.getCurrentItem();
    That will give you the ItemStack you click on.
     
  12. Offline

    paxi

    And howcan I set it? This one method is deprecated
     
  13. Offline

    Warreo

    paxi
    Code:java
    1. event.getInventory().setItem(event.getSlot(), new ItemStack(Material.EMERALD, 1));
     
  14. Offline

    paxi

    This changes the item at the cursor? Okay thank you,I will try it when I am at home

    Warreo

    I found the code which shoudl do what i want but why it doens't work?
    Code:
    Code:java
    1. ItemStack handitem= new ItemStack(Material.AIR);
    2. p.setItemOnCursor(handitem); //p is the player


    I think it should work or? Or need i to reload something ? :O

    Did someone have an idea how i can change it? It doesn't work...

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

    paxi

    Now i have following code:
    Code:java
    1. if (e.getInventory().getName().equalsIgnoreCase("Skills")){
    2. Inventory pinv = e.getInventory();
    3. Player p = (Player) e.getWhoClicked();
    4. ItemStack cursor = e.getCursor();
    5. ItemStack itemclicked = e.getCurrentItem();
    6. if (e.getRawSlot() < 27 && e.getRawSlot() > -1){
    7. cursor = e.getCursor();
    8. itemclicked = e.getCurrentItem();
    9. p = (Player) e.getWhoClicked();
    10. e.setCancelled(true);
    11. }
    12.  
    13.  
    14.  
    15. ItemStack handitem= new ItemStack(Material.AIR);
    16. p.setItemOnCursor(handitem);
    17. p.updateInventory();


    When i get an item of this special inventory i can't put it in the normal player inventory but when i clicked on the item i see the item on the cursor :/! How can i change it? I hope you understand what i mean...

    Of course: All code is called.... also the if....
     
Thread Status:
Not open for further replies.

Share This Page