| 1 | package me.fridtjof.minecarttrains.managers; |
| 2 | |
| 3 | import com.destroystokyo.paper.ParticleBuilder; |
| 4 | import me.fridtjof.minecarttrains.MinecartTrains; |
| 5 | import org.bukkit.Bukkit; |
| 6 | import org.bukkit.Color; |
| 7 | import org.bukkit.Particle; |
| 8 | import org.bukkit.World; |
| 9 | import org.bukkit.entity.Entity; |
| 10 | import org.bukkit.entity.Minecart; |
| 11 | import org.bukkit.scheduler.BukkitTask; |
| 12 | |
| 13 | import java.util.Map; |
| 14 | import java.util.UUID; |
| 15 | |
| 16 | public class CouplerVisualManager |
| 17 | { |
| 18 | |
| 19 | private final MinecartTrains plugin; |
| 20 | private final LinkageManager linkageManager; |
| 21 | private final int viewDistance; |
| 22 | private final int points; |
| 23 | private final ParticleBuilder particle; |
| 24 | private BukkitTask task; |
| 25 | |
| 26 | public CouplerVisualManager(MinecartTrains plugin) |
| 27 | { |
| 28 | this.plugin = plugin; |
| 29 | this.linkageManager = plugin.linkageManager; |
| 30 | this.viewDistance = plugin.configManager.mainConfig.getConfig().getInt("trains.coupler_visual.view_distance"); |
| 31 | this.points = Math.max(2, plugin.configManager.mainConfig.getConfig().getInt("trains.coupler_visual.points")); |
| 32 | this.particle = Particle.DUST.builder() |
| 33 | .color(Color.fromRGB(70, 70, 70), 0.65f) |
| 34 | .count(1) |
| 35 | .offset(0, 0, 0) |
| 36 | .extra(0) |
| 37 | .force(false); |
| 38 | } |
| 39 | |
| 40 | public void start() |
| 41 | { |
| 42 | if (!plugin.configManager.mainConfig.getConfig().getBoolean("trains.coupler_visual.enabled")) |
| 43 | { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | int interval = Math.max(2, plugin.configManager.mainConfig.getConfig().getInt("trains.coupler_visual.interval_ticks")); |
| 48 | task = plugin.getServer().getScheduler().runTaskTimer(plugin, this::drawLinks, interval, interval); |
| 49 | } |
| 50 | |
| 51 | public void stop() |
| 52 | { |
| 53 | if (task != null) |
| 54 | { |
| 55 | task.cancel(); |
| 56 | task = null; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | private void drawLinks() |
| 61 | { |
| 62 | if (Bukkit.getOnlinePlayers().isEmpty()) |
| 63 | { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | Map<UUID, UUID> links = linkageManager.snapshotLinks(); |
| 68 | if (links.isEmpty()) |
| 69 | { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | for (Map.Entry<UUID, UUID> link : links.entrySet()) |
| 74 | { |
| 75 | Entity parent = Bukkit.getEntity(link.getKey()); |
| 76 | Entity child = Bukkit.getEntity(link.getValue()); |
| 77 | if (!(parent instanceof Minecart) || !(child instanceof Minecart)) |
| 78 | { |
| 79 | continue; |
| 80 | } |
| 81 | if (parent.getWorld() != child.getWorld() || parent.getWorld().getPlayers().isEmpty()) |
| 82 | { |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | drawLine((Minecart) parent, (Minecart) child); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private void drawLine(Minecart from, Minecart to) |
| 91 | { |
| 92 | World world = from.getWorld(); |
| 93 | double startX = from.getX(); |
| 94 | double startY = from.getY() + 0.45; |
| 95 | double startZ = from.getZ(); |
| 96 | double deltaX = to.getX() - startX; |
| 97 | double deltaY = (to.getY() + 0.45) - startY; |
| 98 | double deltaZ = to.getZ() - startZ; |
| 99 | |
| 100 | for (int i = 1; i < points; i++) |
| 101 | { |
| 102 | double t = (double) i / points; |
| 103 | particle.location(world, startX + deltaX * t, startY + deltaY * t, startZ + deltaZ * t) |
| 104 | .receivers(viewDistance, true) |
| 105 | .spawn(); |
| 106 | } |
| 107 | } |
| 108 | } |