So I recently attempted to make a bukkit plugin for minecraft.
The idea is that this would just be a test plugin to see if I could do it, and apparently I cant. This is the code I came up with
package me.glowhoo.BlockChanger; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class BlockChanger extends JavaPlugin
{
#Override public void onEnable() {
Bukkit.getLogger().info(this.getDescription().getName() +
" has been enabled");
}
#Override public void onDisable() {
Bukkit.getLogger().info(this.getDescription().getName() + " has been disabled");
}
#Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
/*command: /tpa Glowhoo
* args.length = 1
* args[0] = Glowhoo
* command.getName() = tpa
*/
if (cmd.getName().equalsIgnoreCase("message")){
if(sender instanceof Player)
{
/*ComandSender sender-who sent the command
* Command cmd- the command that was executed
* String commandLabel-the command alias that was used
* String[] args-array of additional arguments
*/
sender.sendMessage(ChatColor.RED +"Hello player!");
}else
{
sender.sendMessage(ChatColor.AQUA +"Hello console!");
}
}
return false;
}
}
So the problem with this, is that whenever I load up the server, it says the "BlockChanger has been enabled", but then when I try to type the only command it has (message) or /message, Nothing happens and it says it is an unrecognized command and to type /help for info. The problem doesn't end there though, whenever I actually try to get the plugin going, 90% of the time i have a plugin.yml error, which I don't completely understand, but if any of you know anything about this issue, please respond
My plugin.yml file is here (NOTE: this plugin.yml copy actually works, but the command itsself doesn't.) :
name: BlockChanger
version: 1.0
main: me.glowhoo.BlockChanger.BlockChanger
description:
commands:
message:
description: -no desc-
usage: /message
Your plugin.yml is incorrect. You need to indent message under commands to define a node. Currently commands is a key value for nothing. Change the relevant part to this:
commands:
message:
usage: /message
This is some time in the future now, but in case you use this again, when you have a command, you need to return true, else it will tell you it is an incorrect command, even if your .yml/.yaml file is set up right. So here is what you would need to do to make it register your command when called:
package me.glowhoo.BlockChanger; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class BlockChanger extends JavaPlugin
{
#Override public void onEnable() {
Bukkit.getLogger().info(this.getDescription().getName() +
" has been enabled");
}
#Override public void onDisable() {
Bukkit.getLogger().info(this.getDescription().getName() + " has been disabled");
}
#Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
/*command: /tpa Glowhoo
* args.length = 1
* args[0] = Glowhoo
* command.getName() = tpa
*/
if (cmd.getName().equalsIgnoreCase("message")){
if(sender instanceof Player)
{
/*ComandSender sender-who sent the command
* Command cmd- the command that was executed
* String commandLabel-the command alias that was used
* String[] args-array of additional arguments
*/
sender.sendMessage(ChatColor.RED +"Hello player!");
}else
{
sender.sendMessage(ChatColor.AQUA +"Hello console!");
}
return true;
}
return false;
}
}
U need to register this class as command executor in onEnable method.
Related
I'm basically making a spigot plugin, for practice.
The server doesn't enable the server, no error in the console. It also no "INFO" for enabling the plugin.
This is my main class
package me.FarrosGaming.SpawnPillager;
import org.bukkit.plugin.java.JavaPlugin;
import me.FarrosGaming.SpawnPillager.commands.PillagerCommand;
public class Main extends JavaPlugin {
#Override
public void onEnable() {
new PillagerCommand(this);
}
#Override
public void onDisable() {
}
}
This is my command class
package me.FarrosGaming.SpawnPillager.commands;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import me.FarrosGaming.SpawnPillager.Main;
public class PillagerCommand implements CommandExecutor {
private Main plugin;
public PillagerCommand(Main plugin) {
this.plugin = plugin;
plugin.getCommand("chase").setExecutor(this);
}
#Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("Only players who can send this command.");
return true;
}
Player player = (Player) sender;
if (player.hasPermission("hello.use")) {
Location location = player.getLocation();
World world = player.getWorld();
for (int i = 0; i < 10; i++) {
world.spawnEntity(location, EntityType.CREEPER);
}
return true;
} else {
player.sendMessage("You do not have a permission to use this command");
}
return false;
}
}
this is my plugin.yml file
name: SpawnPillager
version: 1.0
author: FarrosGaming
description: blabla
main: me.FarrosGaming.SpawnPillager.Main
commands:
chase:
description: Spawn pillager in your location
usage: /chase
Literally, there are no spaces in the plugin.yml
I'm using the newest version of spigot.
make sure you are dropping your plugin's Jar in the right spot, i mean the right server's plugins folder, if you multiple and then reload/ restart your server,
I m telling this coz, me myself, have done this mistake too..
I am having trouble with my plugin. The error is "Cannot find main class 'turtdle.abilities.Main'" (I know I spelled turtle wrong, but it is my username.)
This plugin is for my server. I have already tried completing the plugin.yml file (with author, version, etc.) I also tried changing the plugin name to "Main." I have also tried moving the yml around with no success.
package turtdle.abilities;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin{
public Permission playerPermission1 = new Permission("turtdle.abilities.get");
public Permission playerPermission2 = new Permission("turtdle.place.bedrock");
#Override
public void onEnable() {
getLogger().info("onEnable Has been enabled for abilities plugin! BOOP!");
new PlayerListener(this);
new BlockRestricter(this);
PluginManager pm = getServer().getPluginManager();
pm.addPermission(playerPermission1);
pm.addPermission(playerPermission2);
}
#Override
public void onDisable() {
getLogger().info("onDisable Has been triggered for abilities plugin");
}
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("Hewwooo, " + player.getName());
return true;
}
return false;
}
}
package turtdle.abilities;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
public class PlayerListener implements Listener{
public PlayerListener(Main plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
#EventHandler
public void onDeath (PlayerDeathEvent e) {
Player player = e.getEntity();
if (!player.hasPermission("turtdle.abilities.get")) {
player.sendMessage(ChatColor.AQUA + "OOF");
}
else {
{
player.sendMessage(ChatColor.AQUA + "you should've abused...");
}
}
}
}
package turtdle.abilities;
import org.bukkit.ChatColor;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
public class BlockRestricter implements Listener{
public BlockRestricter(Main plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
#EventHandler
public void onBlockPlace (BlockPlaceEvent e) {
Player player = e.getPlayer();
Block block = e.getBlock();
if (!player.hasPermission("turtdle.place.bedrock") && block.getType().getId() == 7) {
player.sendMessage(ChatColor.RED + "You CAN'T PLACE THIS " + ChatColor.BOLD + "BLOCK! " + ChatColor.RESET + ChatColor.RED + " it is ILLEGAL");
e.setCancelled(true);
}
}
}
plugin.yml:
main: turtdle.abilities.Main
name: TurtdleAbilitiesCore
version: 0.2.9
author: CakeyTheTurtdle
description: ExclusiveWolfHuntplugin
commands:
hello:
description: When you're lonely
usage: /hello
Hey here is my whole error
https://pastebin.com/FAieE0Lr
The other answer is not correct, unfortunately.
In the Pastebin you linked, we can see that the error is the following:
Caused by: java.lang.ClassNotFoundException: turtdle.Main
When Bukkit is trying to load your main class, it is looking for the class turtdle.Main. The reason this is strange is your plugin.yml tells it to look for turtdle.abilities.Main, which it is not doing.
My guess for the cause of the issue is that sometimes when the project is being compiled, your IDE may not actually grab the edited plugin.yml file to put in the final jar. To fix this, complete the following steps:
Open your project folder and delete any/all of the following files/folders if they exist:
bin/
target/
YourPluginName.jar
Go to your server's folder and delete YourPluginName.jar there as well
Recompile your project and add the fresh jar file to your plugins folder.
Hopefully, this should resolve the issue. Your plugin.yml file and code both look fine and in theory should work. This is the only thing I can think of that would cause the issue.
Best of luck!
I am trying to register a bukkit Command on the other Command.
So I want to make "/command1" to register "/command2" so command 2 only can execute after I executed command 1.
I tried for like 10 hours by now to do that, at the moment I am able to register a command without making it into the plugin.yml and that works, just the second command does not get registered.
Main class:
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap;
import org.bukkit.plugin.SimplePluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import java.lang.reflect.Field;
public class Main extends JavaPlugin {
#Getter
CommandMap commandMap;
#Override
public void onEnable() {
loadCommandMap();
this.commandMap.register("command1", new FirstCommand(this));
}
private void loadCommandMap() {
try {
if (Bukkit.getPluginManager() instanceof SimplePluginManager) {
Field f = SimplePluginManager.class.getDeclaredField("commandMap");
f.setAccessible(true);
this.commandMap = (CommandMap) f.get(Bukkit.getPluginManager());
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
FirstCommand:
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
public class FirstCommand extends Command {
private Main plugin;
public FirstCommand(Main plugin) {
super("command1");
this.plugin = plugin;
}
#Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
plugin.getCommandMap().register("command2", new SecondCommand());
sender.sendMessage("Command 1.");
return true;
}
}
Second Command:
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
public class SecondCommand extends Command {
public SecondCommand() {
super("command2");
}
#Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
sender.sendMessage("Command 2");
return true;
}
}
I really hope someone knows why the first command gets registered but the second one does not.
You could try to not register them at runtime, but enable them at runtime.
You can use global, static variables (for example in the main class of your app), e.g.
// in your class "Main"
public static boolean isCommand2Enabled = false;
and when command1 is called, you set it to true
Main.isCommand2Enabled = true;
Your command2 must now only check whether it has already been activated and can be executed:
if(!Main.isCommand2Enabled) {
// I am not activated yet and must return
return false;
}
But I am not quite sure if you might try to define the name of command2 first when command1 is executed (variable command name). You should then maybe use a fixed command and only make the corresponding argument variable.
I don't really understand what you are talking about, but I think this may help you...
Bukkit Tutorial - Registering Commands At Runtime
Now that I have changed the 'Commands' to 'commands' in the plugin.yml I get another error in my cmd when I run my server. The error says '.Jar file does not contain plugin.yml'.
This is my plugin.yml as of now:
name: Wand
version: 1.0
main: me.Pixel.Main
commands:
wand:
And this is my Main file currently:
package me.Pixel;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.Effect;
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.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin implements Listener {
public Main plugin;
public List<String> spells = new ArrayList<String>();
public getTargets getTargets = new getTargets();
public Spark spark = new Spark(this);
public PoisonWave poisonwave = new PoisonWave(this);
public DarkSpark darkSpark = new DarkSpark(this);
#Override
public void onEnable() {
plugin = this;
getServer().getPluginManager().registerEvents(this, this);
spells.add("Spark");
spells.add("PoisonWave");
spells.add("DarkSpark");
}
#Override
public boolean onCommand(CommandSender sender, Command command,
String label, String[] args) {
if(label.equalsIgnoreCase("wand")) {
if(!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "You need to be an in-game player to perform this action!");
} else {
Player p = (Player) sender;
if(sender.hasPermission("wand.wand")) {
ItemStack stack = new ItemStack(Material.BLAZE_ROD);
ItemMeta stackMeta = stack.getItemMeta();
stackMeta.setDisplayName(ChatColor.RED + "Empire Wand");
stack.setItemMeta(stackMeta);
p.getInventory().addItem(stack);
ChatUtilities.sendMessage(p, "You have got yourself a powerful Empire Wand!");
} else {
ChatUtilities.sendMessage(p, ChatColor.RED + "ERROR: No Permission!");
}
}
}
return false;
}
#EventHandler
public void onClick(PlayerInteractEvent e) {
if((e.getAction() == Action.RIGHT_CLICK_AIR) || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
Player p = e.getPlayer();
ItemStack stack = p.getItemInHand();
if(stack != null && stack.getType() == Material.BLAZE_ROD && stack.hasItemMeta() && stack.getItemMeta().getDisplayName().equals(ChatColor.RED + "Empire Wand")) {
int SpellSelected = stack.getDurability();
if(SpellSelected < 2) {
stack.setDurability((short) (SpellSelected + 1));
p.getWorld().playEffect(p.getLocation(), Effect.STEP_SOUND, 119, 30);
} else {
stack.setDurability((short) 0);
}
ChatUtilities.sendMessage(p, "Selected: " + spells.get(SpellSelected));
}
}
if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK) {
Player p = e.getPlayer();
ItemStack stack = p.getItemInHand();
if(stack != null && stack.getType() == Material.BLAZE_ROD && stack.hasItemMeta() && stack.getItemMeta().getDisplayName().equals(ChatColor.RED + "Empire Wand")) {
int SpellSelected = stack.getDurability();
if(SpellSelected == 1) {
this.spark.onCast(p);
} else if (SpellSelected == 0) {
this.poisonwave.onCast(p);
}
}
}
}
}
name: Wand
version: 1.0
main: me.Pixel.Main
commands:
wand:
Your plugin.yml file isn't valid. 'commands' must be all lower case, and there must be 2 spaces before wand.
Also, you should not make commands that way. You should make external classes implementing the CommandExecutor Interface.
I think, if you do it this way, I think you must register the command.
Like this:
getCommand("wand").setExecutor(this);
The plugin.yml file is case senstitive, the name of the list of commands needs to be all lowercase (commands instead of Commands). Since you currently have it capitalized, Bukkit/Spigot doesn't register any commands thus resulting in an "Unknown command. Type "/help" for help." message if you test the /wand command (I'm assuming this is the error you're getting, as you didn't describe the problem nor the expected behavior, but this is what happened when I tested the code, and correcting the name of the commands list made the command execute).
You might need to update the player's inventory after adding the item with p.updateInventory(). In your case, it would be something like this:
p.getInventory().addItem(stack);
p.updateInventory();
I'm trying to create a plugin for minecraft bukkit servers. The goal is to read the string on the first line of an html page. If the result is True it will execute a command.
Here is the code I have right now:
import java.net.URLConnection;
import java.util.Scanner;
import java.util.logging.Logger;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class main extends JavaPlugin{
public final Logger logger = Logger.getLogger("Minecraft");
public void onEnable(){
logger.info("[First] Has Been Enabled.");
}
public void onDisable(){
logger.info("[First] Has Been Disabled.");
}
public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
Player player = (Player) sender;
if(commandLabel.equalsIgnoreCase("hello")){
player.sendMessage(ChatColor.GOLD + "Hello");
}
else if(commandLabel.equalsIgnoreCase("world")){
player.sendMessage(ChatColor.GOLD + "World");
}
else if(commandLabel.equalsIgnoreCase("coolman")){
player.setPlayerListName("coolman");
}
else if(commandLabel.equalsIgnoreCase("vote")){
String sourceLine = null;
// The URL address of the page to open.
URL address = new URL("http://www.koolflashgames.com/test.php");
// Open the address and create a BufferedReader with the source code.
InputStreamReader pageInput = new InputStreamReader(address.openStream());
BufferedReader source = new BufferedReader(pageInput);
// Append each new HTML line into one string. Add a tab character.
try {
sourceLine = source.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(sourceLine == "False"){
player.sendMessage("Thanks for voting!");
}
}
return false;
}
}
Creates the following error in the log:
2012-11-02 16:18:30 [SEVERE] null
org.bukkit.command.CommandException: Unhandled exception executing command 'vote' in plugin first v1.0
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180)
at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:502)
at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:915)
at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:828)
at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:810)
at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:44)
at net.minecraft.server.NetworkManager.b(NetworkManager.java:282)
at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:111)
at net.minecraft.server.ServerConnection.b(SourceFile:35)
at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:561)
at net.minecraft.server.DedicatedServer.q(DedicatedServer.java:213)
at net.minecraft.server.MinecraftServer.p(MinecraftServer.java:474)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:406)
at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
Caused by: java.lang.NullPointerException
at java.io.Reader.<init>(Unknown Source)
at java.io.InputStreamReader.<init>(Unknown Source)
at me.storminmormon30.first.main.onCommand(main.java:43)
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
... 15 more
Looks like the error is somewhere else:
Unhandled exception executing command 'vote'
I don't see that command in the code snippet you have given
You should split each command into it's own class and register in in the plugin.yml, What I would do, is in the onEnable() method, set something like this:
getCommand("command").setExecutor(new CommandExecutor());
That will split off all of the commands into their own class, which would work better, and I would suggest it.
The issue with your code looks like a NPE in your class main on line 43 (its in your onEnable() method)