Solved How to detect if block is inside entity (EnderCrystel)

Discussion in 'Plugin Development' started by dumbasPL, Jul 23, 2017.

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

    dumbasPL

    I want to block players ftom putting blocks inside EnderCrystals using pistons.
    I have this code:
    Code:
    @EventHandler
    public void onExtend(BlockPistonExtendEvent e){
        if(!e.getBlocks().isEmpty()){
            Block b = e.getBlocks().get(e.getBlocks().size() - 1).getRelative(e.getDirection());
            // if block is inside ender crystall then event is canceld
        }
    }
    
     
  2. Offline

    Machine Maker

    @dumbasPL
    I think you can just check if the block type is equal to Material.END_CRYSTAL
     
  3. Offline

    dumbasPL

    EnderCrystal is entity not a block.
    Code:
    Bukkit.getServer().broadcastMessage(b.getType().name());
    
    gives: AIR if i try to do it
     
  4. Offline

    Machine Maker

    @dumbasPL
    Ok, so this is the best way I could find to do it.
    Code:
    for (Entity e : b.getLocation.getChunk().getEntities()) {
        if (e.getType().equals(EntityType.ENDER_CRYSTAL) {
    
            if (e.getLocation().equals(b.getLocation()) {
                //Theres the entity
            }
        }
    }
     
  5. Offline

    dumbasPL

    Ender crystal is 2x2x2 block entity, your code works only if it is pefectly aligned and on only one of 8 posible locations
    I come up witch witch tihs:
    Code:
    @EventHandler
        public void onExtend(BlockPistonExtendEvent e){
            if(!e.getBlocks().isEmpty()){
                Block b = e.getBlocks().get(e.getBlocks().size() - 1).getRelative(e.getDirection());
                for (Entity ee : b.getLocation().getChunk().getEntities()) {
                    if (ee.getType().equals(EntityType.ENDER_CRYSTAL)) {
                        Block el = ee.getLocation().getBlock();
                        Block bl = b.getLocation().getBlock();
                        boolean cancel = false;
                        if (bl.getRelative(0, 0, 0).equals(el)) cancel = true;
                        if (bl.getRelative(1, 0, 0).equals(el)) cancel = true;
                        if (bl.getRelative(0, 0, 1).equals(el)) cancel = true;
                        if (bl.getRelative(1, 0, 1).equals(el)) cancel = true;
                        if (bl.getRelative(0, -1, 0).equals(el)) cancel = true;
                        if (bl.getRelative(1, -1, 0).equals(el)) cancel = true;
                        if (bl.getRelative(0, -1, 1).equals(el)) cancel = true;
                        if (bl.getRelative(1, -1, 1).equals(el)) cancel = true;
                        if (bl.getRelative(-1, 0, 0).equals(el)) cancel = true;
                        if (bl.getRelative(0, 0, -1).equals(el)) cancel = true;
                        if (bl.getRelative(-1, 0, -1).equals(el)) cancel = true;
                        if (bl.getRelative(0, -1, 0).equals(el)) cancel = true;
                        if (bl.getRelative(-1, -1, 0).equals(el)) cancel = true;
                        if (bl.getRelative(0, -1, -1).equals(el)) cancel = true;
                        if (bl.getRelative(-1, -1, -1).equals(el)) cancel = true;
                        if(cancel) e.setCancelled(true);
                    }
                }
            }
        }
    
    It has two false positeves around one corner but i will work for me.
    thx for help. Marking as solved.
     
Thread Status:
Not open for further replies.

Share This Page