[Tutorial] Get the block where a projectile Landed.

Discussion in 'Resources' started by DevRosemberg, Sep 22, 2013.

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

    DevRosemberg

    Hello Bukkit Forums, DevRo_ Here and today im making a tutorial and how to get the Exact block where a projectile landed.

    Im going to do a Simple Snowball Spleef Method to get the Exact block where the Snowball landed and remove it.

    We are going to use a BlockIterator in the actual ProjectileHitEvent like this:

    Code:java
    1. BlockIterator iterator = new BlockIterator(event.getEntity().getWorld(), event.getEntity().getLocation().toVector(), event.getEntity().getVelocity().normalize(), 0.0D, 4);


    That code is getting the EXACT location where the projectile landed.

    Now we are going to create a variable named hitBlock and nullify it so we get no errors.
    The code would be:

    Code:java
    1. Block hitBlock = null;


    Now we are going to use a:

    Code:java
    1. while () {
    2.  
    3. }


    To check the Material of the block where the Snowball landed using something like this:

    Code:java
    1. while (iterator.hasNext()) {
    2. hitBlock = iterator.next();
    3.  
    4. if (hitBlock.getTypeId() != 0) {
    5. break;
    6. }
    7. }
    8.  
    9. if (hitBlock.getType() == Material.SNOW_BLOCK) {
    10. hitBlock.getWorld().playEffect(hitBlock.getLocation(), Effect.STEP_SOUND, hitBlock.getTypeId());
    11. hitBlock.setType(Material.AIR);
    12. }


    As there is no actual method in the BukkitAPI to remove the block we are just setting it to air using:

    Code:java
    1. hitBlock.setType(Material.AIR);


    Well that would be all for now.
    My next tutorial will be on how to create a "Gun" to shoot Snowball and use it to create a Spleef Game.

    Regards.
     
    ChipDev, rbrick, Phasesaber and 5 others like this.
  2. Offline

    LegitJava

    Thanks! How would you use this method for dropping items at the location (block) that the projectile landed upon?
     
  3. Offline

    DevRosemberg

    LegitJava After setting it to air:

    Code:java
    1. hitBlock.getWorld().dropItemNaturally(hitBlock.getLocation(), new ItemStack(Material.DROPMATERIAL, 1));
     
  4. Offline

    LegitJava

    So, I have to set the block to Air in order to drop the item? I don't want the block to be removed in my case, I just want it to drop the projectile (snowball) onto the block type.
     
  5. Offline

    DevRosemberg

    KingFaris11 and ChipDev like this.
  6. Offline

    LegitJava


    The snowball still won't drop when it hits the block. The floor of the block is Jungle Wooden Planks, I set the material to WOOD_DOUBLE_STEP because I figured that meant wooden planks. However, I'm not sure. Here's my code:

    Code:java
    1. @EventHandler
    2. public void onProjectileHit(ProjectileHitEvent e) {
    3.  
    4. BlockIterator iterator = new BlockIterator(e.getEntity().getWorld(), e.getEntity().getLocation().toVector(), e.getEntity().getVelocity().normalize(), 0.0D, 4);
    5. Block hitBlock = null;
    6.  
    7. ItemStack snowball = new ItemStack(Material.SNOW_BALL, 1);
    8. ItemMeta snowballmeta = snowball.getItemMeta();
    9. snowballmeta.setDisplayName(ChatColor.RED + "Dodgeball!");
    10. List<String> lore = new ArrayList<String>();
    11. lore.add(ChatColor.GREEN + "Throw the dodgeball");
    12. lore.add(ChatColor.GREEN + "at the opposing team");
    13. lore.add(ChatColor.GREEN + "to get them out and");
    14. lore.add(ChatColor.GREEN + "increase your team's score!");
    15. snowballmeta.setLore(lore);
    16. snowball.setItemMeta(snowballmeta);
    17.  
    18. while (iterator.hasNext()) {
    19. hitBlock = iterator.next();
    20.  
    21. if (hitBlock.getTypeId() != 0) {
    22. break;
    23. }
    24. }
    25.  
    26. if (hitBlock.getType() == Material.WOOD_DOUBLE_STEP) {
    27. hitBlock.getWorld().dropItemNaturally(hitBlock.getLocation(), new ItemStack(snowball));
    28. }
    29. }
    30. }
     
  7. Offline

    DevRosemberg

    LegitJava WHy are you defining again the snowball as a new ItemStack here:

    Code:java
    1. hitBlock.getWorld().dropItemNaturally(hitBlock.getLocation(), new ItemStack(snowball));


    And to the answer:

    1) Are you registering events?
    2) If you are use this code instead of yours:

    Code:java
    1. @EventHandler
    2. public void onProjectileHit(ProjectileHitEvent e) {
    3. Entity ent = e.getEntity();
    4.  
    5. BlockIterator iterator = new BlockIterator(e.getEntity().getWorld(), e.getEntity().getLocation().toVector(), e.getEntity().getVelocity().normalize(), 0.0D, 4);
    6. Block hitBlock = null;
    7.  
    8. ItemStack snowball = new ItemStack(Material.SNOW_BALL);
    9. ItemMeta snowballmeta = snowball.getItemMeta();
    10. snowballmeta.setDisplayName(ChatColor.RED + "Dodgeball!");
    11. List<String> lore = new ArrayList<String>();
    12. lore.add(ChatColor.GREEN + "Throw the dodgeball");
    13. lore.add(ChatColor.GREEN + "at the opposing team");
    14. lore.add(ChatColor.GREEN + "to get them out and");
    15. lore.add(ChatColor.GREEN + "increase your team's score!");
    16. snowballmeta.setLore(lore);
    17. snowball.setItemMeta(snowballmeta);
    18.  
    19. while (iterator.hasNext()) {
    20. hitBlock = iterator.next();
    21.  
    22. if (hitBlock.getTypeId() != 0) {
    23. break;
    24. }
    25. }
    26. if (ent instanceof Snowball) {
    27. if (hitBlock.getType() == Material.WOOD_DOUBLE_STEP) {
    28. hitBlock.getWorld().dropItem(hitBlock.getLocation(), snowball);
    29. }
    30. }
    31.  


    3) IF that still dosent work use
    Code:java
    1. if (ent instanceof Snowball) {
    2. if (hitBlock.getTypeId() == 5 && hitBlock.getData() == 1) {
    3. hitBlock.getWorld().dropItem(hitBlock.getLocation(), snowball);
    4. }
    5. }
     
    ScrubHunter likes this.
  8. Offline

    DevRosemberg

  9. DevRosemberg Hi, how would you go about hitting a player? For instance, I do it, when the projectile hits a block, it makes a block, not destroy it. But when I hit a player, it makes a block in mid air... Ideas?
     
  10. Offline

    DevRosemberg

  11. DevRosemberg Like, I hit an entity, and a block will pop up above, behind, or to the left of the person (just examples)
     
  12. Offline

    DevRosemberg

    ScrubHunter likes this.
  13. DevRosemberg I Don't want it to happen (sorry I sound stupid)
     
  14. Offline

    DevRosemberg

    xXPumperNickleXx That shouldnt happen, maybe it broke with 1.7.2, let me check and ill get back to you in some hours, im quite busy atm. Just got hired to code A WorldEdit alternate version so im not going to reply for 1 or 2 hours.
     
    ScrubHunter likes this.
  15. Offline

    ScrubHunter

    DevRosemberg
    hey dude, how do I make it so, that when a player get hit by an egg there comes an exploding tnt in their position.
    Any ideas? :)
     
  16. Offline

    DevRosemberg

    ScrubHunter
    Use EntityDamageByEntityEvent, check if the damager entity is an Egg, get the location of the player and spawn a Primed TNT or spawn an explosion.
     
    ScrubHunter likes this.
  17. Offline

    ScrubHunter

    DevRosemberg I have to say I'm kinda a newb for this. I would really preciate a full-code, I will only use it for my server pls, again, not a plugin but just the code <3
     
  18. Offline

    DevRosemberg

    ScrubHunter
    Code:java
    1. @EventHandler
    2. public void onProjectileHit(EntityDamageByEntityEvent event) {
    3. if (!(event.getDamager() instanceof Egg)) {
    4. event.setCancelled(true);
    5. return;
    6. }
    7. event.getEntity().getWorld().createExplosion(event.getEntity().getLocation(), 5, true);
    8. }
     
    ScrubHunter likes this.
  19. Offline

    ScrubHunter

    Omg thx you're the best!
     
  20. Offline

    DevRosemberg

  21. Offline

    SoThatsIt

    why do you set the event to cancelled if the damager isnt an egg? that would block pretty much all damage from arrows or mobs.
     
  22. Offline

    DevRosemberg

    SoThatsIt He never asked me to not do it so i though he wanted to cancel the event if it wasnt an egg.
     
    KingFaris11 likes this.
  23. Offline

    jf11

  24. Offline

    jmmalcolm3

    I am sorry for bringing this back, it's just that, I am new to coding, and I keep getting this error on the Event.getEntity, it says: "The Method- getEntity is undefined for type Event. I am sorry if this is obvious, as said before, I am new. Thanks.
     
  25. Offline

    Gater12

  26. Offline

    DevRosemberg

    :eek:
     
    KingFaris11 and Gater12 like this.
  27. Offline

    Gater12

    DevRosemberg
    Auto-correct?

    Show Spoiler
    Always blame the auto-correct
     
  28. Offline

    Phasesaber

    Always.
     
  29. Hey, that was great, but what about turning that block back to it's original one after a short amount of seconds?
     
  30. Offline

    DevRosemberg

    parat26 Store the Block type from which it came in a HashMap with its location. After the amount of time has passed get the block type from the location and set the locations block to that type.

    jmmalcolm3 I *think* you where doing org.bukkit.event.Event.getType() which dosent excist.

    http://jd.bukkit.org/rb/doxygen/d3/d07/Event_8java_source.html

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
Thread Status:
Not open for further replies.

Share This Page