My commands in spigot plugin doesn't work - java

My spigot plugin doesn't work. On the console, it says the plugin is enabled but i can't run the command in the plugin. Pls help.
This is the main code Plugin.java
package lol.quacnooblol.mypvpplugin;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class Plugin extends JavaPlugin{
#Override
public void onEnable() {
Bukkit.getServer().getLogger().info("Plugin Enabled");
}
#Override
public void onDisable() {
Bukkit.getServer().getLogger().info("Plugin Disabled");
}
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
if(!(sender instanceof Player)) {
sender.sendMessage("You ran this command on the console");
}
Player player = (Player) sender;
if(cmd.getName().equalsIgnoreCase("test")) {
player.sendMessage("You ran the test command in game.");
return true;
}
return true;
}
}
This is the plugin.yml
name: Plugin
version: 0.1
main: lol.quacnooblol.mypvpplugin.Plugin
author: QuacNoobLoL
description: A pvp plugin
command:
test:
usage: /<command>
description: A test command

Change the plugin.yml command to commands
In the future please refer to the plugin.yml documentation and remember even a single letter can break your code!

in your Plugin.yml you need to use three spaces and not tab
here is the fixed file:
name: Plugin
version: 0.1
main: lol.quacnooblol.mypvpplugin.Plugin
author: QuacNoobLoL
description: A pvp plugin
commands:
test:
usage: /<command>
description: A test command
And your Plugin.java onCommand boolean needs the #Override annotation like this:
#Override
public boolean onCommand(CommandSender, Command cmd, String commandLabel, String[] args) {
if(cmd.getName().equalsIgnoreCase("test")){
if(!(sender instanceof Player)){
sender.sendMessage("You ran this command on the console");
}
Player player = (Player)sender;
if(sender instanceof Player){
player.sendMessage("You ran the test command in game.");
}
return true;
}
}
this should work

Related

NullPointerException when registering a new command in main class

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.

I have a minecraft plugin that detects swear words. How do I execute commands when the player curses?

Sa as the title says, I have a plugin that detects swear words. What it does now is that it sends a message to the player. But I also want it to execute a command that is already in the game or from another plugin. I'm not sure how to do this. How is that done?
Here is my current code:
package
import com.beam.sweardetector.SwearDetector;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
public class EventsClass implements Listener {
SwearDetector plugin = SwearDetector.getPlugin(SwearDetector.class);
#EventHandler
public void onPlayerJoinEvent(PlayerJoinEvent event) {
Player player = event.getPlayer();
player.sendMessage(ChatColor.GOLD + "This server is running AntiSwear v1.0 by BeamCRASH");
}
#EventHandler
public void chatevent (AsyncPlayerChatEvent event) {
for(String s: event.getMessage().split(" ")) {
if(plugin.getConfig().getStringList("swears").contains(s)) {
event.setCancelled(true);
event.getPlayer().sendMessage(ChatColor.DARK_RED + "§lSwearing is not allowed on this server!");
}
}
}
}
Any help will be appreciated.
Thank you!
You can execute command as player :
event.getPlayer().performCommand("mycommand");
But, it will not if the player don't have enough permission.
To run command as console, use this :
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mycommand");
Also, you don't need the / at the beginning of command because it's only to say that it's a command and it can change between players.
Warn: you should run those code in sync. If you are async (specially from AsyncEvent), you should use this code :
Bukkit.getScheduler().runTask(plugin, () -> {
// my code
player.performCommand("kill");
});

Having Trouble Devloping Minecraft Plugin

Following a YouTube Tutorial but when I run the plugin Minecraft doesn't register it at all.
The plugin is meant to return "Hi!" when the play does /hello or /hi.
When I put the plugin on my server nothing, doesn't even register in /plugins
Code:
Main.java:
package me.Cheese_Echidna.helloworld;
import me.Cheese_Echidna.helloworld.commands.HelloCommand;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
#Override
public void onEnable() {
new HelloCommand(this);
}
}
HelloCommand.java:
package me.Cheese_Echidna.helloworld.commands;
import me.Cheese_Echidna.helloworld.Main;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class HelloCommand implements CommandExecutor {
#SuppressWarnings("unused")
private Main plugin;
public HelloCommand(Main plugin) {
this.plugin = plugin;
plugin.getCommand("hello").setExecutor(this);
}
#Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("Only players may execute this command!");
return true;
}
Player p = (Player) sender;
if (p.hasPermission("hello.use")) {
p.sendMessage("Hi!");
return true;
} else {
p.sendMessage("You do not have permission to execute this command!");
}
return false;
}
}
plugin.yml:
name: HelloWorld
version: 1.0
author: Cheese_Echidna
main: me.Cheese_Echidna.helloworld.main
description: My first Bukkit plugin
commands:
hello:
aliases: [hi]
description: This is the hello command!
The YT tutorial:
https://www.youtube.com/watch?v=XaU8JKQW0Ao
I have attached a photo of the file structure here:
Any help would be really appreciated.
I have a basic question.
Have you installed the Vanilla Minecraft version on your Minecraft server? To use Bukkit or Spigot plugins you have to use their extensions.
I´m not shure. But the problem might be in the plugin.yml!
first its important that you use exactly one space for tabs in the plugin.yml
than another problem is that you have the path behind main: has to lead to a class
in this case it would be
main: me.Cheese_Echidna.helloworld.main.Main
The corrected plugin.yml should look like this:
name: HelloWorld
version: 1.0
author: Cheese_Echidna
main: me.Cheese_Echidna.helloworld.main.Main
description: My first Bukkit plugin
commands:
hello:
aliases: [hi]
description: This is the hello command!
You forgot to add the API version in your plugin.yml. Very common mistake, don't worry, a lot of people do this. Here's the plugin.yml wiki for more info: Plugin.yml wiki
It's also supposed to look like this. It seems you have the files in folders, not packages.
My plugin.yml for reference

Bukkit/Spigot. When I enter command, it just returns the command

When I use the command /heal in game, it returns with /heal in game chat. I don't understand why this is happening at all.
Here is my code (all in one class):
public boolean onCommand(CommandSender sender, Command cmd, String label, Strings [] args) {
Player player = (Player) sender;
if (cmd.getName().equalsIgnoreCase("heal")) {
if (sender.hasPermission("city.heal")) {
player.setHealth(20);
player.setFoodLevel(20);
sender.sendMessage("§fYou have been §bhealed §fand §bsated§f.");
return true;
}
if (!sender.hasPermission("city.heal")) {
sender.sendMessage("§cNo permission.");
return true;
}
}
return true;
}
And here is my plugin.yml:
main: me.rarfiles.code.Code
name: Test
author: rarfiles
version: 1.0
commands:
heal:
usage: /<command>
Make sure you register your CommandExecutor in the onEnable method.
By doing getCommand("heal").setExecutor(Instance of CommandExecutor);
You also need to make sure you check if the sender is an player by doing
if(sender instanceof Player)
Make sure you imported org.bukkit.Command and not com.mojang.brigadier.Command
Hope this helped

My bukkit plugin won't load

I'm relatively new to making bukkit plugins, and i have a basic understanding of java. My plugin won't work. From what i see on other forums, this is a common error but none of the solutions have worked.
Here is my error:
[16:18:19 ERROR]: Could not load 'plugins/MtgCraft.jar' in folder 'plugins'
org.bukkit.plugin.InvalidPluginException: Cannot find main class `me.sporech.MagictgCraft'
My plugin.yml:
name: MtgCraft
main: me.sporech.MagictgCraft
version: 1.8
author: Sporech
description: A basic plugin
My code is:
package me.sporech;
import java.util.Set;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class MagictgCraft extends JavaPlugin {
public static MagictgCraft plugin;
#Override
public void onEnable(){
getLogger().info("this is the plugin doing it");
}
#Override
public void onDisable(){
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("hello") && sender instanceof Player) {
Player player = (Player) sender;
player.sendMessage("Hello, " + player.getName() + "!");
return true;
}
return false;
}
#EventHandler
public void onPlayerInteractBlock(PlayerInteractEvent event) {
Player player = event.getPlayer();
if (player.getItemInHand().getType() == Material.STICK) {
player.getWorld().strikeLightning(player.getTargetBlock((Set<Material>) null, 200).getLocation());
}
}
}
The error is with your plugin.yml, not your code. Ensure that the plugin.yml is included in the default package and is inside your jar after exporting/zipping it.
It says that your description is invalid ("InvalidDescriptionException"); it may be too short, but that is just a guess. If lengthening your description does not work, try following the description with a ">" and a line break, then writing the description on the next line preceded by at least 8 spaces as shown in the example below from one of my plugins:
description: >
This super simple plugin has so many features your head may just implode.
The above works in my plugins, though honestly it should not be necessary. Still, it's worth a try.
EDIT:
For future readers who don't want to sift through comments, the problem here was that the plugin.yml was not included in the "src" folder or in the default package of the exported jar. Always make sure your plugin.yml is in your exported jar in the default package!

Categories