Advertisement
Guest User

Weeping Angels for Bukkit

a guest
Jan 28th, 2018
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | None | 0 0
  1. public class WeepingAngelsMain extends JavaPlugin implements Listener {
  2.     private Set< UUID > angels = new HashSet< UUID >();
  3.     private Set< UUID > statues = new HashSet< UUID >();
  4.    
  5.     private Set< String > worlds = new HashSet< String >();
  6.     private boolean blacklist;
  7.     private double dotProductMax = 0;   // Ranges from -1 to 1 with 1 being directly in line of sight
  8.     private int speedAmplifier = 10;
  9.     private boolean ignoreCreativeAndSpectator = true;
  10.    
  11.     BukkitRunnable entityManager = new BukkitRunnable() {
  12.         @Override
  13.         public void run() {
  14.             for ( Iterator< UUID > it = angels.iterator(); it.hasNext(); ) {
  15.                 Entity entity = Bukkit.getEntity( it.next() );
  16.                 if ( entity == null ) {
  17.                     it.remove();
  18.                     continue;
  19.                 }
  20.                 if ( !( worlds.contains( entity.getWorld().getName() ) ^ blacklist ) ) {
  21.                     return;
  22.                 }
  23.                 boolean isStatue = false;
  24.                 for ( Player player : Bukkit.getOnlinePlayers() ) {
  25.                     if ( player.hasPermission( "weepingangels.bypass" ) ) {
  26.                         continue;
  27.                     }
  28.                     if ( ( player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR ) && ignoreCreativeAndSpectator ) {
  29.                         continue;
  30.                     }
  31.                     if ( !entity.getWorld().getUID().equals( player.getWorld().getUID() ) ) {
  32.                         continue;
  33.                     }
  34.                     Vector toMob = entity.getLocation().subtract( player.getLocation() ).toVector().normalize();
  35.                     Vector facing = player.getLocation().getDirection();
  36.                     if ( facing.dot( toMob ) > dotProductMax && player.hasLineOfSight( entity ) ) {
  37.                         if ( !statues.contains( entity.getUniqueId() ) ) {
  38.                             statues.add( entity.getUniqueId() );
  39.                             editEntity( entity, true );
  40.                         }
  41.                         isStatue = true;
  42.                         break;
  43.                     }
  44.                 }
  45.                 if ( !isStatue && statues.contains( entity.getUniqueId() ) ) {
  46.                     statues.remove( entity.getUniqueId() );
  47.                     editEntity( entity, false );
  48.                 }
  49.             }
  50.         }
  51.     };
  52.    
  53.     @Override
  54.     public void onEnable() {
  55.         saveDefaultConfig();
  56.         loadConfig();
  57.         Bukkit.getPluginManager().registerEvents( this, this );
  58.         for ( World world : Bukkit.getWorlds() ) {
  59.             for ( Entity entity : world.getEntities() ) {
  60.                 if ( isStatue( entity ) ) {
  61.                     angels.add( entity.getUniqueId() );
  62.                 }
  63.             }
  64.         }
  65.         entityManager.runTaskTimer( this, 0, 1 );
  66.     }
  67.    
  68.     private void loadConfig() {
  69.         FileConfiguration config = YamlConfiguration.loadConfiguration( new File( getDataFolder() + "/config.yml" ) );
  70.         dotProductMax = config.getDouble( "dot-product-maximum" );
  71.         speedAmplifier = config.getInt( "monster-speed" );
  72.         ignoreCreativeAndSpectator = config.getBoolean( "ignore-creative-and-spectator" );
  73.         worlds.addAll( config.getStringList( "worlds" ) );
  74.         blacklist = config.getBoolean( "world-blacklist" );
  75.     }
  76.    
  77.     @EventHandler
  78.     public void onEntitySpawnEvent( EntitySpawnEvent event ) {
  79.         if ( isStatue( event.getEntity() ) ) {
  80.             angels.add( event.getEntity().getUniqueId() );
  81.         }
  82.     }
  83.    
  84.     @EventHandler
  85.     public void onEntityDie( ExplosionPrimeEvent event ) {
  86.         Entity entity = event.getEntity();
  87.         if ( statues.contains( entity.getUniqueId() ) && entity instanceof LivingEntity ) {
  88.             ( ( LivingEntity ) entity ).removePotionEffect( PotionEffectType.SPEED );
  89.         }
  90.     }
  91.    
  92.     public boolean isStatue( Entity entity ) {
  93.         return entity instanceof Monster;
  94.     }
  95.    
  96.     public void editEntity( Entity entity, boolean lookingAt ) {
  97.         if ( lookingAt ) {
  98.             NBTEditor.setEntityTag( entity, ( byte ) 1, "NoAI" );
  99.             if ( entity instanceof LivingEntity && entity instanceof Monster ) {
  100.                 LivingEntity monster = ( LivingEntity ) entity;
  101.                 monster.removePotionEffect( PotionEffectType.SPEED );
  102.             }
  103.         } else {
  104.             NBTEditor.setEntityTag( entity, ( byte ) 0, "NoAI" );
  105.             if ( entity instanceof LivingEntity && entity instanceof Monster ) {
  106.                 LivingEntity monster = ( LivingEntity ) entity;
  107.                 monster.addPotionEffect( new PotionEffect( PotionEffectType.SPEED, 1000000, speedAmplifier, true ), true );
  108.             }
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement