Advertisement
Guest User

Invisible player on TP fix

a guest
Mar 27th, 2013
37
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. public class TeleportFix implements Listener {
  3.  
  4.     public Main plugin;
  5.     public TeleportFix(Main instance) {
  6.         this.plugin = instance;
  7.     }
  8.     private Server server;
  9.    
  10.     private final int TELEPORT_FIX_DELAY = 20; // ticks
  11.     public TeleportFix(Main plugin, Server server) {
  12.         this.plugin = plugin;
  13.         this.server = server;
  14.     }
  15.     @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
  16.     public void onPlayerTeleport(PlayerTeleportEvent event) {
  17.         final Player player = event.getPlayer();
  18.         final int visibleDistance = server.getViewDistance() * 16;
  19.        
  20.         // Fix the visibility issue one tick later
  21.         Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
  22.             @Override
  23.             public void run() {
  24.                 // Refresh nearby clients
  25.                 updateEntities(getPlayersWithin(player, visibleDistance));
  26.                
  27.             }
  28.         }, TELEPORT_FIX_DELAY);
  29.         }
  30.        
  31.    
  32.    
  33.  
  34.     public void updateEntities(List<Player> observers) {
  35.        
  36.         // Refresh every single player
  37.             for (Player player : observers) {
  38.             updateEntity(player, observers);
  39.         }
  40.     }
  41.    
  42.     @SuppressWarnings("unchecked")
  43.     public void updateEntity(Entity entity, List<Player> observers) {
  44.  
  45.         World world = entity.getWorld();
  46.         WorldServer worldServer = ((CraftWorld) world).getHandle();
  47.  
  48.         EntityTracker tracker = worldServer.tracker;
  49.         EntityTrackerEntry entry = (EntityTrackerEntry) tracker.trackedEntities
  50.                 .get(entity.getEntityId());
  51.  
  52.         List<EntityPlayer> nmsPlayers = getNmsPlayers(observers);
  53.  
  54.         // Force Minecraft to resend packets to the affected clients
  55.         if(!entry.trackedPlayers.isEmpty()){
  56.             entry.trackedPlayers.removeAll(nmsPlayers);
  57.             entry.scanPlayers(nmsPlayers);
  58.         }
  59.     }
  60.  
  61.     private List<EntityPlayer> getNmsPlayers(List<Player> players) {
  62.         List<EntityPlayer> nsmPlayers = new ArrayList<EntityPlayer>();
  63.  
  64.         for (Player bukkitPlayer : players) {
  65.             CraftPlayer craftPlayer = (CraftPlayer) bukkitPlayer;
  66.             nsmPlayers.add(craftPlayer.getHandle());
  67.         }
  68.  
  69.         return nsmPlayers;
  70.     }
  71.    
  72.     private List<Player> getPlayersWithin(Player player, int distance) {
  73.         List<Player> res = new ArrayList<Player>();
  74.         int d2 = distance * distance;
  75.  
  76.         for (Player p : server.getOnlinePlayers()) {
  77.             if (p.getWorld() == player.getWorld()
  78.                     && p.getLocation().distanceSquared(player.getLocation()) <= d2) {
  79.                 res.add(p);
  80.             }
  81.         }
  82.  
  83.         return res;
  84.     }
  85. }
Advertisement
RAW Paste Data Copied
Advertisement