Inventory Help Needed!

Discussion in 'Plugin Development' started by DanilaFe, Mar 30, 2014.

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

    DanilaFe

    Hi!
    I was wondering if it was possible to write a plugin that would open an Inventory when an item is right clicked. I only need help with one thing though - can I open an inventory that does not belong anywhere in the world, and by that i mean there is no chest or hopper containing it, it's just there...
    Thanks in advance!
     
  2. Offline

    Mysticate

    Yes it is.
     
  3. Offline

    JBoss925

    Yeah, pretty simple as well. Declare the new inventory, set a type if you so choose, set params and bam there you are.
     
    DanilaFe likes this.
  4. Offline

    xAstraah

    Here's how to code that like pretty much exactly:

    Code:java
    1. package io.github.xAstraah;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.inventory.InventoryClickEvent;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.inventory.Inventory;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class BukkitTutorials extends JavaPlugin implements Listener
    17. {
    18.  
    19. public static Inventory inventory = Bukkit.createInventory(null, 9, "Inventory");
    20.  
    21. public void onEnable()
    22. {
    23. this.getServer().getPluginManager().registerEvents(this, this);
    24. }
    25.  
    26. @EventHandler
    27. public void ItemClick(PlayerInteractEvent e)
    28. {
    29.  
    30. Player p = e.getPlayer();
    31.  
    32. if(p.getInventory().getItemInHand().getType() == Material.COMPASS)
    33. {
    34. e.getAction();
    35. if(Action.RIGHT_CLICK_BLOCK != null)
    36. {
    37. inventory.setItem(4, new ItemStack(Material.DIRT, 1));
    38. p.openInventory(inventory);
    39. }
    40. else if(Action.RIGHT_CLICK_AIR != null)
    41. {
    42. inventory.setItem(4, new ItemStack(Material.DIRT, 1));
    43. p.openInventory(inventory);
    44. }
    45. }
    46.  
    47. }
    48.  
    49. @EventHandler
    50. public void InventoryClick(InventoryClickEvent e)
    51. {
    52.  
    53. Player p = (Player) e.getWhoClicked();
    54. Inventory checkinventory = e.getInventory();
    55. ItemStack clicked = e.getCurrentItem();
    56.  
    57. if(checkinventory.getName().equalsIgnoreCase("Inventory"))
    58. {
    59. e.setCancelled(true);
    60.  
    61. if(clicked.getType() == Material.DIRT)
    62. {
    63. p.sendMessage(ChatColor.GREEN + "You clicked on the dirt!");
    64. }
    65.  
    66. }
    67.  
    68. }
    69.  
    70. }
    71.  


    Screenies:
    http://prntscr.com/35ps6d - When compass clicked
    http://prntscr.com/35ps92 - When dirt in the Bukkit Inventory is clicked
     
    DanilaFe likes this.
  5. Offline

    MooshViolet

    xAstraah You shouldn't really be giving out code like that, it won't help the person learn anything except to copy and paste
     
    DanilaFe likes this.
  6. Offline

    xAstraah

  7. Offline

    DanilaFe

    I don't copy paste code, just so you know. I read through it, and if I don't understand I ask questions. I never actually asked for code, did I?

    Sorry for the VERY late reply, I kinda tried a whole bunch of things and looked through most of the API and actually did it on my own. Thanks for your help guys!
     
  8. Offline

    MooshViolet

    Thats great you read through it; most people don't. I wasn't saying you asked for code, I was simply just saying its not good to give out free code because its not an adequate learning experience if you know what I mean
     
    NerdsWBNerds likes this.
  9. Offline

    xAstraah

Thread Status:
Not open for further replies.

Share This Page