Solved Plugin.yml help with multiple classes

Discussion in 'Plugin Development' started by The_BloodHound, Aug 25, 2015.

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

    The_BloodHound

    Hey there, I'm creating a plugin that has multiple classes for each part of the plugin. They all have a command in them and for the plugin.yml, what do I put as the main? I can do the commands that are found in the main class but not the others. Is there a way to have multiple?
     
  2. Offline

    CrystallFTW

    As the main class you should put the class that extends JavaPlugin.
     
  3. Offline

    mine-care

  4. Offline

    The_BloodHound

  5. Offline

    MCMatters

    @The_BloodHound you can't extend javaplugin from two classes. Learn bukkit api.
     
  6. Zombie_Striker likes this.
  7. Offline

    MCMatters

    code please
     
  8. Offline

    Zombie_Striker

    @The_BloodHound
    You can't have two main Classes. If you want to have two main classes, then you wont have any main classes.

    Create one class that extends JavaPlugin, which will be the main, and then make smaller sub-classes that control smaller aspects (commands, events, utils, ect.)
     
  9. Offline

    teej107

  10. Offline

    MCMatters

  11. Offline

    teej107

  12. Offline

    The_BloodHound

    I get that there can only be one but for some reason I get an error on two certain classes when I remove JavaPlugin.

    And no need to be rude @MCMatters . That's why I'm here asking questions.

    EDIT: Never mind I figured out the problem.
     
  13. Offline

    Zombie_Striker

  14. Offline

    boomboompower

  15. Offline

    MCMatters

    @The_BloodHound I'm not being rude, i am asking you to learn Bukkit API.
     
    Zombie_Striker likes this.
  16. @The_BloodHound
    You can also implement CommandExecutor on your commands.
     
  17. Offline

    The_BloodHound

    Sorry for the late response but this isn't solved yet. I had the JavaPlugin extending problem fixed but anything in other classes are not working at all. I think I'm doing something terribly wrong.
     
  18. Offline

    CrystallFTW

  19. Offline

    teej107

    meguy26 likes this.
  20. Offline

    meguy26

    Unfortunately, "I'm doing something terribly wrong" is not the best error report you could give, post the code with comments where errors occur.
     
  21. Offline

    The_BloodHound

    I have 4 classes. I blanked out the spaces that are semi-confidential and that are just space consuming. I know for a fact that what I blanked out are not the problem.
    Code:
    package me.The_BloodHound;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Application extends JavaPlugin {
    
        public static Application instance;
    
        public void onEnable() {
            instance = this;
            Bukkit.getLogger().info("Applications are working.");
        }
    
        public void onDisable() {
            instance = null;
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            final Player p = (Player) sender;
    
            if (cmd.getName().equalsIgnoreCase("apply")) {
                p.teleport(new Location(Bukkit.getWorld("SDR"), -287.0, 5.1, -414.5));
                p.sendMessage(ChatColor.AQUA + "Welcome to the application center.");
                Bukkit.getScheduler().scheduleSyncDelayedTask(Application.instance,
                        new Runnable() {
    
                            public void run() {
                                p.sendMessage(ChatColor.AQUA
                                        + "You will be given a book and you will have to fill your application on that.");
                            }
                        }, 20 * 1);
                //There are more un-needed application info that I removed to conserve your time. They are all the same as the one above basically.
            }
            return true;
        }
    }
    
    This first one works completely fine.
    Code:
    package me.The_BloodHound;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.permissions.Permission;
    
    public class ClearChat {
    
        Permission cc = new Permission("chat.clear");
    
        public void onEnable() {
            Bukkit.getLogger().info("Clear Chat has been enabled.");
        }
    
        public void onDisable() {
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            Player p = (Player) sender;
    
            if (cmd.getName().equalsIgnoreCase("cl")) {
                if (p.hasPermission("chat.clear") || p.isOp()) {
                    p.getServer()
                            .broadcastMessage(
                                    ChatColor.AQUA
                                            + "                                                                               "
                                            + "Chat has been cleared for your safety.                                         ");
                }
            } else {
                p.sendMessage(ChatColor.RED
                        + "You don't have permission for this command!");
            }
    
            if (cmd.getName().equalsIgnoreCase("cp")) {
                if (p.hasPermission("chat.clear") || p.isOp()) {
                    if (p.getServer().getPlayer(args[0]).isOnline()) {
                        p.getServer()
                                .getPlayer(args[0])
                                .sendMessage(
                                        ChatColor.AQUA + "                                         "
                                                + "Chat has been cleared for your safety.                                         ");
                        p.sendMessage(ChatColor.YELLOW + "Player's chat cleared.");
                    } else {
                        p.sendMessage(ChatColor.RED + "Could not find player!");
                    }
                } else {
                    p.sendMessage(ChatColor.RED
                            + "You don't have permission for this command!");
                }
            }
            return true;
        }
    }
    
    This second one is for chat clearing. It used to work completely fine in a stand alone plugin but no longer works in this one. No errors.
    Code:
    package me.The_BloodHound;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.permissions.Permission;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class NightVision {
    
        Permission nightv = new Permission("night.vision");
    
        public void onEnable() {
            Bukkit.getLogger().info("Night Vision is working!");
        }
    
        public void onDisable() {
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            Player p = (Player) sender;
    
            if (cmd.getName().equalsIgnoreCase("nvon")) {
                if (p.hasPermission("night.vision")) {
                    PotionEffectType nv = PotionEffectType.NIGHT_VISION;
                    PotionEffect nightvisione = nv.createEffect(999999, 1);
                    p.addPotionEffect(nightvisione);
                }
            }
    
            if (cmd.getName().equalsIgnoreCase("nvoff")) {
                if(p.hasPermission("night.vision")) {
                    p.removePotionEffect(PotionEffectType.NIGHT_VISION);
                }
            }
            return true;
        }
    
    }
    
    This third one, it used to work perfectly fine as a stand alone plugin but no longer works. No errors. However, I was still working on /nvoff so that's a work in progress.
    Code:
    package me.The_BloodHound;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.Plugin;
    
    public class Spawning implements Listener {
       
        public void onEnable(){
            Bukkit.getLogger().info("Plugin enabled");
            Bukkit.getServer().getPluginManager().registerEvents(this, (Plugin) this);
        }
       
        public void onDisable(){
            Bukkit.getLogger().info("i cri evry tiem");
        }
       
        @EventHandler
            public void onPlayerJoin(PlayerJoinEvent e){
            Player p = e.getPlayer();
            p.performCommand("warp Spawn");
            }
       
        public boolean onCommand(CommandSender Sender, Command cmd, String label,
                String[] args) {
            Player player = (Player) Sender;
            if (cmd.getName().equalsIgnoreCase("hub") && Sender instanceof Player){
                player.performCommand("warp Spawn");
                player.sendMessage(ChatColor.GREEN + "Now teleporting to spawn.");
            }
            return true;
        }
    }
    
    This last one, again doesn't work in the new plugin. No errors.

    There are no errors at all. It recognizes it as a command from the plugin.yml but I think it doesn't know where the code is for the command.
     
  22. Offline

    mrgreen33gamer

    @The_BloodHound

    You can just create an entire class dedicated to all your commands, and register them from the onEnable method.

    Code:java
    1.  
    2. @Override
    3. public void onEnable(){
    4. getCommand("your_command").setExecutor(new YourCommandClass());
    5. }
    6.  


    But as @teej107 said, you should really learn your Java basics and the API you're using.

    If I'm not getting the post concept then I guess the joke is on me xD!
     
  23. Offline

    The_BloodHound

    Bump
    And I'm trying to keep my plugin organized and keeping the commands in separate classes. I've seen it done in some plugins and it's not working with me.
     
  24. Offline

    mythbusterma

    @The_BloodHound

    The point being that you create a class that serves to manage all your other command classes.
     
  25. Offline

    sam_0208

    @The_BloodHound
    If you want to have the commands in different classes you need to implement CommandExecutor in every class that has a command. In your onEnable you have to set the executor for the command like this:

    Code:
    getCommand("command1).setExecutor(new ClassWithCommand1())
    getCommand("command2).setExecutor(new ClassWithCommand2())
     
    Last edited: Sep 3, 2015
  26. @The_BloodHound Remove the onEnable and onDisable from all classes but the main then register the commands/listeners in the main onEnable
     
    The_BloodHound and mine-care like this.
  27. Offline

    mine-care

    @sam_0208 please follow Java naming conventions
     
    Last edited by a moderator: Sep 2, 2015
    sam_0208 and bwfcwalshy like this.
  28. Offline

    sam_0208

  29. Offline

    mine-care

    @sam_0208 Its ok :- ) Please edit the post to fix them at least now.
    #ItsNeverTooLate!
     
  30. Offline

    The_BloodHound

    Yay! My problem has been fixed! It took a while and many misunderstandings but now I know how to do this in the future. Thanks for all the help <3
     
Thread Status:
Not open for further replies.

Share This Page