Multiple Block Selection

Discussion in 'Plugin Development' started by Sabersamus, Jul 4, 2012.

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

    Sabersamus

    Hello,
    I'm trying to make a custom plugin 'like' world-edit without all the un-needed stuff.

    I have already made the block selection Listener class, but now I'm stuck on getting every block within the selection.

    Here's what i use for selection
    Code:java
    1.  
    2. public class Selector implements Listener{
    3.  
    4. public static HashMap<Player, Block> block1 = new HashMap<Player, Block>();
    5. public static HashMap<Player, Block> block2 = new HashMap<Player, Block>();
    6.  
    7. @EventHandler
    8. public void onSelect(PlayerInteractEvent event){
    9. Player player = event.getPlayer();
    10. Action action = event.getAction();
    11. ItemStack item = player.getItemInHand();
    12.  
    13. if(item.getType() == Material.WOOD_SPADE){
    14. if(action == Action.RIGHT_CLICK_BLOCK){
    15. if(player.hasPermission("fill.select")){
    16.  
    17. if(!block1.containsKey(player) && !block2.containsKey(player)){
    18. block1.put(player, event.getClickedBlock());
    19.  
    20. player.sendMessage(ChatColor.YELLOW + "Selected block one");
    21.  
    22. return;
    23. }
    24.  
    25. if(block1.containsKey(player) && !block2.containsKey(player)){
    26. block2.put(player, event.getClickedBlock());
    27.  
    28. player.sendMessage(ChatColor.YELLOW + "Selected block two");
    29.  
    30. return;
    31. }
    32.  
    33. if(block1.containsKey(player) && block2.containsKey(player)){
    34. block1.remove(player);
    35. block2.remove(player);
    36. block1.put(player, event.getClickedBlock());
    37.  
    38. player.sendMessage(ChatColor.YELLOW + "Selected block one");
    39.  
    40. return;
    41. }
    42.  
    43. }
    44. }
    45. }
    46. }
    47. }


    so now i need a way to get all the blocks between the blocks in the hashmaps :/

    any suggestions/ideas?
     
  2. Offline

    Milkywayz

    Just a heads up, making a plugin 'lile' worldedit is not quite suited for developers that aren't prepared to do a lot of math. But since you are trying to make a custom plugin 'like' worldedit, than your best bet would be to study worldedit. sk89q licensed his plugin under the gpl3, so you can look at his source and make your plugin based on that. I wouldnt recommend coping and pasted any of his code because 1. that won't work, plugins like worldedit cross reference a lot of class files so you'd end up having to copy and paste a ton of files. and 2. thats not coding its copy and paste :p
     
  3. Offline

    Sabersamus

    I got it thanks.

    I talked to a friend and he recommended using 3 loops
    Code:java
    1.  
    2. Location loc1 = Selector.block1.get(player).getLocation();
    3. Location loc2 = Selector.block2.get(player).getLocation();
    4.  
    5. int startX = Math.min(loc1.getBlockX(), loc2.getBlockX());
    6. int endX = Math.max(loc1.getBlockX(), loc2.getBlockX());
    7.  
    8. int startY = Math.min(loc1.getBlockY(), loc2.getBlockY());
    9. int endY = Math.max(loc1.getBlockY(), loc2.getBlockY());
    10.  
    11. int startZ = Math.min(loc1.getBlockZ(), loc2.getBlockZ());
    12. int endZ = Math.max(loc1.getBlockZ(), loc2.getBlockZ());
    13.  
    14. for (int x = startX; x <= endX; x++) {
    15. for (int y = startY; y <= endY; y++) {
    16. for (int z = startZ; z <= endZ; z++) {
    17. Location loc = new Location(player.getWorld(), x,y,z);
    18. Block block = loc.getBlock();
    19. if(material != null){
    20. block.setType(material);
    21. }
    22. }
    23. }
    and it works fine
     
Thread Status:
Not open for further replies.

Share This Page