I have other commands added to this code, but for some reason, this part causes errors at the return false; and all the player.** parts. Code:java if(cmd.getname().equalsIgnoreCase("nightvision on")) player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 200, 1)); //cooldown { if(player.hasPermission("vortex.nightvision")) player.sendMessage(ChatColor.AQUA + "NightVision Mode activated, type /nightvision off to turn nightvision off!") else player.sendMessage((new StringBuilder()).append(ChatColor.RED).append("You have no power here! Donate for power plz :D")).toString(); return false; }
Stracktrace maybe? Also, why are you opening brackets outsite of the if statement? I'm not sure what are you trying to do.
pretty much, im making a whole plugin, with some useful commands. i.e /fw (shoots a firework at player position) i've completed that. in the same class, im adding /nightvision on and /nightvision off which will allow my donors to get the effect "Nightvision"
The errors, those spam things that appear on the console? also, the full onCommand method might be useful.
Dont worry, i've fixed the problem, by creating individual classes for the commands. in the Main.class how do i make sure that the commands will be read by the plugin :s sorry, new to coding
iamcion Register the commands in the main class so they would be "read" if I understand what you're saying
iamcion There are many ways to do it. But they way I do it is to put this in your main class: Code:java public void onEnable() { registerCommands();} public void registerCommands(){ registerCommand("CommandHere", new CommandClass(this));} public void registerCommand(String command, CommandExecutor commandexecutor) { Bukkit.getServer().getPluginCommand(command).setExecutor(commandexecutor); }
added, so how will i 'call' the commands, from the classes which ive added the code on? eg, one of my class names is FireworkCommand.java
iamcion What do you mean by call? What do you want to access within the command class exactly? xD You would call it normally I guess?
Code:java public class Main extends JavaPlugin implements Listener{ public Main()} //on enable settings public void onEnable() { registerCommands(); } public void registerCommands() { registerCommand("fw", new CommandClass(this)); } public void registerCommand(String command, CommandExecutor commandexecutor) { Bukkit.getServer().getPluginCommand(command).setExecutor(commandexecutor); }} I'm trying to call the command "fw" from the class FireworkCommand.java there is an error with the CommandClass in that code btw, :I
iamcion That's because you're supposed to replace CommandClass with your CommandClass with is FireworkCommand After that, your command should work To add new commands, just simply add a new line of: registerCommand("commandhere", new CommandClass(this)); And remember to add your command to your plugin.yml Also what the heck is this? { public Main() } Your class should look like: Code:java package yourpackagehere; import org.bukkit.Bukkit;import org.bukkit.command.CommandExecutor;import org.bukkit.event.Listener;import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPluginimplements Listener{//on enable settingspublic void onEnable() {registerCommands();} public void onDisable() { } public void registerCommands(){registerCommand("fw", new FireworkCommand(this));} public void registerCommand(String command, CommandExecutor commandexecutor) {Bukkit.getServer().getPluginCommand(command).setExecutor(commandexecutor);}} Also remember to implement CommandExecutor in your FireworkCommand.java
errors when i try to call that class? I removed the (this)); after trying to call the command, and it works EDIT by Moderator: merged posts, please use the edit button instead of double posting.
iamcion Either your class is not implementing interface CommandExecutor or there is no specified constructor to accept Main class instance. You soother have to remove the arguments or make a constructor that accepts Main class as parameter.
not sure why. this is the main class Code:java package me.iamcion.firework; import org.bukkit.Bukkit;import org.bukkit.command.CommandExecutor;import org.bukkit.event.Listener;import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPluginimplements Listener{//on enable settingspublic void onEnable() {registerCommands();} public void onDisable() { } public void registerCommands(){registerCommand("fw", new FireworkCommand());registerCommand("fwr", new FireworkCommand());registerCommand("nightvision on", new NightvisionCommand());registerCommand("nightvision off", new NightvisionCommand());} public void registerCommand(String command, CommandExecutor commandexecutor) {Bukkit.getServer().getPluginCommand(command).setExecutor(commandexecutor);}}
You forgot to put a semi column on one of your lines and ask for help with bukkit API. Again, learn to walk before you start running.
Code:java registerCommand("nightvision on", new NightvisionCommand());registerCommand("nightvision off", new NightvisionCommand()); You don't register arguments to the commands. You do this in your onCommand method by checking the length of the arguments. Ex: Code:java if(args.length == 1){if(args[0].equalsIgnoreCase("on"){//code}else if(args[0].equalsIgnoreCase("off"){//code{}else if(args.length == 2){//code} Correct way to register commands: Code:java registerCommand("nightvision", new NightVisionCommand()); Reminder, arrays start at zero so be sure that if your argument length for example is 1, if you do args[0] you will get the first argument. Also, The first argument is the first piece of the string after the command. Ex: /command hi args[0] would return "hi"
iamcion Adding new FireworkCommand(this) works if in your FireworkCommand.java: It implements CommandExecutor and it has a constructor like this: public FireworkCommand(Main plugin) { } and remember to do what scenariocreator did with the arguments for night vision
You need to learn Java before you work with the Bukkit API. There are other things I could correct in your code (such as how you seem to think commands can be more than one word), however there is no point. If you do not know Java, you can not properly utilize the API.
Code:java if(cmd.getname().equalsIgnoreCase("nightvision on"))player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 200, 1));//cooldown{if(player.hasPermission("vortex.nightvision"))player.sendMessage(ChatColor.AQUA + "NightVision Mode activated, type /nightvision off to turn nightvision off!")elseplayer.sendMessage((new StringBuilder()).append(ChatColor.RED).append("You have no power here! Donate for power plz :D")).toString();return false;} Hmmm I wonder what the problem with this code is.. Just in case you can't see it, let's "zoom" in.. Code:java if(cmd.getname().equalsIgnoreCase("nightvision on"))player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 200, 1));//cooldown{ That better? No? Ok, let me explain. Floating "{". There is no if statement it belongs to, no class, no nothing! No wonder you're getting errors iamcion. Please take what Garris0n said and learn Java before attempting to use the API of Bukkit. Please... For the love of cats!!
TGRHavoc Acually code like this: Code:java public void test(){if(permission == null)//codeelse//code{//code}} Is technically correct Code:java if(cmd.getname().equalsIgnoreCase("nightvision on"))player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 200, 1)); Is looked at as a complete if statement. Its all the same to java whether it is like this. Code:java if(cmd.getname().equalsIgnoreCase("nightvision on"))player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 200, 1));//cooldown{if(player.hasPermission("vortex.nightvision"))player.sendMessage(ChatColor.AQUA + "NightVision Mode activated, type /nightvision off to turn nightvision off!")elseplayer.sendMessage((new StringBuilder()).append(ChatColor.RED).append("You have no power here! Donate for power plz :D")).toString();return false;} Or like this: Code:java if(cmd.getname().equalsIgnoreCase("nightvision on")){player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 200, 1));//cooldown} if(player.hasPermission("vortex.nightvision")){player.sendMessage(ChatColor.AQUA + "NightVision Mode activated, type /nightvision off to turn nightvision off!")}else{player.sendMessage((new StringBuilder()).append(ChatColor.RED).append("You have no power here! Donate for power plz :D")).toString();return false;} The curly braces can be used to section off code to make it more readable to the user.