Solved Detecting when an entity such as primed tnt hits a block

Discussion in 'Plugin Development' started by tommycake50, Aug 1, 2013.

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

    tommycake50

    I'm making a game plugin for a server.
    Its kinda like tnt spleef.
    Well basically we have this plugin that launches tnt like it were a snowball, it's called grenades btw.
    Anyway i tried detecting when the tnt spawned and set its fuse to 2 seconds. but the fuse seemed to stay the same :S. I tried for a while but to no avail.
    So im kind of stumped because i dont have an instance of the tnt class and even when i tried making my own version of grenades so i could get an instance of it i had problems with setting velocity and stuff :S as in(i used snowballs and they were visible) lol.
    I'm thinking maybe there is a way to detect when the tnt collides with a surface?
    i haven't found one so im assuming there isn't one.
    Can anyone help?
     
  2. Offline

    Pawnguy7

  3. Offline

    tommycake50

    Yeah but i don't have an instance of the tnt entity.
    I know that event exists but what if it deletes the snowball after it has set the velocity?
     
  4. Offline

    Pawnguy7

    Why would it be deleted?
     
  5. Offline

    callum2904

    use the PlayerInteractEvent
    check if they right click with tnt in there hand
    launch primed tnt
    remove 1 tnt from there inventory
    make a delayed task for 2 seconds then make it explode

    Heres some grenade code i made for a test plugin a while back:

    Oh just pointing out this was made with another plugin that only allows slimeballs to stack upto 1 so it removes all the slime balls in the players hand

    Code:java
    1. import java.util.ArrayList;
    2.  
    3. import me.callum2904.rlm.Main;
    4.  
    5. import org.bukkit.Material;
    6. import org.bukkit.World;
    7. import org.bukkit.entity.Item;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.entity.EntityExplodeEvent;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.event.player.PlayerPickupItemEvent;
    15. import org.bukkit.inventory.ItemStack;
    16.  
    17. public class Grenade implements Listener{
    18. //This Class Is Not Used!
    19.  
    20. public Main plugin;
    21. public Grenade(Main plugin){
    22. this.plugin = plugin;
    23. }
    24. ArrayList<Item> cantPickup = new ArrayList<Item>();
    25. public static boolean blockdamage = false;
    26. @EventHandler
    27. public void slimeGrenade(PlayerInteractEvent event) {
    28. final Player player = event.getPlayer();
    29. World world = player.getWorld();
    30. if (player.getItemInHand().getType() == Material.SLIME_BALL) {
    31. if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    32. final Item grenade = world.dropItem(player.getEyeLocation(), new ItemStack(Material.SLIME_BALL));
    33. grenade.setVelocity(player.getEyeLocation().getDirection());
    34. ItemStack slimeball = new ItemStack(Material.SLIME_BALL, 1);
    35. player.getInventory().remove(slimeball);
    36. cantPickup.add(grenade);
    37. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    38.  
    39. @Override
    40. public void run() {
    41. Grenade.blockdamage = true;
    42. grenade.getWorld().createExplosion(grenade.getLocation(), 3F);
    43. Grenade.blockdamage = false;
    44. grenade.remove();
    45. cantPickup.remove(grenade);
    46. }
    47. }, 40L);
    48. }
    49. }
    50. }
    51.  
    52. @EventHandler
    53. public void onEntityExplode(EntityExplodeEvent event)
    54. {
    55. if(blockdamage && event.getEntity() == null)event.blockList().clear();
    56. }
    57.  
    58. @EventHandler
    59. public void onItemPickup(PlayerPickupItemEvent event){
    60. Item item = event.getItem();
    61. if(cantPickup.contains(item)){
    62. event.setCancelled(true);
    63. }
    64. }
    65. }
     
    tommycake50 likes this.
  6. Offline

    tommycake50

    Because after you have set the velocity why would you need it?
    I'm so stupid.
    why didnt i think of this.
    thanks +1.
     
Thread Status:
Not open for further replies.

Share This Page