Solved Make an inventory once

Discussion in 'Spigot Plugin Development' started by kinukin, Jun 5, 2017.

  1. Code (Text):
    Inventory backpack = Bukkit.createInventory(p, 54,ChatColor.BOLD + "" + ChatColor.BLACK + "Backpack");
    How can I create an inventory once so I don't have to create a new one each time?

    Edit:
    When you create an inventory it's only created once. How can I stop it from keep creating new inventories and load the same inventory?

    I know how to store inventory contents but how can I store the inventory?



    Thank you
     
    #1 kinukin, Jun 5, 2017
    Last edited: Jun 5, 2017
  2. @kinukin could you supply more code so everyone can see what the situation is that you need just one inventory?

    The way you're creating it above is how you can create an inventory. However, I think what you're curious about is function scope.

    Example:
    *Edited*
    Code (Java):
    // This is a global variable named 'x' with a value of '1'
    private int x = 1;

    public void foo() {
        // This will print out the global variable 'x' with a value of '1'
        System.out.println(x);
        // If you change the value of 'x', which is defined outside of foo(), then it will persist outside of the function because it is a global variable.
        x = 2;
    }
    // This will print out the number '2'
    System.out.println(x);
     
     
    #2 ForsakenRealmz, Jun 5, 2017
    Last edited: Jun 5, 2017

  3. When you create an inventory it's only created once. How can I stop it from keep creating new inventories and load the same inventory?

    I know how to store inventory contents but how can I store the inventory?
     
  4. Just make your inventory variable not within a function, then you can run a function to create and set it up then just access the variable in the future.
    Example:
    Code (Text):
    Inventory inv = Bukkit.createInventory(null,9,"Exampe Inv");

    private void setupInv() {
    //put your code here to modify it
    }

    private void openInv(Player p) {
    p.openInventory(inv);
    }
     
  5. If you learnt Java this wouldn't be an issue lol


    Sent from my iPhone using Tapatalk
     
  6. Code (Text):
    public Inventory getInventory() {
    System.out.print("Java?");
    return Bukkit.createInventory().......
    }

    //or

    //global variable for using the same object
    Inventory inv = bukkit.createInventory().....
     
  7. Code (Text):
    public Inventory shopInventory() {
    //create your inventory here
    Inventory shopInv = Bukkit.createInventory(null, 9, "Shop Inv");
    shopInv.addItem(item);
    return shopInv;
    Assuming you have a constructor and such, now you can do in another class
    Code (Text):
    @EventHandler
    public void InventoryClick(InventoryClickEvent e) {
    if(e.getInventory().getTitle().equals(Main.shopInventory().getTitle())) {
    //CODE
    Now, I can't think of a method on the top of my head for creating inventories and using this method like:
    Code (Text):
    public Inventory createInventory(Integer size, String title) {
    Inventory inv = Bukkit.createInventory(null, size, title);
    return inv;
    }
     
  8. You could even create your own custom inventory class if it's complex enough. Pass variables through the constructor and do with it what you wish.


    Sent from my iPhone using Tapatalk