SugarCanePlace(Cancel It From Breaking)

Discussion in 'Plugin Development' started by CONTREKEE, Jan 5, 2015.

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

    CONTREKEE

    I have this code so far and I don't know why the sugarcane is still breaking.

    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public void NoBreakSugarCane(BlockPhysicsEvent e) {
           
            if(e.getBlock().equals(Material.SUGAR_CANE) || e.getBlock().equals(Material.SUGAR_CANE_BLOCK)) {
                e.setCancelled(true);
            }
           
        }
       
        @EventHandler(priority = EventPriority.HIGH)
        public void PlaceSugar(BlockCanBuildEvent e) {
           
            if(e.getMaterial().equals(Material.SUGAR_CANE)) {
                e.setBuildable(true);
               
                e.getBlock().getLocation().getBlock().setType(Material.SUGAR_CANE_BLOCK);
               
            }
           
        }
     
  2. Offline

    SuperOriginal

    You're comparing a block object to a Material... When is it going to be equal?

    You need the getType() method to get a block's material.
     
    Konato_K likes this.
  3. Offline

    Konato_K

    Adding to this, you're using block physics event, so this works when you break the block where sugar cane is standing, but you can still break it manually
    @CONTREKEE
     
    SuperOriginal likes this.
  4. Offline

    CONTREKEE

    @Konato_K I am trying to make it so sugar cane never breaks if there isn't water by it.
     
  5. Offline

    xMakerx

    Check out the BlockBreakEvent, and do what they said above. If you cancel the BlockBreakEvent the block won't be broken.
     
  6. Offline

    CONTREKEE

    @xMakerx The sugarcane still breaks when I do this. I think it is when the server updates and registers the sugar cane isn't by water. Know how I could fix this?


    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public void NoBreakSugarCane(BlockPhysicsEvent e) {
           
            if(e.getBlock().equals(Material.SUGAR_CANE) || e.getBlock().equals(Material.SUGAR_CANE_BLOCK)) {
                e.setCancelled(true);
            }
           
        }
       
        @EventHandler(priority = EventPriority.HIGHEST)
        public void NoNoNoBreakSugarCane(BlockBreakEvent e) {
           
            if(e.getBlock().getType().equals(Material.SUGAR_CANE_BLOCK) || e.getBlock().getType().equals(Material.SUGAR_CANE)) {
                        e.setCancelled(true);
                        e.getBlock().getDrops().clear();
                    }
           
           
        }
       
        @EventHandler(priority = EventPriority.HIGH)
        public void PlaceSugar(BlockCanBuildEvent e) {
           
            if(e.getMaterial().equals(Material.SUGAR_CANE)) {
                e.setBuildable(true);
               
                e.getBlock().getLocation().getBlock().setType(Material.SUGAR_CANE_BLOCK);
               
            }
           
        }
     
  7. Offline

    sirrus86

    As @SuperOriginal said, your issue may be this line here:
    Code:
    @EventHandler(priority = EventPriority.HIGHEST)
        public void NoBreakSugarCane(BlockPhysicsEvent e) {
          
            if(e.getBlock().equals(Material.SUGAR_CANE) || e.getBlock().equals(Material.SUGAR_CANE_BLOCK)) {
                e.setCancelled(true);
            }
          
        }
    Instead of e.getBlock().equals(Material.SUGAR_CANE) you should be doing e.getBlock().getType() == Material.SUGAR_CANE

    Otherwise I believe BlockPhysicsEvent is the event you're looking for, but I could be wrong.
     
  8. Offline

    CONTREKEE

    @sirrus86 It still didn't work, and I am using the block physics event.
     
  9. Offline

    Konato_K

    @CONTREKEE Could you post the code of what "didn't work"?
     
  10. Offline

    xMakerx

    @CONTREKEE

    Ugh, our stuff should work, are you sure you registered your events. Make sure to do that, because that's what I suspect is the reason this stuff doesn't work.
     
  11. Offline

    CONTREKEE

    @xMakerx Yeah, I registered the events and here is the code @Konato_K

    Code:
          @EventHandler(priority = EventPriority.HIGHEST)
            public void NoBreakSugarCane(BlockPhysicsEvent e) {
            
                if(e.getBlock().getType().equals(Material.SUGAR_CANE) || e.getBlock().equals(Material.SUGAR_CANE_BLOCK)) {
                    e.setCancelled(true);
                }
            
            }
       
        @EventHandler(priority = EventPriority.HIGHEST)
        public void NoNoNoBreakSugarCane(BlockBreakEvent e) {
           
            if(e.getBlock().getType().equals(Material.SUGAR_CANE_BLOCK) || e.getBlock().getType().equals(Material.SUGAR_CANE)) {
                        e.setCancelled(true);
                        e.getBlock().getDrops().clear();
                    }
           
           
        }
       
        @EventHandler(priority = EventPriority.HIGH)
        public void PlaceSugar(BlockCanBuildEvent e) {
           
            if(e.getMaterial().equals(Material.SUGAR_CANE)) {
                e.setBuildable(true);
               
                e.getBlock().getLocation().getBlock().setType(Material.SUGAR_CANE_BLOCK);
               
            }
           
        }
     
  12. Offline

    teej107

    @CONTREKEE
    fault here
     
  13. Offline

    drpk

    e.getBlock().getDrops().clear();

    IIRC this will clear a clone of the list of drops.
     
  14. Offline

    teej107

    As well as editing the event outcome is pointless if you are cancelling it.
     
  15. Offline

    xMakerx

    @teej107

    I didn't even notice the .equals() instead of == for the enum. Wow, I should really stop staying up all night.
     
  16. Offline

    CONTREKEE

    Ok, here is the code now, and the sugarcane still breaks after a little bit.
    @teej107 @drpk


    Code:
          @EventHandler(priority = EventPriority.HIGHEST)
            public void NoBreakSugarCane(BlockPhysicsEvent e) {
            
                if(e.getBlock().getType().equals(Material.SUGAR_CANE)) {
                    e.setCancelled(true);
                }
            
            }
       
        @EventHandler(priority = EventPriority.HIGHEST)
        public void NoNoNoBreakSugarCane(BlockBreakEvent e) {
           
            if(e.getBlock().getType().equals(Material.SUGAR_CANE_BLOCK) || e.getBlock().getType().equals(Material.SUGAR_CANE)) {
                        e.setCancelled(true);
                    }
           
           
        }
       
        @EventHandler(priority = EventPriority.HIGH)
        public void PlaceSugar(BlockCanBuildEvent e) {
           
            if(e.getMaterial().equals(Material.SUGAR_CANE_BLOCK) || e.getMaterial().equals(Material.SUGAR_CANE)) {
                e.setBuildable(true);
               
            }
           
        }
     
  17. Offline

    xMakerx

    @CONTREKEE

    You still didn't do what @teej107 said. You need to change line 4 and 13 to use == instead of .equals().
     
  18. Offline

    teej107

    @xMakerx I didn't say he should use ==. Though he should. I was pointing out that the fault was comparing Material to a Block.

    EDIT: He should compare the Material SUGAR_CANE_BLOCK
     
  19. Offline

    xMakerx

    @teej107

    Shit, again I screwed up. This is why sleep-deprivation is a bad thing.
     
  20. Offline

    CONTREKEE

    Code:
          @EventHandler(priority = EventPriority.HIGHEST)
            public void NoBreakSugarCane(BlockPhysicsEvent e) {
           
                if(e.getBlock().getType() == Material.SUGAR_CANE_BLOCK) {
                    e.setCancelled(true);
                }
           
            }
      
        @EventHandler(priority = EventPriority.HIGHEST)
        public void NoNoNoBreakSugarCane(BlockBreakEvent e) {
          
            if(e.getBlock().getType() == Material.SUGAR_CANE_BLOCK || e.getBlock().getType().equals(Material.SUGAR_CANE)) {
                        e.setCancelled(true);
                    }
          
          
        }
      
        @EventHandler(priority = EventPriority.HIGH)
        public void PlaceSugar(BlockCanBuildEvent e) {
          
            if(e.getMaterial().equals(Material.SUGAR_CANE_BLOCK) || e.getMaterial().equals(Material.SUGAR_CANE)) {
                e.setBuildable(true);
              
            }
          
        }
    @xMakerx @teej107 Ok I updated it.


    Then it does this after a little.


    Starts out fine:

    2015-01-06_15.18.14.png

    Then does this:

    2015-01-06_15.18.15.png

    And overtime it does all of it
     
  21. Offline

    xMakerx

    @CONTREKEE

    Have your plugin print out a message when it finds that a sugar cane block was broken to debug to see if the events are really being called, then show a picture of your console and the new code.
     
  22. Offline

    teej107

  23. Offline

    CONTREKEE

    @xMakerx @teej107 Both the events BlockPhysicsEvent and BlockFadeEvent are not being called when the sugar cane breaks.

    Code:
          @EventHandler(priority = EventPriority.HIGHEST)
            public void NoBreakSugarCane(BlockFadeEvent e) {
            
                if(e.getBlock().getType() == Material.SUGAR_CANE_BLOCK) {
                    System.out.print("Not breaking ================================");
                    e.setCancelled(true);
                }
            
            }
         
          @EventHandler(priority = EventPriority.HIGHEST)
            public void NooBreakSugarCane(BlockPhysicsEvent e) {
            
                if(e.getBlock().getType() == Material.SUGAR_CANE_BLOCK) {
                    System.out.print("Not breaking ================================");
                    e.setCancelled(true);
                }
            
            }
       
        @EventHandler(priority = EventPriority.HIGH)
        public void NoNoNoBreakSugarCane(BlockBreakEvent e) {
           
            if(e.getBlock().getType() == Material.SUGAR_CANE_BLOCK || e.getBlock().getType().equals(Material.SUGAR_CANE)) {
                System.out.print("Not breaking ================================");
                        e.setCancelled(true);
                    }
           
           
        }
       
        @EventHandler(priority = EventPriority.HIGH)
        public void PlaceSugar(BlockCanBuildEvent e) {
           
            if(e.getMaterial().equals(Material.SUGAR_CANE_BLOCK) || e.getMaterial().equals(Material.SUGAR_CANE)) {
                System.out.print("Not breaking ================================");
                e.setBuildable(true);
               
            }
           
        }
    Well the BlockPhysicsEvent only gets called when I place the sugar cane.
     
    Last edited by a moderator: Jan 6, 2015
  24. Offline

    xMakerx

    @CONTREKEE

    I believe it's System.out.println. If that doesn't work use the Plugin Logger. Also, please post where you register your events.
     
  25. Offline

    CONTREKEE

    @xMakerx Yeah I did the System.out.print thing and it didn't get called.


    Code:
          {
           Bukkit.getPluginManager().registerEvents(this, this);
            System.out.println("Test enabled");
          }
     
  26. Offline

    teej107

    It doesn't matter.


    @CONTREKEE Try and do a temporary test event using the BlockEvent and do some debugging in there. Idk if it the testing will work the way I think but try.
     
  27. Offline

    CONTREKEE

    @teej107 I'm not quite sure what you are saying.
     
  28. Offline

    teej107

    @CONTREKEE Make a temporary BlockEvent just using System.out.println() or something to let you know that it was executed. I don't know if that event will get fired when any of the other sub-events do but try it.
     
  29. Offline

    CONTREKEE

    @teej107 An error pops up but I can't copy the error from the console.
     
  30. Offline

    teej107

    @CONTREKEE Yes you can. Where are you getting the console from?
     
Thread Status:
Not open for further replies.

Share This Page