I'm experimenting with spigot plugins for my Minecraft server. In the process of attempting to create custom commands, such as heal & feed, I get the following error in my ide console for the main java file (MyFirstPlugin.java):
Method invocation 'setExecutor' may produce 'NullPointerException':14
Method invocation 'setExecutor' may produce 'NullPointerException':15
The full MyFirstPlugin.java
file:
package me.nathan.myfirstplugin;
import me.nathan.myfirstplugin.commands.PluginCommands;
import me.nathan.myfirstplugin.events.PluginEvents;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;
public final class MyFirstPlugin extends JavaPlugin {
@Override
public void onEnable() {
PluginCommands commands = new PluginCommands();
getServer().getPluginManager().registerEvents(new PluginEvents(), this);
getCommand("heal").setExecutor(commands);
getCommand("feed").setExecutor(commands);
getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "[MyFirstPlugin]: Plugin is enabled!");
}
@Override
public void onDisable() {
// Plugin shutdown logic
getServer().getConsoleSender().sendMessage(ChatColor.RED + "[MyFirstPlugin]: Plugin is disabled!");
}
}
And this error for the commands class (PluginCommands.java):
Method invocation 'getDefaultValue' may produce 'NullPointerException':21
The full PluginCommands.java
file:
package me.nathan.myfirstplugin.commands;
import org.bukkit.ChatColor;
import org.bukkit.attribute.Attribute;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class PluginCommands implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("Only players can use that command!");
return true;
}
Player player = (Player) sender;
if (cmd.getName().equalsIgnoreCase("heal")){
double maxHealth = player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getDefaultValue();
player.setHealth(maxHealth);
sender.sendMessage(ChatColor.GREEN + "You have been healed!");
}
if (cmd.getName().equalsIgnoreCase("feed")){
player.setFoodLevel(20);
sender.sendMessage(ChatColor.GREEN + "You have been fed!");
}
return true;
}
}
When I start the server and load the plugin, I get the following errors in the command prompt:
[23:13:20] [Server thread/ERROR]: Error occurred while enabling MyFirstPlugin v1.0-SNAPSHOT (Is it up to date?)
java.lang.NullPointerException: Cannot invoke "org.bukkit.command.PluginCommand.setExecutor(org.bukkit.command.CommandExecutor)" because the return value of "me.nathan.myfirstplugin.MyFirstPlugin.getCommand(String)" is null
at me.nathan.myfirstplugin.MyFirstPlugin.onEnable(MyFirstPlugin.java:14) ~[?:?]
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot-1.17.1.jar:3205-Spigot-18c71bf-6788550]
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:342) ~[spigot-1.17.1.jar:3205-Spigot-18c71bf-6788550]
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[spigot-1.17.1.jar:3205-Spigot-18c71bf-6788550]
at org.bukkit.craftbukkit.v1_17_R1.CraftServer.enablePlugin(CraftServer.java:505) ~[spigot-1.17.1.jar:3205-Spigot-18c71bf-6788550]
at org.bukkit.craftbukkit.v1_17_R1.CraftServer.enablePlugins(CraftServer.java:419) ~[spigot-1.17.1.jar:3205-Spigot-18c71bf-6788550]
at net.minecraft.server.MinecraftServer.loadWorld(MinecraftServer.java:604) ~[spigot-1.17.1.jar:3205-Spigot-18c71bf-6788550]
at net.minecraft.server.dedicated.DedicatedServer.init(DedicatedServer.java:266) ~[spigot-1.17.1.jar:3205-Spigot-18c71bf-6788550]
at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:995) ~[spigot-1.17.1.jar:3205-Spigot-18c71bf-6788550]
at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:303) ~[spigot-1.17.1.jar:3205-Spigot-18c71bf-6788550]
at java.lang.Thread.run(Thread.java:831) [?:?]
[23:13:20] [Server thread/INFO]: Server permissions file permissions.yml is empty, ignoring it
[23:13:20] [Server thread/INFO]: Done (24.130s)! For help, type "help"
I feel like I have tried everything to fix this with my limited knowledge on the subject but if anyone could help, it would be very much appreciated :)