Search code examples
javanullpointerexceptionbukkit

The constructor Commands() is undefined - Java / SpigotMC


I know there are other questions like this but none that I could find are fully helpful to this case.

So basically on the line that says

getCommand("minealchemy").setExecutor(new Commands());

I get this error:

The constructor Commands() is undefined

Any help? When i put "null" in the () for Commands ... Commands(null)... then i get a NullPointerException error...

Here are all of my classes:

Main Class:

package me.zachbears27;

import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

import me.zachbears27.utils.Commands;

public class Main extends JavaPlugin implements Listener {

    @Override
    public void onDisable() {
    }

    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
        registerConfig();

        getCommand("minealchemy").setExecutor(new Commands());
    }

    public void registerConfig() {
        saveDefaultConfig();
    }

}

Commands Class:

package me.zachbears27.utils;

import java.io.File;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import me.zachbears27.utils.Methods;
import net.md_5.bungee.api.ChatColor;

public class Commands implements CommandExecutor {

    private final Methods methods;
    public Commands(Methods methods) {
        this.methods = methods;
    }

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

            Player p = (Player) sender;

            if (cmd.equalsIgnoreCase("minealchemy")) {
                if(args.length > 0) {
                    //Get File
                    File file = methods.getPlayerFile(p.getUniqueId());
                    FileConfiguration fileSettings = YamlConfiguration.loadConfiguration(file);

                    //Make Sure It's False
                    fileSettings.set("Enabled", false);

                    //Save Inventory
                    methods.savePlayerInv(p.getUniqueId(), p.getInventory().getContents());

                    //Clear It
                    p.getInventory().clear();

                    //Put In God Mode
                    p.setInvulnerable(true);

                    //Open Inventory
                    p.openWorkbench(p.getLocation(), true);
                } else {
                    p.sendMessage(ChatColor.RED + "Incorrect Arguments... Please use \"start\" or \"list\".");
                }
            }

            return true;
        }
}

Methods Class:

package me.zachbears27.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.UUID;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;

import me.zachbears27.Main;
import net.md_5.bungee.api.ChatColor;

public class Methods {


private final Main plugin;
public Methods(Main plugin) {
    this.plugin = plugin;
}


    public ArrayList<String> getAllItems() {
        ArrayList<String> allItems = new ArrayList<String>();
        //Basics
        String Air = ChatColor.GREEN + "Air" + ChatColor.RESET;
        String Earth = ChatColor.GREEN + "Earth" + ChatColor.RESET;
        String Fire = ChatColor.GREEN + "Fire" + ChatColor.RESET;
        String Metal = ChatColor.GREEN + "Metal" + ChatColor.RESET;
        String Water = ChatColor.GREEN + "Water" + ChatColor.RESET;
        String Motion = ChatColor.GREEN + "Motion" + ChatColor.RESET;
        String Big = ChatColor.GREEN + "Big" + ChatColor.RESET;
        String Time = ChatColor.GREEN + "Time" + ChatColor.RESET;
        String Small = ChatColor.GREEN + "Small" + ChatColor.RESET;

        allItems.add(Air);
        allItems.add(Earth);
        allItems.add(Fire);
        allItems.add(Metal);
        allItems.add(Water);
        allItems.add(Motion);
        allItems.add(Big);
        allItems.add(Time);
        allItems.add(Small);

        //Animals
        String Cat = ChatColor.GOLD + "Cat" + ChatColor.RESET;

        allItems.add(Cat);

        return allItems;
    }

    public String addItems(String item1, String item2) {
        String returnStatement = ChatColor.RED + "X";
        if(item1.equals("Air") && item2.equals("Fire")) {
            returnStatement = "Mist";
        }
        return returnStatement;
    }

    public File getPlayerFile(UUID playerUUID) {

        //Creates The Player File
        File playerFile =  new File (plugin.getDataFolder() + File.separator + "Player Data", playerUUID + ".yml");
        return playerFile;
    }

    public void savePlayerInv(UUID playerUUID, ItemStack[] inv) {

        File playerFile = getPlayerFile(playerUUID);
        FileConfiguration playerData = YamlConfiguration.loadConfiguration(playerFile);
        playerData.set("SavedInventory", inv);
    }
}

Solution

  • Change the .setExecutor(new Commands()) in the onEnable() function to

    .setExecutor(new Commands(new Methods(this)))