Opening a fake inventory

Discussion in 'Plugin Development' started by BrushPainter, Feb 21, 2014.

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

    BrushPainter

    Hey guys, I made a inventory called "myInventory".

    Here it is:

    Code:java
    1. public class myInventory {
    2.  
    3. public final Inventory myInventory = Bukkit.createInventory(null, 9, "Choose a reward!");
    4.  
    5.  
    6.  
    7. public void Head() { {
    8.  
    9. {
    10. myInventory.setItem(1, new ItemStack(Material.GOLDEN_APPLE, 1, (short) 1));
    11.  
    12. ItemStack ds = new ItemStack(Material.DIAMOND_SWORD, 1);
    13. ds.addEnchantment(Enchantment.DAMAGE_ALL, 5);
    14. myInventory.setItem(2,ds);
    15.  
    16. myInventory.setItem(3, new ItemStack(Material.DIAMOND_BLOCK, 8));
    17.  
    18. myInventory.setItem(4, new ItemStack(Material.MONSTER_EGG, 1, (short)50));
    19.  
    20. myInventory.setItem(5, new ItemStack(Material.TNT, 64));
    21.  
    22. myInventory.setItem(6, new ItemStack(Material.MOB_SPAWNER, 1,(short)91));
    23.  
    24. ItemStack p4book = new ItemStack(Material.ENCHANTED_BOOK, 1);
    25. p4book.addEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 4);
    26. myInventory.setItem(7,p4book);
    27.  
    28. myInventory.setItem(8, new ItemStack(Material.SKULL, 3, (short)1));
    29.  
    30. myInventory.setItem(9, new ItemStack(Material.NETHER_STAR, 1));
    31.  
    32. }
    33. }
    34. }



    It's working %100 fine, but I added a command "orei" and I want it to open the inventory when the command is sent.

    Here is my attempt:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2.  
    3. if(cmd.getName().equalsIgnoreCase("orei")){
    4. player.openInventory(myInventory);
    5.  
    6. }
    7. }
    8.  


    Please help?
     
  2. Offline

    iPoke111

    You could use a method to create an inventory rather than a class, I think.
    For example, you could make a method something like createInventory() and create the inventory in there, and return the inventory. Then you could use player.openInventory(createInventory());
     
  3. Offline

    legoman519

    Try renaming the inventory because the class name and inventory name are the same.
     
  4. Offline

    BrushPainter

    iPoke111

    Hmm, I'm not really getting what you're saying about the first part, I under stand how you explained to call it up after it's made, but when you said "For example, you could make a method something like createInventory() and create the inventory in there, and return the inventory." I got lost, please give me an example?

    Thanks a lot. :D
     
  5. Offline

    iPoke111

    BrushPainter
    Heh. I mean like, either inside your class or in another one, make a method like:
    Code:java
    1. //method
    2. public static Inventory createInventory() {
    3. Inventory inv = Bukkit.getServer().createInventory(null, 9, "bleh");
    4. //stuff
    5. return inv;
    6. }
    7. //calling it
    8. player.openInventory(ClassName.createInventory());
     
  6. Offline

    BrushPainter

  7. Offline

    legoman519

    Ok I am not trying to be mean but dude really... you need a Player player = (player) sender;
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. Player player = (player) sender;
    3. if(cmd.getName().equalsIgnoreCase("orei")){
    4. player.openInventory(myInventory);
     
Thread Status:
Not open for further replies.

Share This Page