Will this cause a infinite-loop?

Discussion in 'Plugin Development' started by Redstone_Craft, Jun 1, 2012.

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

    Redstone_Craft

    I was just wondering if this will cause an infinite-loop because first this is what it does in a plugin I am making (myJobs)

    During its enable process, it will check for Vault plugin, if it returns null, it disables the plugin saying that its required (same thing in the plugin.yml file, it shows depend: Vault), and then it checks if the plugin is disabled. If it is disabled, it will enable it, then re-do the whole process to connect to the economy plugin.

    Code:
        // Other stuff
       
        public void loadEconomyJoints() throws VaultPluginNotFoundException {
            Plugin vaultPlugin = getServer().getPluginManager().getPlugin("Vault");
           
            if(vaultPlugin != null) {
                if(!vaultPlugin.isEnabled()) {
                    getServer().getPluginManager().enablePlugin(vaultPlugin);
                    this.loadEconomyJoints();
                } else {
                    l.log(Level.INFO, "[MyJobs] Successfully jointed to Vault!");
                }
            } else {
                getServer().getPluginManager().disablePlugin(myjobs);
                throw new VaultPluginNotFoundException("Vault plugin not found");
            }
        }
    
    Then for the plugin.yml
    Code:
    name: MyJobs
    version: 0.1
    author: RedstoneCraft
    description: Its my job, and YOUR job!
    main: com.gmail.linuxpro13.myjobs.MyJobs
    
    ## Dependicies
    depend: Vault
    
    ## commands and permissions
    commands:
      myjobs:
        description: Main MyJobs command
        usage: "Usage: /<command> join|leave|list|info [job]"
        permission: myjobs.command
      
    permissions:
      myjobs.*:
        description: All the MyJobs permissions
        default: op
        children:
          myjobs.command: true
          myjobs.join: true
          myjobs.quit: true
          myjobs.list: true
          myjobs.info: true
          myjobs.getpaid: true
    
      myjobs.command:
        description: Permission for MyJobs command
        default: op
    
      myjobs.join:
        description: Permission to join a MyJobs job
        default: op
    
      myjobs.quit:
        description: Permission to leave a MyJobs job
        default: op
    
      myjobs.list:
        description: Permission to list all the MyJobs jobs
        default: op
    
      myjobs.info:
        description: Permission to view info on a MyJobs jobs
        default: op
    
      myjobs.getpaid:
        description: Permission to be paid
        default: op
    
     
  2. Offline

    CorrieKay

    if they dont have vault at all, or theres some persistant issue with vault loading, yes, this will cause an endless loop, but that shouldnt be an issue.
     
  3. Offline

    Redstone_Craft

    So just recode the whole thing or remove some parts like the parts of the code that will check if it's disabled, then to check if it returns null, and to enable it.
     
  4. Redstone_Craft You have the depend line in your plugin.yml, so your plugin won't load if Vault is not there and if it's there it will enable after. You can safely remove the whole code. ;)
     
  5. This. That's what dependencies were made for. Your plugin will always enable after Vault, and won't enable at all when it is not there.
    So go ahead and get rid of that method.

    However, your syntax in the plugin.yml file is wrong. The dependencies have to be in a YAML list format, even when there is only one.
    You wrote "depend: Vault", it has to be "depend: [Vault]".
    That's probably what made you check it manually in the first place.
     
  6. Redstone_Craft
    Also, why are you re-enabling the plugin ? If it's disabled it means the administrator manually disabled it, and it gives you no right to override his decision.
    So, as other said, just use the depend (correctly as a list with [ ] ) and get rid of that code :p

    EDIT: fixed freakin' brackets
     
  7. Offline

    Redstone_Craft

    Okay, I will do that; and isn't it a square bracket not a block bracket :p

    Square bracket is []
    Block bracket is {}
    (Just saying)
     
  8. Offline

    Sleaker

    Vault loads at startup, not the normal load time. So checking if Vault is started when your plugin loads will always return true if it's on the server unless for some reason it has an error and didn't startup.

    If you want to hard-depend on Vault, just add it to your depend list. and yes, you use [] for multiple dependencies.
     
  9. Sleaker sorry for thread hijacking, but could you please make sll supported plugins a dependency of vault?
    Example of the problem:
    - Plugin XYZ depends on Vault (to use iConomy).
    - Plugin XYZ has the depends line in the plugin.yml so it loads after Vault.
    - Now XYZ loads first, then Vault. You see the problem? XYZ checks for the economy provider in onEnable, but at this time there is no eco prov. cause iConomy hasn't loadet yet... ;)
     
  10. Offline

    Sleaker

    I can't make Vault a dependency of everything, it actually causes more load problems.
    however, Vault doesn't need to load last, it can load before any of them and it actually does. What happens is Vault detects which ones are going to load, and loads the connector for them anyway. The only issue you have as a hooking plugin is if you try to invoke methods before the load sequence is finished. IE you can hook the implementations just fine and the load order wont matter at all, but don't use them until after the loadup.

    If you have operations that you need to do right when your plugin loads which utilize the economy, permission, or chat APIs instead, schedule them to run in a once off task. This will cause the operation to get cached and run immediately after the server starts, and once everything has finished linking up.
     
  11. Why? I thought that'sexactly what Softdepends is for... ;)

    But this code would be so epic:
    Code:java
    1.  
    2. public Economy economy = null;
    3. public void onEnable() {
    4. Server s = getServer();
    5. PluginManager pm = s.getPluginManager();
    6.  
    7. log = s.getLogger();
    8. PluginDescriptionFile pdf = getDescription();
    9. prefix = "["+pdf.getName()+"] ";
    10. if(pm.getPlugin("Vault") == null)
    11. log.info(prefix+"Vault not found!");
    12. else
    13. {
    14. RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(Economy.class);
    15. if(economyProvider != null)
    16. economy = economyProvider.getProvider();
    17. else
    18. log.info(prefix+"No economy provider!");
    19. RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(Permission.class);
    20. }

    After that a economy available check would just be if(econcomy != null)
    I want to be able toinitialize the whole system in onEnable, but economyProvider will be null if iConomy hasn't loaded before Vault.
     
  12. actually, it wouldn't create an infinity loop, after about 100 tries it gives an StackOverFlowError, but thats is also unwanted, :p
     
  13. Offline

    Sleaker

    You can initialize the whole system. Like I just said, Vault creates the instance of the Linking class before the plugin loads because it expects to load it. Thus, the Economy instance is already registered when you go to grab it.

    Common Misconceptions:
    1) You can't hook an economy before the economy loads.
    2) The Hook requires the economy to be loaded before it actually is instantiated.
    3) When a plugin accesses the hook it's accessing the economy plugin directly.

    What Actually Happens:
    1) Vault checks to see if any of the Economy plugin classes it works with are anywhere in memory (that means going to be loaded or already loaded)
    2) Vault creates an instance of the Economy implementation for each Economy plugin it finds, and registers it into the Bukkit Service providor.
    3) Each Economy implementation checks to see if the plugin it hooks is enabled yet, if so it links up immediately. If not, it uses listeners to wait until they are enabled.
    4) When another plugin loads that wants to use the Economy service it can find it regardless of where it's at in the load order because Vault already loaded the implementation.



    If you're talking about forcing anyone that uses Vault in their plugin to depend on it, I can't do that. It's up to the plugin developer that is hooking into Vault to do that. Go track down anyone that's not and bash them. Vault also loads pre-world. Not many plugins do this, so if a plugin is loading during the normal cycle it shouldn't have any problems even if it doesn't explicitly depend on vault.
     
    V10lator and Bone008 like this.
  14. Sleaker Thanks for explaining this. :)
    So my code example should work (economy should not be null) but a economy.isEnabled() in onEnable may return false, while it should return true a tick after?
     
  15. Offline

    Sleaker

    correct.
     
    V10lator likes this.
  16. Offline

    Redstone_Craft

    One little tip.
    If you get null pointer warnings.
    Try this:

    public static Economy economy = null;

    Then that will remove the warning ;)

    Not sure, but last time one of the plugins I made threw 100 stack overflow exceptions.

    So maybe if i kept that code in the plugin, I'd probably get a java.lang.StackOverflowException thrown 100 times :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
Thread Status:
Not open for further replies.

Share This Page