get block on location

Discussion in 'Plugin Development' started by totokaka, May 13, 2011.

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

    totokaka

    Hi
    I'm writing om my first plugin right now and need some help.
    he plugin i'm writing is a basic stuck plugin wich teleports you to the highest posible location above your head. I've selected to do it this way:
    1. get location to comand sender
    2. set the location to int values
    3. change locy to 128
    4. a while loop: as long as block on the location is air: locy++
    5. set locx, locy and locz to a location
    6. tp player to location
    I'm having trouble with the while loop, I can't find a way to find what kind of block it is o given cordiantes, how should i do this?

    PS: This is my first week with java and bukkit.
     
  2. Offline

    narrowtux

    When you have got a Location-Object, you can simply just
    Code:
    Block b = location.getBlock();
    
     
    com. BOY likes this.
  3. Offline

    totokaka

    hmm, tried it ant it seams to work, but when i try it in game i get teleportedt to my own location only on y 128
    here is my onCommand:
    Code:
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                String commandName = cmd.getName().toLowerCase();
    
                Player player = (Player) sender;
                World currentWorld = player.getWorld();
                if (commandName.equals("stuck")) {
    
                    Location loc = player.getLocation();
                    int locx = (int)loc.getX();
                    int locy = 128;
                    int locz = (int)loc.getZ();
                    Location nloc = new Location(currentWorld, locx, locy, locz);
                    Block b = nloc.getBlock();
    
                    player.sendMessage("You are located at:" + locx + "," + locy + "," + locz);
                    while(b.getType() == Material.AIR){
                        locy = locy++;
                        nloc = new Location(currentWorld, locx, locy, locz);
                    }
                    locy = locy+2;
                    nloc = new Location(currentWorld, locx, locy, locz);
                    player.teleportTo(nloc);
                    player.sendMessage("you should now have been teleported");
                    return true;
                } else {
                    player.sendMessage("That was not a valid command");
    
                }
                return true;
            }
     
  4. Offline

    narrowtux

    Seems right, just the contents of the while-loop are false:
    Code:
    while(b.getType().equals(Material.AIR)){
        locy--;
        nloc.setY(locy);
        b = nloc.getBlock();
    }
    
    1. In Java, you can't compare two objects with the == operator. You have to use the method Object.equals(Object other), like I did in the condition of the while-loop
    2. you want to go down on the y-axis, so do just locy-- and not locy = locy --
    3. You can modify the Y-coordinate of the location object by doing .setY(the-y-coord)
    4. you have to update the block after you updated the location. If you don't do this, it will run into an endless loop
     
  5. Offline

    totokaka

    thank you!
    But it's still something wrong, I just get teleported to y 128.
    here is my onCommand:
    Code:
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                String commandName = cmd.getName().toLowerCase();
    
                Player player = (Player) sender;
                World currentWorld = player.getWorld();
                if (commandName.equals("stuck")) {
    
                    Location loc = player.getLocation();
                    int locx = (int)loc.getX();
                    int locy = 128;
                    int locz = (int)loc.getZ();
                    Location nloc = new Location(currentWorld, locx, locy, locz);
                    Block b = nloc.getBlock();
    
                    player.sendMessage("You are located at:" + locx + "," + locy + "," + locz);
                    while(b.getType().equals(Material.AIR)){
                        locy--;
                        nloc.setY(locy);
                        b = nloc.getBlock();
                    }
                    locy = locy+2;
                    nloc.setY(locy);
                    player.teleportTo(nloc);
                    player.sendMessage("you should now have been teleported");
                    return true;
                } else {
                    player.sendMessage("That was not a valid command");
    
                }
                return true;
            }
    thank you for your time(and for theaching me java =)
     
  6. Offline

    narrowtux

    Maybe it's also the wrong start value for y? Because the y-coord in mine craft just ranges from 0-127.
    Ah, now it's getting clearer.
    Minecraft tells that blocks above 127 have got the type Bedrock. So the while loop will never run it's contents :D
    Just do int locy = 127; and it should be fine.
     
  7. Offline

    totokaka

    YES!
    got it to work, did some other changes than locy = 127, her is the final onCommand:
    Code:
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
                String commandName = cmd.getName().toLowerCase();
    
                Player player = (Player) sender;
                World currentWorld = player.getWorld();
                if (commandName.equals("stuck")) {
    
                    Location loc = player.getLocation();
                    int locx = (int)loc.getX();
                    int locy = 127;
                    int locz = (int)loc.getZ();
                    Location nloc = new Location(currentWorld, locx, locy, locz);
                    Block b = nloc.getBlock();
    
                    do{
                        locy--;
                        nloc.setY(locy);
                        b = nloc.getBlock();
                        if(!b.getType().equals(Material.AIR)){
                            double flocx = nloc.getX();
                            double flocy = nloc.getY();
                            double flocz = nloc.getZ();
                            flocx = flocx+0.5;
                            flocz = flocz+0.5;
                            flocy = flocy+2.0;
                            Location floc = new Location(currentWorld, flocx, flocy, flocz);
                            player.teleportTo(floc);
                            player.sendMessage("you should now have been teleported");
                            }
                        }while(b.getType().equals(Material.AIR));
    
                    return true;
                } else {
                    player.sendMessage("That was not a valid command");
    
                }
                return true;
            }
    thank you so much!
     
  8. Offline

    Shamebot

  9. Offline

    narrowtux

    It's better to code that yourself, to get more experience ;)
     
  10. Offline

    totokaka

  11. Offline

    Shamebot

    @narrowtux These methods should be way faster because they use the minecraft highmap, I think.
    Also I think he doesn't need the if block in the loop, he could just do the stuff below it and he should use (I think it's) World.getTypeIdAt(...) because it's uncached.
     
  12. Offline

    narrowtux

    I don't think you have to look at performance when performing a command and a while-loop that has a maximum of 128 runs.
     
  13. Offline

    Shamebot

    True but as said the blocks get cached and I always try to save some performance rather than loosing a bit here and there and it sums up.
     
  14. Offline

    narrowtux

    We've got high-performance CPUs with multicore and 2+ GHz for this. This short lookup will take the processor somewhat about ~100µs, if not less.
     
  15. Offline

    totokaka

    @Shamebot
    I know i don't need the if block in the while loop, but for some reason i like doing it that way.
     
Thread Status:
Not open for further replies.

Share This Page