player not working?

Discussion in 'Plugin Development' started by iamcion, Jun 18, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    iamcion

    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
    1. if(cmd.getname().equalsIgnoreCase("nightvision on"))
    2. player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 200, 1));
    3. //cooldown
    4. {
    5. if(player.hasPermission("vortex.nightvision"))
    6. player.sendMessage(ChatColor.AQUA + "NightVision Mode activated, type /nightvision off to turn nightvision off!")
    7. else
    8. player.sendMessage((new StringBuilder()).append(ChatColor.RED).append("You have no power here! Donate for power plz :D")).toString();
    9.  
    10. return false;
    11. }
     
  2. Offline

    Konato_K

    Stracktrace maybe?

    Also, why are you opening brackets outsite of the if statement? I'm not sure what are you trying to do.
     
  3. Offline

    iamcion

    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"
     
  4. Offline

    Gater12

  5. Offline

    iamcion

    tbh, i dont know what a stacktrace is :I
     
  6. Offline

    Konato_K

    The errors, those spam things that appear on the console?

    also, the full onCommand method might be useful.
     
  7. Offline

    iamcion

    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 :p
     
  8. Offline

    LordVakar

    iamcion
    Register the commands in the main class so they would be "read" if I understand what you're saying
     
  9. Offline

    iamcion

    code? :p
     
  10. Offline

    LordVakar

    iamcion
    There are many ways to do it.
    But they way I do it is to put this in your main class:
    Code:java
    1. public void onEnable() {
    2. registerCommands();
    3. }
    4.  
    5. public void registerCommands()
    6. {
    7. registerCommand("CommandHere", new CommandClass(this));
    8. }
    9.  
    10. public void registerCommand(String command, CommandExecutor commandexecutor) {
    11. Bukkit.getServer().getPluginCommand(command).setExecutor(commandexecutor);
    12. }
     
  11. Offline

    iamcion

    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
     
  12. Offline

    LordVakar

    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?
     
  13. Offline

    iamcion

    Code:java
    1. public class Main extends JavaPlugin
    2. implements Listener
    3. {
    4.  
    5. public Main()
    6. }
    7. //on enable settings
    8. public void onEnable() {
    9. registerCommands();
    10. }
    11.  
    12. public void registerCommands()
    13. {
    14. registerCommand("fw", new CommandClass(this));
    15. }
    16.  
    17. public void registerCommand(String command, CommandExecutor commandexecutor) {
    18. Bukkit.getServer().getPluginCommand(command).setExecutor(commandexecutor);
    19. }
    20. }
    21.  


    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
     
  14. Offline

    LordVakar

    iamcion
    That's because you're supposed to replace CommandClass with your CommandClass with is FireworkCommand
    After that, your command should work :D
    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?
    1. {
    2. public Main()
    3. }
    Your class should look like:
    Code:java
    1. package yourpackagehere;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.command.CommandExecutor;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. public class Main extends JavaPlugin
    9. implements Listener
    10. {
    11. //on enable settings
    12. public void onEnable() {
    13. registerCommands();
    14. }
    15.  
    16. public void onDisable() {
    17.  
    18. }
    19.  
    20. public void registerCommands()
    21. {
    22. registerCommand("fw", new FireworkCommand(this));
    23. }
    24.  
    25. public void registerCommand(String command, CommandExecutor commandexecutor) {
    26. Bukkit.getServer().getPluginCommand(command).setExecutor(commandexecutor);
    27. }
    28. }
    29.  


    Also remember to implement CommandExecutor in your FireworkCommand.java
     
  15. Offline

    iamcion

    errors when i try to call that class?
    [​IMG]

    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.
     
    Last edited by a moderator: Jun 8, 2016
  16. Offline

    Gater12

    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.
     
  17. Offline

    iamcion

    not sure why. this is the main class
    Code:java
    1. package me.iamcion.firework;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.command.CommandExecutor;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. public class Main extends JavaPlugin
    9. implements Listener
    10. {
    11. //on enable settings
    12. public void onEnable() {
    13. registerCommands();
    14. }
    15.  
    16. public void onDisable() {
    17.  
    18. }
    19.  
    20. public void registerCommands()
    21. {
    22. registerCommand("fw", new FireworkCommand());
    23. registerCommand("fwr", new FireworkCommand());
    24. registerCommand("nightvision on", new NightvisionCommand());
    25. registerCommand("nightvision off", new NightvisionCommand());
    26. }
    27.  
    28. public void registerCommand(String command, CommandExecutor commandexecutor) {
    29. Bukkit.getServer().getPluginCommand(command).setExecutor(commandexecutor);
    30. }
    31. }
    32.  
     
  18. Offline

    Gater12

  19. Offline

    iamcion

    the registerCommand
    is putting out some errors
     
  20. Offline

    Protophite

    lol this makes me laugh. please go learn how to code java before coding plugins.
     
  21. Offline

    iamcion

    please shut up, i'm getting there. -.-
     
  22. Offline

    Protophite

    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.
     
  23. Offline

    iamcion

    i'm learning.
    and I dont see any problem with them, i dont seem to be missing a semi-colon
     
  24. Offline

    scenariocreator

    Code:java
    1. registerCommand("nightvision on", new NightvisionCommand());
    2. 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
    1. if(args.length == 1)
    2. {
    3. if(args[0].equalsIgnoreCase("on")
    4. {
    5. //code
    6. }
    7. else if(args[0].equalsIgnoreCase("off")
    8. {
    9. //code
    10. {
    11. }
    12. else if(args.length == 2)
    13. {
    14. //code
    15. }


    Correct way to register commands:
    Code:java
    1. 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"
     
  25. Offline

    LordVakar

    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
     
  26. Offline

    Garris0n

    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.
     
    Protophite likes this.
  27. Offline

    TGRHavoc

    Code:java
    1. if(cmd.getname().equalsIgnoreCase("nightvision on"))
    2. player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 200, 1));
    3. //cooldown
    4. {
    5. if(player.hasPermission("vortex.nightvision"))
    6. player.sendMessage(ChatColor.AQUA + "NightVision Mode activated, type /nightvision off to turn nightvision off!")
    7. else
    8. player.sendMessage((new StringBuilder()).append(ChatColor.RED).append("You have no power here! Donate for power plz :D")).toString();
    9. return false;
    10. }
    11.  

    Hmmm I wonder what the problem with this code is.. Just in case you can't see it, let's "zoom" in..
    Code:java
    1. if(cmd.getname().equalsIgnoreCase("nightvision on"))
    2. player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 200, 1));
    3. //cooldown
    4. {
    5.  

    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!!
     
  28. Offline

    scenariocreator

    TGRHavoc Acually code like this:
    Code:java
    1. public void test()
    2. {
    3. if(permission == null)
    4. //code
    5. else
    6. //code
    7. {
    8. //code
    9. }
    10. }

    Is technically correct
    Code:java
    1. if(cmd.getname().equalsIgnoreCase("nightvision on"))
    2. 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
    1. if(cmd.getname().equalsIgnoreCase("nightvision on"))
    2. player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 200, 1));
    3. //cooldown
    4. {
    5. if(player.hasPermission("vortex.nightvision"))
    6. player.sendMessage(ChatColor.AQUA + "NightVision Mode activated, type /nightvision off to turn nightvision off!")
    7. else
    8. player.sendMessage((new StringBuilder()).append(ChatColor.RED).append("You have no power here! Donate for power plz :D")).toString();
    9. return false;
    10. }

    Or like this:
    Code:java
    1. if(cmd.getname().equalsIgnoreCase("nightvision on"))
    2. {
    3. player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 200, 1));
    4. //cooldown
    5. }
    6.  
    7. if(player.hasPermission("vortex.nightvision"))
    8. {
    9. player.sendMessage(ChatColor.AQUA + "NightVision Mode activated, type /nightvision off to turn nightvision off!")
    10. }else{
    11. player.sendMessage((new StringBuilder()).append(ChatColor.RED).append("You have no power here! Donate for power plz :D")).toString();
    12. return false;
    13. }

    The curly braces can be used to section off code to make it more readable to the user.
     
Thread Status:
Not open for further replies.

Share This Page