Circular Reference Alternative

Discussion in 'Plugin Development' started by OMGitzFROST, Oct 3, 2019.

  1. Offline

    OMGitzFROST

    Background:
    Currently, I am trying to create two Task Operators. A SettingOperator class and a MessageOperator class. Below I provided a background as to what I am trying to achieve with both classes.

    Purpose of my SettingOperator - Creates player preferences such as a normal desktop application and stores them to a file in the data folder. These preferences can be updated via player commands, configuration files, etc.

    Purpose of my MessageOperator - Handles logging localized messages and configures the plugins current locale (as long as it is currently supported by the plugin otherwise defaults to English).

    Problem:
    Currently, I am initializing both operators as such on the onEnable() Method
    onEnable() Method (open)
    Code:
    private static PluginName instance;
    private SettingOperator setting;
    private MessageOperator message;
    
    @Override
    public void onEnable() {
        instance = this;
      
        // TASK OPERATORS
        setting = new SettingOperator(this);
        message = new MessageOperator(this);
    }
    
    /* CLASS INSTANCES */
    
    @Contract (pure = true)
    public static AdminConsole getInstance() {
        return instance;
    }
    
    public static SettingOperator getSettingOP() {
        return setting;
    }
    
    public static MessageOperator getMessageOP() {
        return message;
    }
    

    Then I am trying to call the references to both classes because both use each other interchangeably.
    Operator Classes (open)
    Code:
    *** These Classes Are Into Separate Files, but for the sake of easy readability, I put them in one spoiler ***
    
    public class SettingOperator {
        private final MessageOperator message = PluginName.getMessageOP();
    }
    
    public class MessageOperator {
        private final SettingOperator message = PluginName.getSettingOP();
    }
    
    

    Question:
    Is there a better way of doing this, because I tend to catch an overflow exception and does not tell me what kind of problem it is causing? Any help is appreciated and would love some constructive criticism.
     
    Last edited: Oct 3, 2019
  2. Offline

    timtower Moderator Moderator

    @OMGitzFROST Give them a constructor to the plugin. Then add a getter.
    No static at all.
     
  3. Offline

    Zombie_Striker

    @OMGitzFROST
    You could try setting those fields in the onEnable instead of within those classes. That way, you have control over when those variables are set.
     

Share This Page