[Util] LaggySpectatorFix (arrows/exp/etc)

Discussion in 'Resources' started by codename_B, Dec 24, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    codename_B

    This is an issue that's fixed in Spigot but I don't believe is fixed in Bukkit, and if you use anything other than vanilla Team api (eg per player scoreboards) then you can't use the friendly-fire thing either.

    This essentially does the same thing as disabling a bounding box, but also has the side effect of disabling movement. It's a compromise, but it works.

    It also works for arrows fired from mobs, such as skeletons and snowmen, which is nice!

    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.craftbukkit.v1_6_R3.entity.CraftEntity;
    import org.bukkit.entity.*;
     
    import java.lang.Runnable;
     
    /**
    * If you use this in your plugin please give credit somewhere.
    *
    * If it helps your server, just tweet me a thanks @VladToBeHere
    *
    * I know it's horrible and messy but it does work and if you just use
    * Bukkit vs Spigot it's an ok option IMO.
    */
    public class LaggySpectatorFix implements Runnable {
     
        /**
        * This must run every tick in order to correctly update if a player should be "dead" or not.
        */
        public void run() {
            // if it does break anything
            // breaking code, needs review
            for(Player player : Bukkit.getOnlinePlayers()) {
                if(isSpectator(player)) {
                    boolean removed = false;
                    // 5 is a good compromise, nothing in any game should be moving faster than 5 blocks per tick
                    // seems to take into account exp particles too, so that's good!
                    for(Entity e : player.getNearbyEntities(5.0, 5.0, 5.0)) {
                        if(e instanceof Projectile) {
                            if(e instanceof Fish) {
                                // do nothing
                            } else if(!e.isOnGround()) {
                                removed = true;
                                break;
                            }
                        } else if(e instanceof ExperienceOrb) {
                            removed = true;
                            break;
                        }
                    }
                    // so we either set them dead or remove them dead, this is pretty swag
                    if(removed) {
                        CraftEntity ce = (CraftEntity) player;
                        if(!ce.getHandle().dead) {
                            ce.getHandle().dead = true;
                        }
                    } else {
                        CraftEntity ce = (CraftEntity) player;
                        if(ce.getHandle().dead) {
                            ce.getHandle().dead = false;
                        }
                    }
                }
            }
        }
     
        public boolean isSpectator(Player player) {
            // this is where we return magical trevor-like things
          return true;
    }
     
    }
    
     
    jofkos, Rezeh, Ad237 and 5 others like this.
  2. Offline

    super292

    codename_B gg, Will be using this in my minigames, and will give credit, thanks.
     
Thread Status:
Not open for further replies.

Share This Page