Solved GetConfig in other class

Discussion in 'Spigot Plugin Development' started by jerskisnow, Dec 7, 2017.

  1. Hello people, i am making a Survival Plugin and i have mulitple packages with multiple classes but i cant use p.sendMessage(getConfig().getString("rules"));

    This is my code:

    Code (Text):
    package me.jerskisnow.SSGSurvival.Commands;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import me.jerskisnow.SSGSurvival.Main;
    import net.md_5.bungee.api.ChatColor;

    public class rules implements CommandExecutor {
     
        Main main;

        public rules(Main plugin) {

            main = plugin;

        }
     
        public rules(Main instance) {
            plugin = instance;
            }

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
         
            if(cmd.getName().equalsIgnoreCase("rules")) {
                p.sendMessage(plugin.getConfig().getString("rules")));
             
            }
            return true;
        }

    }
     
    But when i am using this he is giving an error because i have 2 times public rules(Main ......)
    Can someone help me?
     
    #1 jerskisnow, Dec 7, 2017
    Last edited: Dec 8, 2017
  2. Just remove one of the constructors.

    Also, classes should be UpperCamalCase and not all lowercase.
     
  3. then I can't use: getconfig :/
     
  4. If you only remove one of the constructors you can still use it. As long as you don’t remove both because then you will get a NPE.
     
  5. So this will work:

    Code (Text):
    package me.jerskisnow.SSGSurvival.Commands;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import me.jerskisnow.SSGSurvival.Main;

    public class rules implements CommandExecutor {
       
        Main main;

        public rules(Main plugin) {

            main = plugin;

        }
       

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
           
            if(cmd.getName().equalsIgnoreCase("rules")) {
                p.sendMessage(main.getConfig().getString("rules"));
               
            }
            return true;
        }

    }
     
     
  6. Yes, it will work fine.
     
  7. use file f = new file("plugins/pluginname", "example.yml") :V
     
  8. Two words: Object orientation
     

Share This Page