I have some problems with player.getTargetBlock(null, 500);

Discussion in 'Plugin Development' started by halvors, May 14, 2011.

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

    halvors

    My problem is that the playger.getTargetBlock(null, 500) won't work :(

    Here is my code:
    Code:
    if (event.hasItem()) {
                        int item = 348;
    
                        if (item != 0) {
                            if (event.getItem().getTypeId() == item) {
                                if (worldConfig.lightningEnable) {
                                    if ((action == Action.LEFT_CLICK_BLOCK) || (action == Action.LEFT_CLICK_AIR)) {
                                        world.strikeLightning(player.getTargetBlock(null, 500).getLocation());
                                    }
                                } else {
                                    player.sendMessage(ChatColor.RED + "Lightning is disabled!");
                                }
                            }
                        } else {
                            player.sendMessage(ChatColor.RED + "Error: Wand not set in configuration file!");
                        }
                    }
    
    If someone want to look in github too it's here.
     
  2. Offline

    vildaberper

    Lower it to 200, thats more than enough. :)
     
  3. Offline

    halvors

    Is it not working with more than 200?
     
  4. sure it is, but 200 is about the distance you see with fog on far
    also, it removes a lot of lag.

    btw, any errors?
     
  5. Offline

    halvors

    No errors. :(
     
  6. You could also try this:
    Code:
            Block target = player.getWorld().getBlockAt(player.getLocation());
            for (Block b : player.getLineOfSight(null, 200)) {
                if (!b.getType().equals(Material.AIR)) { target = b; break; }
            }
    
    I used that for my thundersword
     
  7. Offline

    DreadKyller

    @Streammz why did you use Block target = player.getWorld().getBlockAt(player.getLocation()); instead of Block target =player.getLocation.getBlock();

    @halvors can you paste where you set up the worldConfig? are you ever even enabling it, or something wrong with that part maybe.
     
  8. @DreadKyller becouse location.getBlock() actually returns this.world.getBlockAt(this) which is the exact same as my method, just my method should be a few ms faster theoretically
     
  9. Offline

    DreadKyller

    ok, yah, that makes sense, it doesn't have to go through the extra method...
     
  10. Offline

    halvors

    Last edited by a moderator: Jul 16, 2016
  11. Offline

    Shamebot

    Code:
        private List<Block> getLineOfSight(HashSet<Byte> transparent, int maxDistance, int maxLength) {
            if (maxDistance > 120) {
                maxDistance = 120;
            }
            ArrayList<Block> blocks = new ArrayList();
            Iterator<Block> itr = new BlockIterator(this, maxDistance);
            while (itr.hasNext()) {
                Block block = itr.next();
                blocks.add(block);
                if (maxLength != 0 && blocks.size() > maxLength) {
                    blocks.remove(0);
                }
                int id = block.getTypeId();
                if (transparent == null) {
                    if (id != 0) {
                        break;
                    }
                } else {
                    if (!transparent.contains((byte)id)) {
                        break;
                    }
                }
            }
            return blocks;
        }
    That's the code from CraftLivingEntity.
    So 120 is max, isn't it?
    Edit: Need to check in which direction the BlockIterator iterates.
     
  12. Offline

    halvors

    I got it work, not know what that was wrong. But works great now.
     
  13. Offline

    xZise

    Indeed: The max distance will be always 120.

    Fabian
     
  14. Offline

    Raphfrk

    Well, you can directly call the iterator :).

    Code:
    Iterator<Block> itr = new BlockIterator(player, maxDistance);
    Block block = null;
    while (itr.hasNext()) {
                block = itr.next();
    }
    return block;
    
    However, the problem with this is that it will load chunks along the entire line of the iterator.

    These chunks aren't unloaded, so it represents a memory leak.
     
  15. Offline

    Shamebot

    @Raphfrk could he also use 0 in getTargetBlock?
    The comment in BlockIterator says 0 means no limit, so this could lead to a memory leak as well?
     
  16. Offline

    Raphfrk

    Yeah, that would work too. I have a memory of initial testing (where it filled in the rays with blocks) gaving really long paths.

    For max effect, you would go to around y = 120 and try to keep the angle horizontal :).
     
  17. Offline

    Shamebot

    And how about the memory leak ? I imagine it would be huge,when loading so many chunks
     
  18. Offline

    Raphfrk

    Well, it would be lots of chunks in a line, but yeah, it is probably best to still limit the range to 120.
     
Thread Status:
Not open for further replies.

Share This Page