package com.dndcraft.plugins.namehide;

import com.dndcraft.craftcodex.api.CraftCodexAPI;
import com.dndcraft.fake.armorstand.FakeArmorStand;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.ScoreboardManager;
import org.bukkit.scoreboard.Team;
import org.bukkit.util.Vector;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class Think implements Runnable {
	
	static ScoreboardManager manager = Bukkit.getScoreboardManager ( );
	static Scoreboard        board   = manager.getNewScoreboard ( );
	Team       t                = board.registerNewTeam ( "defaultTeam" );
	boolean    sneakHidesName   = ( ( Boolean ) ConfigurationLoader.GetValue (
			"sneakHidesName" ) ).booleanValue ( );
	int        hideNameDistance = ( ( Integer ) ConfigurationLoader.GetValue (
			"sneakHideDistance" ) ).intValue ( );
	boolean    sprintShowName   = ( ( Boolean ) ConfigurationLoader.GetValue (
			"showNameWhenSprinting" ) ).booleanValue ( );
	ArmorStand fakeArmorStand;
	
	public void run ( ) {
		this.t.setOption ( Team.Option.NAME_TAG_VISIBILITY , Team.OptionStatus.NEVER );
		
		for ( Player p : Bukkit.getServer ( ).getOnlinePlayers ( ) ) {
			this.t.addEntry ( p.getDisplayName ( ) );
			
			p.setScoreboard ( board );
			
			List < Player > visiblePlayers = new ArrayList <> ( );
			List < org.bukkit.entity.Entity > entList = p.getNearbyEntities ( 50.0D , 50.0D , 50.0D );
			
			for ( org.bukkit.entity.Entity ent : entList ) {
				if ( !( ent instanceof Player pl ) ) { continue; }
				
				if ( pl == p || !p.canSee ( pl ) || pl.isSleeping ( ) || !pl.isOnline ( ) ||
						pl.getGameMode ( ) == GameMode.SPECTATOR ) { continue; }
				
				boolean bottom = Utils.PlayerCanSee ( p , pl.getLocation ( ).add ( 0.0D , 0.5D , 0.0D ) ,
													  ( this.sprintShowName && pl.isSprinting ( ) ) );
				
				boolean top = Utils.PlayerCanSee ( p , pl.getLocation ( ).add ( 0.0D , 1.7999999523162842D , 0.0D ) ,
												   ( this.sprintShowName && pl.isSprinting ( ) ) );
				
				if ( pl.isSneaking ( ) ) {
					top = Utils.PlayerCanSee ( p ,
											   pl.getLocation ( ).add ( 0.0D , 1.2000000476837158D , 0.0D ) ,
											   false );
				}
				
				if ( ( !bottom && !top ) || (
						this.sneakHidesName && pl.isSneaking ( ) && p.getLocation ( )
								.distance ( pl.getLocation ( ) ) > this.hideNameDistance ) ) { continue; }
				
				visiblePlayers.add ( pl );
				
			}
			
			List < UUID > allPlayerSees = new ArrayList <> ( );
			if ( PlayerStand.Stands.get ( p.getUniqueId ( ) ) != null ) {
				for ( PlayerStand stand : PlayerStand.Stands.get ( p.getUniqueId ( ) ) ) {
					allPlayerSees.add ( stand.target.getUniqueId ( ) );
				}
			}
			for ( Player pl : visiblePlayers ) {
				if ( pl.hasPotionEffect ( PotionEffectType.INVISIBILITY ) || !p.canSee (
						pl ) || pl.isSleeping ( ) || !pl.isOnline ( ) || pl.getGameMode ( ) == GameMode.SPECTATOR ) {
					continue;
				}
				allPlayerSees.remove ( pl.getUniqueId ( ) );
				
				Vector pos    = pl.getLocation ( ).toVector ( );
				float  offset = pl.isSneaking ( ) ? 1.4F : 1.85F;
				
				if ( fakeArmorStand == null ) {
					fakeArmorStand = FakeArmorStand.create (
							new Location ( p.getWorld ( ) , pos.getX ( ) , pos.getY ( ) + offset , pos.getZ ( ) ) );
					
					int playerCCAccountID = CraftCodexAPI.getApi ( ).getAccountManager ( )
							.getAccount ( p.getUniqueId ( ) ).getAccountID ( );
					String alias = CraftCodexAPI.getApi ( ).getAccountManager ( )
							.getCurrentCharacter ( playerCCAccountID ).getAlias ( );
					String chatName = "MODREQ IF YOU SEE THIS";
					if ( alias != null ) {
						if ( !alias.equals ( "" ) && !alias.equals ( " " ) && !alias.equalsIgnoreCase ( "null" ) ) {
							chatName = alias;
						} else {
							chatName = CraftCodexAPI.getApi ( ).getAccountManager ( )
									.getCurrentCharacter ( playerCCAccountID ).getName ( );
						}
					} else {
						chatName = CraftCodexAPI.getApi ( ).getAccountManager ( )
								.getCurrentCharacter ( playerCCAccountID ).getName ( );
					}
					fakeArmorStand.setCustomName ( chatName );
					p.setPassenger ( fakeArmorStand );
					new PlayerStand ( p , pl , fakeArmorStand );
				} else {
					////new Location(p.getWorld(), pos.getX(), pos.getY() + offset, pos.getZ()));
				}
			}
			
			for ( UUID uuid : allPlayerSees ) {
				
				PlayerStand stand = PlayerStand.GetStandForPlayer ( p , Bukkit.getPlayer ( uuid ) );
				
				if ( stand != null ) { stand.Remove ( ); }
			}
		}
	}
}




Take this code and reformat/order/organize it in a way that is easy to read for human developers