-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add option for totems to save players from void death
- Loading branch information
1 parent
942031e
commit 7d4e771
Showing
3 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/me/youhavetrouble/purpurextras/listeners/VoidTotemListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package me.youhavetrouble.purpurextras.listeners; | ||
|
||
import org.bukkit.EntityEffect; | ||
import org.bukkit.Location; | ||
import org.bukkit.Material; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.EventPriority; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.entity.EntityDamageEvent; | ||
import org.bukkit.event.player.PlayerMoveEvent; | ||
import org.bukkit.event.player.PlayerQuitEvent; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.potion.PotionEffect; | ||
import org.bukkit.potion.PotionEffectType; | ||
|
||
import java.util.*; | ||
|
||
public class VoidTotemListener implements Listener { | ||
|
||
private final Collection<PotionEffect> totemEffects = new ArrayList<>(); | ||
private final HashMap<UUID, Location> lastGroundedLocations = new HashMap<>(); | ||
|
||
public VoidTotemListener() { | ||
totemEffects.add(new PotionEffect(PotionEffectType.REGENERATION, 20*45, 1)); | ||
totemEffects.add(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 20*40, 0)); | ||
totemEffects.add(new PotionEffect(PotionEffectType.ABSORPTION, 20*5, 1)); | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
public void onPlayerMove(PlayerMoveEvent event) { | ||
if (!event.hasChangedPosition()) return; | ||
Location location = event.getTo().clone(); | ||
if (location.subtract(0, 0.05, 0).getBlock().getType().isAir()) return; | ||
lastGroundedLocations.put(event.getPlayer().getUniqueId(), event.getTo().toCenterLocation()); | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
public void onPlayerQuit(PlayerQuitEvent event) { | ||
lastGroundedLocations.remove(event.getPlayer().getUniqueId()); | ||
} | ||
|
||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) | ||
public void onPlayerDeathInVoid(EntityDamageEvent event) { | ||
if (!(event.getEntity() instanceof Player player)) return; | ||
if (!event.getCause().equals(EntityDamageEvent.DamageCause.VOID)) return; | ||
Location location = player.getLocation(); | ||
if (location.getY() > location.getWorld().getMinHeight()) return; | ||
|
||
if (player.getHealth() - event.getFinalDamage() > 0) return; | ||
|
||
if (!player.getInventory().getItemInMainHand().getType().equals(Material.TOTEM_OF_UNDYING) | ||
&& !player.getInventory().getItemInOffHand().getType().equals(Material.TOTEM_OF_UNDYING) | ||
) return; | ||
|
||
event.setCancelled(true); | ||
|
||
Location safeLocation = lastGroundedLocations.getOrDefault(player.getUniqueId(), location.getWorld().getSpawnLocation()); | ||
player.teleportAsync(safeLocation).thenRun(() -> useTotem(player)); | ||
} | ||
|
||
private void useTotem(Player player) { | ||
ItemStack totem = null; | ||
if (player.getInventory().getItemInMainHand().getType().equals(Material.TOTEM_OF_UNDYING)) { | ||
totem = player.getInventory().getItemInMainHand(); | ||
} else if (player.getInventory().getItemInOffHand().getType().equals(Material.TOTEM_OF_UNDYING)) { | ||
totem = player.getInventory().getItemInOffHand(); | ||
} | ||
if (totem == null) return; | ||
totem.subtract(); | ||
player.setFallDistance(0); | ||
player.setHealth(1); | ||
player.playEffect(EntityEffect.TOTEM_RESURRECT); | ||
player.getActivePotionEffects().forEach(potionEffect -> player.removePotionEffect(potionEffect.getType())); | ||
player.addPotionEffects(this.totemEffects); | ||
} | ||
|
||
} |