Just a annoying loop problem

Discussion in 'Spigot Plugin Development' started by MaveCrit, May 24, 2016.

  1. So hey there!
    I am so confused right now, after hours of testing i finally found a way to get my plugin to work but it doesnt work perfect at all.

    So what i want to do:
    I want to check the nearest location which is saved in a config.
    So at the startup i store all my Locations from the file into a ArrayList
    Then i want to check which location is in the radius of 10 from the player.

    My current way to do that:

    Code (Text):
        public String getNearPortal(Player p){
            String none = "┬žcThere is no near portal";
             for (int i = 0; i < LocationGetter.Portals.size(); i++) {
                  Location loc = (Location)LocationGetter.Portals.get(i);
                  String sloc = "" + loc.getWorld().getName() + "," + loc.getX() + "," + loc.getY() + "," + loc.getZ() + "," + loc.getPitch() + "," + loc.getYaw();
                   if (loc.distance(p.getLocation()) < 10.0D)
                  {
                      for (int id = 1; id < LocationGetter.Portals.size(); id++) {
                           String[] v = Main.locations.getString("portal_" + id).split(",");
                           String[] s = sloc.split(",");
                           
                           String eins = v[1] + ".0";
                           String zwei = v[2] + ".0";
                           String drei = v[3] + ".0";
                           
                           if(v[0].equals(s[0]) && eins.equals(s[1]) && zwei.equals(s[2]) && drei.equals(s[3])){
                               return "portal_" + id;
                           }
                     //return sloc;
                      }      
                  }
             }
            return none;
        }
    So and i was so happy because IT WORKS!
    BUT:
    Only for one portal, it only works for "portal_1" so i think the "int id = 1" isnt counting up.

    Does anyone have a solution for my problem?
     
  2. Serializator

    Supporter

    Please explain exactly what you're trying to accomplish, because I think that this can be done much easier that you're doing.
    I know you said that you want to get the nearest location stored from the player but at the same time it also has to be within a distance of 10, but why the nested loop?
     
  3. So okay mhm its very hard to explain i think but i will try it.
    So my portals werent on a specific block like:
    You enter the block and kazoo you get teleported

    I created a particle circle with the radius of 3, so when the player enters a portal (the radius) a command will be fired (or a teleport or whatever).
    So now i try to check which portal the player has entered because the command which should be fired is stored in the config:
    upload_2016-5-24_18-6-17.png

    And yeah i tried it with that way to check which command should be fired from all the portals
     
  4. Can you show us how you store the portal locations? There must be a better way to do this than the current version, which should solve your problem.
     
  5. JustisR

    Supporter

    Solving problems within your loops is extremely easy with the use of dun dun dun...
    System.out.print("Checkpoint!");
    Simply place ^ in your code, inserting your index if you want.
    See where your code stops running, and then you know what conditions aren't being met, and can then address the problem.

    This method of debugging works for most small errors like this, and I would recommend trying it out every time you encounter similar errors before coming onto spigot for help.
     
  6. I have already done that so i come to the conclusion that the loop doesnt "loop" :c
    And that is what iam asking, why doesnt the loop loop?
     
  7. Sure.
    I store the location in the config like that:
    upload_2016-5-24_18-38-36.png
    And put it in the ArrayList like that:

    Code (Text):
        public static void LoadAll() {
           
           
            for (int i = 1; Main.locations.contains("portal_" + i); i++) {
                Portals.add(GetLocation("portal_" + i));
                 Bukkit.getConsoleSender().sendMessage("┬žaPortal " + i + " has been found!");
               }

        }
        public static Location GetLocation(String Key) {
            if (!Main.locations.contains(Key)) {
                return null;
            }
            String[] v = Main.locations.getString(Key).split(",");

            return new Location(Bukkit.getWorld(v[0]), Double.parseDouble(v[1]),
                    Double.parseDouble(v[2]), Double.parseDouble(v[3]),
                    Float.parseFloat(v[4]), Float.parseFloat(v[5]));
        }
    But this works. Its just the loop.
    It works for portal_1 but not for other portals :c
     
  8. JustisR

    Supporter

    If you did what I said, you wouldn't be needing to ask that question though. >.<
    What loop isn't looping? The outermost loop?
    If so, the answer to that question is obvious.

    LocationGetter.Portals.size() is equal to 1.

    Use System.out.print(LocationGetter.Portals.size()); if you want.
    But the fact is, if it's stopping right there, you know the size is 1, because those are the conditions of your loop.

    If it's not 1, then it's not stopping there, and you need to debug again.
    If it is 1, then ask yourself...
    Why would LocationGetter.Portals only have just 1 object in it?
    (Ignoring that horrible use of statics btw)
     
  9. Statics are just for testing everything, i want to implement a working system in a plugin which i will code in a few weeks. :p

    And yeah i'm not such silly..
    Just for a visual explanation:

    I implemented my method "getNearPortal()" in a command, when i type: "/gtap select" it will search for a portal in the radius of 10.
    When i stand next to "portal_1" everything works fine and my plugin says "The selected portal is: portal_1"
    When i do the same with "portal_2" it says: No near portal was found.

    I checked the locations and they are EXACTLY the same so it should match and say: "portal_2 is the nearest blahblah" but it doesnt.
    For some reason my code only checks for id = 1.
    It doesnt loop.
     
  10. Okay nevermind there is something confusing.
    I tried to add some more portals and WOW: it works.
    For all portals BUT NOT for the newest portal.

    So if i have: portal_1, portal_2, portal_3, portal_4
    it will work for 1,2 and 3.

    If i have portal_1, portal_2, portal_3, portal_4, portal_5
    it will work for 1,2,3 and 4.

    Now i'm finally confused.