Solved only the main plugin file works?

Discussion in 'Plugin Development' started by Meeyt, Aug 5, 2014.

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

    Meeyt

    ok, so, heres the deal, i made a plugin and made all these commands, but in seperate classes, and so i load up my new plugin everything looks fine, except only one of the commands work, the command in the main plugin file. i tried putting the commands in the same file but i have no idea how to do that. what does the main plugin file even mean? anyway, if you could help the code is
    ===================
    package dt;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;

    public final class dt extends JavaPlugin {

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if(cmd.getName().equalsIgnoreCase("yellow")) {
    if(args.length > 0){ String message = sender.getName();
    for(String desc : args){message = desc;}
    getServer().broadcastMessage(ChatColor.YELLOW+message);
    return true;
    }
    } else {
    sender.sendMessage(ChatColor.RED + "You must put text after the command. usage: /yellow <text>");
    }
    return false;
    }

    }
    =====================
    and the others are essentially the same, with only the command name and color different,
    the plugin yml is good, it loads all the command info but the command doesnt actually work,
     
  2. Offline

    thijs_a

    first, when you post an part of code you must use the code syntax.
    In an different class than the main class you mustn't use
    Code:java
    1. extends JavaPlugin

    To load commands in an other class you must put this in you onEnable in the main class:
    Code:java
    1. getCommand("command").setExecutor(class);

    I hope i didn't make a misspell i'm on my phone
     
  3. Offline

    Meeyt

    yes, ive done that and it all looks good, but the "setExecuter(class);' thing isnt working, idk really what it is meant to be.
    and also, when i remove the "extends JavaPlugin" thing, getServer() becomes undefined?
     
  4. Offline

    thijs_a

    Can you post your whole main class?

    EDIT: or all your classes :p?
     
  5. Offline

    Meeyt

    thijs_a yes, the getserver thing is fixed, but i still cant figure out setExecutor, ill give you my main class with the onEnable and such and one of the other classes, the other classes are all duplicates except for a few minor details.

    Main Class
    Code:java
    1. package dt;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandExecutor;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public final class dt extends JavaPlugin {
    10.  
    11. @Override
    12. public void onEnable(){
    13. getLogger().info("onEnable has been invoked!");
    14. getCommand("mey").setExecutor(dt.DTmey);
    15. }
    16.  
    17.  
    18. @Override
    19. public void onDisable(){
    20. getLogger().info("onDisable has been invoked");
    21. }
    22.  
    23. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    24. if(cmd.getName().equalsIgnoreCase("yellow")) {
    25. if(args.length > 0){ String message = sender.getName();
    26. for(String desc : args){message = desc;}
    27. getServer().broadcastMessage(ChatColor.YELLOW+message);
    28. return true;
    29. }
    30. } else {
    31. sender.sendMessage(ChatColor.RED + "You must put text after the command. usage: /yellow <text>");
    32. }
    33. return false;
    34. }
    35.  
    36.  
    37.  
    38.  
    39. }
    40.  


    Other Class
    Code:java
    1. package dt;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8.  
    9. public final class DTmey implements CommandExecutor {
    10.  
    11. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    12. if(cmd.getName().equalsIgnoreCase("mey")) {
    13. if(args.length > 0){ String message = sender.getName();
    14. for(String string : args){message = message+" "+string;}
    15. Bukkit.getServer().broadcastMessage(ChatColor.YELLOW+message);
    16. return true;
    17. }
    18. } else {
    19. sender.sendMessage(ChatColor.RED + "You must put a description after the command. usage: /mey <desc>");
    20. }
    21. return false; }
    22.  
    23.  
    24. }
    25.  
    26.  
     
  6. Offline

    kurtv

    Meeyt you forgot the constructor on DTmay
     
  7. Offline

    Meeyt

    kurtv what do i need to put in?
     
  8. Offline

    thijs_a

    Does
    Code:java
    1. getCommand("mey").setExecutor(dt.DTmey);

    give errors?
     
  9. Offline

    kurtv

    Meeyt
    in DTmay put this in the begining


    dt plugin;

    public DTmay (dt plugin){
    this.plugin = plugin
    }

    dont forget the indentation xD

    Meeyt and when you set the executor you have to put getCommand("mey").setExecutor(new DTmey(this))

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  10. Offline

    Meeyt

    ok thanks, it looks like it is working now

    kurtv thijs_a actually, there seems to be an error with my plugin.yml and i dont know what, time to analyse the console error messages :D and probably fail. :D

    nope, i got it working again, but now one of my commands isnt working too well, it only shows the last argument when it is supposed to show them all.

    EDIT ok, now it is working, kinda
    it shows all the arguments,
    but on different lines, ill send some code in my next message

    ok, so heres the deal.

    Code:
    package dt;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class dt extends JavaPlugin { 
     
        @Override
        public void onEnable(){
            getLogger().info("onEnable has been invoked!");
            getCommand("mey").setExecutor(new DTmey(this));
            getCommand("mer").setExecutor(new DTmer(this));
            }
         
         
        @Override
        public void onDisable(){
            getLogger().info("onDisable has been invoked");
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("yellow")) {
            if(args.length > 0){ String message = "error";
        for(String string : args) {message = string;
     
        Bukkit.getServer().broadcastMessage(ChatColor.YELLOW+message);
        }
            return true;
            }
            } else {
            sender.sendMessage(ChatColor.RED + "You must put a description after the command. usage: /mey <desc>");
        }
            return false; }
         
     
        }
    
    soo, yeah

    EDIT:
    i think i had this problem before, but i fixed it, but now i forgot how i fixed it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  11. Offline

    Cloaking_Ocean

    ok so what you're doing is looping through all the arguments and printing them out. what you need to do is build a string together.

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if(cmd.getName().equalsIgnoreCase("yellow")) {
    if(args.length > 0){ String message = "error";

    String s = "";
    for(String string : args) {
    s = s + args + " ";

    }
    s = s.trim();
    Bukkit.getServer().broadcastMessage(ChatColor.YELLOW+s);

    return true;
    }
    } else {
    sender.sendMessage(ChatColor.RED + "You must put a description after the command. usage: /mey <desc>");
    }
    return false; }


    }


    Srry for the weird spacing i'm not the best at pasting code.
    The main idea is to make a string and add on including spaces. and then after it's combined u can trim it if u want then broadcast it.
     
  12. Offline

    kurtv

    Meeyt can you tell me the exact problem xD
     
  13. Offline

    fireblast709

    Cloaking_Ocean ew concatenation.
    Code:java
    1. String s = Joiner.on(" ").join(args);
    Gotta love Google Guava (shaded in Bukkit, so no real reason not to use it)
     
  14. Offline

    Meeyt

    fireblast709 kurtv Cloaking_Ocean
    ok i got it working, i think, i havent actually tested it yet but before i got java errors in the message and i essentially made a password generator, lets hope this works

    ahhrg, ive made it work on the same line with no error messages, but now spaces dont appear in the actually message.

    EDIT:
    nvm i fixed it

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  15. Offline

    xize

    Why is the class final?, try without that.

    setExecutor() is not needed for the command since the command method is intherithed in JavaPlugin, only use setExecutor when using commands in other classes.

    now if you test it do you still got any errors?, and if so could you please show the stacktrace?
     
Thread Status:
Not open for further replies.

Share This Page