Solved Setup Itemstacks In another class??

Discussion in 'Spigot Plugin Development' started by imfeared, May 30, 2017.

  1. So, I was wondering If I could setup my items in another class or add an instance in my main class for it to work and actually add the item to the inventory.

    If there are any bugs or errors that I do not see, please tell me...


    MAIN CLASS -----------------------------------------------------------

    Code (Text):

    public class Main extends JavaPlugin implements Listener {

        private static Plugin plugin;

        public void onEnable() {

            registerEvents(this, new ListenerClass());
            plugin = this;
            getCommand("k").setExecutor(new CommandClass());

        }

        public void onDisable() {
            plugin = null;
        }


        public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
            for (Listener listener : listeners) {
                Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
            }
        }

        public static Plugin getPlugin() {
            return plugin;
        }
     
    COMMAND CLASS -----------------------------------------------------------

    Code (Text):

    public class CommandClass extends ItemStacks implements CommandExecutor {

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] arg) {
            Player p = (Player) sender;

            if (label.equalsIgnoreCase("k")) {
                this.showInventory(p);
            }

            return true;
        }
     

    LISTENER CLASS -----------------------------------------------------------

    Code (Text):

    public class ListenerClass implements Listener {



        @EventHandler
        public void onClick(InventoryClickEvent e) {

            if (e.getInventory().getTitle().equals(ChatColor.RED + "Test Inv") && e.getSlot() == 0) {
                e.setCancelled(true);
                Player p = (Player) e.getWhoClicked();
                p.playSound(p.getLocation(), Sound.ENTITY_ZOMBIE_VILLAGER_DEATH, 25, 4);
            }

        }

    }
     

    ITEMSTACK CLASS -----------------------------------------------------------

    Code (Text):

    public class ItemStacks {

        ItemStack UnUsed;

        private void setupItemss() {

            this.UnUsed = makeUnUsed();

        }

        public ItemStack makeUnUsed() {
            UnUsed = new ItemStack(Material.MINECART);
            ItemMeta un = UnUsed.getItemMeta();
            un.setDisplayName(ChatColor.RED + "LEL");
            UnUsed.setItemMeta(un);
            return UnUsed;
        }

        public void showInventory(Player p) {
            Inventory inv = Bukkit.getServer().createInventory(null, 9, ChatColor.RED + "Test Inv");
            inv.setItem(0, UnUsed);
            p.openInventory(inv);
        }
     
     
  2. Code (Text):

    import java.util.ArrayList;

    public class ItemStacks {

        private ItemStack unUsed;

        public ItemStack makeUnUsed() {
            unUsed = new ItemStack(Material.MINECART);
            ItemMeta un = unUsed.getItemMeta();
            un.setDisplayName(ChatColor.RED + "LEL");
            unUsed.setItemMeta(un);
            return unUsed;
        }

        public void showInventory(Player p) {
            Inventory inv = Bukkit.getServer().createInventory(null, 9, ChatColor.RED + "Test Inv");
            ItemStack[] toAdd = new ItemStack[1];
            toAdd[0] = makeUnUsed();
            inv.addItem(toAdd)
            p.openInventory(inv);
        }
     
    Your main issue must have been that you were never actually calling your makeItemss method, this fix to class removes that as it was not needed, I recommend using a constructor instead of a private void method.

    The Constructor way would be like this:

    Code (Text):

    import java.util.ArrayList;

    public class ItemStacks {

        private ItemStack unUsed;

        public ItemStacks(){
            makeUnUsed();
        }

        public ItemStack makeUnUsed() {
            unUsed = new ItemStack(Material.MINECART);
            ItemMeta un = unUsed.getItemMeta();
            un.setDisplayName(ChatColor.RED + "LEL");
            unUsed.setItemMeta(un);
            return unUsed;
        }

        public void showInventory(Player p) {
            Inventory inv = Bukkit.getServer().createInventory(null, 9, ChatColor.RED + "Test Inv");
            ItemStack[] toAdd = new ItemStack[1];
            toAdd[0] = makeUnUsed();
            inv.addItem(toAdd)
            p.openInventory(inv);
        }
     
    This way it makes it so whenever any class (like your command executor that extends this) is called on when making a new ItemStacks(); will immediately create the unUsed ItemStack
     
    • Like Like x 5


  3. That worked!!! THANKS!! :D
     
    • Like Like x 1