Skip to content

Commit

Permalink
add option for totems to save players from void death
Browse files Browse the repository at this point in the history
  • Loading branch information
YouHaveTrouble committed Mar 26, 2022
1 parent 942031e commit 7d4e771
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.youhavetrouble</groupId>
<artifactId>PurpurExtras</artifactId>
<version>1.13.1</version>
<version>1.14.0</version>
<packaging>jar</packaging>

<name>PurpurExtras</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public PurpurConfig() {
enableFeature(FurnaceBurnTimeListener.class, getBoolean("settings.furnace.burn-time.enabled", false));
this.furnaceBurnTimeMultiplier = getDouble("settings.furnace.burn-time.multiplier", 1.0);

enableFeature(VoidTotemListener.class, getBoolean("settings.totem.work-on-void-death", false));

saveConfig();
}

Expand Down
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);
}

}

0 comments on commit 7d4e771

Please sign in to comment.