Getting Block ID

Discussion in 'Plugin Development' started by CrazyGuy3000, Nov 1, 2013.

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

    CrazyGuy3000

    Hi there,
    I am using this code to try and get the id of the block that is below the player, how do I go about doing this as the current method isn't working for me (Deprecated)

    Code:java
    1.  
    2. int ID = playerLoc.getWorld().getBlockAt(playerLoc).getRelative(0, -1, 0).getTypeId();
     
  2. Offline

    xTrollxDudex

    CrazyGuy3000
    Don't even try.... Use getType(), it returns the Material enum, which is easier to remember than ids IMO
     
  3. Offline

    CrazyGuy3000

    xTrollxDudex
    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent event) {
    3. Player player = event.getPlayer();
    4. Location playerLoc = player.getLocation();
    5. Material block = playerLoc.getWorld().getBlockAt(playerLoc).getRelative(0, -1, 0).getType();
    6. Material plate = playerLoc.getWorld().getBlockAt(playerLoc).getType();
    7. if (block.equals(Material.STONE)){
    8. if (plate.equals(Material.STONE_PLATE))
    9. {
    10. player.setVelocity(player.getLocation().getDirection().multiply(3));
    11. player.setVelocity(new Vector(player.getVelocity().getX(), 1.0D, player.getVelocity().getZ()));
    12. }
    13. }
    14. }


    This doesn't work,
    any ideas :L?
     
  4. Offline

    xTrollxDudex

    CrazyGuy3000
    Use PlayerInteractEvent and check if the action is Action.PHYSICAL
     
  5. Offline

    tyler53

    also when you create the player variable (just a side note) you might want to cast it to the Player class, so
    Code:java
    1. Player player = (Player) event.getPlayer();


    It may not ALWAYS be 100% necessary but it is a good practice
     
  6. Offline

    CrazyGuy3000

  7. Offline

    user_43347

    It already returns a Player class instance, why would you recast it? And how is that good practice?
     
  8. Offline

    tyler53

    Simply because when you get the sender or the actor of something its not ALWAYS a player. re-casting it doesn't hurt, does it?

    I mean by all means, if it is, please let me know, I'm still learning Bukkit/java.
     
  9. Offline

    xTrollxDudex

    It's not when it's completely redundant...
    CrazyGuy3000
    PHP:
    @EventHandler
    public void onIneract(PlayerInteractEvent e){
        if(
    e.getAction() == Action.PHYSICAL){
            if(
    e.getClickedBlock().getType() == Material.STONE_PLATE && e.getClickedBlock().getRelative(BlockFace.DOWN).getType() == Material.STONE){
                
    //do stuff
            
    }
        }
    }
     
  10. Offline

    CrazyGuy3000


    I think you've misunderstood my code,
    I was trying to fling the player when he stood on one of these pressure plates...
     
  11. Offline

    user_43347

    In this case, getPlayer() literally returns a Player instance. There is no way for it in this event to not return a player.

    It doesn't hurt, but it's a waste and bad practice.
     
  12. Offline

    tyler53

  13. Offline

    xTrollxDudex

Thread Status:
Not open for further replies.

Share This Page