Skip to content

Commit

Permalink
Adding temporary entity invulnerability
Browse files Browse the repository at this point in the history
Adding a moment of invulnerability for the entity so it doesn't take damage in the immediate teleport. Restores it to the state before (in case entity was already invulnerable, we don't want to make it vulnerable)
  • Loading branch information
CoolLord22 committed Dec 7, 2022
1 parent 430cd66 commit 22fdb61
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/main/java/com/coollord22/otheranimalteleport/OATMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,26 @@ public static void teleportLeashedEnt(Entity ent, Location from, Location to, Pl

plugin.log.logInfo("Attempting to null the leash holder.", Verbosity.HIGHEST);
((LivingEntity) ent).setLeashHolder(null);

boolean invulnerable = ent.isInvulnerable();

new BukkitRunnable() {
@Override
public void run() {
plugin.log.logInfo("Protecting entity with damage resistance.", Verbosity.HIGHEST);
plugin.log.logInfo("Protecting entity with invulnerability and resistance.", Verbosity.HIGHEST);
ent.setInvulnerable(true);
((LivingEntity) ent).addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 40, 5));

plugin.log.logInfo("Teleporting entity " + ent.getType() + " with ID: " + ent.getEntityId(), Verbosity.HIGH);
ent.teleport(to);

plugin.log.logInfo("Re-attaching leash holder as " + p.getName() + ".", Verbosity.HIGHEST);
((LivingEntity) ent).setLeashHolder(p);

if(plugin.toUseTickets)
fromChunk.removePluginChunkTicket(plugin);

ent.setInvulnerable(invulnerable);
}
}.runTaskLater(plugin, 2);
}
Expand All @@ -40,16 +48,22 @@ public static void teleportEnt(Entity ent, Location from, Location to, Player p,
if(plugin.toUseTickets)
fromChunk.addPluginChunkTicket(plugin);

boolean invulnerable = ent.isInvulnerable();

new BukkitRunnable() {
@Override
public void run() {
plugin.log.logInfo("Protecting entity with damage resistance.", Verbosity.HIGHEST);
plugin.log.logInfo("Protecting entity with invulnerability and resistance.", Verbosity.HIGHEST);
ent.setInvulnerable(true);
((LivingEntity) ent).addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 40, 5));

plugin.log.logInfo("Teleporting entity" + ent.getType() + " with ID: " + ent.getEntityId(), Verbosity.HIGH);
ent.teleport(to);

if(plugin.toUseTickets)
fromChunk.removePluginChunkTicket(plugin);

ent.setInvulnerable(invulnerable);
}
}.runTaskLater(plugin, 2);
}
Expand Down

0 comments on commit 22fdb61

Please sign in to comment.