Java Method is fire in wrong order

Discussion in 'Spigot Plugin Development' started by Ohwnead, Aug 12, 2018.

  1. Hi, for multiple time I encounter a problem,

    When I have multiple methode fire one after one like that
    Code (Text):
    RemoveFromParticipating(UUID, xIle, zIle);
    Island.RemoveIslandNoMorePlayer(p);
    Island.RemoveHasIsland(p);
    It fire the last event first, wich avoid the two before event, wich test if player has island
    (the last event remove the var has island) (the two avoided event still fire)

    I encounter that multiple time (not only with method for config editing, I have created a thread for a teleportation wich was avoiding an event cancelling), is there a solution ?

    Thanks for help
     
  2. We'd need more source (for example the three methods you call). It's quite likely you do something in another thread in the first two methods, which means the third line can run first.
     
  3. Yeah, I've try to explain it in first post but i'm bad in English sorry,
    The two first method need a var to true, wich the last method set it to false,
    but the last method is executed first, the two last method still run, but the var is to false and script can't run

    Method that launch other method :
    Code (Text):
    public class RemoveFromIsland
    {

        public void RemoveFromIsland(OfflinePlayer p)
        {
            Main plugin = Main.getPlugin(Main.class);
            File PlayerFile = new File(plugin.getDataFolder(), p.getUniqueId().toString() + ".yml");
            File IslandFile = new File(plugin.getDataFolder(), "island.yml");
            FileConfiguration PlayerCFG;
            FileConfiguration IslandCFG;

            HasIsland HasIsland = new HasIsland();
            IsParticipating IsParticipating = new IsParticipating();
            IsOwner IsOwner = new IsOwner();
            RemoveIslandNoMorePlayer RemoveIslandNoMorePlayer = new RemoveIslandNoMorePlayer();
            RemoveHasIsland RemoveHasIsland = new RemoveHasIsland();

            if (PlayerFile.exists()
                    && IslandFile.exists())
            {
                if (HasIsland.HasIsland(p))
                {
                    PlayerCFG = YamlConfiguration.loadConfiguration(PlayerFile);

                    int xIle = PlayerCFG.getInt("Island.IleX");
                    int zIle = PlayerCFG.getInt("Island.IleZ");
                    String UUID = p.getUniqueId().toString();

                    if (IsOwner.IsOwner(p))
                    {
                        IslandCFG = YamlConfiguration.loadConfiguration(IslandFile);

                        IslandCFG.createSection(xIle + " " + zIle + ".owner");

                        try
                        {
                            IslandCFG.save(IslandFile);
                        }
                        catch (Exception e)
                        {
                        }

                        RemoveFromParticipating(UUID, xIle, zIle);
                        RemoveIslandNoMorePlayer.RemoveIslandNoMorePlayer(p);
                        RemoveHasIsland.RemoveHasIsland(p);
                    }
                    else if (IsParticipating.IsParticipating(p))
                    {
                        RemoveFromParticipating(UUID, xIle, zIle);
                        RemoveIslandNoMorePlayer.RemoveIslandNoMorePlayer(p);
                        RemoveHasIsland.RemoveHasIsland(p);
                    }
                }
            }
        }

        public void RemoveFromParticipating(String UUID, int xIle, int zIle)
        {
            Main plugin = Main.getPlugin(Main.class);
            File IslandFile = new File(plugin.getDataFolder(), "island.yml");
            FileConfiguration IslandCFG;

            if (IslandFile.exists())
            {
                IslandCFG = YamlConfiguration.loadConfiguration(IslandFile);

                List<String> ListeParticipant = IslandCFG.getStringList(xIle + " " + zIle + ".participants");
                for (String s : ListeParticipant)
                {
                    if (s.equals(UUID))
                    {
                        ListeParticipant.remove(s);
                        IslandCFG.set(xIle + " " + zIle + ".participants", ListeParticipant);

                        try
                        {
                            IslandCFG.save(IslandFile);
                        }
                        catch (Exception ex)
                        {
                        }

                        break;
                    }
                }
            }
        }
    }
    Other method by "normal order"
    Code (Text):
    public class RemoveIslandNoMorePlayer
    {

        public void RemoveIslandNoMorePlayer(OfflinePlayer p)
        {
            Main plugin = Main.getPlugin(Main.class);
            File PlayerFile = new File(plugin.getDataFolder(), p.getUniqueId().toString() + ".yml");
            File IslandFile = new File(plugin.getDataFolder(), "island.yml");
            FileConfiguration PlayerCFG;
            FileConfiguration IslandCFG;

            HasIsland HasIsland = new HasIsland();

            if (PlayerFile.exists()
                    && IslandFile.exists())
            {
                PlayerCFG = YamlConfiguration.loadConfiguration(PlayerFile);
                IslandCFG = YamlConfiguration.loadConfiguration(IslandFile);

                if (HasIsland.HasIsland(p))
                {
                    int xIle = PlayerCFG.getInt("Island.IleX");
                    int zIle = PlayerCFG.getInt("Island.IleZ");

                    List<String> list = IslandCFG.getStringList(xIle + " " + zIle + ".participants");
                    if (list.isEmpty())
                    {

                        IslandCFG.set(xIle + " " + zIle, null);
                        IslandCFG.set("IsAmount", IslandCFG.getInt("IsAmount") - 1);

                        try
                        {
                            IslandCFG.save(IslandFile);
                        }
                        catch (Exception ex)
                        {
                        }

                        //Destruc island
                    }
                }
            }
        }
    }
     
    Code (Text):
    public class RemoveHasIsland
    {

        public void RemoveHasIsland(OfflinePlayer p)
        {
            Main plugin = Main.getPlugin(Main.class);
            File PlayerFile = new File(plugin.getDataFolder(), p.getUniqueId().toString() + ".yml");
            FileConfiguration PlayerCFG;

            if (PlayerFile.exists())
            {
                PlayerCFG = YamlConfiguration.loadConfiguration(PlayerFile);

                PlayerCFG.set("Island.HasIle", false);
                PlayerCFG.createSection("Island.IleX");
                PlayerCFG.createSection("Island.IleZ");

                try
                {
                    PlayerCFG.save(PlayerFile);
                }
                catch (Exception ex)
                {
                }
            }
        }
    }
    all method work well alone
    Sorry for naming convention, and other things like that
     
    #3 Ohwnead, Aug 12, 2018
    Last edited: Aug 12, 2018
  4. Adding code between each method make it run again, so i put each method separated for the moment
    Any sugestion of how to patch it ? Scheduler will be to much or may i let it separated ?

    Thanks for help
     
  5. It looks to me that they all run in normal order. Its probably something setting the value to false before the three methods even run.
     
  6. The plugin is blank, it contain the three class, and some class to put a compass on inventory, it's not the first time i encounter this situation, when i put more than 2 or 3 method it run in a bad order :s
     
  7. I've found why it make this, java run the methode one after one, but fire due to length of code, or something, in wrong order, simply by adding a scheduler with a 0, 1, 2 ticks, or by moving code for "separating" methode.

    nothing else work.

    thanks for help
     
  8. You're still definitely doing something wrong in your code. Java isn't broken, Spigot isn't broken, your plugin is broken. Using a scheduler fixed a problem that shouldn't exist in the first place.

    Post all code you have (all classes) and tell us what triggers the execution.
     
  9. You have all code, i know for the scheduler, I already find another solution wich is to separate methode,
    I have retry with a real blank plugin (only class uper and test class (only make getConfigString(Get if player has island, and where is island etc...)) and already the same issue

    But my new code work well, I didn't put thread as solved cu'z it's not the first time i get this problem, and if there is solution ^^'

    I found something new, It only append when there is config near in the class, strange...
     
  10. I don't have the HasIsland class, nor the event / command class which triggers this whole thing.

    Moreover, I don't know what the config looks like before this, during this and after this.

    The only thing the last method sets to false is Island.HasIle in the player file. The first method edits island.yml, so it has nothing to do with the outcome of the last method. Nor does it check anything in the player file.

    The second method does check HasIsland.HasIsland(p), but it wouldn't even run if that's false (it's checked in the caller of the three methods). Except of course, when the file is edited in another thread or by something externally.
     
  11. HasIsland and other boolean, is juste a methode to get on island or player config file to get if the player hasisland to true or false,
    if true, in the island configurationfile, there is only the island, islandowner, islandplayer, and islandlocation, wich is get in another plugin if player click on menu. (bug still ocure without this plugin)

    it does nothig except if statement

    the only classes wich edit something is the 3 showed classes

    There is nothing that is strange, and differ from another plugin, this bug is really weird sorry

    Only the 3 classes (without HasIsland etc) still make the bug occure
     
  12. I have found what did that
    While i was creating a new methode, i ve seen that if methode is faster than Config to get & save data, the next method can't reload, or is overwritted by previus or something like that (I thinks it comme from that)

    Any idea of how to deal with it ?
    The best way will be to get all config's code and to put it in one class but i wanna keep multi classes for config :s

    Working again with no edit on code, I didn't understand nothing off this...
    just remove blank line and save
     
    #12 Ohwnead, Aug 13, 2018
    Last edited: Aug 13, 2018
  13. You'd load it once, and only save when you change something. Although all your writes are done synchronously, so perhaps something is configured weirdly in the file system?

    Doing a load once will boost your performance as well.
     
  14. How can I load once with so much classes, can I put a a File or Config as a parameter or something like this?
    Didn't understand what you mean by loading once, per class ?
     

Share This Page