prevent water overwriting by placing a block over

Discussion in 'Plugin Development' started by atesin, Dec 6, 2017.

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

    atesin

    i am witing a plugin with water .. i want to prevent water to be overwritten by placing a block over it , but i cant figure out how to do it with simple methods

    i know i can "register" water blocks in a set, list or map, but this way i have to go to check on every BlockPlaceEvent if the block belogs my structure .. i would like a way to do previous checks before go to search the array .. ideally something like

    blockPlaceEvent(BlockPlaceEvents e)
    {
    Block previous = e.getPreviousBlock();
    if (previous.getMaterial() == Material.STATIONARY_WATER)
    myPlugin.testOverwrite(e.getBlock);
    }
     
  2. Offline

    Zombie_Striker

    @atesin
    For 1.12, BlockPlaceEvent has both the 'current' (getBlockPlace)block and the previous block instance (getBlock).
     
  3. Offline

    MightyOne

  4. Offline

    atesin

    doesnt seem to work .. i tried with this code

    Code:
      public void onBlockPlace(BlockPlaceEvent e)
       {
         System.out.println("get block = "+e.getBlock());
         System.out.println("get block place = "+e.getBlockPlaced());
    ... placing a sign and a dirt block over a water block, and the result was this

    Code:
    [10:51:45 INFO]: get block = CraftBlock{chunk=CraftChunk{x=-17z=16},x=-258,y=73,z=260,type=WALL_SIGN,data=3}
    [10:51:45 INFO]: get block place = CraftBlock{chunk=CraftChunk{x=-17z=16},x=-258,y=73,z=260,type=WALL_SIGN,data=3}
    [12:11:58 INFO]: get block = CraftBlock{chunk=CraftChunk{x=-17z=16},x=-258,y=73,z=260,type=GRASS,data=0}
    [12:11:58 INFO]: get block place = CraftBlock{chunk=CraftChunk{x=-17z=16},x=-258,y=73,z=260,type=GRASS,data=0}
    >version
    [12:12:14 INFO]: This server is running CraftBukkit version git-Bukkit-22564a9 (MC: 1.12.2) (Implementing API version 1.12.2-R0.1-SNAPSHOT)
    i also tried with e.getBlock().getLocation().getBlock() but the result is the same ... also i tried the
    BlockCanBuildEvent .. seems to work but only with signs afaik

    ...

    maybe some pseudocode like this should work... but until i dont know if could be more optimal, i find more complicated than my current solution

    Code:
    myEvent (playerInteractEvent e)
    {
      if (e.getClickType() != ClickType.RIGHT_CLICK)
        return;
    
      if (e.getPlayer.getInventory.getItemInMainHand == Material.AIR)
        return;
    
      if (e.getBlock().getRelative(e.getBlockFace()).getType == Material.STATIONARY_WATER)
        myPlugin.checkWaterOverwrite(e.getBlock());
    }
    my current solution is below ... supposing that i register block locations in a set when i generate the structure ... what i dont like is that i must iterate the set on every single block placed in the server (method isPortalBlock(block)), and i think is not so good if the set grows enough

    Code:
    myCheck(BlockPlaceEvent e)
       {
         if (plugin.isPortalBlock(e.getBlock()))
           e.setCancelled(true);
       }
     
    Last edited: Dec 7, 2017
  5. Offline

    MightyOne

    This IS optimal. Except that you do the ovveride check with the wrong block in the end.
     
  6. Offline

    atesin

    that check could be done next inside the called method checkWaterOverwrite(block)

    so how to deny the player to "place" the block with this event? .. this is not BlockPlaceEvent
     
Thread Status:
Not open for further replies.

Share This Page