Solved GUI Inventory Help

Discussion in 'Plugin Development' started by jrobi230, Dec 23, 2013.

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

    jrobi230

    Hey, Sorry if this sounds like a noob question but im only learning java so yeah.... My problem is that i have a custom inventory defined in a Listener class but i want the inventory to be visible in another Listener class. Im making a gui plugin where a person right clicks a peice of paper and it open the Inventory But my inventory and PlayerInteractEvent are in seperate classes and i want to make it though so that when the person right clicks the paper it open the menu.

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4. if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK);
    5. if (player.getItemInHand().getType() == Material.PAPER);
    6. player.openInventory(inv);
     
  2. Offline

    mrkirby153

    jrobi230

    Use
    Code:java
    1. getServer().getPluginManager().registerEvents(new YourClassName(), this);
     
  3. Offline

    KaiPol

    Wouldn't it be this.<YourClassName>, this
    Or does it not matter?
     
  4. Offline

    mrkirby153

    KaiPol

    You need to instance your class. So it would be new yourClassName. The beauty of OOP :)
     
  5. Offline

    Ranil

    Unless you do that before hand with a public <YourClassName> class = new <YourClassName>();
     
  6. Offline

    jrobi230

    mrkirby153

    When i use that i get an error saying "The method getServer() is undefined for the type <MYCLASSNAME>. And where would i put it

    mrkirby153

    Just with the same plugin when players put items in their enderchest and the server closes, the enderchest wont save the inventory. Do you need to use a hashmap or something?
     
  7. Offline

    mrkirby153

    jrobi230

    getServer() is extended through JavaPlugin. You may want to either pass an instance of your plugin in the constructor and instead use plugin.getServer(). I thought you were working in your main plugin class. As for the inventory not saving, I would suggest you listen for an onInventoryClose (probably not the correct name) and write the data of the inventory to a hashmap<String, ItemStack[]> for "caching" and when the server stops you can write all the data to a file like enderchests.yml and load from it when the server starts up. The only time stuff won't be written to the file if the server crashes or something
     
  8. Offline

    jrobi230

    mrkirby153

    how would i write to the hashmap and load it when the server starts up i have made a hash map and an InventoryCloseEvent.
     
  9. Offline

    mrkirby153

    jrobi230

    Code:java
    1. for(String s : HashMap.keySet()){
    2. getConfig().set(s+".value", HashMap.get(s));
    3. }
    4. saveConfig()
    5.  
    to save to a config
    Code:java
    1. for(String s : getConfig().getKeys(false)){
    2. HashMap.put(s, config.getString(s+".value))
    3. }

    To load. Please don't copy-pasete this in as your attributes may be different. Also make sure what you are trying to save is serializable. Sorry for bad formatting. On an ipad
     
  10. Offline

    jrobi230

    mrkirby153

    Thanks for this i was just wondering do i put this in the onEnable() or in the InventoryCloseEvent?
     
  11. Offline

    mrkirby153

    jrobi230

    Define the hashmap at the top of your main class file and save it to a file on InventoryCloseEvent. Make sure you're saving the correct inventory, not just any inventory that's closed
     
  12. Offline

    jrobi230

    mrkirby153
    Im getting errors with your coding in your previous comment and how would i go about saving an actual inventory would i go enderchest.put(Player, einv); or enderchest.put(null, einv); My enderchest inventory is called einv to btw.

    And I'm really sorry for asking you so many questions but I'm very new to java and I'm glad your'e not one of those people to pee off and go learn java. Again thanks for persevering with me!
     
  13. Offline

    Garris0n

    Use the player name to save/get.
     
  14. Offline

    jrobi230

    Garris0n

    I only want to save the enderchest after the player has exited it not any other inventories the player has.
     
  15. Offline

    Garris0n

    Then only save it in the Map when they close the inventory. Also, note that it won't save through reloads/restarts.
     
  16. Offline

    jrobi230

    Garris0n How would i do that because i have my HashMap and my InvetoryCloseEvent. Also how would i listen for a specific inventory closing?
     
  17. Offline

    JRL1004

    jrobi230 Try InventoryCloseEvent and add a check to see if the inventory is in the HashMap
     
  18. Offline

    xTigerRebornx

    jrobi230 Check the name of the inventory that is being closed......Ninjad?
     
  19. Offline

    jrobi230

    JRL1004
    How would i do that would i go enderchest.putAll(einv); or something along those lines?
     
  20. Offline

    Garris0n

    Do some research on HashMaps, that doesn't make any sense.
     
  21. Offline

    Goblom

    (Total Self Promotion)
    http://dev.bukkit.org/bukkit-plugins/gui/

    Code:java
    1. GuiAPI api = new GuiAPI();
    2.  
    3. api.tempGUI(
    4. "My Special Title",
    5. 1,
    6. new ItemStack[] {
    7. new ItemStack(Material.PAPER),
    8. new ItemStack(Material.STONE),
    9. new ItemStack(Material.BUCKET)
    10. },
    11. new String[] {
    12. "doPaper",
    13. "doStone",
    14. "doBucket"
    15. }
    16. ).open(player);


    (I hope this is what you are asking for)

    Edit: Supid Formatting
     
  22. Offline

    jrobi230

    Don't worry i fixed it myself guys THANKS for ALL YOUR HELP

    But special thanks to:
    Garris0n
    mrkirby153
     
Thread Status:
Not open for further replies.

Share This Page