Solved Help with constructors

Discussion in 'Spigot Plugin Development' started by AppleMonJeans, Jan 31, 2019.

  1. I have always kind of sucked with constructors, I have looked up tutorials upon tutorials and read multiple articles. I need some help with a project I have been working on. What I am trying to do is have a method that opens a GUI in a class and in a separate class I would have a command that than calls that method, but I cant figure out how to do the constructors. I have thought about resorting too making my method static but I do not want to start bad habits.

    Code (Text):
    package me.wrektyourday.battlepass.guis;

    import me.wrektyourday.battlepass.Main;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import java.util.ArrayList;
    import java.util.List;

    public class MainGui {

        public MainGui() {

        Main plugin;
        public MainGui(Main instance) {
            plugin = instance;

        public void openMainGui(Player p) {

            Integer invSize = plugin.getConfig().getInt("gui-size");
            String invName = plugin.stringFormater(plugin.getConfig().getString("gui-name"));

            Inventory mainGui = Bukkit.getServer().createInventory(null, invSize, invName);

            String challengesItem = plugin.getConfig().getString("main-gui.challenges.item");
            Short challengesId = Short.valueOf(plugin.getConfig().getString(""));
            ItemStack challenges = new ItemStack(Material.getMaterial(challengesItem), 1 , challengesId);
            ItemMeta challengesMeta = challenges.getItemMeta();
            String challengesName = plugin.stringFormater(plugin.getConfig().getString(""));
            List<String> coloredLore = new ArrayList<>();
            for (String s : plugin.getConfig().getStringList("main-gui.challenges.lore")) {
                s = plugin.stringFormater(s);

            String statusItem = plugin.getConfig().getString("main-gui.status.item");
            Short statusId = Short.valueOf(plugin.getConfig().getString(""));
            ItemStack status = new ItemStack(Material.getMaterial(statusItem), 1 , statusId);
            ItemMeta statusMeta = status.getItemMeta();
            String statusName = plugin.stringFormater(plugin.getConfig().getString(""));
            for (String s : plugin.getConfig().getStringList("main-gui.status.lore")) {
                s = plugin.stringFormater(s);

            String tiersItem = plugin.getConfig().getString("main-gui.tiers.item");
            Short tiersId = Short.valueOf(plugin.getConfig().getString(""));
            ItemStack tiers = new ItemStack(Material.getMaterial(tiersItem), 1 , tiersId);
            ItemMeta tiersMeta = tiers.getItemMeta();
            String tiersName = plugin.stringFormater(plugin.getConfig().getString(""));
            for (String s : plugin.getConfig().getStringList("main-gui.tiers.lore")) {
                s = plugin.stringFormater(s);

            Integer challengesSlot = plugin.getConfig().getInt("main-gui.challenges.slot");
            Integer statusSlot = plugin.getConfig().getInt("main-gui.status.slot");
            Integer tiersSlot = plugin.getConfig().getInt("main-gui.tiers.slot");
            mainGui.setItem(challengesSlot, challenges);
            mainGui.setItem(statusSlot, status);
            mainGui.setItem(tiersSlot, tiers);

    Code (Text):
    package me.wrektyourday.battlepass;

    import me.wrektyourday.battlepass.guis.MainGui;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class TestCommand implements CommandExecutor {

        public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {

            Player p = Bukkit.getPlayer(sender.getName());

            MainGui gui = new MainGui();

            return true;

    Please point me in the right direction :p
  2. Seems like you need to know what OOP is.

    Basically, constructors allow you to create a new Object. In this case, your new object would be MainGui. From your code, you currently have 2 constructors: a default constructor (doesn't have any parameters), and the other one below that takes a Main object. Because your MainGui#openMainGui(p) uses the Main object to access its methods, you want to do this in your CommandClass to create a new MainGui object.

    Code (Text):
    MainGui gui = new MainGui(Main);
    Replace Main with the plugin instance.
  3. Thanks so much! I got it working nicely.