Check on what block an snowball landed

Discussion in 'Plugin Development' started by Norbu10, Mar 2, 2014.

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

    Norbu10

    I have some code and when he hits a block with a snowball the block will react i have 2 questions about that :

    1 My code doesnt work :
    Code:java
    1. @EventHandler
    2. public void onProjectileHitEvent(ProjectileHitEvent event) {
    3. if(event.getEntity() instanceof Snowball){
    4. Entity snowball = event.getEntity();
    5. Block b = snowball.getLocation().getBlock();
    6. LivingEntity p = (LivingEntity) snowball.getShooter();
    7. }
    8. }

    2 How can you get the blocks metadata like :
    STAINED_CLAY:14 ? And how can you set something to : STAINED_CLAY:14 (I think it is (byte) 14 but that doesnt work)
     
  2. Offline

    OracleTarget

    Norbu10

    I thinks getLocation returns the block above, under or on the side of the block you expect the snowball to hit.

    Second question: getData() just add a supresswarning.
     
  3. Offline

    Norbu10

    My code doesnt work :( :
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onHit(ProjectileHitEvent e) {
    4. Entity projectile = e.getEntity();
    5. if (projectile instanceof Snowball){
    6. Location loc = projectile.getLocation();
    7. final Block blk = loc.getBlock();
    8. List<Entity> Near = projectile.getNearbyEntities(5, 5, 5);
    9. if (blk.getType() == Material.STAINED_CLAY){
    10. if (blk.getData() == (byte) 14){
    11. if (Near instanceof Player){
    12. ((Player) Near).playSound(loc, Sound.EAT, 100, 1);
    13. ((Player) Near).playSound(loc, Sound.DRINK, 100, 1);
    14. blk.setTypeId(124);
    15. Bukkit.getScheduler().scheduleSyncDelayedTask(main,new Runnable(){
    16. public void run() {
    17. blk.setType(Material.STAINED_CLAY);
    18. blk.setData((byte) 14) ;
    19. }
    20. }, 100L);
    21. }
    22. } else if (blk.getData() == (byte) 5)
    23. if (Near instanceof Player){
    24. ((Player) Near).playSound(loc, Sound.EAT, -100, 1);
    25. ((Player) Near).playSound(loc, Sound.DRINK, -100, 1);
    26. blk.setTypeId(124);
    27. Bukkit.getScheduler().scheduleSyncDelayedTask(main,new Runnable(){
    28. public void run() {
    29. blk.setType(Material.STAINED_CLAY);
    30. blk.setData((byte) 5) ;
    31. }
    32. }, 100L);
    33. }
    34. }
    35. }
    36. }
     
  4. Offline

    Gater12

    Norbu10
    You are checking if List object is an instanceof Player? Loop through the list and check if the Entity is an instanceof Player
     
  5. Offline

    Norbu10

    like so?

    i put ((Player) Near) so it only runs that when it is an player (I THINK!)
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void onHit(ProjectileHitEvent e) {
    4. Entity projectile = e.getEntity();
    5. if (projectile instanceof Snowball){
    6. Location loc = projectile.getLocation();
    7. final Block blk = loc.getBlock();
    8. List<Entity> Near = projectile.getNearbyEntities(5, 5, 5);
    9. if (blk.getType() == Material.STAINED_CLAY){
    10. if (blk.getData() == (byte) 14){
    11. ((Player) Near).playSound(loc, Sound.EAT, 100, 1);
    12. ((Player) Near).playSound(loc, Sound.DRINK, 100, 1);
    13. blk.setTypeId(124);
    14. Bukkit.getScheduler().scheduleSyncDelayedTask(main,new Runnable(){
    15. public void run() {
    16. blk.setType(Material.STAINED_CLAY);
    17. blk.setData((byte) 14) ;
    18. }
    19. }, 100L);
    20. }
    21. } else if (blk.getData() == (byte) 5)
    22. if (Near instanceof Player){
    23. ((Player) Near).playSound(loc, Sound.EAT, -100, 1);
    24. ((Player) Near).playSound(loc, Sound.DRINK, -100, 1);
    25. blk.setTypeId(124);
    26. Bukkit.getScheduler().scheduleSyncDelayedTask(main,new Runnable(){
    27. public void run() {
    28. blk.setType(Material.STAINED_CLAY);
    29. blk.setData((byte) 5) ;
    30. }
    31. }, 100L);
    32. }
    33. }
    34. }
     
  6. Offline

    Gater12

    Norbu10
    There's a reason it's called a List, it holds stuff. So it contains a List of entities, and one of them might be a player. So you need to loop through the contents of the List and if that entity from the list is an instanceof Player, cast and do whatever you need to.

    Also, you need to know enhanced for loops.
     
  7. Offline

    Norbu10

    Example?
     
  8. Offline

    Gater12

    Norbu10
    Code:java
    1. for(Entity e : Near){
    2. if(e instanceof Player){
    3. Player p = (Player) e;
    4. /* Stuff */
    5. }
    6. }
     
  9. Offline

    Jombi

    Norbu10
    Code:java
    1. public void onProjectileHit(ProjectileHitEvent event){
    2. if(event.getEntity() instanceof Snowball){
    3. Location location = event.getEntity().getLocation();
    4. Block block = location.getBlock();
    5. //Get block data
    6. }
    7. }
     
  10. Offline

    Norbu10

    Code:java
    1.  
    2.  
    3. @SuppressWarnings("deprecation")
    4. @EventHandler
    5. public void onHit(ProjectileHitEvent e) {
    6. Entity projectile = e.getEntity();
    7. if (projectile instanceof Snowball){
    8. Location loc = projectile.getLocation();
    9. final Block blk = loc.getBlock();
    10. List<Entity> Near = projectile.getNearbyEntities(5, 5, 5);
    11. if (blk.getType() == Material.STAINED_CLAY){
    12. if (blk.getData() == (byte) 14){
    13. for(Entity e1 : Near){
    14. if(e1 instanceof Player){
    15. Player p = (Player) e1;
    16. p.playSound(loc, Sound.EAT, 100, 1);
    17. p.playSound(loc, Sound.DRINK, 100, 1);
    18. }
    19. }
    20. blk.setTypeId(124);
    21. Bukkit.getScheduler().scheduleSyncDelayedTask(main,new Runnable(){
    22. public void run() {
    23. blk.setType(Material.STAINED_CLAY);
    24. blk.setData((byte) 14) ;
    25. }
    26. }, 100L);
    27. }
    28. } else if (blk.getData() == (byte) 5)
    29. if (Near instanceof Player){
    30. for(Entity e1 : Near){
    31. if(e1 instanceof Player){
    32. Player p = (Player) e1;
    33. p.playSound(loc, Sound.EAT, -100, 1);
    34. p.playSound(loc, Sound.DRINK, -100, 1);
    35. }
    36. }
    37. blk.setTypeId(124);
    38. Bukkit.getScheduler().scheduleSyncDelayedTask(main,new Runnable(){
    39. public void run() {
    40. blk.setType(Material.STAINED_CLAY);
    41. blk.setData((byte) 5) ;
    42. }
    43. }, 100L);
    44. }
    45. }
    46. }

    My code doesnt work? It wont do anything
    Error in console : http://pastebin.com/CPJBewv3[syntax=java]
     
Thread Status:
Not open for further replies.

Share This Page