Commands in other classes.

Discussion in 'Plugin Development' started by frogman6102, Jun 30, 2014.

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

    frogman6102

    Hello!

    I've been having this issue and if I want to create better bigger plugins I really need to fix this.
    The tittle explains most of it but here's the issue, I want to create commands in other classes. THIS IS THE BASIC, I LEFT OUT ALOT AND JUST NEED TO KNOW HOW TO FIX THIS ERROR. Yes, I did do the extends JavaPlugin in the class with the onEnable() methods. Ignore the spelling errors.


    CODE:
    public void onEnable() {
    this.getCommand("command").setExecutor(new extraCommands()); // I get no errors here
    }

    extraCommands:

    (this class implements CommandExecutor)

    public void onCommand(CommandSender sender, Command command, String label, String args[]){
    if(command.getName().equalsIgnoreCase("command"){
    sender.sendMessage("works.");
    }else{}
    return false;
    }

    THE ERROR:
    Plugin Already Initialized.
    It says this when I run the server with the plugin installed.
     
  2. Offline

    xTigerRebornx

    frogman6102 the onCommand() method should have a return type of boolean, not void.
    Edit: Provide full code, not snippets. And a stacktrace
     
  3. Offline

    frogman6102

    xTigerRebornx
    Ok, yea, it was a typo on the void for a return type. Also, how do I copy and paste a stack trace on Windows?
    P.S: I'll get the original code tomorrow it's later here so I'm heading off to bed.
     
  4. Offline

    xTigerRebornx

    frogman6102 Either directly from the console, or open the log files and copy-paste the stacktrace from that.
     
  5. Offline

    DSCxSander

    If i use commands in a antoher class is use this in the main class:

    getCommand("yourcommand").setExecutor(new ClassName());
     
  6. Offline

    TGRHavoc

    I'm going to go out on a limb here and say that your "extraCommands" class extends JavaPlugin... If it is then remove it.

    DSCxSander
    He is...
     
  7. Offline

    frogman6102

    SECOND CLASS:

    Code:
    package me.frogman6102.EntityDecor;
     
     
     
     
    import org.bukkit.ChatColor;
    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.Player;
     
    public class EntityDecorCommands{
        EntityClass one = new EntityClass();
     
     
        public boolean onCommand(CommandSender p, Command command, String label, String args[]){
            if(command.getName().equalsIgnoreCase("spawnentity")){
                p.sendMessage("command work.");
            if(p instanceof Player){
                Player p1 = (Player) p;
                if(p1.hasPermission("entitydecor.spawnentity")){
                    if(args.length == 1){
                       
                        if(args[0].equalsIgnoreCase("diamond")){
                           
                            Location entitySpawnLoc = p1.getLocation();
                            World entitySpawnWorld = entitySpawnLoc.getWorld();
                            p1.sendMessage(ChatColor.GREEN + "You have now spawned a Entity for decoration!");
                entitySpawnWorld.dropItem(entitySpawnLoc, one.diamond);
                return true;
                           
                        }else{}
                    if(args[0].equalsIgnoreCase("emerald")){
                           
                            Location entitySpawnLoc = p1.getLocation();
                            World entitySpawnWorld = entitySpawnLoc.getWorld();
                            p1.sendMessage(ChatColor.GREEN + "You have now spawned a Entity for decoration!");
                entitySpawnWorld.dropItem(entitySpawnLoc, one.emerald);
                return true;
                           
                        }else{}
                  if(args[0].equalsIgnoreCase("iron")){
       
            Location entitySpawnLoc = p1.getLocation();
            World entitySpawnWorld = entitySpawnLoc.getWorld();
            p1.sendMessage(ChatColor.GREEN + "You have now spawned a Entity for decoration!");
            entitySpawnWorld.dropItem(entitySpawnLoc, one.iron);
            return true;
       
        }else{}
                  if(args[0].equalsIgnoreCase("gold")){
                       
                        Location entitySpawnLoc = p1.getLocation();
                        World entitySpawnWorld = entitySpawnLoc.getWorld();
                       
                        p1.sendMessage(ChatColor.GREEN + "You have now spawned a Entity for decoration!");
            entitySpawnWorld.dropItem(entitySpawnLoc, one.gold);
            return true;
                       
                    }else{}
                  if(args[0].equalsIgnoreCase("coal")){
                       
                        Location entitySpawnLoc = p1.getLocation();
                        World entitySpawnWorld = entitySpawnLoc.getWorld();
                        p1.sendMessage(ChatColor.GREEN + "You have now spawned a Entity for decoration!");
            entitySpawnWorld.dropItem(entitySpawnLoc, one.coal);
            return true;
                       
                    }else{}
           
                   
                       
                    }else{
                        p1.sendMessage(ChatColor.GREEN + "You must provide a entity type! These include: Diamond, Emerald, Iron Ingot, Gold Ingot, Coal, and adding more!");
                    }
                   
                   
                }else{
                    p1.sendMessage(ChatColor.GREEN + "You dont have permission!");
                }
               
            }else{
                p.sendMessage("You cannot do this from Console!");
            }
            }else{}
           
            return false;
        }
    }
    
    MAIN CLASS:


    Code:java
    1. package me.frogman6102.EntityDecor;
    2.  
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.inventory.meta.ItemMeta;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class EntityClass extends JavaPlugin {
    13.  
    14. ItemStack diamond;
    15. ItemMeta diamond_meta;
    16.  
    17. ItemStack iron;
    18. ItemMeta iron_meta;
    19.  
    20. ItemStack emerald;
    21. ItemMeta emerald_meta;
    22.  
    23. ItemStack gold;
    24. ItemMeta gold_meta;
    25.  
    26. ItemStack coal;
    27. ItemMeta coal_meta;
    28. public void onEnable(){
    29.  
    30. diamond = new ItemStack(Material.DIAMOND);
    31. iron = new ItemStack(Material.IRON_INGOT);
    32. gold = new ItemStack(Material.GOLD_INGOT);
    33. coal = new ItemStack(Material.COAL);
    34. emerald = new ItemStack(Material.EMERALD);
    35.  
    36.  
    37. diamond_meta = diamond.getItemMeta();
    38. iron_meta = iron.getItemMeta();
    39. gold_meta = gold.getItemMeta();
    40. emerald_meta = emerald.getItemMeta();
    41. coal_meta = coal.getItemMeta();
    42.  
    43. diamond_meta.setDisplayName(ChatColor.GREEN + "EntityDecor_diamond");
    44. iron_meta.setDisplayName(ChatColor.GREEN + "EntityDecor_iron");
    45. emerald_meta.setDisplayName(ChatColor.GREEN + "EntityDecor_emerald");
    46. gold_meta.setDisplayName(ChatColor.GREEN + "EntityDecor_gold");
    47. coal_meta.setDisplayName(ChatColor.GREEN + "EntityDecor_coal");
    48.  
    49.  
    50. diamond.setItemMeta(diamond_meta);
    51. iron.setItemMeta(iron_meta);
    52. emerald.setItemMeta(emerald_meta);
    53. gold.setItemMeta(gold_meta);
    54. coal.setItemMeta(coal_meta);
    55.  
    56. this.getCommand("spawnentity").setExecutor(new ExtraDecorCommands);
    57.  
    58. Bukkit.getServer().getPluginManager().registerEvents(new EntityDecorEvent(), this);
    59.  
    60. }
    61. public void onDisable(){
    62.  
    63. }
    64. }
    65.  


    This is the error: plugin already initialized.

    TGRHavoc DSCxSander xTigerRebornx
     
  8. Offline

    xTigerRebornx

    frogman6102 Don't make a new instance of your class that extends JavaPlugin, pass in an instance through the constructor or use the methods Bukkit provides to get a valid instance.
     
  9. Offline

    frogman6102

    xTigerRebornx
    So don't have the second class extend CommandExecutor?
     
  10. Offline

    xTigerRebornx

    frogman6102 Nowhere in my post did I say that. I said that you shouldn't make a new instance of your class that extends JavaPlugin. Where you got that, I don't know.
    That in mind, your commands class also doesn't implement CommandExecutor.
     
  11. Offline

    frogman6102

    My main class (with onEnable, etc) extends JavaPlugin, in my Commands class it implements CommandExecutor, no where else in my code do I extend JavaPlugin
     
  12. Offline

    xTigerRebornx

    frogman6102 I never said you extended JavaPlugin anywhere else in your code. I am saying that you are creating a new instance of your class that extends JavaPlugin. If you don't understand what I mean by this, go and learn Java.
     
  13. Offline

    frogman6102

    Yes, I do understand. So, your saying instead of puting
    getCommand("spawnentity").setExecutor(new EntityDecorCommands());

    put

    EntityDecorCommands second_class = new EntityDecorCommands();

    getCommand("spawnentity").setExecutor(second_class);
     
  14. Offline

    1Rogue


    He's saying get rid of this:


    By calling "new" you create a new, separate instance of your main class, thus having two main class instances. These are not the same class, you should instead pass your current instance upon constructing the new EntityDecorCommands class
     
  15. Offline

    frogman6102

    I don't create a new class instance in my commands class. In my main class I create a new instance of my Commands Class. If I get what your saying than I shouldn't create another instance of my commands class. Although, how do I do this without calling
    new EntityDecorCommands()
    when setting the executor? I tried setting the executor to the main class but that didn't help.
     
  16. Offline

    1Rogue


    So EntityClass isn't your main class? Because you create a new instance of it in your Commands class, and EntityClass extends JavaPlugin
     
  17. Offline

    frogman6102

    EntityClass is my main class. Were do I create a new instance of the main class in my commands class, I don't see it?
     
  18. Offline

    1Rogue

     
  19. Offline

    xTigerRebornx

    frogman6102
    Code:
    public class EntityDecorCommands{
        EntityClass one = new EntityClass(); // here
     
  20. Offline

    frogman6102

    Ooh! I found it. But, if I get rid of that then how will I be able to access varibles like ItemStacks in my commands class from my main class?

    Such as here,
    one.diamond
    How will I be able to access that variable of the diamond from the main class?

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

    Skye

    Make a constructor for your command class so that you can pass a reference of your plugin's main class to it.

    http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
     
  22. Offline

    frogman6102

    Just an idea, but could I possibly create a getting method like this?

    public ItemStack getItem(String name){
    if(name.equalsIgnoreCase("diamond"){
    return this.diamond;
    }else{}
    if(name.equalsIgnoreCase("emerald"){
    return this.emerald;
    }else{}
    // and so on
    }
    ?
    Skye

    And then call that Method somehow in my commands class?
    Wait, then I would have to create another instance of my main class...

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

    Skye

    You could, but you'll still need to get the class instance before calling its methods or it'll return null for any non-static values. If you're going to be adding a lot of those items, though, it may be wiser to use a HashMap to store your item stacks.
     
  24. Offline

    frogman6102

    So, I would have to make all the ItemStack varibles static correct?
    Skye
     
  25. Offline

    Skye

    That is one way, but resorting to static variables to sidestep this problem will stunt your understanding Java object instantiation and referencing.

    The plugin's main class itself is an Object; and like any other Object, it can be passed to newly instantiated Objects (like your command class) through their constructors.

    Ex.
    Code:java
    1. public class Main {
    2. private int value = 1337;
    3. private Cmd cmdObject = new Cmd(this);
    4.  
    5. public int getValue() {
    6. return value;
    7. }
    8. }
    9.  
    10. public class Cmd {
    11. private Main main;
    12.  
    13. public Cmd(Main instance) {
    14. main = instance;
    15. }
    16.  
    17. public void someMethod() {
    18. System.out.println(main.getValue());
    19. }
    20. }
     
  26. Offline

    frogman6102

    Ok, I'm going to watch some youtube videos on this. I am confused.
     
    Skye likes this.
  27. Offline

    Skye

    It is good to get a grasp on this before further plugin development. I would search for generic videos on Java constructors and object references, and not something Bukkit-specific. :)
     
  28. Offline

    ReggieMOL

Thread Status:
Not open for further replies.

Share This Page