Solved Quick Question

Discussion in 'Plugin Development' started by AwesomeJames221, Jun 13, 2013.

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

    AwesomeJames221

    Is there a Java code for bukkit that allows users to NOT be able to break any blocks BUT tallgrass and flowers/roses Without dieing.

    More info: Players die instantly if they break a block. i want them to live if they mistakenly break grass

    Thank a bunch

    If it helps, this is what i have so far:
    Code:
    @EventHandler
    public void onBreak(BlockBreakEvent event) {
        Player player = event.getPlayer();
        event.setCancelled(true);
        player.setHealth(0);
        player.sendMessage(ChatColor.YELLOW + "[NoBreak]" + ChatColor.GREEN + "You Can Not Break Blocks");
    }
     
  2. Offline

    Tirelessly

    Yes. Use the event object to get the broken block, then use that block object and find it's type. Then compare that to the types in the Material enum. I'm not going to spoonfeed you code. Here's the javadocs: http://jd.bukkit.org/dev/doxygen/
     
  3. Offline

    AwesomeJames221

    That's the thing i searched that directory for a long time trying to find a way to incorporate it in the plugin..
     
  4. Offline

    Tirelessly

    What have you tried.
     
  5. Offline

    AwesomeJames221


    I'm trying to make to so if someone mistakenly break grass they don't die. I'm trying to use this:

    Code:
    @EventHandler
    public void onBreak1(BlockBreakEvent event) {
        Player player = event.getPlayer();
        Block block = event.getBlock();
        event.setCancelled(true);
        player.sendMessage(ChatColor.YELLOW + "[NoBreak]" + ChatColor.GREEN + "You Can Not Break Blocks");
    }
    I just don't know how to put the ID(31:1) = TallGrass to make that block to be not broken on mistake.
     
  6. Offline

    Tirelessly

    That doesn't show that you've tried anything. Start with getting the block instance from the event, much like you did with the player instance. Or did you just copy that code from online?
     
  7. Offline

    Haias

    If statements, man.
    Code:
    // I'll split it into multiple lines so it's easier on the eyes.
    boolean isGrass = (event.getBlock().getTypeId() == 31 && event.getBlock().getData() == 1);
    boolean isRose = (event.getBlock().getTypeId() == 38); // not sure on the id.
    if (!(isGrass || isRose)) {
    // stuff in here will only happen if the block is NOT tall grass nor a rose.
    }
    
    Here's a different java docs page. It has all classes listed down on the bottom left, so it's easier to search up "Block" and find the desired class.
    http://jd.bukkit.org/dev/apidocs/ (they say this page is less classy, but I like not having to hit those tiny 'expand' arrows)

    EDIT: Holy crap the syntax tags are so ugly. Switching to code tags.
     
    AwesomeJames221 likes this.
  8. Offline

    AwesomeJames221


    I want to thank you for this code, i believe i got the hang of Java right now seeing this code and the website.

    Thank you again.
     
Thread Status:
Not open for further replies.

Share This Page