I want to create a new command in a minecraft plugin im currenly developing. it should give a player an item. I registered the command in my main class and in the plugin.yml but when the server loads the plugin, it always throws a NullPointerException.
i would really appreciate any help
Thanks!
Here is my main class:
package dungeonsremake.dungeonsremake;
import dungeonsremake.dungeonsremake.commands.GiveCustomItem;
import org.bukkit.plugin.java.JavaPlugin;
public final class Main extends JavaPlugin {
@Override
public void onEnable() {
this.getCommand("item").setExecutor(new GiveCustomItem());
}
@Override
public void onDisable() {
getLogger().info("Shutting down Plugin");
}
}
And here is my Command class:
package dungeonsremake.dungeonsremake.commands;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public class GiveCustomItem implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
System.out.println("activated");
if(!(sender instanceof Player)) {
return true;
}
Player player = (Player) sender;
if(command.getName().equalsIgnoreCase("item")) {
ItemStack fot = new ItemStack(Material.LEGACY_RED_ROSE);
fot.setAmount(1);
player.getInventory().addItem(fot);
return true;
}
return true;
}
}
and here my plugin.yml:
name: DungeonsRemake
main: dungeonsremake.dungeonsremake.Main
version: 1.0
commands:
item:
usage: /<command>
description: gives the player a custom item.
aliases: [giveitem, item, customitem, ci, gci]
error message:
java.lang.NullPointerException: Cannot invoke "org.bukkit.command.PluginCommand.setExecutor(org.bukkit.command.CommandExecutor)" because the return value of "dungeonsremake.dungeonsremake.Main.getCommand(String)" is null
at dungeonsremake.dungeonsremake.Main.onEnable(Main.java:15) ~[?:?]
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-api-1.18.2-R0.1-SNAPSHOT.jar:?]
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:342) ~[spigot-api-1.18.2-R0.1-SNAPSHOT.jar:?]
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[spigot-api-1.18.2-R0.1-SNAPSHOT.jar:?]
at org.bukkit.craftbukkit.v1_18_R2.CraftServer.enablePlugin(CraftServer.java:517) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3465-Spigot-ffceeae-3ec79a2]
at org.bukkit.craftbukkit.v1_18_R2.CraftServer.enablePlugins(CraftServer.java:431) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3465-Spigot-ffceeae-3ec79a2]
at net.minecraft.server.MinecraftServer.loadWorld0(MinecraftServer.java:612) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3465-Spigot-ffceeae-3ec79a2]
at net.minecraft.server.MinecraftServer.loadLevel(MinecraftServer.java:414) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3465-Spigot-ffceeae-3ec79a2]
at net.minecraft.server.dedicated.DedicatedServer.e(DedicatedServer.java:263) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3465-Spigot-ffceeae-3ec79a2]
at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:1007) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3465-Spigot-ffceeae-3ec79a2]
at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:304) ~[spigot-1.18.2-R0.1-SNAPSHOT.jar:3465-Spigot-ffceeae-3ec79a2]
at java.lang.Thread.run(Thread.java:833) [?:?]
As per the documentation:
Commands need to be registered in the PluginDescriptionFile to exist at runtime.
The reason it cannot find your item
command is because the indentation in yaml matters. If your IDE doesn't have a built-in parser, you can use an online one like this to see the issue.
commands:
item:
usage: /<command>
description: gives the player a custom item.
aliases: [giveitem, item, customitem, ci, gci]
That should be the correct indentation.