Inventories + Items Overflowing

Discussion in 'Plugin Development' started by BurnerDiamond, Jan 3, 2015.

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

    BurnerDiamond

    I'm making a kitpvp plugin which requires lots of inventories and item lores.

    Is there anyway I can put all of these in an Inventories class so my plugin looks cleaner and easier to use?
     
  2. Offline

    tytwining

  3. Offline

    ColaCraft

    Just create another class for it and use a constructor to reference your main class when you need to. Plugins can certainly have multiple classes (this is the one I am working on now)
    [​IMG]

    To use a constructor, in your main class add this:

    Code:
    private static <NameOfMainClass> plugin;
    
    public void onEnable(){
         plugin = this;
    }
    
    public void onDisable(){
         plugin = null;
    }
    
    public static Main getInstance(){
          
        if(plugin == null){
            plugin = new <NameOfMainClass>();
        }
          
        return plugin;
    }
    Then, when you are referencing anything in your main class use

    Code:
    <NameOfMainClass>.getInstance().whatever
     
  4. Offline

    BurnerDiamond

    I tried this tutorial but it doesn't work in my case:

    http://bukkit.org/threads/custom-chest-inventory.330869/#post-2952015

    I'm just trying to have an Inventory class for it. I know you can use multiple classes but can't I just store all my inventories in an Inventory class?

    Code:
    public final Inventory chest =  Bukkit.createInventory(null, 27, "Kit Selector");    
    Code:
    public class Inventories {
    public Inventories(Main plugin) {
    @ColaCraft

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

    ColaCraft

    Here is how I do it in my plugin, feel free to use it.

    Code:
        public void addItem(Material m, Inventory i, String name, String lore, Integer price){
           
            ItemStack a = new ItemStack(m, 1);
           
            ItemMeta b = a.getItemMeta();
           
            List<String> c = new ArrayList<String>();
           
            c.add(lore);
            c.add(ChatColor.AQUA + "" + price + " coins");
           
            b.setDisplayName(name);
           
            b.setLore(c);
           
            a.setItemMeta(b);
           
            i.addItem(a);
           
           
           
        }
       
        public void openShop(Player p){
           
            Inventory shop = Main.getInstance().getServer().createInventory(null, 54, ChatColor.DARK_RED + "Mortal Shop");
           
            addItem(Material.GLASS, shop, "Glass", "Durability: 2", 500);
            addItem(Material.DIRT, shop, "Dirt", "Durability: 3", 650);
            addItem(Material.LOG, shop, "Log", "Durability: 5", 1000);
           
            p.openInventory(shop);
           
        }
     
  6. Offline

    BurnerDiamond

    I feel there is an easier way here is my code

    Code:
    package me.bd.kitpvp;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class NetherStarOpen implements Listener {
        
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
           
            ItemStack tank = new ItemStack(Material.DIAMOND_CHESTPLATE);
            ItemMeta tankim = tank.getItemMeta();
            tankim.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "TANK");
            ArrayList<String> tanklore = new ArrayList<String>();
            tanklore.add(ChatColor.YELLOW + "Take multiple hits as you bash ");
            tanklore.add(ChatColor.YELLOW + "your opposing players without ");
            tanklore.add(ChatColor.YELLOW + "taking a heart of damage");
            tankim.setLore(tanklore);
            tank.setItemMeta(tankim);
           
            ItemStack striker = new ItemStack(Material.DIAMOND_SWORD);
            ItemMeta strikerim = striker.getItemMeta();
            strikerim.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "STRIKER");
            ArrayList<String> strikerlore = new ArrayList<String>();
            strikerlore.add(ChatColor.YELLOW + "Strike fear with your sword ");
            strikerlore.add(ChatColor.YELLOW + "but be careful since you're ");
            strikerlore.add(ChatColor.YELLOW + "not as well armoAQUA");
            strikerim.setLore(strikerlore);
            striker.setItemMeta(strikerim);
           
            ItemStack archer = new ItemStack(Material.BOW);
            ItemMeta archerim = archer.getItemMeta();
            archerim.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "ARCHER");
            ArrayList<String> archerlore = new ArrayList<String>();
            archerlore.add(ChatColor.YELLOW + "You may not have the best ");
            archerlore.add(ChatColor.YELLOW + "melee combat but ranged will ");
            archerlore.add(ChatColor.YELLOW + "easily keep you safe and make ");
            archerlore.add(ChatColor.YELLOW + "it easier to kill");
            archerim.setLore(archerlore);
            archer.setItemMeta(archerim);
           
            ItemStack jumper = new ItemStack(Material.PISTON_BASE);
            ItemMeta jumperim = jumper.getItemMeta();
            jumperim.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "JUMPER");
            ArrayList<String> jumperlore = new ArrayList<String>();
            jumperlore.add(ChatColor.YELLOW + "They say that a rabbit's foot ");
            jumperlore.add(ChatColor.YELLOW + "brings good luck, you jump like ");
            jumperlore.add(ChatColor.YELLOW + "a rabbit and you're only using ");
            jumperlore.add(ChatColor.YELLOW + "gold, any luckier than that?");
            jumperim.setLore(jumperlore);
            jumper.setItemMeta(jumperim);
           
            ItemStack runner = new ItemStack(Material.SUGAR);
            ItemMeta runnerim = runner.getItemMeta();
            runnerim.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "RUNNER");
            ArrayList<String> runnerlore = new ArrayList<String>();
            runnerlore.add(ChatColor.YELLOW + "Who needs good armor? Use your speed ");
            runnerlore.add(ChatColor.YELLOW + "to quickly run and attack. It's a ");
            runnerlore.add(ChatColor.YELLOW + "great tactic since you have a  ");
            runnerlore.add(ChatColor.YELLOW + "great sword");
            runnerim.setLore(runnerlore);
            runner.setItemMeta(runnerim);
           
            Inventory chest = Bukkit.createInventory(null, 27, "Kit Selector");                  
            chest.setItem(4, new ItemStack(tank));
            chest.setItem(1, new ItemStack(striker));
            chest.setItem(7, new ItemStack(archer));
            chest.setItem(19, new ItemStack(jumper));
            chest.setItem(22, new ItemStack(runner));
           
            Player player = event.getPlayer();
           
                if ((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
                  if (player.getItemInHand().getItemMeta().getDisplayName() != null) {
                    if (player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.YELLOW + "" + ChatColor.BOLD + "KIT SELECTOR")) {
                        player.openInventory(chest);
               
               
                    
                       
                       
                                    
                }
        }
                }
        }
       
    
    }
    As you see I have an overflow of it chest and items, I want to shift it all in a seperate class so it would only show this

    Code:
      Player player = event.getPlayer();
          
                if ((event.getAction() == Action.RIGHT_CLICK_AIR) || (event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
                  if (player.getItemInHand().getItemMeta().getDisplayName() != null) {
                    if (player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.YELLOW + "" + ChatColor.BOLD + "KIT SELECTOR")) {
                        player.openInventory(chest);
    And I want a seperate class only showing this

    Code:
    ItemStack tank = new ItemStack(Material.DIAMOND_CHESTPLATE);
            ItemMeta tankim = tank.getItemMeta();
            tankim.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "TANK");
            ArrayList<String> tanklore = new ArrayList<String>();
            tanklore.add(ChatColor.YELLOW + "Take multiple hits as you bash ");
            tanklore.add(ChatColor.YELLOW + "your opposing players without ");
            tanklore.add(ChatColor.YELLOW + "taking a heart of damage");
            tankim.setLore(tanklore);
            tank.setItemMeta(tankim);
          
            ItemStack striker = new ItemStack(Material.DIAMOND_SWORD);
            ItemMeta strikerim = striker.getItemMeta();
            strikerim.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "STRIKER");
            ArrayList<String> strikerlore = new ArrayList<String>();
            strikerlore.add(ChatColor.YELLOW + "Strike fear with your sword ");
            strikerlore.add(ChatColor.YELLOW + "but be careful since you're ");
            strikerlore.add(ChatColor.YELLOW + "not as well armoAQUA");
            strikerim.setLore(strikerlore);
            striker.setItemMeta(strikerim);
          
            ItemStack archer = new ItemStack(Material.BOW);
            ItemMeta archerim = archer.getItemMeta();
            archerim.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "ARCHER");
            ArrayList<String> archerlore = new ArrayList<String>();
            archerlore.add(ChatColor.YELLOW + "You may not have the best ");
            archerlore.add(ChatColor.YELLOW + "melee combat but ranged will ");
            archerlore.add(ChatColor.YELLOW + "easily keep you safe and make ");
            archerlore.add(ChatColor.YELLOW + "it easier to kill");
            archerim.setLore(archerlore);
            archer.setItemMeta(archerim);
          
            ItemStack jumper = new ItemStack(Material.PISTON_BASE);
            ItemMeta jumperim = jumper.getItemMeta();
            jumperim.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "JUMPER");
            ArrayList<String> jumperlore = new ArrayList<String>();
            jumperlore.add(ChatColor.YELLOW + "They say that a rabbit's foot ");
            jumperlore.add(ChatColor.YELLOW + "brings good luck, you jump like ");
            jumperlore.add(ChatColor.YELLOW + "a rabbit and you're only using ");
            jumperlore.add(ChatColor.YELLOW + "gold, any luckier than that?");
            jumperim.setLore(jumperlore);
            jumper.setItemMeta(jumperim);
          
            ItemStack runner = new ItemStack(Material.SUGAR);
            ItemMeta runnerim = runner.getItemMeta();
            runnerim.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "RUNNER");
            ArrayList<String> runnerlore = new ArrayList<String>();
            runnerlore.add(ChatColor.YELLOW + "Who needs good armor? Use your speed ");
            runnerlore.add(ChatColor.YELLOW + "to quickly run and attack. It's a ");
            runnerlore.add(ChatColor.YELLOW + "great tactic since you have a  ");
            runnerlore.add(ChatColor.YELLOW + "great sword");
            runnerim.setLore(runnerlore);
            runner.setItemMeta(runnerim);
          
            Inventory chest = Bukkit.createInventory(null, 27, "Kit Selector");                 
            chest.setItem(4, new ItemStack(tank));
            chest.setItem(1, new ItemStack(striker));
            chest.setItem(7, new ItemStack(archer));
            chest.setItem(19, new ItemStack(jumper));
            chest.setItem(22, new ItemStack(runner));
    @ColaCraft

    I basically just want a class to store all items and inventories. Only!

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

    ColaCraft

    @BurnerDiamond

    Yeah, just in the inventories class, modify the code I posted for your needs. Then when you want to open it use <InventoryClass>.openShop(p);

    You can modify what I have to add more Lore or whatever too, so that way it would look cleaner. I have that in an API class right now
     
  8. Offline

    BurnerDiamond

    @ColaCraft

    Code:
    private static <NameOfMainClass> plugin;
    public void onEnable(){
         plugin = this;
    }
    public void onDisable(){
         plugin = null;
    }
    public static Main getInstance(){
        
        if(plugin == null){
            plugin = new <NameOfMainClass>();
        }
        
        return plugin;
    }
    Where exactly would I set my inventories here?
     
  9. Offline

    ColaCraft

    Rename the class you are storing the inveories in to CustomInv and then before onEnable put

    CustomInv c = new CustomInv();

    Then to reference the open shop you can do c.openShop(p);
     
  10. Offline

    BurnerDiamond

    @ColaCraft I have a problem with the first plugin = this.

    It gives me a problem where it tell me to turn it into this

    Code:
    package me.bd.kitpvp;
    
    public class Inventories {
        private static Inventories plugin;
       
        public void onEnable(){
             plugin = this;
        }
        public void onDisable(){
             plugin = null;
        }
        public static Main getInstance(){
          
            if(plugin == null){
                plugin = new Main();
            }
          
            return plugin;
        }
    
    }
    
    Second would it be like this?

    Code:
    
    
           
    private static <NameOfMainClass> plugin;
    public void onEnable(){
         plugin = this;
    
    ItemStack striker = new ItemStack(Material.DIAMOND_SWORD);
            ItemMeta strikerim = striker.getItemMeta();
            strikerim.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "STRIKER");
            ArrayList<String> strikerlore = new ArrayList<String>();
            strikerlore.add(ChatColor.YELLOW + "Strike fear with your sword ");
            strikerlore.add(ChatColor.YELLOW + "but be careful since you're ");
            strikerlore.add(ChatColor.YELLOW + "not as well armoAQUA");
            strikerim.setLore(strikerlore);
            striker.setItemMeta(strikerim);
    }
    public void onDisable(){
         plugin = null;
    }
    public static Main getInstance(){
    
        if(plugin == null){
            plugin = new <NameOfMainClass>();
        }
    
        return plugin;
    }
    
    What would I put in my main and what would I put on my constructor

    Anybody?

    I've tried countless things without success but nothing seems to be working...

    I've tried everything I could think of and I've researched as much as possible I know there is a simple solution but I can't find it

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
  11. Offline

    BurnerDiamond

    Okay I'm a complete idiot I finally understood it but one more question. How would I do this for lore items?
     
  12. Offline

    ColaCraft

    Just save the Lore in an ArrayList
     
  13. Offline

    BurnerDiamond

    @ColaCraft could I have an example and possibly a link to a tutorial?

    Could you use my lore class and make an example with an Inventory class and a Main class? I just need an example using my things

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 31, 2016
Thread Status:
Not open for further replies.

Share This Page