Block lava and water placement

Discussion in 'Plugin Development' started by djmaster329, Feb 21, 2012.

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

    djmaster329

    Hi,

    I've been trying to build my own plugin.
    Unfortunately two things don't work.

    I tried to block lava and water placement by using this code:
    Lava:
    Code:
    if(event.getBlock().getType() == Material.LAVA_BUCKET) {
                    event.setCancelled(true);
                    event.getPlayer().sendMessage("You tried to place LAVA, which is not allowed!");
                    log.info(event.getPlayer().getDisplayName().toString() + " placed LAVA!");
                }
                if(event.getBlock().getType() == Material.LAVA) {
                    event.setCancelled(true);
                    event.getPlayer().sendMessage("You tried to place LAVA, which is not allowed!");
                    log.info(event.getPlayer().getDisplayName().toString() + " placed LAVA!");
                }
    Water:
    Code:
    if(event.getBlock().getType() == Material.WATER_BUCKET) {
                event.setCancelled(true);
                event.getPlayer().sendMessage("You tried to place WATER, which is not allowed!");
                log.info(event.getPlayer().getDisplayName().toString() + " placed WATER!");
                }
                if(event.getBlock().getType() == Material.WATER) {
                    event.setCancelled(true);
                    event.getPlayer().sendMessage("You tried to place WATER, which is not allowed!");
                    log.info(event.getPlayer().getDisplayName().toString() + " placed WATER!");
                    }
    But for some reason, the lava/water doesn't get removed and the message doesn't show up either.

    Does anyone know what causes this?
     
  2. Offline

    Don Redhorse

    yes... the block which is placed is water or lava not the bukket
     
  3. Offline

    djmaster329

    I tried both the LAVA and LAVA_BUCKET material, but neither of them work.
     
  4. Offline

    Njol

    You have to use PlayerBucketEmptyEvent, not BlockPlaceEvent.
     
  5. Offline

    djmaster329

    Aha, will try.

    Thanks

    I added this to my code:
    Code:
    public void bukketWatchdog(PlayerBucketEmptyEvent event) {
            if(BlockLava == true){
                if(event.getBucket() == Material.LAVA_BUCKET) {
                    event.setCancelled(true);
                    event.getPlayer().sendMessage("You tried to place LAVA, which is not allowed!");
                    log.info(event.getPlayer().getDisplayName().toString() + " placed LAVA!");
                }
            }
            if(BlockWater == true){
                if(event.getBucket() == Material.WATER_BUCKET){
                    event.setCancelled(true);
                    event.getPlayer().sendMessage("You tried to place WATER, which is not allowed!");
                    log.info(event.getPlayer().getDisplayName().toString() + " placed WATER!");
                }
            }
        }
    But it still doesn't work.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  6. Offline

    CXdur

    To block bucket placing use playerinteract, to block the BLOCK ( id 8, 9 , 10 ? ) use the blockplaceevent.
     
  7. Offline

    djmaster329

    Could you give me an example of how that code should look like?
    I'm new to Java ;)

    Thanks
     
  8. Offline

    Njol

    Is the method called? You can check this by logging something on the first line of the method. If it's not called, check whether you used the new event system correctly (which I assume), and try to use PlayerBucketEvent with a check whether the event was an instance of PlayerBucketEmptyEvent.
     
  9. Offline

    djmaster329

    Hmm.

    Neither PlayerBucketEmptyEvent or PlayerBucketEvent is called.
    Perhaps something is wrong with my code?
    Code:
    package me.djmaster329.ServerGuard;
     
    import java.util.logging.Logger;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.PlayerBucketEmptyEvent;
    import org.bukkit.event.player.PlayerBucketEvent;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
     
     
     
    public class ServerGuard extends JavaPlugin implements Listener {
        public static boolean BlockLava = true;
        public static boolean BlockWater = true;
        public static boolean BlockTNT = true;
        public static boolean BlockBedrock = true;
        public static boolean BlockFire = true;
        Logger log = Logger.getLogger("Minecraft");
       
        @Override
        public void onEnable() {
           
            //this method is called once your plugin is enabled
     
            //this gets the plugin manager for our plugin
            PluginManager pm = this.getServer().getPluginManager();
     
            /*
            * this registers our listener. The first argument is the listener the second the plugin
            * if you put the listener into a different class you have to put an istance of that class
            * as the first argument
            */
            pm.registerEvents(this, this);
            log.info("ServerGuard is enabled!");
        }
     
        public void onDisable() {
            //this method is called once your plugin is diabled you can save stuff or clean up here
            log.info("ServerGuard is disabled!");
        }
     
        /*
        * The on command method is called every time a command which is registered for your plugin is called
        * @sender: this is the sender of the command. Can be a player or the command line
        * [USER=55020]CMD[/USER]: a command object. Look into the API docs for what you can do with it
        * @commandLabel: a string of which command has been issued. Useful if you have more than one command
        * @args: an array of arguments supplied to the command
        */
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
           
            if(cmd.getName().equalsIgnoreCase("disable")){
                if(sender instanceof Player && ((Player)sender).isOp()){
                    if(getServer().getPluginManager().isPluginEnabled(this) == true){
                        getServer().getPluginManager().disablePlugin(this);
                    }   
            }
                return true;
            }
           
            if(cmd.getName().equalsIgnoreCase("enable")){
                if(sender instanceof Player && ((Player)sender).isOp()){
                    if(getServer().getPluginManager().isPluginEnabled(this) == false){
                        getServer().getPluginManager().enablePlugin(this);
                    }
                return true;
                }
            }
           
            //Adding commands for turning on and off blockPlaceEvents
            if(cmd.getName().equalsIgnoreCase("lava-on")){
                if(sender instanceof Player && ((Player)sender).isOp()){
                    BlockLava = true;
                return true;
                }
            }
            if(cmd.getName().equalsIgnoreCase("lava-off")){
                if(sender instanceof Player && ((Player)sender).isOp()){
                    BlockLava = false;
                return true;
                }
            }
           
            if(cmd.getName().equalsIgnoreCase("water-on")){
                if(sender instanceof Player && ((Player)sender).isOp()){
                    BlockWater = true;
                return true;
                }
            }
            if(cmd.getName().equalsIgnoreCase("water-off")){
                if(sender instanceof Player && ((Player)sender).isOp()){
                    BlockWater = false;
                return true;
                }
            }
           
            if(cmd.getName().equalsIgnoreCase("TNT-on")){
                if(sender instanceof Player && ((Player)sender).isOp()){
                    BlockTNT = true;
                return true;
                }
            }
            if(cmd.getName().equalsIgnoreCase("TNT-off")){
                if(sender instanceof Player && ((Player)sender).isOp()){
                    BlockTNT = false;
                return true;
                }
            }
           
            if(cmd.getName().equalsIgnoreCase("bedrock-on")){
                if(sender instanceof Player && ((Player)sender).isOp()){
                    BlockBedrock = true;
                return true;
                }
            }
            if(cmd.getName().equalsIgnoreCase("bedrock-off")){
                if(sender instanceof Player && ((Player)sender).isOp()){
                    BlockBedrock = true;
                return true;
                }
            }
           
            if(cmd.getName().equalsIgnoreCase("fire-on")){
                if(sender instanceof Player && ((Player)sender).isOp()){
                    BlockFire = true;
                return true;
                }
            }
            if(cmd.getName().equalsIgnoreCase("fire-off")){
                if(sender instanceof Player && ((Player)sender).isOp()){
                    BlockFire = false;
                return true;
                }
            }
            return false;
            }
       
     
        /*
        * this is an event hanldler event handlers have to be in classes which implement Listener
        * the @EventHandler annotation marks this method as an event handler
        * the method name can be anything you like
        * event handler have one parameter which also defines which event they handle
        */
        @EventHandler
        public void loginHandler(PlayerLoginEvent event) {
            /*
            * this method is called every time a player logs in you can find out more about the event
            * via the given parameter
            * e.g. we can determine which player logged in and send him a welcome message
            */
            //event.getPlayer().sendMessage("Welcome to the sever!");
        }
     
        /*
        * This is another event handler with high priority. So if there are other handlers for the same
        * event this one will be called first
        */
       
        @EventHandler(priority = EventPriority.HIGH)
        public void blockWatchdog(BlockPlaceEvent event) {
            /*
            if(BlockLava == true){
                if(event.getBlock().getType() == Material.LAVA){
                    event.setCancelled(true);
                    event.getPlayer().sendMessage("You tried to place LAVA, which is not allowed!");
                    log.info(event.getPlayer().getDisplayName().toString() + " placed LAVA!");
                }
            }
            if(BlockWater == true){
                if(event.getBlock().getType() == Material.WATER){
                    event.setCancelled(true);
                    event.getPlayer().sendMessage("You tried to place WATER, which is not allowed!");
                    log.info(event.getPlayer().getDisplayName().toString() + " placed WATER!");
                }
            }*/
            if(BlockTNT == true){
                if(event.getBlock().getType() == Material.TNT) {
                event.setCancelled(true);
                event.getPlayer().sendMessage("You tried to place TNT, which is not allowed!");
                log.info(event.getPlayer().getDisplayName().toString() + " placed TNT!");
                }
            }
            if(BlockBedrock == true){
                if(event.getBlock().getType() == Material.BEDROCK) {
                event.setCancelled(true);
                event.getPlayer().sendMessage("You tried to place BEDROCK, which is not allowed!");
                log.info(event.getPlayer().getDisplayName().toString() + " placed BEDROCK!");
                }
            }
            if(BlockFire == true){
                if(event.getBlock().getType() == Material.FIRE) {
                event.setCancelled(true);
                event.getPlayer().sendMessage("You tried to place FIRE, which is not allowed!");
                log.info(event.getPlayer().getDisplayName().toString() + " placed FIRE!");
                }
            }
           
        }
        public void bucketWatchdog(PlayerBucketEmptyEvent event) {
            if(BlockLava == true){
                if(event.getBucket() == Material.LAVA_BUCKET) {
                    event.setCancelled(true);
                    event.getPlayer().sendMessage("You tried to place LAVA, which is not allowed!");
                    log.info(event.getPlayer().getDisplayName().toString() + " placed LAVA!");
                }
            }
            if(BlockWater == true){
                if(event.getBucket() == Material.WATER_BUCKET){
                    event.setCancelled(true);
                    event.getPlayer().sendMessage("You tried to place WATER, which is not allowed!");
                    log.info(event.getPlayer().getDisplayName().toString() + " placed WATER!");
                }
            }
        }
       
    }
     
  10. Offline

    Sorroko

    Somehow comparing a bucket item to a material doesnt seem right, try using event.getBucket().name() you will have to check the output as I have no idea what it returns. maybe "WaterBucket" no idea

    EDIT:
    I found another solution which might work:
    Code:
    event.getItemStack().getType()== Material.LAVA_BUCKET
     
  11. Offline

    Njol

    you forgot @EventHandler
    getBucket() returns a Material, so it's perfectly valid. But I agree that the method name is bad, it should be getBucketMaterial(), getBucketType() or getBucket().getType() (where getBucket() would return an itemstack)
     
  12. Offline

    djmaster329

    Okay, I will try it as soon as I'm back home

    Thank
     
  13. Offline

    Sorroko

    Njol djmaster329 Yeah I agree about the name but using event.getItemStack might be a better option as you could check for more buckets and remove them(if they somehow managed to get a stack of them), just an idea
     
  14. Offline

    Njol

    getItemStack() returns the bucket AFTER it was emptied, which usually is an empty bucket.
     
  15. Offline

    Sorroko

    ah well blame bukkit for that
     
  16. Offline

    Njol

  17. Offline

    djmaster329

    The problem is fixed :)
    I just forgot to add @EventHandler above the void.
    Thanks you very much for your help :)
     
  18. Offline

    djmaster329

    If I want to add permissions to it.
    Did I do this right:
    Code:
    if(BlockBedrock == true){
                if(event.getBlock().getType() == Material.BEDROCK) {
                    if(event.getPlayer().hasPermission("ServerGuard.bedrock")){
                       
                    }else{
                        event.setCancelled(true);
                        event.getPlayer().sendMessage(ChatColor.RED + "You tried to place BEDROCK, which is not allowed!");
                        log.info(event.getPlayer().getDisplayName().toString() + " placed BEDROCK!");
                    }
                }
               
                }
            }
     
  19. Offline

    xAtilax

    which is the permissions of TNT:
    ServerGuard v1.2
     
  20. Offline

    djmaster329

    I did not add them yet. I will probably do this this week.
     
  21. Offline

    xAtilax

    ok thx pls say me..
     
Thread Status:
Not open for further replies.

Share This Page