Solved Help with Inventories

Discussion in 'Plugin Development' started by rbrick, Nov 25, 2013.

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

    rbrick

    So i am working on making a backpack plugin(i am planning to release it :D ) and i am having trouble with keeping the inventory on close and making it different for each player. I have everything working, with out any errors other than well it deleting the inventory on close. please help if possible!

    Main class:
    Show Spoiler
    Code:java
    1. package me.rbrick.backpack;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5.  
    6. public class Main extends JavaPlugin {
    7.  
    8. public void onEnable(){
    9. Bukkit.addRecipe(new Recipe().BackPackRecipe());
    10. Bukkit.getServer().getPluginManager().registerEvents(new BackPackListener(), this);
    11. }
    12.  
    13.  
    14. }
    15.  


    Recipe Class:
    Show Spoiler

    Code:java
    1. import java.util.List;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.inventory.ItemStack;
    5. import org.bukkit.inventory.ShapedRecipe;
    6. import org.bukkit.inventory.meta.ItemMeta;
    7.  
    8. public class Recipe extends Main {
    9.  
    10. public ItemStack backpack(){
    11. ItemStack backpack = new ItemStack(Material.CHEST);
    12. ItemMeta backpackmeta = backpack.getItemMeta();
    13. backpackmeta.setDisplayName("§b§lBackPack");
    14. List<String> backpacklore = new ArrayList<String>();
    15. backpacklore.add("§7Right Click to bring up a inventory for storing your stuff on the go!");
    16. backpackmeta.setLore(backpacklore);
    17. backpack.setItemMeta(backpackmeta);
    18. return backpack;
    19. }
    20.  
    21. public ShapedRecipe BackPackRecipe(){
    22. ShapedRecipe backpack = new ShapedRecipe(this.backpack());
    23. backpack.shape(new String[] {"LLL","L L","LLL" });
    24. backpack.setIngredient('L', Material.LEATHER);
    25. return backpack;
    26.  
    27. }
    28.  
    29. }



    Inventory/Listener class:
    Show Spoiler

    Code:java
    1. package me.rbrick.backpack;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.block.Action;
    7. import org.bukkit.event.player.PlayerInteractEvent;
    8. import org.bukkit.inventory.Inventory;
    9.  
    10. public class BackPackListener extends Main implements Listener {
    11.  
    12. @EventHandler
    13. public void onPlayerInteract(PlayerInteractEvent e){
    14.  
    15. if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
    16. if(e.getPlayer().getItemInHand().equals(new Recipe().backpack())){
    17. e.setCancelled(true);
    18. e.getPlayer().openInventory(this.BackPackInv());
    19.  
    20. }
    21. }
    22.  
    23. }
    24. public Inventory BackPackInv(){
    25. Inventory inv = Bukkit.createInventory(null, 9,"§b§lBackPack");
    26. return inv;
    27. }
    28. }
    29.  


    Figured id put my code in there. thanks again!
     
  2. Offline

    JPG2000

    rbrick Whats the exact issue?

    Also, check out my link for Inventory Menu's. Its a in-depth tutorial.

    And for now, could you describe the error?
     
  3. Offline

    rbrick

    Its not an error. i just do not know how to make virtual inventories keep there inventory on closing of said inventory(basically making it act like a placed chest/enderchest)
     
  4. Offline

    JPG2000

    rbrick Heres how I would do it (Methods for giving a pack, and creating):
    Code:java
    1. public static HashMap<String, Inventory> inv = new HashMap<String, Inventory>(); //HashMap of players with backpack
    2.  
    3. public static void givePack(Player player) { //Opens the inventory, or creates on if non existent
    4. if (inv.containsKey(player.getName())) { //Menu has it
    5. player.openInventory(inv.get(player.getName()));
    6.  
    7. } else { //Hashmap doesn't have player, create
    8. Inventory inventory = Bukkit.createInventory(null, 18, player.getName() + "'s backpack!"); //Create inventory
    9. inv.put(player.getName(), inv); //Put them in map
    10. }
    11.  
    12. public static Inventory getPack(Player player) { //Gets a players inventory (back pack)
    13. if (inv.containsKey(player.getName())) { //Playrers in map
    14. return inv.get(player.getName()); //Return inv
    15. } else { //Player doesn't have a pack
    16. givePack(player); //Runs the method above, will create a inventory, see the lines above
    17. }
    18. }
     
  5. Offline

    sgavster

    rbrick Make a HashMap<String, Inventory>, add them to it on join, then when you open the inventory do player.openInventory((Inventory) hasMapName.get(player.getName()));
     
  6. Offline

    JPG2000

    rbrick And how to use mine:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
    2. Player player = (Player) sender;
    3. if (alias.equalsIgnoreCase("backpack")) {
    4. givePack(player);
    5.  
    6. } else if (alias.equalsIgnoreCase("giveitempack")) {
    7. getPack(player).addItem(new ItemStack(Material.DIAMOND, 1));
    8. }
    9. }


    NOTE: My command isn't the best, because I didn't check for sender etc. it was an example

    ALSO: the 'givePack' and other methods are assumed to be in the same class as the command/where ver you user. If not, simply CLASS_THE_METHODS_IN.givePack("name"); etc

    sgavster Off topic, but don't store Player objects in list's: Store there name's instead

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  7. Offline

    sgavster

    JPG2000 Oops, should have done that, i'll edit my post(I usually do, I use ArrayLists more often)
     
  8. Offline

    JPG2000

    sgavster ArrayList's and list's are the same...
     
  9. Offline

    sgavster

    JPG2000 I know, I just wasn't thinking at the time! Everyone makes mistakes!
     
  10. Offline

    JPG2000

    sgavster Hahah, man, were so spamming and bumping this! :)

    Anyways, rbrick try my code examples, it works :p
     
    rbrick and sgavster like this.
  11. Offline

    rbrick

    to open it on right click of the backpack i would call the method in the interact listener right?
     
    JPG2000 likes this.
  12. Offline

    JPG2000

Thread Status:
Not open for further replies.

Share This Page