Resource Location Adjuster

Discussion in 'Spigot Plugin Development' started by TaskID, Nov 26, 2020.

    • Like Like x 1
    • Useful Useful x 1
  1. couple issues.
    1. im not understanding your yaw computation. to my understanding, youre allowing 16 formal directions? keeping this math in mind, you could just do more simplistic math, shown below
    2. your setting of middle x coordinates is going to put them on the wrong block in the negatives. when you cast to int, you truncate the decimal. this works fine in the positive numbers because hacking off the decimals makes the number smaller. however, it makes the negative number bigger. then you add 0.5 which puts them on the wrong block.

    Code (Java):
    public static Location adjustLocation(Location location) {
        if(location == null) return null;
        location.setPitch(0F);
        location.setYaw(Math.round(location.getYaw() / 22.5F) * 22.5F); // get the nearest 22.5 integer multiple then multiply back. this number can be changed to 45 instead of 22.5 for 8 formal directions
        location.setX(location.getBlockX() + 0.5); // #getBlockX floors the number instead of truncates, which gets the right value in all cases
        location.getZ(location.getBlockZ() + 0.5);
        return location;
    }
     
    • Winner Winner x 2
  2. This so unbelievably scummy for you to say that Warren might as well just delete his comment. My god, the dude is trying to help you.

    Ironic. If only you read the code Warren posted before complaining...
     
    • Winner Winner x 2
    • Like Like x 1
  3. I don't get what the point really is of the function. What's the point of the for loop. Because it looks like you are just counting to 360 with a -22.5 offset for some reason.

    Why not just do this
    Code (Text):
        private static final float DIRECTION_PRECISENESS = 4;
        public static Location adjustLocation(Location location) {
            if(location == null) return null;

            int direction = (int) ((location.getYaw() / 360) * DIRECTION_PRECISENESS);
         
            return new Location(location.getWorld(),
                    location.getBlockX() + 0.5,
                    location.getBlockY(),
                    location.getBlockZ() + 0.5,
                    direction * 360 / DIRECTION_PRECISENESS,
                    0);
        }
     
     
  4. It’s a sliding window, the loop goes up by +45 and checks if the yaw is within -22.5 (half window negative) and +22.5 (half window positive). If that is the case, it will lock the yaw to the negative edge +22.5 or the center of the window. It forces you to look at the closest cardinal direction.
     
    • Agree Agree x 1
  5. Seems like a weird way to calculate the closest cardinal direction, considering you can just round it to the nearest 45 degree angle.
     
    • Funny Funny x 1
  6. Lmao you are right but I won't delete my for loop now because it is working and I feel cool about it xD
     
    • Optimistic Optimistic x 1
  7. Updating it only takes a few clicks, and you learn more by doing so. Why bother posting a public resource when you're not planning to make it as efficient as possible?
     
    • Agree Agree x 3
  8. The dude is literally just a troll that post trash code and doesn't take advice from others with more experience. At this point just do not post anything. No one would want an inefficient code that can easily be made efficient. A for loop, come on now.
     
    • Agree Agree x 3
  9. You're all right guys, I was not at home and too lazy to edit it on my phone. I updated it now :)
     
    • Funny Funny x 2
    • Like Like x 1