ProjectileHitEvent help

Discussion in 'Plugin Development' started by Unknown_Stalker_, Jan 7, 2014.

Thread Status:
Not open for further replies.
  1. Code:java
    1. package me.goo.Main.KitListeners;
    2.  
    3. import java.util.Random;
    4.  
    5. import me.goo.Main.Main;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.Location;
    9. import org.bukkit.Material;
    10. import org.bukkit.Sound;
    11. import org.bukkit.block.Block;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.entity.Projectile;
    14. import org.bukkit.entity.Snowball;
    15. import org.bukkit.event.EventHandler;
    16. import org.bukkit.event.Listener;
    17. import org.bukkit.event.block.Action;
    18. import org.bukkit.event.entity.ProjectileHitEvent;
    19. import org.bukkit.event.player.PlayerInteractEvent;
    20. public class SpidermanListener implements Listener{
    21. final Main plugin;
    22.  
    23. public SpidermanListener(Main instance) {
    24. plugin = instance;
    25. }
    26.  
    27. @EventHandler
    28. public void onPlayerInteract(PlayerInteractEvent e){
    29.  
    30. Player player = e.getPlayer();
    31. if((e.getAction() == Action.RIGHT_CLICK_AIR) || (e.getAction() == Action.RIGHT_CLICK_BLOCK)){
    32. if(player.getItemInHand().getType() == Material.STRING){
    33. player.launchProjectile(new Random().nextBoolean() ? Snowball.class : Snowball.class);
    34. player.playSound(player.getLocation(), Sound.WITHER_SHOOT, 0.1F, 1.0F);
    35. e.setCancelled(true);
    36. }
    37.  
    38. }
    39. }
    40. @EventHandler
    41. public void onProjectileHit(ProjectileHitEvent e) {
    42. Block p = (Block) e.getEntity();
    43. double x = p.getLocation().getX();
    44. double y = p.getLocation().getY();
    45. double z = p.getLocation().getZ();
    46. final Location loc2 = new Location(p.getWorld(), x, y, z);
    47. Projectile projectile = (Projectile) e.getEntity();
    48. Player player = (Player) projectile.getShooter();
    49. if(player.getItemInHand().getType() == Material.STRING){
    50. if (plugin.checkKit(player, "spiderman")) {
    51. if(loc2.getBlock().getType() == null){
    52. loc2.getBlock().setType(Material.WEB);
    53. Bukkit.getServer().getScheduler().runTaskLater(plugin, new Runnable() {
    54. public void run() {
    55. loc2.getBlock().setType(Material.AIR);
    56. }
    57.  
    58. }, 60L);
    59. }
    60.  
    61. }
    62. }
    63. }
    64. }
    65.  
    66.  
    67.  
    68.  


    Trying to make it so when you right click a string it throws a snowball and at the snowball's location it will create a web that will last there for about 3 seconds then disapear, getting a couple errors

    if (plugin.checkKit(player, "spiderman")) {

    that just checks kit in the coder btw, so don't worry about that

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  2. the errors could be useful.... (if you can paste them in the OP)
     
  3. Offline

    ShearsSheep

    Can you show us the errors on the console? Its really important!
     
  4. Offline

    AmShaegar

    Code:java
    1. Block p = (Block) e.getEntity();

    This is impossible. You cannot cast Entity to Block. Use this instead:
    Code:java
    1. Block p = e.getBlock();


    If you need the block, you do not need to get x, y and z, create a Location and get the Block of that Location. Just use the Block. To use it in the Scheduler make it final. After these modifications and correct indentaion your code looks something like this:
    Code:java
    1. @EventHandler
    2. public void onProjectileHit(ProjectileHitEvent e) {
    3. final Block b = e.getEntity().getLocation().getBlock();
    4. Projectile projectile = (Projectile) e.getEntity();
    5. Player player = (Player) projectile.getShooter();
    6. if(player.getItemInHand().getType() == Material.STRING){
    7. if (plugin.checkKit(player, "spiderman")) {
    8. if(b.getType() == null){
    9. b.setType(Material.WEB);
    10. Bukkit.getServer().getScheduler().runTaskLater(plugin, new Runnable() {
    11. public void run() {
    12. b.setType(Material.AIR);
    13. }
    14.  
    15. }, 60L);
    16. }
    17.  
    18. }
    19. }
    20. }


    What I am still not getting:
    Code:java
    1. if(b.getType() == null){

    This will be never true because a Block always has a type. In your case probably Material.AIR.
     
Thread Status:
Not open for further replies.

Share This Page