Solved Make CMD to listen to config

Discussion in 'Plugin Development' started by yPedx, Mar 23, 2017.

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

    yPedx

    I just cannot get this to work.. I don't know how to either. I tried to look up how people actually did it, but how do I make a command get it's message from the config?

    The command class:​
    Code:
    package me.developer.kriss.commands;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import me.developer.kriss.Main;
    
    public class CMDApply implements CommandExecutor {
    
        public CMDApply (Main pl) {
        }
      
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("apply")) {
                    Player player = (Player) sender;
                  
                    String applyMessage = plugin.getConfig().getString("Apply Message").replaceAll("&", "§");
                    player.sendMessage(applyMessage);
            }
            return true;
        }
        }

    Main class:​
    Code:
    package me.developer.kriss;
    
    import java.util.logging.Logger;
    
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.developer.kriss.commands.CMDApply;
    import me.developer.kriss.commands.CMDForums;
    import me.developer.kriss.commands.CMDRules;
    import me.developer.kriss.commands.CMDVote;
    import me.developer.kriss.commands.CMDWebsite;
    import me.developer.kriss.event.player.EVENTJoin;
    
    public class Main extends JavaPlugin {
      
        CMDApply cmdapp = new CMDApply(this);
        CMDApply cmdapply = new CMDApply(this);
      
        CMDForums cmdfor = new CMDForums(this);
        CMDForums cmdforums = new CMDForums(this);
      
        CMDWebsite cmdweb = new CMDWebsite(this);
        CMDWebsite cmdwebsite = new CMDWebsite(this);
      
        public void onEnable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();
            logger.info(pdfFile.getName() + " has been enabled!");
          
            registerEvents();
            registerConfig();
          
            getCommand("vote").setExecutor(new CMDVote());
            getCommand("rules").setExecutor(new CMDRules());
          
        }
    
        public void onDisable() {
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = getLogger();
            logger.info(pdfFile.getName() + " has been disabled!");
        }
          
            private void registerEvents() {
                PluginManager pm = getServer().getPluginManager();
              
                pm.registerEvents(new EVENTJoin (this), this);
        }
            private void registerConfig() {
                getConfig().options().copyDefaults(true);
                saveConfig();
    }
    }
    Config.yml:​
    Code:
    # CMDS by yPedx
    # Version 2.3
    # --------------
    
    Apply Message: '&eApply &7❱❱ goo.gl/eZbTdD'
    
    Forums Message: '&eForums &7❱❱ &cNo forums yet, sorry :('
    
    Website Message: '&eWebsite &7❱❱ &cSuchPvPMC.buycraft.net'
     
  2. You need to create a variable to store the Main class instance in the CMDApply class, so you can access it inside onCommand for example.
    In CMDApply's constructor, set that variable to the parameter pl (in onCommand you passed 'this' (meaning the current class instance) to the constructor)

    example:
    Code:
    MainClass main;
    
    public OtherClass(MainClass main) {
    this.main = main;
    }
    
    private someOtherMethod() {
    main.blabla();
    }
     
  3. Offline

    mine-care

    Ouch! Please no! Don't watch TheBcBroz... There are certain bits in their code that are 1. outdated/wrong and 2. ready to cause trouble...
    Aha Typical BcBroz, well you don't need that
    or that, because bukkit does it for you already...
    Also no need to create methods for such short pieces of code...

    A
    Aha! another BcBroz technique! Do not cast 'sender' to Player before checking if it is indeed a Player...
     
  4. Offline

    yPedx

    Wait.. I got very confused about what you just wrote there, I'm no expert in this.. I'm trying to learn though.

    I feel sad now :p Knowing the video was omd, it was the only thing I found back then xD I'll try get to know the 'easier' ways to do it from the web, and not youtube.
     
  5. Offline

    mine-care

    @yPedx Haha don't you didnt know ;) We are used to seeing people facing problems with BcBroz code. Its a routine to spot the mistakes, infact knowing that the code comes from TheBcBroz is enough for us to tell you what mistakes it contains without even seeing it. A simple search in the forums for TheBcBroz reveals many many threads with people having the same problems over and over because they follow those outdated and somewhat wrong tutorials.
    Remember, easier ways are not always the best! as they say
    hehe :D

    If you haven't already, i sugest you study the basics of Java before starting with bukkit as it makes things an awfull lot simpler to understand, follow and troubleshoot! :)
     
  6. Offline

    yPedx

    I never get to learn anything, I don't have enough knowledge to even understand the basics of Java. I've given up on bukkit
    I'm disgusted by bcbroz, now that I know what I see there is old crap.

    I'll just close this thread
     
  7. Offline

    mine-care

    @yPedx Oh no! Don't give up! :(
    Java is simple and fun!
    Start from here this is a good page, there are also others that you may find more helpful!
    Give it a try :- )
     
  8. Offline

    yPedx

    Even though this thread is closed/solved, I actually managed to figure it out! I had no knowledge about this at ALL, but this page helped me so much! http://bukkit.gamepedia.com/Plugin_Tutorial

    I was doing so much wrong xD
     
  9. Offline

    mine-care

    @yPedx Perfect! :) See! :D
    Dont give up with Java. Java can indeed be very peculiar at times but just play with it!
    I started from Bukkit plugin developmnet and ended up studying Java as part of my university course!
     
  10. Offline

    mehboss

    There are also books that have helped me learn alot better (such as java for dummies).
     
  11. Offline

    yPedx

    I've got a program on my phone called SoloLearn, thats where I first started when learning Java.
     
  12. Offline

    mine-care

    @yPedx Haven't really tried it myself to be honnest but i suppose it is good ;) nice one!
     
Thread Status:
Not open for further replies.

Share This Page