Test for if specific type of block is broken.

Discussion in 'Plugin Development' started by Jay23, Jul 29, 2014.

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

    Jay23

    I'm running a listener that listens for BlockBreakEvent, but I would like for it to then check for if the block broken was a specific type. More specifically, I want to test for when a player breaks a six-sided piston. I was thinking about doing something like this.


    Code:java
    1. @EventHandler
    2. public void onCrateBreak(BlockBreakEvent event) {
    3.  
    4. Player player = event.getPlayer();
    5. Block block = event.getBlock();
    6.  
    7. if(block == Material.PISTON_BASE);


    Of course, I can't do
    Code:java
    1. if(block == Material.PISTON_BASE);

    because block and material are incompatible types, but is there any way I could get the material type of a block and check if it matches up with the required material type?
     
  2. Offline

    Chiller

    Jay23

    Code:java
    1. if (block.getType() == Material.PISTON_BASE) {
    2.  
    3. }
     
    Jay23 likes this.
  3. Offline

    Jay23

    Chiller

    How could I add the damage value to make it a six-sided piston? Something like this?
    Code:java
    1. if(block.getType() == Material.PISTON_BASE(byte)6)
     
  4. Offline

    Flamedek

    Jay23 You will need to check those seperatly, first check if the type is piston base, if it is then check if block.getData is 6
     
    Jay23 likes this.
  5. Offline

    Jay23

  6. Offline

    Chiller

    Jay23
    Code:java
    1. if (block.getType() == Material.PISTON_BASE && block.getData() == (byte) 6) {
    2.  
    3. }
     
    Jay23 likes this.
  7. Offline

    Jay23

    Chiller

    Efficiency! I like it!
     
  8. Offline

    KrypticIce

    @Chiller How can i do this for 2 blocks but when one player mines it the plugin sends them a message this is what i have so far:
    Code:
    package me.CoreDestory.Package;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
       
         public void onEnable() {
             Bukkit.getServer().getLogger().info("Illumi CoreDestory Enabled");
             Bukkit.getServer().getPluginManager().registerEvents(this, this);
    }
    
    public void onDisable() {
             Bukkit.getServer().getLogger().info("Illumi CoreDestory Disabled!");
    }
        
         @EventHandler
         public void BlockDetectEvent(BlockBreakEvent event) {
             Player player = event.getPlayer();
             Block block = event.getBlock();
             if(player.hasPermission("Corebreak.Shadow"))
             if(block.getType() == Material.IRON_BLOCK){
                 getServer().broadcastMessage(player.getName() + " §3has broken Shadows core! All Hope players will recive a loot kit");
                 getServer().broadcastMessage(player.getName() + "§3when a owner or tech is online.");
             }else{
                 player.sendMessage(ChatColor.DARK_RED + "You do not have permission to mine your own base!");
             }
            
                }
         @EventHandler
            public void onCrateBreak(BlockBreakEvent event) {
        
                Player player = event.getPlayer();
                Block block = event.getBlock();
                if(player.hasPermission("Corebreak.Hope"))
                    if(block.getType() == Material.DIAMOND_BLOCK){
                     getServer().broadcastMessage(player.getName() + " §3has broken Shadows core! All Shadow players will recive a loot kit");
                     getServer().broadcastMessage(player.getName() + "§3when a owner or tech is online.");
                 }else{
                     player.sendMessage(ChatColor.DARK_RED + "You do not have permission to mine your own base!");
                 }
                
                   
                 }
    
           }
    
    And this is the error i receive:
    Code:
    25.01 11:32:43 [Server] INFO ... 15 more
    25.01 11:32:43 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:298) ~[spigot-1.7.10.jar:git-Spigot-1649]
    25.01 11:32:43 [Server] INFO at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_60]
    25.01 11:32:43 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_60]
    25.01 11:32:43 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_60]
    25.01 11:32:43 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_60]
    25.01 11:32:43 [Server] INFO at me.CoreDestory.Package.Main.BlockDetectEvent(Main.java:25) ~[?:?]
    25.01 11:32:43 [Server] INFO Caused by: java.lang.ClassCastException: org.bukkit.event.block.BlockBreakEvent cannot be cast to org.bukkit.entity.Player
     
  9. Code:java
    1.  
    2. ClassCastException: org.bukkit.event.block.BlockBreakEvent cannot be cast to org.bukkit.entity.Player
    3.  
     
  10. Offline

    FerusGrim

    Please don't hijack support threads. Create your own.
     
Thread Status:
Not open for further replies.

Share This Page