Getting a cylinder of blocks

Discussion in 'Plugin Development' started by Coopah, Jan 13, 2016.

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

    Coopah

    So, I'm trying to get a cylinder of blocks around a player and then check if say 60% of them are air. This would theoretically determine whether a players in a cave or not. This is what I've got so far and it doesn't seem to get passed the check of the block type, it seems to stop there.

    Code:
    public boolean isInCave(Player p) {
    
            Location l = p.getLocation();
    
            double i = l.getX();
            double k = l.getZ();
    
            int r = 5;
            int rsquared = r*r;
           
            for(int x = -r; x < r; x++){
    
                for(int z = -r; z < r; z++){
    
                    if(x*x + z*z > rsquared)
    
                        continue;
    
                    Location loc = new Location(p.getWorld(), i+x, 4, k+z);
    
                    int amount = 0;
                   
                    if (loc.getBlock().getType() == Material.AIR) {
    
                        amount = amount + 1;
    
                        if (amount > 6) {
    
                            return true;
    
                        } else {
    
                            return false;
    
                        }
                    }
                }
            }
    
            return false;
    
        }
     
  2. Offline

    ski23

    Try removing the continue and doing it a bit differently.
    I would check the converse of that and if so return false
     
  3. Offline

    567legodude

    @Coopah It could also be because you are setting the Y level to 4 every time.
     
  4. Offline

    Coopah

  5. Offline

    mythbusterma

    @Coopah

    Have you changed your code? Also, you can do World#getHighestBlockAt(...) to find out if the block faces the sky.
     
  6. Offline

    tommyhoogstra

    Hmm,

    Code:
      for(int x = -r; x < r; x++){
                for(int z = -r; z < r; z++){
                    if(x*x + z*z > rsquared)
                        continue;
                    Location loc = new Location(p.getWorld(), i+x, 4, k+z);
                    int amount = 0;
                  
                    if (loc.getBlock().getType() == Material.AIR) {
                        amount = amount + 1;
                        if (amount > 6) {
                            return true;
                        } else {
                            return false;
    
    You're setting the amount to 0 every iteration, change it to

    Code:
      
    
                    int amount = 0;
    
    for(int x = -r; x < r; x++){
    
                for(int z = -r; z < r; z++){
    
                    if(x*x + z*z > rsquared)
    
                        continue;
    
                    Location loc = new Location(p.getWorld(), i+x, 4, k+z);
                  
                    if (loc.getBlock().getType() == Material.AIR) {
    
                        amount++;
    
                        if (amount > 6) {
    
                            return true;
    
                        } else {
    
                            return false;
    
     
  7. Offline

    Coopah

    @tommyhoogstra @mythbusterma
    I fixed that and added a y dynamic, but no success.
    Code:
    public boolean isInCave(Player p) {
    
            Location l = p.getLocation();
    
            double i = l.getX();
            double k = l.getZ();
            double a = l.getY();
    
            int r = 5;
            int rsquared = r*r;
    
            int amount = 0;
    
            for(int x = -r; x < r; x++){
    
                for(int z = -r; z < r; z++){
    
                    for(int y = -r; y < r; y++){
    
                        if(x*x + z*z > rsquared)
    
                            continue;
    
                        Location loc = new Location(p.getWorld(), i+x, a+y, k+z);
                        Bukkit.broadcastMessage("test");
    
                        if (loc.getBlock().getType() == Material.AIR) {
                            Bukkit.broadcastMessage("test3");
    
                            amount++;
    
                            if (amount > 6) {
                                Bukkit.broadcastMessage("test5");
    
                                return true;
    
                            } else {
                                Bukkit.broadcastMessage("test2");
    
                                return false;
    
                            }
                        }
                    }
                }
            }
    
            return false;
    
        }
    It seems to only run once:
    [​IMG]
     
  8. Offline

    mythbusterma

    @Coopah

    Because, in theory, you would be testing the blocks under the player's feet, since y begins at negative radius.
     
  9. Offline

    tommyhoogstra

    if its only running once you might wanna double check the math:


    1. if(x*x + z*z > rsquared)

    2. continue;
    is what i suspect, though, the forloop logic could be wrong.

    Code:
        int radius = 1;
    
                    for(int q = 0; q <=5; q++){
                        final float x = (float) (radius * Math.cos(q) );
                        final float z = (float) (radius * Math.sin(q) );
    can be used to generate a cylinder, just gotta add the Y
     
  10. Offline

    mythbusterma

    @tommyhoogstra

    He's still checking the blocks under the player's feet.
     
  11. Offline

    tommyhoogstra

    Why is that? He is adding to the players Y axis
     
  12. Offline

    Coopah

  13. Offline

    mythbusterma

    @Coopah

    I told you exactly what you're doing wrong (or at least one thing that would definitely cause an error).

    Don't "bump" the thread without demonstrating you've made some sort of effort to rectify the obvious issue.
     
    Areoace and teej107 like this.
  14. Offline

    Coopah

  15. Offline

    mythbusterma

    @Coopah

    He made a mistake in his logic. You really shouldn't get snarky with people that are not only trying to help you, but obviously understand your code better than you do. I read your updated code and said you're still checking the blocks under the player's feet.

    You're doing this:
    Code:
    double a = player.getLocation().getY();
    // ...
    int r = 5;
    // ...
    
    for (int y = -r; y < r; i++) {
       // during the first iteration, y is equal to -5
       Location loc = new Location (player.getWorld(), x, a + y,  z); // the height of the player's location plus negative 5
       // ...
    }
    
    Can you explain to me how you're not checking the block under the player's feet?
     
  16. Offline

    Coopah

    @mythbusterma
    I get 'snarky' because you post replies like this,
    and this...
    and this........
    Not only is the last thing highly provocative, but it's pointless. I'm bumping because I clearly don't see the issue. You're posting something that tells me an issue without providing information on why it is caused or even how to fix it, how is this going to help me?

    Not everyone is as experienced and observant as you. It's like a teacher giving a student an algebraic equation when they've never seen one before and saying the problem is the 'x's' aren't balanced. How is the student going to solve that?
     
  17. Offline

    Areoace

    Hue. I've noticed you do this a bit Coopy, as much as I've learned to tolerate it, it really isnt acceptable behavior on the forums. They're not being highly provocative, they're just explaining what youre doing wrong. After all, isn't that what these forums are about? :)

    EDIT: Most people expect you to have a decent understanding of Java on the forums, saying that not everyone is as experienced of observant as a reason for you to get snarky doesnt really make sense.

    That aside, @mythbusterma was right about what he said.
     
  18. Offline

    mythbusterma

    @Coopah

    I'm not sure how neutrally stating that you haven't made the required adjustments to your code is provocative, but alright.

    Also, my question asking if you have changed your code was neutral as well, and quite relevant. I don't see the issue with either of these.

    If you didn't understand why I said what I said, tag/quote me and ask a question. That demonstrates thoughtful and deliberate action. If you just type "bump," I'm left to assume you ignored my post and are asking someone to do it for you.

    Now, have you fixed the issue?
     
    Areoace likes this.
  19. Offline

    Coopah

    @Areoace
    Can you just not reply on my posts? Me and you both know who one another is, I clearly don't like you and you aren't helpful so just fuck off. Thanks :)
    Edit: just found the ignore feature

    @mythbusterma
    I disagree with you, I don't understand how you wouldn't think that is provaktive
    But, I'm not here to argue so I will just respect your opinion and move on.

    Even if I change the y, it still seems to only be checking everything once?
     
  20. Offline

    mythbusterma

    @Coopah

    It doesn't matter how many times you check the blocks under the player's feet, if you check them at all you'll get the incorrect result.

    Also, I don't appreciate you being so rudely childish to my friend.
     
    Areoace and teej107 like this.
  21. Offline

    teej107

    Is that meant to be a question?
    If suggestions still don't work, post updated code. It may also help to do some rubberduck debugging (Srsly do it).
     
  22. Offline

    timtower Administrator Administrator Moderator

    Locked because this is getting out of hand.
    @Coopah There is really no need to get rude
     
    teej107 likes this.
Thread Status:
Not open for further replies.

Share This Page