Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class WeepingAngelsMain extends JavaPlugin implements Listener {
- private Set< UUID > angels = new HashSet< UUID >();
- private Set< UUID > statues = new HashSet< UUID >();
- private Set< String > worlds = new HashSet< String >();
- private boolean blacklist;
- private double dotProductMax = 0; // Ranges from -1 to 1 with 1 being directly in line of sight
- private int speedAmplifier = 10;
- private boolean ignoreCreativeAndSpectator = true;
- BukkitRunnable entityManager = new BukkitRunnable() {
- @Override
- public void run() {
- for ( Iterator< UUID > it = angels.iterator(); it.hasNext(); ) {
- Entity entity = Bukkit.getEntity( it.next() );
- if ( entity == null ) {
- it.remove();
- continue;
- }
- if ( !( worlds.contains( entity.getWorld().getName() ) ^ blacklist ) ) {
- return;
- }
- boolean isStatue = false;
- for ( Player player : Bukkit.getOnlinePlayers() ) {
- if ( player.hasPermission( "weepingangels.bypass" ) ) {
- continue;
- }
- if ( ( player.getGameMode() == GameMode.CREATIVE || player.getGameMode() == GameMode.SPECTATOR ) && ignoreCreativeAndSpectator ) {
- continue;
- }
- if ( !entity.getWorld().getUID().equals( player.getWorld().getUID() ) ) {
- continue;
- }
- Vector toMob = entity.getLocation().subtract( player.getLocation() ).toVector().normalize();
- Vector facing = player.getLocation().getDirection();
- if ( facing.dot( toMob ) > dotProductMax && player.hasLineOfSight( entity ) ) {
- if ( !statues.contains( entity.getUniqueId() ) ) {
- statues.add( entity.getUniqueId() );
- editEntity( entity, true );
- }
- isStatue = true;
- break;
- }
- }
- if ( !isStatue && statues.contains( entity.getUniqueId() ) ) {
- statues.remove( entity.getUniqueId() );
- editEntity( entity, false );
- }
- }
- }
- };
- @Override
- public void onEnable() {
- saveDefaultConfig();
- loadConfig();
- Bukkit.getPluginManager().registerEvents( this, this );
- for ( World world : Bukkit.getWorlds() ) {
- for ( Entity entity : world.getEntities() ) {
- if ( isStatue( entity ) ) {
- angels.add( entity.getUniqueId() );
- }
- }
- }
- entityManager.runTaskTimer( this, 0, 1 );
- }
- private void loadConfig() {
- FileConfiguration config = YamlConfiguration.loadConfiguration( new File( getDataFolder() + "/config.yml" ) );
- dotProductMax = config.getDouble( "dot-product-maximum" );
- speedAmplifier = config.getInt( "monster-speed" );
- ignoreCreativeAndSpectator = config.getBoolean( "ignore-creative-and-spectator" );
- worlds.addAll( config.getStringList( "worlds" ) );
- blacklist = config.getBoolean( "world-blacklist" );
- }
- @EventHandler
- public void onEntitySpawnEvent( EntitySpawnEvent event ) {
- if ( isStatue( event.getEntity() ) ) {
- angels.add( event.getEntity().getUniqueId() );
- }
- }
- @EventHandler
- public void onEntityDie( ExplosionPrimeEvent event ) {
- Entity entity = event.getEntity();
- if ( statues.contains( entity.getUniqueId() ) && entity instanceof LivingEntity ) {
- ( ( LivingEntity ) entity ).removePotionEffect( PotionEffectType.SPEED );
- }
- }
- public boolean isStatue( Entity entity ) {
- return entity instanceof Monster;
- }
- public void editEntity( Entity entity, boolean lookingAt ) {
- if ( lookingAt ) {
- NBTEditor.setEntityTag( entity, ( byte ) 1, "NoAI" );
- if ( entity instanceof LivingEntity && entity instanceof Monster ) {
- LivingEntity monster = ( LivingEntity ) entity;
- monster.removePotionEffect( PotionEffectType.SPEED );
- }
- } else {
- NBTEditor.setEntityTag( entity, ( byte ) 0, "NoAI" );
- if ( entity instanceof LivingEntity && entity instanceof Monster ) {
- LivingEntity monster = ( LivingEntity ) entity;
- monster.addPotionEffect( new PotionEffect( PotionEffectType.SPEED, 1000000, speedAmplifier, true ), true );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement