Question Only place blocks in water

Discussion in 'Plugin Help/Development/Requests' started by nivek1212, Dec 29, 2014.

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

    nivek1212

    Hey guys,

    I'm making a minigame and I want players to only place blocks in water. But when I do the following

    Code:
        @EventHandler
        void onPlace(BlockPlaceEvent e) {
            if(!e.getBlockReplacedState().equals(Material.WATER)){
                e.setCancelled(true);
            }
        }
    
    }
    
    I can't place blocks anywhere.
     
  2. Offline

    ColonelHedgehog

    Two things. First of all, not sure why you're checking for the block's replaced state rather than its material.

    Use e.getBlock().getType(); instead of the getBlockReplacedState() statement.

    Also, make sure you're using == instead of .equals when comparing enums. And lastly, make sure to check for both WATER and STATIONARY_WATER.
     
  3. Offline

    nivek1212

    I want that Players only place blocks in water, thats why I use getBlockReplacedState().

    Now I have this
    Code:
    @EventHandler
        void onPlace(BlockPlaceEvent e) {
            if(!(e.getBlock().getType() == Material.WATER) || !(e.getBlock().getType() == Material.STATIONARY_WATER)){
                e.setCancelled(true);
            }
        }
    Material.STATIONARY_WATER cannot resolved
     
  4. Offline

    ColonelHedgehog

    Alright, a couple more things. Firstly, use && instead of || in this instance. Also, instead of doing !(variable == something), use variable != something.
     
  5. Offline

    nivek1212

    Hm now I get this error: Incompatible operand types org.bukkit.Material and net.minecraft.server.v1_7_R3.Material in Eclipse
    Code:
        @EventHandler
        void onPlace(BlockPlaceEvent e) {
            if(e.getBlock().getType() != Material.WATER && e.getBlock().getType() != Material.SAND){
                e.setCancelled(true);
            }
        }
    (Used sand, because STATIONARY_WATER gets underlined)
     
  6. Offline

    ColonelHedgehog

    It looks like you imported the wrong Material class. You need: org.bukkit.Material
     
  7. Offline

    nivek1212

    Code:
    package me.nivek1212.towerJump.Listeners;
    
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    
    public class blockPlaceListener implements Listener {
      
        @EventHandler
        void onPlace(BlockPlaceEvent e) {
            if(e.getBlock().getType() != Material.WATER && e.getBlock().getType() != Material.STATIONARY_WATER){
                e.setCancelled(true);
            }
        }
    
    }
    
    Still can't place anywhere.

    EDIT: I can actually place water, but I want to place blocks ONLY IN water
     
  8. Offline

    nivek1212

  9. Offline

    nivek1212

    *bump*
     
Thread Status:
Not open for further replies.

Share This Page