Sign casting Issues.

Discussion in 'Plugin Development' started by iPhysX, Dec 15, 2011.

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

    iPhysX

    Code:java
    1. Block bl = loc.getBlock().getRelative(BlockFace.UP);
    2.  
    3. bl.setType(Material.SIGN_POST);
    4. Sign s = (Sign) bl;
    5. s.setLine(0, altar);


    Im having trouble with this.
    I'm being told that i cannot cast Sign to Block.
    I'm sure i've done that before, has something changed, or am i just wrong?
    Thanks!
     
  2. Offline

    Kierrow

    I believe you need to get the BlockState of that block and cast THAT to a Sign.
    Or at least I would suppose so, since Sign extends BlockState...

    Try that. Hope I could help.
     
    iPhysX likes this.
  3. In other words, use bl.getState() ;)
     
  4. Offline

    WizzleDonker

    Code:java
    1.  
    2. Block bl = loc.getBlock().getRelative(BlockFace.UP);
    3. bl.setType(Material.SIGN_POST);
    4. Sign s =(Sign) bl.getState();
    5. s.setLine(0, altar);
    6. s.update(true);
    7.  
     
  5. Offline

    iPhysX

    Yeah I got it working :) Thanks for the replies.
    It's really messy, but i'll share.

    Code:java
    1. public static void create(Location loc, String name) {
    2. ClassPlugin.log.info(ClassPlugin.prefix + " " + name
    3. + " created, X:" + loc.getX() + ", Y:"
    4. + loc.getY() + ", Z:" + loc.getZ());
    5. loc.getBlock().setType(Material.OBSIDIAN);
    6. Block bl = loc.getBlock().getRelative(BlockFace.UP);
    7. bl.setType(Material.SIGN_POST);
    8. Sign s = (Sign) bl.getState();
    9. s.setLine(0, name);
    10.  
    11. String query = "INSERT INTO attrib_altars (altar,locX,locY,locZ) values ('"
    12. + name + "', '" + loc.getX() + "','" + loc.getY() + "', '" + loc.getZ() + "');";
    13. try {
    14. ClassPlugin.manageMySQL.insertQuery(query);
    15. } catch (MalformedURLException e) {
    16. e.printStackTrace();
    17. } catch (InstantiationException e) {
    18. e.printStackTrace();
    19. } catch (IllegalAccessException e) {
    20. e.printStackTrace();
    21. }
    22. }
     
  6. Offline

    desht

    Doesn't look that bad to me :)

    One comment though: where does your name parameter come from? Is it something directly (or even indirectly) taken from what a player can type in? If so, you have a SQL injection vulnerability there. I recommend looking into using parameterised SQL statements (a good idea both for security and performance).

    http://xkcd.com/327/
    http://bobby-tables.com/java.html
     
  7. Offline

    iPhysX

    The name is just something i can use to check if a specific altar exists in the database :)
    Thanks for the response.

    @desht
     
Thread Status:
Not open for further replies.

Share This Page