Blocks error

Discussion in 'Plugin Development' started by Sahee, Dec 30, 2012.

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

    Sahee

    Hello! I'm trying to make my first plugin for bukkit. Plugin will teleport to random location using button on sponge. I getting some errors with blocks:
    Code:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_4_6.block.CraftBlockState cannot be cast to org.bukkit.material.Button
        at me.Sahee.RandomLocationButton.Main.onPlace(Main.java:34)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 18 more
    My code:
    Code:
      public void onPlace(BlockPlaceEvent e)
        {
        Player p = e.getPlayer(); // player
        Block top = e.getBlock();
        int id = top.getTypeId(); 
        Material isbutton = Material.getMaterial(id);
     
      if(isbutton == Material.STONE_BUTTON || isbutton == Material.WOOD_BUTTON){ // if button
        Button przycisk = ((Button) top.getState()); //
        Block za = top.getRelative(przycisk.getAttachedFace()); //get block what is used to create button
        int id1 = za.getTypeId(); 
        Material spongee = Material.getMaterial(id1);
     
        if(spongee == Material.SPONGE){ //if its sponge start ;)
         
            if(p.hasPermission("RLB.makebutton") || p.isOp()){
                p.sendMessage("[RLM] You created random teleport button succesful");
                     
            }else{
            top.setType(Material.AIR);
            p.sendMessage("[RML] You don't have permissions to make random Teleport buttons. Sorry!");
            //remove button and message no perms     
            }} 
        }else{
        //if material is not sponge 
        }
     
     
        }
      
     
  2. Offline

    fireblast709

    It might be nice to know what exact line of the snippet caused the error
     
  3. Offline

    MP5K

    this line causes the error: Button przycisk = ((Button) top.getState()); //

    hello Sahee,
    try to cast the block to Button
    (" Button przycisk = ((Button) top; ")
     
  4. Offline

    fireblast709

    Almost true. I forgot to read the error properly, but he is casting a BlockState to Button. Change the line to
    Code:java
    1. Button przycisk = (Button)top.getState().getData();
     
  5. Offline

    MP5K

  6. Offline

    fireblast709

    MP5K no, you cast the MaterialData to Button (as Button extends MaterialData)
     
    MP5K likes this.
  7. Offline

    Ewe Loon

    if i'm not mistaken Block top = e.getBlock(); will return the block that is being attached to ( the one you clicked on )
    the function e.getBlockAgainst(); should give you the block clicked
    to get the block being placed, use Block placed = e.getBlockPlaced();

    also i notice you are setting the button to air if permissions fail,
    you should be using e.setCancelled(true);
    this will simply cancel placing the button

    use these imports

    import org.bukkit.Material;
    import org.bukkit.event.block.BlockPlaceEvent;
    code should look like this
    Code:
    @EventHandler
    public void onPlace(BlockPlaceEvent e){
    Player p = e.getPlayer(); // player
    int bk = e.getBlockAgainst().getTypeId();
    int itm = e.getBlockPlaced().getTypeId();
      if(itm == Material.STONE_BUTTON.getId() || itm== Material.WOOD_BUTTON.getId()){ // if button
      if(bk == Material.SPONGE.getId()){ //if its sponge start ;) 
      if(p.hasPermission("RLB.makebutton") || p.isOp()){
      p.sendMessage("[RLM] You created random teleport button succesful");             
      }else{
      e.setCancelled(true);
      p.sendMessage("[RML] You don't have permissions to make random Teleport buttons. Sorry!");//remove button and message no perms 
      }
      }
      }
    }
    
    grr, indents failed
     
  8. Offline

    Sahee

    I love you. Now its working. getTypeId() & getId()--> Ammazing ;D

    Ok guys, i made my 1st plugin. RandomLocationTeleport using buttons. Now where i should post this to get "approved" :D

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

    tommycake50

    Sahee likes this.
Thread Status:
Not open for further replies.

Share This Page