How to do block selection?

Discussion in 'Plugin Development' started by ludo0777, May 31, 2012.

Thread Status:
Not open for further replies.
  1. I need people to be able to select blocks (like WorldEdit or similar) with a tool. Then I need to get every block in the selection and pass them on so I can use them. How do I do this?
     
  2. Look at this post I just did.
    Code:
                  int radius = 5;
                  int pX = player.getLocation().getX();
                  int pY = player.getLocation().getY();
                  int pZ = player.getLocation().getZ();
     
                  for (int x = (pX - radius); x <= (pX + radius); x++)
                  {
                    for(int y = (pY - radius); y <= (pY + radius); y++)
                    {
                    for(int z = (pZ - radius); z <= (pZ + radius); z++)
                    {
                    player.getWorld().getBlockAt(new Location(player.getWorld(), (double)x, (double)y, (double)z)).setType(Material.STONE);
    That is the basic code Here is an example of it grabbing a radius from args.
    Put this in the main class

    Code:
     
        public static int argID;
        public static int argRadius;   
     
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
    {
    if(args.length > 0 && args[0].equalsIgnoreCase("commandradius"))
                {
                    if(args.length > 1)
                    {
                        // This will run if not enough args are specified...
                        if(args[1].isEmpty() && args[2].isEmpty())
                        {
                            // use me for help dialogs.
                        }
                        else
                        {
                            //sender.sendMessage("- " + args[0]);
                            String sArgs = args[1];
                            //sender.sendMessage("Radius " + args[1]);
                            argRadius = Integer.valueOf(sArgs);
                            argId = Integer.valueOf(args[2]);
                            //sender.sendMessage("Block ID was " + args[2]);
                            CommandClass.radius((Player) sender);
                        }
                    }
                }
    }
    then CommandClass.class
    Code:
    public static void radius(Player player)
          {
              //player.getPlayer().sendMessage("  " + ChatColor.GOLD + Main.argRadius); // tests if Radius was passed
                int radius = Main.argRadius;
                int pX = player.getLocation().getBlockX();
                int pY = player.getLocation().getBlockY();
                int pZ = player.getLocation().getBlockZ();
     
           
              if(radius >=101)
              {
                  player.getPlayer().sendMessage("You must specify a number smaller than 101.");
                  player.getPlayer().sendMessage("You did a radius of " + radius);
     
              }
              else
              {
                  for (int x = (pX - radius); x <= (pX + radius); x++)
                  {
                    for(int y = (pY - radius); y <= (pY + radius); y++)
                    {
                    for(int z = (pZ - radius); z <= (pZ + radius); z++)
                    {
                      //player.getPlayer().sendMessage("location => [" + x + ", " + y + ", " + z + "]");
                                //    player.getPlayer().sendMessage("1");
                                  player.getWorld().getBlockAt(new Location(player.getWorld(), (double)x, (double)y, (double)z)).setType(Material.getMaterial(argID));
                            else
                            {
                                //    player.getPlayer().sendMessage("2");
                            }
                    }
                  }
                  }
              }
    Hopefully this works. I just wrote it in time between two final exams right now. Let me know if it doesn't as I can't compile it and test it now since I never installed my graphics drivers on my laptop at school.

    EDIT: oh btw the command would be "/commandradius <radius> <block-id-to-set-to>"
     

  3. That isn't really what I need, I need to be able to get all the blocks in a cuboid worked out from two locations.
     
  4. Offline

    gus

    i'd suggest using the worldedit api. if you're using eclipse just add the newest version of worldedit as an external jar. from there, you're kinda on your own tbh. :/ i haven't found a single tutorial. however i do know you can use the worldedit selection tools and have it return a set of all blocks selected.

    if you don't wanna do that, i guess you could use a Block[][][] array. it'd be messy, but that's what comes to mind for me.
     
  5. I would use World Edit but I don't know how...
     
  6. This is what I use as a helper method:
    Code:
        public static Selection getWorldEditSelection(Player ply) {
            Plugin we = Bukkit.getPluginManager().getPlugin("WorldEdit");
            if(we != null && we instanceof WorldEditPlugin) {
                return ((WorldEditPlugin) we).getSelection(ply);
            }
            return null;
        }
    It will return a WorldEdit selection for the given player if there is any. If there is none or WorldEdit isn't running on the server, it will return null.

    You can use getMinimumPoint() and getMaximumPoint() to get the lowest and highest point in the selection (that's not necessarily what the user set as pos1 and pos2).

    Note: Of course you need to add WorldEdit to your Build Path and you should add "depend: [WorldEdit]" to your plugin.yml.
     
    ferrybig and gus like this.
  7. Offline

    gus

    thank you dude, i needed that as much as he did. someone needs to write something up for the WE api. or i just need to look at the source code more carefully...i never expected that the method i was looking for would be in the main class definition though...
     
Thread Status:
Not open for further replies.

Share This Page