Solved Invincibility for 3 seconds

Discussion in 'Plugin Development' started by willy00, Dec 9, 2013.

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

    willy00

    Hi, I'm making a plugin where you can place down a nether portal and everyone within 1 block of x and z and whatever height will be teleported to this 1 block. I would like to make them have invincibilty for 3 seconds when they get teleported but im not sure how.
    here are my variables set:
    Code:java
    1. public void onPlayerInteract (PlayerInteractEvent event) {
    2. Player player = event.getPlayer();
    3. final Block b = event.getClickedBlock();
    4. Material c = event.getClickedBlock().getType();
    5. Player victims = (Player) player.getNearbyEntities(1, 128, 1);
    6.  


    Here is my code:
    Code:java
    1. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    2. if (player.getItemInHand() == new ItemStack (Material.PORTAL)) {
    3. b.setType(Material.ENDER_PORTAL_FRAME);
    4. player.getInventory().remove(Material.PORTAL);
    5. cooldown.add(b);
    6. if (!(victims.equals(null))) {
    7. player.teleport(b.getLocation());
    8. victims.teleport(player);
    9. b.setType(c);
    10. player.getInventory().addItem(new ItemStack (Material.PORTAL));
    11. //give the player and victims invincibility for 3 seconds
    12.  
    13. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    14. public void run() {
    15. cooldown.remove(b);
    16. }
    17. }, 100);
    18. return;
     
  2. Offline

    Wingzzz

    You need to setup a Collection of names that are in a class where it is accessible, this way you can add names to this collection, and on EntityDamageEvent, check if the entity is an instanceof Player etc... Then if they're a player loop through the collection of names and compare it the player's name, if it matches cancel the event/setDamage(0.0D);. Now, the way you add names is up to you, but simply get the collection and add the name to it when you teleport the players then create a new delayed task that after 3 seconds (20L * 3L) remove the name from the collection- effectively removing their invincibility.
     
  3. Offline

    GusGold

    Wingzzz
    Or... you could just
    Code:java
    1. PotionEffect invincibility = new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 60, 4);
    2. for(Player player:playersThatWereTeleported){
    3. player.addPotionEffect(invincibility);
    4. }
    because a resistance level of >=4 will give the player invincibility.

    willy00
    To add it to your code, just replace your line 11 with
    Code:java
    1. player.addPotionEffect(invincibility);
    2. victim.addPotionEffect(invincibility);
    but make sure to initialize
    Code:java
    1. PotionEffect invincibility = new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 60, 4);
    when you initialize your event class
     
  4. Offline

    willy00

    Thx so much.
     
  5. Offline

    GusGold

Thread Status:
Not open for further replies.

Share This Page