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!
Related
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.
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");
});
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
So in my server /me is an enabled command. I wanted to disable this because I don't want people to be able to do this.
I'm learning java, so I decided to code something that disabled /me myself.
So I wrote the following code:
package com.ste999.disableme;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.player.AsyncPlayerChatEvent;
public class Main extends JavaPlugin implements Listener
#Override
public void onEnable() {
getLogger().info("disable me enabled");
PluginManager pm = this.getServer().getPluginManager();
pm.registerEvents(this, (this));
}
#Override
public void onDisable() {
getLogger().info("disable me disabled");
}
#EventHandler
public void OnMe(AsyncPlayerChatEvent event)
{
Player p = event.getPlayer();
if(!p.hasPermission("ste999.me")) {
if (event.getMessage().startsWith("/me")) {
event.setCancelled(true);
p.sendMessage("§4Dont me me!");
}
}
}
}
with the following plugin.yml file:
name: Disable_Me
main: com.ste999.disableme.Main
version: 1.0
load: startup
description: this is should disable me
commands:
Now if someone without op would run /me hello it shouldn't output to the chat and the user should get a message like Dont me me!
But it doesn't. the user is still able to do /me hello without op and the code should prevent that
As I'm fairly new to java this error is probably easy to find, and any help would be much appreciated.
The problem is that AsyncPlayerChatEvent only gets called when actually typing chat messages (not commands). For commands you have to use PlayerCommandPreprocessEvent as wonderfully explained by Mischa in the comments. Changing the event will make it work:
#EventHandler
public void disableMeCommand(PlayerCommandPreprocessEvent event) {
Player p = event.getPlayer();
if(!p.hasPermission("ste999.me")) {
if(event.getMessage().startsWith("/me")) {
event.setCancelled(true);
p.sendMessage("§4Dont me me!");
}
}
}
However, note that PlayerCommandPreprocessEvent should be avoided. Luckily there is another way to disable a command completely in a bukkit server. You should have a commands.yml file located in your server folder. Simply add the "me" alias and set it to null inside the file:
aliases:
me:
- null
I am new to creating minecraft plugins, however not new to programming, I am following a tutorial very thoroughly, the video has good ratings so it is trusted, when watching the video the guy has no problems what so ever (Youtube video on developing minecraft plugins) , so I did some research into solutions but always the line through the code.
Eclipse gives me the option for: #SuppressWarnings("deprecation") which allows the code to be used still but I would rather have no need of that usage.
Basically my question is why is there the need of the line going through the code and how do I find a solution to get rid of it.
Main class:
package com.jc1;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class Core extends JavaPlugin
{
public Permission pPermission = new Permission("playerAbilities.allowed");
#Override
public void onEnable()
{
new BlockListener(this);
PluginManager pm = getServer().getPluginManager();
pm.addPermission(pPermission);
}
#Override
public void onDisable()
{
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if(cmd.getName().equalsIgnoreCase("giveitems") && sender instanceof Player)
{
Player p = (Player) sender;
if(p.hasPermission("playerAbilities.allowed"))
{
p.setItemInHand(new ItemStack(Material.DIAMOND_BOOTS));
}
return true;
}
return false;
}
}
Secondary class:
package com.jc1;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
public class BlockListener implements Listener
{
public BlockListener(Core plugin)
{
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
#EventHandler
public void onBlockPlace(BlockPlaceEvent e)
{
Player p = e.getPlayer();
if(!p.hasPermission("playerAbilities.allowed"))
{
e.setCancelled(true);
}
}
}
The method is deprecated, meaning that it is not recommended to be used anymore and is most likely replaced with another method.
Methods that are deprecated may still work as intended.
A simple search for the method reveals (this) documentation, stating:
players can duel wield now use the methods for the specific hand instead
which referes to the #see references:
getItemInMainHand() and getItemInOffHand().
Use this:
player.getInventory().getItemInMainHand()
Instead of:
player.getItemInHand()
Hope this helps! :D