Regarding Multiple Classes

Discussion in 'Plugin Development' started by stuntguy3000, Feb 20, 2013.

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

    stuntguy3000

    Hello,

    Just a question on using multiple classes, scenario:

    Now i have a plugin, which sends the player a message on join. I have 3 classes (MainClass,ListenerClass,UtilClass). When the player joins a event in ListenerClass fires, which is something like this:

    UtilClass.test(p);

    UtilClass.test checks p against a config setting and then sends p the config message.

    My question, what is the way to do this. I have had some issues and i can't find any documentation on it (Something to do with plugin always returning null). Thanks.

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
    kittenchunks likes this.
  2. Offline

    kittenchunks

    construct your classes, its easier than using static
    like, in the main class:
    UtilClass utlclass = new UtilClass(this);
    then make a method return that, so youcan use it in your listener
    like main.getUtilClass().test(p);
     
  3. Offline

    stuntguy3000

    I..half get it?
     
  4. Offline

    RealDope

    UtilClass:
    Code:JAVA
    1.  
    2. <MainClassName> plugin;
    3. public <ClassName>(<MainClassName> plugin) {
    4. // This is called a constructor
    5. this.plugin = plugin;
    6. }
    7.  


    MainClass
    Code:JAVA
    1.  
    2. <UtilClass> util = new <UtilClass>(this);
    3.  
     
  5. Offline

    stuntguy3000

    That's what i have..

    public class TSUtil {
    TSPlugin plugin;
    public TSUtil(TSPlugin plugin) {
    this.plugin = plugin;
    }


    public String test(Player p)
    {
    String value = p.getName();

    return value;
    }
    }
     
  6. Offline

    ZeusAllMighty11

    You never initialized TSPlugin ?
     
  7. Offline

    stuntguy3000

    What do you mean? o.o
     
  8. Offline

    keto23

    Code:java
    1.  
    2. public class TSUtil {
    3. private TSPlugin plugin;
    4. public TSUtil(TSPlugin plugin) {
    5. this.plugin = plugin;
    6. }
    7. public String test(Player p) {
    8. return p.getName();
    9. }
    10. }
    11.  


    Code:java
    1.  
    2. public class TSPlugin extends JavaPlugin {
    3. public TSUtil util;
    4. // In the onEnable Make sure to put "util = new TSUtil(this);" register the listener with "new TSListener(this)"
    5. }
    6.  


    Code:java
    1.  
    2. public class TSListener implements Listener {
    3. private TSPlugin plugin;
    4. public TSListener(TSPlugin plugin) {
    5. this.plugin = plugin;
    6. }
    7.  
    8. /* EVENT:
    9.   plugin.util.test(player);
    10.   */
    11. }
    12.  
     
  9. Offline

    stuntguy3000

    Thanks keto! Works like a charm.
     
Thread Status:
Not open for further replies.

Share This Page