Solved trying to check a area around a player for a type of block

Discussion in 'Plugin Development' started by Soulcrafter245, Feb 4, 2017.

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

    Soulcrafter245

    hello i'm work on a magic plugin but i can't get the idea/find how to look at a area where the player is and in that area look for a block.

    i was going to try to get the loc of the block then look for the player in a area of 3 block away.
    Code:
        public static boolean isblocknear(Player player, Material material, int depth) {
            World world = Bukkit.getWorld("Spawn");
            Location playerloc = ((Player) world.getPlayers()).getLocation().clone();
           
        for (int blocks = 1; blocks <= depth; blocks++) { // From 1 to depth
            if (playerloc.getBlock().getType() == material) { // If this is the material -> return true (break/exit loop)
    
               }   
            }
        return false;
        }
     
  2. Offline

    Zombie_Striker

    @Soulcrafter245
    1. Create three for int loops. They will be called XYZ for the XYZ coordinates. The will be equal to the player's location minus <some number>, and loop up until it reaches <some number>.
    2. Get the player, get their world, and then use .getBlockAt(X,Y,Z) to get the block at that XYZ.
    3. If that block is equal to the one you're looking for, do what you want with that block and break out of all three loops.
     
  3. Offline

    Soulcrafter245

    so this what i got done on finding the players loc and checking when a player moves
    i don't what the cpu use going up so it update only when a player moves. i suck at loops so i wonder if you can help setting the area that it need to look at. plus the area going to be 3 by 3 blocks just to find the block it looking for

    here is what i got so far
    Code:
      @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            Location l = e.getPlayer().getLocation();
                Location normalizedLocation = null;
            if(normalizedLocation == null)
                normalizedLocation = l;
            if(normalizedLocation.getBlockX() != l.getBlockX() || normalizedLocation.getBlockZ() != l.getBlockZ()) {
                l.setX(l.getBlockX());
                l.setY(l.getBlockY());
                l.setZ(l.getBlockZ());
                normalizedLocation = l;
                /* Only called when a player crosses the border between two blocks. In other words: Only called
                 * when the player's BlockX or BlockZ coordinate changes.
                 */
                onPlayerStep(e);
            }
        }
    
    
        public void onPlayerStep(PlayerMoveEvent e) {
             Player p = e.getPlayer();
              int x = p.getLocation().getBlockX(),
                  y = p.getLocation().getBlockY(),
                  z = p.getLocation().getBlockZ();
                
                for() {
                   //doSomething
           }
        }
     
    Last edited by a moderator: Feb 4, 2017
  4. Offline

    Zombie_Striker

    normalizedLocation will always be null, so it will always be equal to l. Remove the unneeded lines so that normalizedLocation just equals l.

    Since normalizedLocation will always be equal to l, this will never be true. That means the rest of your code won't run. What was this meant to do?

    You are setting these variables equal to itself. L's X is equal to L's X. It is not logically possible for it to be anything else. Remove these lines.

    You are resetting normalizedLocation back to l, the thing it was originally. What was this meant to do?

    Main problem: No. I will not write code for you. If you don't know how to set up for loops, you don't know how to work with one of the most basic, efficent, and useful features of Java. If you don't know how to set up a for loop, I highly recommend you re-learn Java again (since this is most likely not the only thing you don't have a grasp on). If you have not officially learned Java yet, you can find a tutorial at the link below:
    https://bukkit.org/threads/plugin-dev-sticky-learning-java-where-to-learn.395662/

    And no, unless you have a low-grade computer or run a really old laptop, for loops should not cause any lag.
     
  5. Offline

    Soulcrafter245

    i know how to setup it just loops it just take time for me to get everything to work right. so i thing i got this loop made.. i don't need the y loc of the player.
    Code:
        public void onPlayerMove(PlayerMoveEvent e) {
            Player player = e.getPlayer();
            Location loc = player.getLocation();
            int temp = 0;
    
            loc = loc.add(3, 0, 3);
    
            for(int i=0; i < 7 ; i++)
            {
                for(int j =0; j < 7 ; j++ )
                {
                    if( loc.getBlock().getType() == Material.CAULDRON )
                    {
                        temp++;
                    }
                    loc = loc.add(0,0,-1);
                }
                loc = loc.add(-1,0,7);
            }
            temp = 0;
        }/code]
     
  6. Offline

    Zombie_Striker

    @Soulcrafter245
    Has your problem been solved? If so, mark this thread as solved.

    BTW adding to loc will modify the lock instance. You do not need to reset loc each time you add values to the X and Z.
     
  7. Offline

    Soulcrafter245

    really? i'll change it
     
Thread Status:
Not open for further replies.

Share This Page