[SOLVED]Prevent farmland trampling

Discussion in 'Plugin Development' started by mollekake, Sep 10, 2012.

Thread Status:
Not open for further replies.
  1. Hello guys.
    In my plugin, i've made it so anyone not OP can't place or break blocks, BUT, when they jump on farmland, it gets uprooted :(
    how can i stop this?
     
  2. Make a PlayerMoveEvent and continue with the main code, if the player isn't op, to reduce server lag from checking everyones move event.
    The main code would be checking if the block the player is standing on is soil/dirt or not and set it soil/dirt if it is^^
    Could be something like this: [not tested yet]

    Code:
        public void preventUprooting(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            Block block = player.getLocation().subtract(0, 2, 0).getBlock();
            if (!(player.isOp())) {
                if (block.getType() == Material.SOIL) {             
                    block.setType(Material.SOIL);
                }
             
                if (block.getType() == Material.DIRT) {
                    block.setType(Material.DIRT);
                }
            }
         
        }
     
  3. hmm, maybe i should try that, but i would think that the wheat uproots and then it gets set to soil
     
  4. Offline

    AndyMcB1

    Factions has this feature. May I have your source code?
     
  5. Ahmet094 setting dirt to dirt or soil to soil while the player is 2 blocks above should do what exactly *confused* ?
    mollekake If you find a solution please share it as there are many plugins out there which have that problem.
     
  6. found this:
    Code:
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event)  {
            // Physical means jump on it
            if (event.getAction() == Action.PHYSICAL) {
                Block block = event.getClickedBlock();
                if (block == null) return;
                // If the block is farmland (soil)
                if (block.getType() == Material.SOIL && !plugin.wheatTrampling) {
                    // Deny event and set the block
                    event.setUseInteractedBlock(org.bukkit.event.Event.Result.DENY);
                    event.setCancelled(true);
                    block.setTypeIdAndData(block.getType().getId(), block.getData(), true);
                }
            }
        }
    in this plugin: http://dev.bukkit.org/server-mods/superwheat/
    will try, brb

    Works like a charm!!
    Code:
           
    Player player = event.getPlayer();
            if (!player.isOp()){
                // Physical means jump on it
                if (event.getAction() == Action.PHYSICAL) {
                    Block block = event.getClickedBlock();
                    if (block == null) return;
                    // If the block is farmland (soil)
                        if (block.getType() == Material.SOIL){
                            // Deny event and set the block
                            event.setUseInteractedBlock(org.bukkit.event.Event.Result.DENY);
                            event.setCancelled(true);
                            block.setTypeIdAndData(block.getType().getId(), block.getData(), true);
                        }
                }
            }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  7. mollekake Nice but a bit overkilled. This code will do the same:
    Code:java
    1. @EventHandler
    2. public void noUproot(PlayerInteractEvent event)
    3. {
    4. if(event.getAction() == Action.PHYSICAL && event.getClickedBlock().getType() == Material.SOIL)
    5. event.setCancelled(true);
    6. }

    What did I strip off? Well, first the null check on the block - it won't be null, just believe me there and don't worry. Next the op check, well, re-add it if you want or replace it with a permissions check or... Well, next I removed everything manipulating the event and/or the block as all this is still handled by cancelling the event and things like setTypeIdAndData are all other than lightweight API calls (physics/light updates in-game).
     
  8. yeah i didn't strip it down, just amde it work, but that looks pretty compact and good
     
  9. Isn't player.getLocation(); the head of the player? so -2 would be the block he's standing on.
     
  10. Ahmet094 No, the head is at player.getEyeLocation(); (which is player.getLocation(); with +1 at the y axis). Also I still don't get what you want to archive by setting the block to what it already is. But anyway, the method mollekake found is way more lightweight (starts only when needed, not at every 0,0000000[insert0shere]01 head movement of every player). :)
     
  11. Ouh, you're right.. sry, my mistake. Well I was a bit confused about it.. if he plants seed and the ground is soil and a player jumps on it => soil becomes dirt => seed pops out? And to prevent the soil becoming dirt i used the method.. Anyway, mollekakes method fits a lot better :)
     
Thread Status:
Not open for further replies.

Share This Page