Talking between classes

Discussion in 'Plugin Development' started by JJSfluffyduck, Feb 4, 2013.

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

    JJSfluffyduck

    I am try to build a larger plugin but i need a way to talk between "Non main" classes e.g.
    I got three classes so far
    FGMain
    FGListener
    FGGroups
    I can talk between FGMain and the other non main classses

    using

    Code:
    public static FGMain PMain;
     
    public FGGroups(FGMain instance) {
            PMain = instance;
        }
    but i cant work out a way to talk to FGGroups from FGListener or thus veruser

    any help would be great
     
  2. Offline

    teunie75

    I always use this:
    Code:
    private main plugin;
    public <classname>(<main class> <main class>) {
    this.plugin = main;
    }
    
    My main class is named main, and I use these for listener, executor.
    Code:
    this.getCommand("<commandname>").setExecutor(new <executing class>(this));
    
    Listener:
    Code:
    PluginManager manager = this.getServer().getPluginManager();
    manager.registerEvents(new <listener classname>(this), this);
    
    Example from my code for a plugin:

    Listener:
    Code:
    public class register implements Listener {
    private main plugin;
    public register(main main) {
    this.plugin = main;
    }
    @EventHandler(priority = EventPriority.NORMAL)
    public void onBlockBreak(BlockBreakEvent event){ // Rest of code
    
    Executor
    Code:
    public class getamount implements CommandExecutor {
    private main plugin;
    public getamount(main main) {
    this.plugin = main;
    }
    
    Main class
    Code:
    PluginManager manager = this.getServer().getPluginManager();
    manager.registerEvents(new register(this), this); // For the listener(register class)
    this.getCommand("diamond").setExecutor(new getamount(this));//For the executor //classname getamount and command diamond.
    
     
    ZeusAllMighty11 likes this.
  3. Offline

    JJSfluffyduck

    that talking between the main class and the listener but how would you talk between two classes that arent main
     
  4. Offline

    teunie75

    Not sure but replace main with the class you want to use...
    What do you want to do actually?
     
  5. Offline

    Frazz86

    He wants to get methods from FGListener and use them in FGGroups and the other way around, he knows how to connect them with the main file though.
     
    JJSfluffyduck likes this.
  6. Offline

    JJSfluffyduck

    yer excatly
     
  7. Offline

    teunie75

    Import the classes to eachother and connect them just like with the main.
     
  8. Offline

    ZeusAllMighty11

    Do this to talk between classes:


    private ClassName variable = new ClassName();


    variable.doStuf();
     
  9. Offline

    JJSfluffyduck

    Where would i add that line off code? @ the start or...
     
  10. Offline

    ZeusAllMighty11

    Maybe read up on some basic java
     
  11. Offline

    JJSfluffyduck

    I know java. But if i call the groups call.from a non main class it havent been started so it gives me errors
     
Thread Status:
Not open for further replies.

Share This Page