package com.graywolf336.trailingperspective.workers;

import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;

import com.graywolf336.trailingperspective.TrailingPerspectiveMain;
import com.graywolf336.trailingperspective.Util;
import com.graywolf336.trailingperspective.enums.Settings;
import com.graywolf336.trailingperspective.interfaces.ITrailer;
import com.graywolf336.trailingperspective.interfaces.ITrailerWorker;

public class TrailersPerspectiveWorker implements ITrailerWorker {
    private TrailingPerspectiveMain pl;
    private Long changeInterval;

    public TrailersPerspectiveWorker(TrailingPerspectiveMain plugin) {
        this.pl = plugin;
        this.changeInterval = Util.getTime(Settings.CHANGE_INTERVAL.asString(), 120000L);
        this.pl.getLogger().info("TrailersPerspectiveWorker started with a change internval of " + this.changeInterval + " milliseconds.");
    }

    @SuppressWarnings("deprecation")
    public void run() {
        if (this.pl.getServer().getOnlinePlayers().size() <= this.pl.getTrailerManager().getTrailers().size())
            return;

        for (ITrailer trailer : this.pl.getTrailerManager().getTrailers()) {
            if (trailer.isOnline() && trailer.getPlayer().getGameMode() == GameMode.SPECTATOR && (trailer.getCurrentPerspectiveTrailingTime() > this.changeInterval || trailer.getCurrentPerspectiveTrailingTime() == 0)) {
                Player p = null;

                // Check if they have trailed someone yet or not
                if (trailer.getPlayersLastTrailed().isEmpty()) {
                    p = Util.getRandomAlivePlayerNotInList(trailer.getPlayer().getName());
                } else {
                    String lastUser = trailer.getPlayersLastTrailed().get(trailer.getPlayersLastTrailed().size() - 1);
                    p = Util.getRandomAlivePlayerNotInList(trailer.getPlayer().getName(), lastUser);

                    // if we couldn't get anyone, then just get someone who isn't this trailer
                    if (p == null) {
                        p = Util.getRandomAlivePlayerNotInList(trailer.getPlayer().getName());
                    }

                    // If the player isn't null and it's the same player as who was before,
                    // then don't try anything fancy and just move onto the next trailer
                    if (p != null && p.getName().equalsIgnoreCase(lastUser) && trailer.getCurrentPerspectiveTrailingTime() != 0) {
                        continue;
                    }
                }

                if (p == null) {
                    trailer.setNoLongerTrailingAnyone();
                } else {
                    trailer.setPlayerCurrentlyTrailing(p);
                    trailer.getPlayer().teleport(p);
                    trailer.getPlayer().setSpectatorTarget(null);

                    final Player target = p;
                    this.pl.getServer().getScheduler().runTaskLater(this.pl, () -> {
                        trailer.getPlayer().setSpectatorTarget(target);
                    }, 2L);

                    this.pl.getLogger().info(trailer.getUsername() + " is now trailing the perspective of " + p.getName());
                    trailer.getPlayer().sendTitle("", ChatColor.GREEN + "Now Trailing: " + p.getDisplayName());
                }
            }
        }
    }
}

@@ -2699,17 +2699,17 @@
 ler.
-s
+g
 etPlayer
 Curr
@@ -2708,80 +2708,34 @@
 ayer
-CurrentlyTrailing(p);%0A                    trailer.getPlayer().teleport(p
+().setSpectatorTarget(null
 );%0A 
@@ -2757,33 +2757,33 @@
     trailer.
-g
+s
 etPlayer
 ().setSpecta
@@ -2774,34 +2774,27 @@
 ayer
-().setSpectatorTarget(null
+CurrentlyTrailing(p
 );%0A%0A
@@ -2925,33 +2925,133 @@
  -%3E 
-%7B%0A                       
+trailer.getPlayer().teleport(target), 1L);%0A                    this.pl.getServer().getScheduler().runTaskLater(this.pl, () -%3E
  tra
@@ -3097,34 +3097,11 @@
 get)
-;%0A                    %7D
 , 
-2
+3
 L);%0A

Improve the spectator target setting, teleport after clearing previous target