Trouble Creating An Inventory

Discussion in 'Plugin Development' started by Rocketboy901, Oct 8, 2013.

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

    Rocketboy901

    I am attempting to make a plugin that opens a virtual inventory when you right click a cauldron.
    What am I doing wrong? :)

    Here is my code:

    Code:java
    1. package me.Rocketboy901.Trashcans;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.*;
    5. import org.bukkit.event.block.Action;
    6. import org.bukkit.event.player.PlayerInteractEvent;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Material;
    11.  
    12.  
    13. public class Main extends JavaPlugin implements Listener {
    14.  
    15. @Override
    16. public void onEnable() {
    17. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    18. }
    19.  
    20. @EventHandler(priority = EventPriority.HIGH)
    21. public void onInteract(PlayerInteractEvent event) {
    22. Player player = event.getPlayer();
    23.  
    24. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    25. if (event.getClickedBlock().equals(Material.CAULDRON)){
    26. Bukkit.createInventory(player, 18, ChatColor.RED + "" + ChatColor.BOLD + "Trashcan");
    27.  
    28. }
    29.  
    30. }
    31. }
    32.  
    33. }
    34.  
     
  2. Rocketboy901
    have the inventory be a variable, and have the player open that inventory variable.
     
  3. Offline

    Rocketboy901

    Still doesn't work! :( EDIT: It works if I remove the if (getClickedBlock().equals(Material.Caldren) statment so it has something to do with that...
     
  4. Offline

    GaaTavares

    I think you should define the type of the inventory, InventoryType.CHEST, or whatever type you want.
     
  5. Offline

    Rocketboy901

    It opens an inventory if I remove the if (getClickedBlock().equals(Material.<block>); What should I do?
     
  6. Offline

    Chrono7

    Here's a hint: You are comparing a Block to a Material... you should be comparing a Material to a Material.
     
  7. Rocketboy901

    What I told you will work for the Inventory. Just make sure the rest of your code is all good.
     
  8. Offline

    _Skyden_

    Create the inventory on your onEnable method, then in your event handler just get the player to open it. At the moment you are just creating the inventory when the block is right clicked and not doing anything else with it.

    Hope I helped.
     
  9. Offline

    JPG2000

    Rocketboy901 I would create the inventory, globally, and staticly like so:
    Code:java
    1. public static Inventory customInventory = Bukkit.createInventory(player, 18, ChatColor.RED + "" + ChatColor.BOLD + "Trashcan");


    Then, you can do (from the Main class)
    Code:java
    1. player.openInventory(customInventory);


    And from a diffirent class:
    Code:java
    1. player.openInventory(Main.customInventory);
     
  10. Offline

    PatrickH123

    Here you go:

    Code:java
    1. package me.Rocketboy901.Trashcans;
    2.  
    3. import org.bukkit.block.Block;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.*;
    6. import org.bukkit.event.block.Action;
    7. import org.bukkit.event.player.PlayerInteractEvent;
    8. import org.bukkit.inventory.Inventory;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.ChatColor;
    12. import org.bukkit.Material;
    13.  
    14. public class Trashcans extends JavaPlugin implements Listener {
    15. public static Inventory trashcan;
    16. @Override
    17. public void onEnable() {
    18. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    19. }
    20.  
    21. @EventHandler(priority = EventPriority.HIGH)
    22. public void onInteract(PlayerInteractEvent event) {
    23. Player player = event.getPlayer();
    24.  
    25. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    26. Block block = event.getClickedBlock();
    27. if(block.getType() == Material.CAULDRON){
    28. trashcan = Bukkit.createInventory(player, 18, ChatColor.RED + "" + ChatColor.BOLD + "Trashcan");
    29. player.openInventory(Trashcans.trashcan);
    30.  
    31. }
    32.  
    33. }
    34. }
    35.  
    36. }
     
  11. Offline

    Rocketboy901

    Guys, I fixed this myself a while back! Thanks though.
     
Thread Status:
Not open for further replies.

Share This Page