Created
April 1, 2020 12:34
-
-
Save jamesxsc/8d130abbd162a0bda3757afe5cf0d3bf to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package uk.tethys.coronasafety; | |
| import org.bukkit.Bukkit; | |
| import org.bukkit.Sound; | |
| import org.bukkit.entity.Player; | |
| import org.bukkit.event.Listener; | |
| import org.bukkit.plugin.java.JavaPlugin; | |
| import org.bukkit.scheduler.BukkitRunnable; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import java.util.UUID; | |
| import java.util.stream.Collectors; | |
| public class CoronaSafety extends JavaPlugin implements Listener { | |
| @Override | |
| public void onEnable() { | |
| plugin = this; | |
| new CoronaCheck().runTaskTimer(this, 0, 10); | |
| super.onEnable(); | |
| } | |
| @Override | |
| public void onDisable() { | |
| super.onDisable(); | |
| } | |
| private static CoronaSafety plugin; | |
| static class CoronaCheck extends BukkitRunnable { | |
| private final HashMap<UUID, Integer> coronaRingTaskIdMap; | |
| CoronaCheck() { | |
| coronaRingTaskIdMap = new HashMap<>(); | |
| } | |
| @SuppressWarnings("unchecked") | |
| @Override | |
| public void run() { | |
| for (Player player : Bukkit.getOnlinePlayers()) { | |
| List<Player> nearby = (List<Player>) (List<?>) player.getNearbyEntities(2, 1.5d, 2).stream() | |
| .filter(ent -> ent instanceof Player).collect(Collectors.toList()); | |
| nearby.sort((o1, o2) -> (int) (player.getLocation().distance(o1.getLocation()) | |
| - player.getLocation().distance(o2.getLocation()))); | |
| if (nearby.isEmpty()) { | |
| if (coronaRingTaskIdMap.containsKey(player.getUniqueId())) | |
| Bukkit.getServer().getScheduler().cancelTask(coronaRingTaskIdMap.get(player.getUniqueId())); | |
| continue; | |
| } | |
| double distance = player.getLocation().distance(nearby.get(0).getLocation()); | |
| if (distance <= 3) { | |
| if (coronaRingTaskIdMap.containsKey(player.getUniqueId())) { | |
| Bukkit.getServer().getScheduler().cancelTask(coronaRingTaskIdMap.get(player.getUniqueId())); | |
| } | |
| coronaRingTaskIdMap.put(player.getUniqueId(), new CoronaRing(player, distance).runTaskTimer(plugin, 0, | |
| (int) (distance * 6d)).getTaskId()); | |
| } | |
| } | |
| } | |
| } | |
| static class CoronaRing extends BukkitRunnable { | |
| private final Player player; | |
| private final double distance; | |
| CoronaRing(Player player, double distance) { | |
| this.player = player; | |
| this.distance = distance; | |
| } | |
| @Override | |
| public void run() { | |
| player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_BIT, | |
| (float) (2f - distance / 1.3f), (float)(2f - distance / 4f)); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment