Config is other classes

Discussion in 'Plugin Development' started by CheeseNips, Sep 26, 2015.

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

    CheeseNips

    Help i get this error [​IMG]





    Code:
    Code:
    package xyz.CheeseNips;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class Teamspeak extends JavaPlugin {
    
        public void onEnable() {
            getCommand("website").setExecutor(new website());
       
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.YELLOW + getConfig().getString("TeamSpeak"));
                return true;
            }
           
            Player player = (Player) sender;
           
            if (cmd.getName().equalsIgnoreCase("ts")) {
                player.sendMessage(ChatColor.GOLD + getConfig().getString("TeamSpeak")); }
            return false;
               
            }
    
    }
    ]


    Other class where i get errors:
    Code:
    package xyz.CheeseNips;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class website implements CommandExecutor{
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] arg) {
           
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.YELLOW + getConfig().getString("Website"));
                return true;
            }
           
            Player player = (Player) sender;
           
            if (cmd.getName().equalsIgnoreCase("website")) {
                player.sendMessage(ChatColor.GOLD + getConfig().getString("Website"));
            }
            return false;
        }
    
    }
    
     
  2. Offline

    teej107

    because your "website" class doesn't have the method "getConfig". Also please follow naming conventions.
     
  3. Offline

    CheeseNips

    @teej107 What would be the method for it sorry i'm new too making plugin ?
     
  4. Offline

    teej107

    New to plugins or new to Java? I suggest you learn the latter before attempting to use the Bukkit API.
     
    Zombie_Striker and mine-care like this.
  5. Offline

    epe07

    Code:
    getCommand("website").setExecutor(new website(this));
    
    and in the other class:
    Code:
    package xyz.CheeseNips;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class website implements CommandExecutor{
       (PluginName) plugin;
    
      
      public Website((Plugin Name) instance){
         plugin=instance;
       }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] arg) {
          
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.YELLOW + getConfig().getString("Website"));
                return true;
            }
          
            Player player = (Player) sender;
          
            if (cmd.getName().equalsIgnoreCase("website")) {
                player.sendMessage(ChatColor.GOLD + getConfig().getString("Website"));
            }
            return false;
        }
    
    }
    
    And now you can just plugin.getConfig() in other class. I use this.
    I had same problem.
     
  6. @epe07 What is with the casting? Just pass JavaPlugin or your main class. That way you can access the JavaPlugin methods like getConfig.
     
  7. Offline

    epe07

    I didnt mean casting :3

    I did forget it. Just without them. (I mean just plugin name)
     
  8. Offline

    PixelBeaver

    Your CommandExecutor class needs to have "extends JavaPlugin" in the class declaration.
     
  9. Offline

    Zombie_Striker

    @PixelBeaver
    No. You should only have one class that extends JavaPlugin, which would be the main class.
     
    boomboompower likes this.
  10. Offline

    DoggyCode™

    public void config(){
    plugin.getConfig();
    }
     
  11. Offline

    Scimiguy

    @DoggyCode Please don't write him any code.

    He knows extremely little (possibly nothing) about Java at this point, he should be learning that, not copy-pasting someone else's code
     
  12. Offline

    PixelBeaver

    @Zombie_Striker I meant that if he wanted to use getConfig() in that class he would have to make it extend JavaPlugin
     
  13. Offline

    RoboticPlayer

    @PixelBeaver wrong. Add a pass the JavaPlugin class to the class you are trying to use the methods in.
     
  14. Offline

    PixelBeaver

    @henderry2019 I'm just stating what works for me, just trying to help :)
     
  15. Offline

    PixelBeaver

    A way that I can reference to the getConfig() method is by putting
    Code:
    public static MainClassName plugin;
    This allows you to reference your main class and use it's methods, such as getConfig(). Whenever you want to use the getConfig() method, you'll have to use it like this:
    Code:
    plugin.getConfig().otherstuffhere
    Hopefully this helps!
     
  16. Offline

    teej107

    True, it's just terrible practice. Static shouldn't be used as an access anywhere modifier.
     
  17. Offline

    PixelBeaver

  18. Offline

    teej107

    Just because you can do something doesn't mean you should.
     
  19. Offline

    mcdorli

    You know, that's why the people here hates theBCBroz channel. The code presented there just barely works, is very inefficient and uses a lot of bad code style, like statics.
     
  20. Offline

    PixelBeaver

    @teej107 @mcdorli
    If you're so against using the variable method "static," you can also just use:
    Code:
    public MainClassName plugin;
     
  21. Offline

    teej107

    Encapsulation issues aside, I am not against using the "variable method" static. Quite a few people on here abuse and misuse it.
     
  22. Offline

    PixelBeaver

    Oh okay, I understand then.
     
Thread Status:
Not open for further replies.

Share This Page