DamageEffectEvents.java (3977B)
1 package me.night.nullvalkyrie.events.listeners; 2 3 import me.night.nullvalkyrie.NullValkyrie; 4 import me.night.nullvalkyrie.util.Util; 5 import org.bukkit.Bukkit; 6 import org.bukkit.Location; 7 import org.bukkit.World; 8 import org.bukkit.block.Block; 9 import org.bukkit.entity.ArmorStand; 10 import org.bukkit.entity.Entity; 11 import org.bukkit.entity.Zombie; 12 import org.bukkit.event.EventHandler; 13 import org.bukkit.event.Listener; 14 import org.bukkit.event.entity.EntityDamageByEntityEvent; 15 import org.bukkit.scheduler.BukkitRunnable; 16 17 import java.text.DecimalFormat; 18 import java.util.*; 19 20 public class DamageEffectEvents implements Listener { 21 public final World world = Bukkit.getWorld("world"); 22 public final Map<Entity, Integer> indicators = new HashMap<>(); 23 private final DecimalFormat formatter = new DecimalFormat("#"); 24 25 @EventHandler 26 public void onDamage(EntityDamageByEntityEvent e) { 27 double damage = e.getFinalDamage(); 28 if (e.getEntity() instanceof Zombie) { 29 Location loc = e.getEntity().getLocation().clone().add(getRandomOffset(), 1, getRandomOffset()); 30 assert world != null; 31 world.spawn(loc, ArmorStand.class, armorStand -> { 32 armorStand.setMarker(true); 33 armorStand.setVisible(false); 34 armorStand.setGravity(false); 35 armorStand.setSmall(true); 36 armorStand.setCustomNameVisible(true); 37 armorStand.setCustomName(Util.color("&c&l" + formatter.format(damage))); 38 indicators.put(armorStand, 30); 39 }); 40 removeStands(); 41 } 42 } 43 44 public void removeStands() { 45 new BukkitRunnable() { 46 final Set<Entity> stands = indicators.keySet(); 47 final List<Entity> removal = new ArrayList<>(); 48 49 @Override 50 public void run() { 51 for (Entity stand : stands) { 52 int ticksLeft = indicators.get(stand); 53 if (ticksLeft == 0) { 54 stand.remove(); 55 removal.add(stand); 56 continue; 57 } 58 ticksLeft--; 59 indicators.put(stand, ticksLeft); 60 } 61 removal.forEach(stands::remove); 62 } 63 }.runTaskTimer(NullValkyrie.getPlugin(NullValkyrie.class), 0L, 1L); 64 } 65 66 public boolean isSpawnable(Location loc) { 67 Block feetBlock = loc.getBlock(), headBlock = loc.clone().add(0, 1, 0).getBlock(), upperBlock = loc.clone().add(0, 2, 0).getBlock(); 68 return feetBlock.isPassable() && !feetBlock.isLiquid() && headBlock.isPassable() && !headBlock.isLiquid() && upperBlock.isPassable() && !upperBlock.isLiquid(); 69 } 70 71 private double getRandomOffset() { 72 double random = Math.random(); 73 if (Math.random() > 0.5) random *= -1; 74 return random; 75 } 76 77 public int getRandomWithNeg(int size) { 78 int random = (int) (Math.random() * (size + 1)); 79 if (Math.random() > 0.5) random *= -1; 80 return random; 81 } 82 83 public Location generateRandomCoord(int size, World world) { 84 int ranX = getRandomWithNeg(size), ranZ = getRandomWithNeg(size); 85 Block block = world.getHighestBlockAt(ranX, ranZ); 86 return block.getLocation(); 87 } 88 89 public Location generateRandomCoordIsSpawnable(int size) { 90 while (true) { 91 assert world != null; 92 Location coord = generateRandomCoord(size, world); 93 boolean spawnable = isSpawnable(coord); 94 if (spawnable) { 95 return coord; 96 } 97 } 98 } 99 } 100 // 101 // @EventHandler 102 // public void onEntityDeath(EntityDeathEvent event) { 103 // if (!entities.containsKey(event.getEntity())) return; 104 // event.setDroppedExp(0); 105 // event.getDrops().clear(); 106 // entities.remove(event.getEntity()).tryDropLoot(event.getEntity().getLocation()); 107 // }