Solved Enter the config from another class

Discussion in 'Plugin Development' started by Skylandz, Feb 4, 2016.

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

    Skylandz

    So basically i have 2 classes in 2 different packages. The first one is the Core class that extends JavaPlugin and has the onEnable() etc. The second one is a AntiSwear listener, and i want to enter the config from the AntiSwear class, but when i type getConfig() it says "The method getConfig() is undefind for the type antiswear". Is there a way i can enter the config from the AntiSwear class?

    My AntiSwear class:
    Code:
    package lightninghub.listeners;
    
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    
    public class AntiSwear implements Listener {
      
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e){
            for (String word : e.getMessage().toLowerCase().split(" ")){
                if(getConfig().getStringList("badwords").contains(word)){
                    e.setCancelled(true);
                    e.getPlayer().sendMessage(ChatColor.RED + "Your attempt to swear has been logged. do not do it again as it may result in a mute!");
                }
            }
        }
    }
    it acctualy isn't getting logged, i have to make that yet, but its just to scare people off :)

    My main class:

    Code:
    package lightninghub.core;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import lightninghub.commands.Fakejoin;
    import lightninghub.commands.Fakeleave;
    import lightninghub.commands.Fakeop;
    import lightninghub.commands.News;
    import lightninghub.commands.Rules;
    import lightninghub.listeners.AntiSwear;
    import lightninghub.listeners.PlayerListeners;
    
    public class Core extends JavaPlugin {
      
      
          
      
        @Override
        public void onEnable(){
            // System logs!
            System.out.println("[LightningHub]Plugin v" + getDescription().getVersion() + " has been enabled!");
            System.out.println("[LightningHub]Plugin is in development. There might be bugs!");
            System.out.println("[LightningHub]" + ChatColor.RED + "MESSAGE WILL BE REMOVED AS SOON AS PLUGIN IS OUT OF BETA!");
            System.out.println(ChatColor.RED + "THE SERVER IS NOW ONLINE!");
          
            //Plugin listeners!
            PluginManager pm = Bukkit.getServer().getPluginManager();
            pm.registerEvents(new PlayerListeners(), this);
            pm.registerEvents(new AntiSwear(), this);
          
            //Plugin command!
            getCommand("fjoin").setExecutor(new Fakejoin());
            getCommand("fleave").setExecutor(new Fakeleave());
            getCommand("fop").setExecutor(new Fakeop());
            getCommand("news").setExecutor(new News());
            getCommand("rules").setExecutor(new Rules());
          
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
      
        @Override
        public void onDisable(){
            System.out.println("[LightningHub]Plugin v" + getDescription().getVersion() + " has been disabled!");
            System.out.println(ChatColor.RED + "THE SERVER IS NOW OFFLINE!");
        }
    
    }
    by the way, the plugin is only for personal use if any of you guys were wondering.

    EDIT: As you guys can see i have used ChatColor in my onEnable() at System.out.println(), but i am not sure if that is possible. if it isn't i will remove it ofcourse
     
  2. Offline

    teej107

    @Skylandz Pass your Core instance through parameters. The best way would be to pass the Core value through the constructor and assign it to a field.
     
  3. Put this in your listener class:
    Code:
        //Skeleton
        @SuppressWarnings("unused")
        private Main main;
        public CLASSNAME(MAINCLASS main) {
            this.main = main;
        }
        
    Then just do main.getConfig()
     
  4. Offline

    Skylandz

    Maybe i should've added that im pretty new to java & bukkit in general. I am one of the guys that made the mistake to just go straight into bukkit developing before properly learning the java language. As im not that far into java and i thought this was an easy fix, can u go a little bit more in depth about this, because half of the stuff you are telling me to do right now i have no idea how it works or how i have to do it.

    EDIT: probs people are going to tell me to just leave bukkit coding and just learn java before coming back, and i agree thats probably the right thing to do, but i just want this to be solved before i do so.

    EDIT 2: @StealWonders where you put the MAINCLASS capitals, do i just have to change those into the name of the class that extends JavaPlugin, aka the Core class?
     
  5. Offline

    JoaoBM

    @Skylandz You replace MAINCLASS for your MainClass name. In your case it would be Core. As for the CLASSNAME it's the same thing but with the class you are using. So would be AntiSwear.
     
  6. Yes
     
  7. Offline

    Skylandz

    @StealWonders so i did what u told me to do and my AntiSwear class doesn't give any errors anymore, but now my Core class does.

    Code:
    @Override
        public void onEnable(){
            // System logs!
            System.out.println("[LightningHub]Plugin v" + getDescription().getVersion() + " has been enabled!");
            System.out.println("[LightningHub]Plugin is in development. There might be bugs!");
            System.out.println("[LightningHub]" + ChatColor.RED + "MESSAGE WILL BE REMOVED AS SOON AS PLUGIN IS OUT OF BETA!");
            System.out.println(ChatColor.RED + "THE SERVER IS NOW ONLINE!");
           
            //Plugin listeners!
            PluginManager pm = Bukkit.getServer().getPluginManager();
            pm.registerEvents(new PlayerListeners(), this);
            pm.registerEvents(new AntiSwear(), this);
           
            //Plugin command!
            getCommand("fjoin").setExecutor(new Fakejoin());
            getCommand("fleave").setExecutor(new Fakeleave());
            getCommand("fop").setExecutor(new Fakeop());
            getCommand("news").setExecutor(new News());
            getCommand("rules").setExecutor(new Rules());
           
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
    At the pm.registerEvents(new AntiSwear(), this); it says: "The constructor AntiSwear() is undefined"
     
  8. Online

    timtower Administrator Administrator Moderator

    @Skylandz Add "this" to the parameters.
    And please don't use System.out, use getLogger().info instead
     
  9. Offline

    Skylandz

    Ok, the plugin is working fine now. i tested it and no error messages popped up. i changed the System.out to getLogger().info, althought it works exactly the same. if someone could explain that to me ( @timtower )?

    Anyway, im setting this thread to resolved because the problem i had has been resolved, thnx for the help guys!
     
Thread Status:
Not open for further replies.

Share This Page