.getTargetBlock

Discussion in 'Plugin Development' started by JjPwN1, Sep 10, 2012.

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

    JjPwN1

    I am making a reach plugin, where when you type /reach, it toggles reach mode and in reach mode you can break blocks farther away (obviously). I am having trouble with the .getTargetBlock part... what do I put as the 2 arguments?

    Code:
        @EventHandler
        public void onLeftClick(PlayerInteractEvent event){
            if(isReaching == true){
                if(event.getAction().equals(Action.LEFT_CLICK_AIR) || event.getAction().equals(Action.LEFT_CLICK_BLOCK)){
                    Block targetBlock = event.getPlayer().getTargetBlock(arg0, arg1);
                    targetBlock.breakNaturally();
                }
            }
        }
    }
     
  2. Offline

    vildaberper

    arg0 is blocks to ignore (?), but i usually set it to null.
    arg1 is how far it'll look, about 200 is more than enough.
     
  3. Offline

    JjPwN1

    It isn't working... XD

    Code:
        public boolean isReaching = false;
       
        public void onEnable(){
            getLogger().info("version v" + this.getDescription().getVersion() + " is enabled!");
        }
        public void onDisable(){
            getLogger().info("version v" + this.getDescription().getVersion() + " is disabled!");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if(player.hasPermission("bbyreach.reach") || player.isOp()){
                if(cmd.getName().equalsIgnoreCase("bbyreach")){
                    if(isReaching == false){
                        isReaching = true;
                        player.sendMessage(ChatColor.GOLD + "[BbyReach] " + ChatColor.DARK_RED + "Reach mode activated!");
                    }else if(isReaching == true){
                        isReaching = false;
                        player.sendMessage(ChatColor.GOLD + "[BbyReach] " + ChatColor.DARK_RED + "Reach mode deactivated!");
                    }
                }
            }
            return false;
        }
        @EventHandler
        public void onLeftClick(PlayerInteractEvent event){
            if(isReaching == true){
                if(event.getAction().equals(Action.LEFT_CLICK_AIR) || event.getAction().equals(Action.LEFT_CLICK_BLOCK)){
                    Block targetBlock = event.getPlayer().getTargetBlock(null, 200);
                    targetBlock.breakNaturally();
                }
            }
        }
    }
    It doesn't break the targetBlock. The only working function of this plugin is the /bbyreach activate and deactivate.
     
  4. Offline

    vildaberper

    You never register your listener. ;)
     
  5. Offline

    JjPwN1

    Awww shet.

    vildaberper
    One more question: How do you place a block instead of destroy?

    Code:
        public void onRightClick(PlayerInteractEvent event){
            if(isReaching == true){
                if(event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
                    Block targetBlock = event.getPlayer().getTargetBlock(null, 200);
                    targetBlock.??
                }
            }
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  6. Offline

    vildaberper

    if(player.hasPermission("bbyreach.reach") || player.isOp()){

    can be replaced with:

    if(player.hasPermission("bbyreach.reach")){

    because ops always have permission. :)

    }else if(isReaching == true){

    can be replaced with:

    }else{

    because if isReaching isn't true, it must be false.

    It's also a good thing to check if the CommandSender is a player before you cast it as one. ;)

    I'm a programming nazi. :D

    Doublepost, I'm far too lazy to fix that.

    The way I do it is ugly, but it works. You need to do something like this:
    Code:
    Block lastBlock = player.getTargetBlock(null, 1);
     
    for(int i = 2; i <= 200; i++){
        Block current = player.getTargetBlock(null, i);
       
        if(!current.getType().equals(Material.AIR))
            break;
        else
            lastBlock = current;
    }
    // lastBlock is the block you want to change
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  7. Offline

    JjPwN1

    Where is the double post?
     
  8. Offline

    vildaberper

    Kinda OT, but I just made one. :)

    Did my answer help you?
     
  9. Offline

    JjPwN1

    Question!

    I got the right click and stuff down, but I am unsure how to get the block above the target block, and place the block in hand there. Thanks for your answer, though. Here is my current code:

    Code:
        @EventHandler
        public void onRightClick(PlayerInteractEvent event){
            if(isReaching == true){
                if(event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
                    Block targetBlock = event.getPlayer().getTargetBlock(null, 200);
                    Location targetBlockLocation = targetBlock.getLocation();
                    Player player = event.getPlayer();
                    Bukkit.getWorld("world").getBlockAt(targetBlockLocation).setTypeId(player.getItemInHand().getTypeId());
                }
            }
        }
    }
    How would I get the block's Y coordinate and add one block to it?
     
  10. Offline

    Giant

    Please don't state false information! Even ops do NOT always have permissions! They ONLY have it by default if it set to either "true" or "op" in your plugin.yml!


    JjPwN1
    use targetBlockLocation.add(0, 1, 0);
     
  11. Offline

    JjPwN1

    Ah, yes. Thank you.
     
Thread Status:
Not open for further replies.

Share This Page