Updating 1.7 to 1.8 issues

Discussion in 'Plugin Development' started by pjr8, Dec 1, 2015.

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

    pjr8

    Hello, I have 2 problems that I can't fix. I am still a starter and if someone can help me, thank you so much! I know it might seem it is easy or something, but I can't fix it. I have no clue.
    Code:
        Bukkit.getServer().getScheduler()
          .scheduleSyncRepeatingTask(this, new Runnable()
          {
            public void run()
            {
              Player[] arrayOfPlayer;
              int j = (arrayOfPlayer = Bukkit.getServer().getOnlinePlayers()).length;
              for (int i = 0; i < j; i++)
              {
                Player p = arrayOfPlayer[i];
                if ((!Main.tagged.contains(p.getName())) &&
                  (!p.isDead()))
                {
                  PlayerInventory ir = p.getInventory();
                  double amt = 5.0D;
                  if (ir.getHelmet() != null) {
                    if (ir.getHelmet().getItemMeta().hasLore())
                    {
                      double added = Main.getHpgenFromLore(
                        ir.getHelmet(), "HP REGEN");
                      amt += added;
                    }
                  }
                  if (ir.getChestplate() != null) {
                    if (ir.getChestplate().getItemMeta().hasLore())
                    {
                      double added = Main.getHpgenFromLore(
                        ir.getChestplate(), "HP REGEN");
                      amt += added;
                    }
                  }
                  if (ir.getLeggings() != null) {
                    if (ir.getLeggings().getItemMeta().hasLore())
                    {
                      double added = Main.getHpgenFromLore(
                        ir.getLeggings(), "HP REGEN");
                      amt += added;
                    }
                  }
                  if (ir.getBoots() != null) {
                    if (ir.getBoots().getItemMeta().hasLore())
                    {
                      double added = Main.getHpgenFromLore(
                        ir.getBoots(), "HP REGEN");
                      amt += added;
                    }
                  }
                  if (p.getHealth() + amt > p.getMaxHealth()) {
                    p.setHealth(p.getMaxHealth());
                  } else {
                    p.setHealth(p.getHealth() + amt);
                  }
                }
              }
            }
          }, 1L, 20L);
      }
    }
    The problem here is that ArrayPlayer with the getOnlinePlayers.length does not work.
    Code:
        Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
        {
          public void run()
          {
            Player[] arrayOfPlayer;
            int j = (arrayOfPlayer = Bukkit.getServer().getOnlinePlayers()).length;
            for (int i = 0; i < j; i++)
            {
              Player p = arrayOfPlayer[i];
              Main.this.barHealth(p);
              if (p.getExp() < 1.0F) {
                p.setExp((float)(p.getExp() + 0.02D));
              }
              if (p.getLevel() + 2 > 100) {
                p.setLevel(100);
              } else {
                p.setLevel(p.getLevel() + 2);
              }
            }
          }
        }, 1L, 1L);
    Same problem with the arrayOfPlayer
     
  2. Offline

    adam753

    And did you have the creator's permission to decompile this plugin so you could update it yourself?
     
  3. Offline

    TheNewTao

    If you are trying to loop through all the players, it would be a lot easier to create a foreach loop with Player something and the Bukit.getOnlinePlayers() collection.
     
  4. Offline

    pjr8

    It was my friends, private plugin, I asked him and he said sure. He doesn't want to update it anymore so I guessed I can get help
     
  5. Offline

    Scimiguy

    Yeah you should be using a ForEach loop for this, instead of getting the length and such, this is very inefficient.

    for (Player p : Bukkit.getOnlinePlayers()) {

    }
     
  6. Offline

    pjr8

    I had no idea what a for each loop was, so I used something I thought would work and it did, turns out that is a for each loop, I wouldn't be able to have done it without you guys, I appriciate it.

    Also how do I put this to solved?
     
    Last edited by a moderator: Dec 1, 2015
  7. Offline

    Mrs. bwfctower

    Actually in almost all cases, there is little or no difference in efficiency between an enhanced for loop and a 'regular' for loop. One exception is when to get the element at a specific index the rest of the entire collection must be traversed, but that isn't the case. An enhanced for loop with an Iterable actually compiles to something very similar to a while loop with the expression Iterator#hasNext().
     
  8. Offline

    Scimiguy

    @Mrs. bwfctower
    I'm not referring to loop vs loop, I'm referring to his implementation vs my example.

    His implementation:
    Code:
            Player[] arrayOfPlayer;
            int j = (arrayOfPlayer = Bukkit.getServer().getOnlinePlayers()).length;
            for (int i = 0; i < j; i++)
            {
    Steps:
    1. Create a new instance of a Player Array
    2. Return Collection of online players
    3. Assign collection to new instance of Player Array
    4. Return length of new Array
    5. Assign new length to new int j
    6. Initialize new int i for purpose of for loop
    7. Do loop

    My example:
    Code:
    for (Player p : Bukkit.getOnlinePlayers()) {
    
    }
    Steps:
    1. Return collection of online players
    2. Do Loop
     
  9. Offline

    Mrs. bwfctower

    Ok I'm sorry, but this is not correct.
    Note that #getOnlinePlayers() returns an array, at least in 1.7.

    First of all, no new instance of a player array is created. A variable is defined, sure, but that hardly affects the performance.

    In steps 2 and 4, well returning an object doesn't affect the performance in any significant way.

    In step 3, you're not assigning a collection to a new instance of an array. You are setting the pointer of a local variable to the pointer of the array of players online (remember #getOnlinePlayers() returns a player array).

    Steps 5 and 6 are just creating a couple local variables, which has almost no impact at all.

    Then the loop. For both examples, you are iterating through an array (#getOnlinePlayers() returns an array). A foreach loop compiles to just about the same thing as a for loop.

    The only differences are really readability (both are still very readable though), and the defining of a couple extra local variables. If that is highly inefficient, then I don't know what to tell you.

    EDIT: Since a for loop compiles to just about the same thing as a while loop, both versions compile to almost exactly the same thing. Since they do this, there is no performance difference.
     
    Last edited: Dec 1, 2015
  10. Offline

    Scimiguy

    Of course a few extra things is not gonna be noticeable. Just about everyone knows that.

    But considering this entire thread is about how his loop isn't working, it'd make sense to change to a more sensible method of handling the problem, rather than using the less efficient method.

    Impact will always be extremely minimal, except in huge-use cases. This isn't about overall impact, but comparative impact.
     
  11. Offline

    Mrs. bwfctower

    Top of the thread, "Edit Title", select the Solved element from the dropdown on the left.

    It's not working because he's using a different version of the API. The method he was using, if implemented on a 1.7 server, would be just as efficient as a foreach loop.
    It's the same, even in huge-use cases. The only thing different is that you're storing a couple more pointers.
    If you wish to continue this, feel free to start a thread or PM me.
     
  12. Offline

    mcdorli

    Do you even know java? A foreach loop is basic java. Start on this page:
    https://docs.oracle.com/javase/tutorial/
     
Thread Status:
Not open for further replies.

Share This Page