Can't find what is wrong here....

Discussion in 'Plugin Development' started by aikoels, Jul 15, 2012.

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

    aikoels

    So I am new to java so I tried my hand at a very simple plugin

    I had some trolls on a server and I decided to make a plugin where when you type "/planetminecraft" It does 3 things:
    1. Smites them
    2. Says to them "Do Not Troll Please." In red letters
    3. Announces to the whole server "name thinks he is from planet minecraft!" using the /say command, I have not started the code for that part, but I know it, all except for how to make it say it in the server chat, so I know how to get someones name then say it in red, I just don't know how to have it announce to the server.

    Sorry if my coding is bad, this is only my first time.

    Console Error:
    Code:
    02:45:39 [SEVERE] Could not load 'plugins/PlanetMinecraft.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.ClassCastException: class com.github.aikoels.PlanetMinecraft.PlanetMinecraft
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:151)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:213)
        at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:189)
        at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:53)
        at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:166)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:432)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.ClassCastException: class com.github.aikoels.PlanetMinecraft.PlanetMinecraft
        at java.lang.Class.asSubclass(Class.java:3018)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:141)
        ... 8 more
    
    My Java:
    Code:
    package com.github.aikoels.PlanetMinecraft;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Server;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    public class PlanetMinecraft{
     
        public PlanetMinecraft (PlanetMinecraft plugin) {}
     
        public boolean onCommand (CommandSender sender, Command cmd, String commandLabel, String[] args, org.bukkit.Location location) {
              Player player = (Player) sender;
              World world = player.getWorld();
              if(commandLabel.equalsIgnoreCase("planetminecraft"))
                  if(args.length == 0) {
                      Player targetplayer = player.getServer().getPlayer(args[0]);
                      location = player.getLocation();
                      world.strikeLightning(location);
                      player.sendMessage(ChatColor.RED + "Please, Do Not Troll.");
                 
                  } else if(args.length > 0) {
                      Player targetplayer = player.getServer().getPlayer(args[0]);
                      location = player.getLocation();
                      world.strikeLightning(location);
                      player.sendMessage(ChatColor.RED + "Please, Do Not Troll.");
                }
              return false;
        }
    }
     
     
    
    My plugin.yml:
    Code:
    name: PlanetMinecraft
    main: com.github.aikoels.PlanetMinecraft.PlanetMinecraft
    version: 1.0
     
    commands:
      planetminecraft:
          description: This kills the player and announces to the server.
          usage:
          permission: PlanetMinecraft.PlanetMinecraft
          permission-message: Awwww you can't be from Planet Minecraft because you don't have permission to do /planetminecraft :(
     
  2. Offline

    Kodfod

    you need to extend JavaPlugin
     
  3. Offline

    Lolmewn

    You shouldn't do Player player = (Player) sender ;
    if you don't know if the sender is a player. It could also be the console, giving you a big error afterwards ;)
    Check if it is a player with
    Code:
    if(sender instanceof Player){
        Player p = (Player) sender;
    }
     
  4. Offline

    aikoels

    This what you mean?

    Code:
    package com.github.aikoels.PlanetMinecraft;
     
    import org.bukkit.ChatColor;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class PlanetMinecraft extends JavaPlugin {
     
        public PlanetMinecraft (PlanetMinecraft plugin) {}
       
        public boolean onCommand (CommandSender sender, Command cmd, String commandLabel, String[] args, org.bukkit.Location location) {
              World world = player.getWorld();
              if(commandLabel.equalsIgnoreCase("planetminecraft"))
                  if(sender instanceof Player){
                      Player p = (Player) sender;
                      location = player.getLocation();
                      world.strikeLightning(location);
                      player.sendMessage(ChatColor.RED + "Please, Do Not Troll.");
                  }
              return false;
        }
    }
     
        
     
  5. Offline

    Lolmewn

    aikoels almost.
    There's no way to get the world if you don't have the player variable ;)
     
  6. Offline

    aikoels

    So where should I put the player variable? And thanks for all your help.
     
  7. Offline

    Lolmewn

    Basically, this is the way to go.
    1. Check if the command being issued is the command you want to work with. You did this already, by checking the commandlabel.
    2. Check if the sender is a player. If not, tell him only players can do that.
    3. Check permissions. You can, but don't have to, create a Player object first. Just do sender.hasPermission("your.node"); I don't think you need this right now.
    4. Since you need it, cast the sender to the player object. You did this too. After having the player in a variable you can just p.getWorld().strikeLightning(p.getLocation()); to let lightning strike.
    That's all you gotta do I think :)
     
  8. Offline

    aikoels

    Sorry again, but I think I did everything you said, eclipse is showing no warnings/errors, however I still can't run the plugin, here is my code now:
    Code:
    package com.github.aikoels.PlanetMinecraft;
     
    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 PlanetMinecraft extends JavaPlugin {
     
        public PlanetMinecraft (PlanetMinecraft plugin) {}
       
        public boolean onCommand (CommandSender sender, Command cmd, String commandLabel, String[] args, org.bukkit.Location location) {
              Player p = (Player) sender;
              if(commandLabel.equalsIgnoreCase("planetminecraft"))
                  if(sender instanceof Player){
                      sender.hasPermission("PlanetMinecraft.PlanetMinecraft");
                      location = p.getLocation();
                      p.getWorld().strikeLightning(p.getLocation());
                      p.sendMessage(ChatColor.RED + "Please, Do Not Troll.");
                  } else {
                      p.sendMessage(ChatColor.RED + "Sorry, you must be a player to do this command.");
                  }
              return false;
        }
    }
     
       
    
     
  9. You most likely because you changed the main class' constructor, which you shouldn't have.
    So remove:
    public PlanetMinecraft (PlanetMinecraft plugin) {}

    And use public void onEnable() { ... } if you want to add some stuff when the plugin enables.
     
  10. Offline

    aikoels

    I did that... but it gave me so many errors to fix, it said I even had to change the "(" and ")" to ";" in the
    Code:
    public boolean onCommand (CommandSender sender, Command cmd, String commandLabel, String[] args, org.bukkit.Location location) {
    it also says I have to delete the "args" in it... I know that isn't right, so should I do what you say and just leave the errors?
     
  11. Offline

    MrDent009

    Your plugin.yml is corrupt too...

    usage: /<command> <player>
    ..............
     
  12. Offline

    aikoels

    I actually deleted that because in a pm on a server someone said only do that if it had the player to do it to, for example if I wanted to do /planetminecraft player then set that up, then he said if not, keep it blank.
     
  13. Just deleting that line won't make anything require you to replace () with ; ... you did something else wrong, the code is valid without that line.

    And you also have invalid arguments for onCommand(), there is no Location object in the command arguments...
    So your onCommand() will not trigger because it's basically a diferent method if it has diferent arguments.

    To let Eclipse (if using) generate the propper overwriteable method, just click some empty space inside the class (not inside a method!) and press ctrl+space and select onCommand from that list, it will generate it with the propper arguments, use that and don't modify the arguments, only the contents, remove the super() or whatever was generated in extra.

    But just to be sure you have the correct arguments, here it is:
    Code:
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
     
  14. Offline

    aikoels

    Sorry, but doing that only gives me errors, here are my errors:
    Illegal modifier for parameter onCommand; only final is permitted- only way to fix that is to delete the public infront of the boolean
    Syntax error on token ",", ; expectedThat refers to the "," in between sender, Command.
    Syntax error on token ",", ; expectedThat refers to the "," in between command, String.
    Syntax error on token "(", ; expectedRefers to the "(" infront of CommandSender.
    Syntax error, insert ";" to complete LocalVariableDeclarationStatement That refers to the ] right after String[ it wants me to put a ; after it, but then it tells me to delete the "args" part.

    Here is my current java, but what you are saying seems to not help it :/

    Code:
    package com.github.aikoels.PlanetMinecraft;
     
    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 PlanetMinecraft extends JavaPlugin {
     
        public void onEnable() {
       
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
              Player p = (Player) sender;
              if(commandLabel.equalsIgnoreCase("planetminecraft"))
                  if(sender instanceof Player){
                      sender.hasPermission("PlanetMinecraft.PlanetMinecraft");
                      location = p.getLocation();
                      p.getWorld().strikeLightning(p.getLocation());
                      p.sendMessage(ChatColor.RED + "Please, Do Not Troll.");
                  } else {
                      p.sendMessage(ChatColor.RED + "Sorry, you must be a player to do this command.");
                  }
              return false;
        }
    }
     
    }
       
    
     
  15. You added:
    Code:
    public void onEnable() {
    which made it trigger those syntax errors because you opened a bracked and didn't close it.

    I only told you to use onEnable() if you needed something triggered when plugin enables, if you're just going to keep it empty, don't write it...
     
  16. Offline

    aikoels

    Did some more work on it, thanks for all your help

    Code:
    package com.github.aikoels.PlanetMinecraft;
     
    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;
     
        class PlanetMinecraft extends JavaPlugin {
     
        public void onEnable() {
            getLogger().info("PlanetMinecraft has been enabled.");
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
              Player p = (Player) sender;
              if(sender instanceof Player){
                  if(commandLabel.equalsIgnoreCase("planetminecraft"))
                      sender.hasPermission("PlanetMinecraft.PlanetMinecraft");
                      location = p.getLocation();
                      p.getWorld().strikeLightning(p.getLocation());
                      p.sendMessage(ChatColor.RED + "Please, Do Not Troll.");
                      return true;
                   
              } else if (commandLabel.equalsIgnoreCase("basic2")) {
                      if (p == null) {
                    sender.sendMessage("This command can only be used by a player!");
                   
              return false;
        }
      }
    }
       
        public void onDisable(){
            getLogger().info("PlanetMinecraft has been disabled.");
        }
        }
    
     
  17. The enabled and disabled messages are already sent by Bukkit when it loads the plugin, you're just spamming with extra messages there :p
     
    ferrybig and aikoels like this.
  18. Offline

    aikoels

    Lol, apparently I am using some out dated tutorials then :p

    Here is my new code, still doesn't work though :/

    Code:
    package com.github.aikoels.PlanetMinecraft;
     
    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;
     
        class PlanetMinecraft extends JavaPlugin {
     
        public void onEnable() {}
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
              Player p = (Player) sender;
              if(sender instanceof Player){
                  if(commandLabel.equalsIgnoreCase("planetminecraft"))
                      sender.hasPermission("PlanetMinecraft.PlanetMinecraft");
                      Player target = p.getServer().getPlayer(args[0]);
                      target.setHealth(0);
                      p.getWorld().strikeLightning(p.getLocation());
                      p.sendMessage(ChatColor.RED + "Please, Do Not Troll.");
                      return true;
               
              } else if (commandLabel.equalsIgnoreCase("planetminecraft")) {
                      if (p == null) {
                    sender.sendMessage("This command can only be used by a player!");
               
              return false;
        }
      }
            return false;
    }
     
        public void onDisable(){}
        }
     
     
     
     
     
    
    Console errors:

    Code:
    22:30:03 [SEVERE] Could not load 'plugins/PlanetMinecraft.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NoSuchMethodException: com.github.aikoels.PlanetMinecraft.PlanetMinecraft.<init>()
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:151)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:213)
        at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:189)
        at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:53)
        at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:166)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:432)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NoSuchMethodException: com.github.aikoels.PlanetMinecraft.PlanetMinecraft.<init>()
        at java.lang.Class.getConstructor0(Class.java:2706)
        at java.lang.Class.getConstructor(Class.java:1657)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:143)
        ... 8 more
    
     
  19. Your class is not public.
    class PlanetMinecraft extends JavaPlugin
    ->
    public class PlanetMinecraft extends JavaPlugin

    It was public before, I don't know why you removed that.

    Also, use Ctrl+i to indent the code, it's a mess :}
     
  20. Offline

    aikoels

    Hmm, now it loads up :D but I get internal error occurred when using the command :/

    Code:
    package com.github.aikoels.PlanetMinecraft;
     
    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 PlanetMinecraft extends JavaPlugin {
     
        public void onEnable() {}
     
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                  Player p = (Player) sender;
                if(sender instanceof Player){
                  if(commandLabel.equalsIgnoreCase("planetminecraft"))
                      sender.hasPermission("PlanetMinecraft.PlanetMinecraft");
                      Player target = p.getServer().getPlayer(args[0]);
                      target.setHealth(0);
                      p.getWorld().strikeLightning(p.getLocation());
                      p.sendMessage(ChatColor.RED + "Please, Do Not Troll.");
                      return true;
                 
              } else if (commandLabel.equalsIgnoreCase("planetminecraft")) {
                      if (p == null) {
                    sender.sendMessage("This command can only be used by a player!");
                 
              return false;
                      }
              }
                return false;
            }
     
        public void onDisable(){}
    }
     
     
     
    
    Console error when used in console:
    Code:
    22:44:56 [WARNING] Unexpected exception while parsing console command
    org.bukkit.command.CommandException: Unhandled exception executing command 'planetminecraft' in plugin PlanetMinecraft v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:479)
        at org.bukkit.craftbukkit.CraftServer.dispatchServerCommand(CraftServer.java:475)
        at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:612)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:581)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.command.ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
        at com.github.aikoels.PlanetMinecraft.PlanetMinecraft.onCommand(PlanetMinecraft.java:14)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 7 more
    
    Console error when used in game:
    Code:
    22:45:58 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'planetminecraft' in plugin PlanetMinecraft v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:479)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:821)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:781)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:34)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
        at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:567)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
        at com.github.aikoels.PlanetMinecraft.PlanetMinecraft.onCommand(PlanetMinecraft.java:18)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 12 more
    
     
  21. The console error is expected because you're blindly casting sender to Player when it can also be a ConsoleSender, which is obviously NOT a player.
    Read about that at tip 1: http://forums.bukkit.org/threads/how-to-make-your-plugin-better.77899/

    But the 2nd error is because you used an index in an array that doesn't have that index... the 0 index in your case, that's the first element, which means the array is absolutely empty.
    You're using args[0] without checking if the command has that argument, by using if(args.length > 0) you know you can use the index 0 afterwards.
     
  22. Offline

    aikoels

    would (args.length == 0) work?
     
  23. If you want to check if there are no arguments, yeah... but (args.length == 1) means it has 1 argument and you can use args[0].
     
  24. Offline

    aikoels

    ok, but when I put in "cs.sendMessage()" for the send message it says I have to create a variable "cs" I probably already have the variable under a different name, but what is it? I thought it may be player, but I already have the p. before I paste that in.
     
  25. "sender" ... ?

    sender.sendMessage();
     
  26. Offline

    aikoels

    ok, did all that you say, but I still get the errors but only when in console, in game it is fine
    However, it says I have to put in "/planetminecraft name" is there a way to from here make it so that I can just do /planetminecraft and it smites me?
    java:
    Code:
    package com.github.aikoels.PlanetMinecraft;
     
    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 PlanetMinecraft extends JavaPlugin {
     
        public void onEnable() {}
     
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                  Player p = (Player) sender;
                  if (args.length == 1)
                if(sender instanceof Player){
                  if(commandLabel.equalsIgnoreCase("planetminecraft"))
                      sender.hasPermission("PlanetMinecraft.PlanetMinecraft");
                      Player target = p.getServer().getPlayer(args[0]);
                      target.setHealth(0);
                      p.getWorld().strikeLightning(p.getLocation());
                      p.sendMessage(ChatColor.RED + "Please, Do Not Troll.");
                      return true;
                 
              } else if (commandLabel.equalsIgnoreCase("planetminecraft")) {
                      if (p == null) {
                      sender.sendMessage("This command can only be used by a player!");
                 
              return false;
                      }
              }
                return false;
            }
     
        public void onDisable(){}
        }
     
     
     
    
     
  27. Offline

    Lolmewn

    You're still blindly casting objects.
    Basically, this is what happens.
    The console issues a command, your command.
    You do Player p = (Player) sender;
    Java has no clue what's going on, because the console is not a player. --> Error!
    That's why you should check if it is a player first, before creating the Player object.

    Take a look at this code. It *should* work.
    Code:java
    1.  
    2. @Override
    3. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    4. if(label.equalsIgnoreCase("planetminecraft")){
    5. if(args.length == 0){
    6. //Only the command itself is being done, no arguments.
    7. if(sender instanceof Player){
    8. Player p = (Player)sender;
    9. p.getWorld().strikeLightning(p.getLocation());
    10. p.sendMessage("Please, no trolling.");
    11. return true;
    12. }else{
    13. sender.sendMessage("As a console, you can't issue commands!");
    14. return true;
    15. }
    16. }else{
    17. //Command is like this: /planetminecraft {argument}
    18. //Possibly, there are more arguments. We only need one anyway.
    19. Player target = this.getServer().getPlayer(args[0]);
    20. target.getWorld().strikeLightning(target.getLocation());
    21. //some more stuff here
    22. return true;
    23. }
    24. }
    25. return false;
    26. }
     
  28. Offline

    aikoels

    Thanks to everyone for all the help, it is now fully working, I plan to make it announce the players death later, but for now this is amazing.
     
Thread Status:
Not open for further replies.

Share This Page