Solved Selecting an area with 2 positions

Discussion in 'Plugin Development' started by Desle, Mar 7, 2014.

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

    Desle

    Hi,

    I'm trying to select an area with 2 postions, how would I do this? I want to loop through all locations between.
     
  2. Offline

    thepluginbros

    Hi, this is an code that get's all the block from 2 positions!
    Code:java
    1. Location loc1 = new Location(world, x, y, z);
    2. Location loc2 = new Location(world, x, y, z);
    3.  
    4. if (sender instanceof Player) {
    5. int topBlockX = (loc1.getBlockX() < loc2.getBlockX() ? loc2.getBlockX() : loc1.getBlockX());
    6. int bottomBlockX = (loc1.getBlockX() > loc2.getBlockX() ? loc2.getBlockX() : loc1.getBlockX());
    7.  
    8. int topBlockY = (loc1.getBlockY() < loc2.getBlockY() ? loc2.getBlockY() : loc1.getBlockY());
    9. int bottomBlockY = (loc1.getBlockY() > loc2.getBlockY() ? loc2.getBlockY() : loc1.getBlockY());
    10.  
    11. int topBlockZ = (loc1.getBlockZ() < loc2.getBlockZ() ? loc2.getBlockZ() : loc1.getBlockZ());
    12. int bottomBlockZ = (loc1.getBlockZ() > loc2.getBlockZ() ? loc2.getBlockZ() : loc1.getBlockZ());
    13.  
    14. for(int x = bottomBlockX; x <= topBlockX; x++){
    15. for(int z = bottomBlockZ; z <= topBlockZ; z++){
    16. for(int y = bottomBlockY; y <= topBlockY; y++) {
    17. Location location = new Location(world, x, y, z);
    18. //Do your things!
    19. }
    20. }
    21. }
    22. }
     
    Desle likes this.
  3. Offline

    tamajpm

    If you want to save 2 locations in like a configuration file you can create a hashmap with the variables String and Location. The string will be the username from the player who is selecting the 2 locations. For this method you need to create 2 hashmap's. One for the first position and the other for the second position. So if someone select point1 you want to put the name and selected point location into the first hashmap. And for the second location the same but into the second hashmap. After this you can save them.

    Code:java
    1. public static Map<String, Location> point1 = new HashMap<String, Location>();
    2. public static Map<String, Location> point2 = new HashMap<String, Location>();
    3.  
    4. @EventHandler
    5. public void onPlayerInteract(PlayerInteractEvent e) {
    6. Player p = e.getPlayer();
    7.  
    8. if(e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
    9. point1.put(p.getName(), e.getClickedBlock().getLocation());
    10. e.setCancelled(true);
    11. } else if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    12. point2.put(p.getName(), e.getClickedBlock().getLocation());
    13. e.setCancelled(true);
    14. }
    15. }
     
    Desle likes this.
Thread Status:
Not open for further replies.

Share This Page