Solved Opening Player Inventory (Kit Selector)

Discussion in 'Plugin Development' started by Wiesel000, Sep 11, 2014.

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

    Wiesel000

    Hey guys,
    This is my code:

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. Player player = e.getPlayer();
    4.  
    5. Inventory inv = Bukkit.createInventory(null, InventoryType.CHEST, ChatColor.GOLD + "Select your kit!");
    6.  
    7. ItemStack sword = new ItemStack(Material.DIAMOND_SWORD);
    8. ItemMeta smeta = sword.getItemMeta();
    9. smeta.setDisplayName(ChatColor.GREEN + "PVP");
    10. smeta.setLore(Arrays.asList("Choose the PVP kit!"));
    11.  
    12. inv.setItem(0, sword);
    13.  
    14. if (e.getAction() != Action.RIGHT_CLICK_AIR || e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
    15. if (player.getInventory().getItemInHand().getType() != Material.PAPER) return;
    16.  
    17. if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK && player.getInventory().getItemInHand().getType() == Material.PAPER) {
    18.  
    19. player.openInventory(inv);
    20. }
    21. }


    And it does not work in game, well, I can't open the inventory when I click on a paper.
    I tried checking for if the paper has meta data (which it has) but it still didn't work.
    Please help me, I'd appreciate it!
    Thanks,
    Wiesel.
     
  2. Offline

    Mr_toaster111

    Try putting in some debug messages and seeing where it makes it and, did you register the events?
     
  3. Offline

    Webster56

    Have you registered your events in your main class ?
     
  4. Offline

    CraftCreeper6

    I would recommend you create a method called invGUI and accept a player input:
    Code:java
    1. public void invGUI(Player player){
    2. //Do your GUI here
    3. }

    Then just do:
    invGUI(player);
    in your interact event.

    On the other hand; for Checking clicking in the inventory do;
    Code:java
    1. public void invClick(InventoryClickEvent e){
    2. Player p = e#getPlayer
    3. switch(e#getCurrentItem#getType){
    4. case ITEM:
    5. // DO STUFF
    6. break;
    7. default:
    8. break;
    9. }
    10.  
    11. }

    Make sure to add checks to make sure it's the correct Inventory.
     
  5. Offline

    Wiesel000

    Oh, i totally forgot to register the event in the main class :D silly me :p thanks for your help tho
     
  6. Offline

    Wiesel000


    I tried using your invGUI(player); method, it didn't work.
    This is what i did:

    Code:java
    1. public void showKitSelector(Player player) {
    2.  
    3. kitselector = Bukkit.createInventory(null, 9*6, ChatColor.GOLD + "Select your kit!");

    ........
    and in the listener for clicking an item, i did (plugin is a variable of the Main.class) : plugin.KitSelector.showKitSelector(player);

    As I said, didnt work out for me. Thanks for your reply though.
     
  7. Offline

    ChipDev

    Erm. Don't tell me you did switch(e#getCurrentItem#getType())..
     
  8. Offline

    CraftCreeper6

    Remember to do:
    Code:java
    1. p#openInventory([inv]);
     
  9. Offline

    caderape

    Create a field public Inventory Selectkit;
    then create it in you onEnable, Selectkit = bukkit.createinventory(null, 54, name);

    And in your code, you forgot to set the item meta.
    sword.setitemmeta(smeta)

    and... You're checking two time the same thing with your e.getaction()
     
  10. Offline

    Unica

    Code:java
    1. public Inventory getCustomInventory(){
    2. Inventory inv = Bukkit.createInventory(null, 9, "lol");
    3. inv.setItem(0, someItemStack());
    4. return inv;
    5. }


    If you don't need the player object in the inventory, u could do this.
    But you could also just do
    Code:java
    1. public void openInv(Player p){
    2. Inventory inv = blabla;
    3. p.openInventory(inv); //Make sure this is the last line
    4. }
     
  11. Offline

    fireblast709

    Wiesel000 line 14 seems to be always evaluating to true (thus always returning)
    And the issue with that is...?
     
  12. Offline

    ChipDev

    If he copy Pasted the #'s.
     
  13. Offline

    fireblast709

    ChipDev ah like that. I thought you were criticising the enum switch itself :p
     
    ChipDev likes this.
Thread Status:
Not open for further replies.

Share This Page