Advertisement
GenuineSounds

Untitled

Mar 4th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 KB | None | 0 0
  1. package com.genuineflix.caption.caption;
  2.  
  3. import java.util.Comparator;
  4.  
  5. import net.minecraft.client.Minecraft;
  6. import net.minecraft.client.audio.ISound;
  7. import net.minecraft.entity.Entity;
  8.  
  9. public class CaptionWorld extends Caption {
  10.  
  11.     public Entity entity;
  12.     public ISound sound;
  13.     public double posX, prevPosX;
  14.     public double posY, prevPosY;
  15.     public double posZ, prevPosZ;
  16.     public double size = 1;
  17.     public static Comparator<CaptionWorld> DISTANCE = new Comparator<CaptionWorld>() {
  18.  
  19.         @Override
  20.         public int compare(final CaptionWorld o1, final CaptionWorld o2) {
  21.             if (Minecraft.getMinecraft().thePlayer == null)
  22.                 return 0;
  23.             final double d1 = o1.getDistanceTo(Minecraft.getMinecraft().thePlayer);
  24.             final double d2 = o2.getDistanceTo(Minecraft.getMinecraft().thePlayer);
  25.             if (d1 < d2)
  26.                 return 1;
  27.             if (d1 > d2)
  28.                 return -1;
  29.             return 0;
  30.         }
  31.     };
  32.  
  33.     public CaptionWorld(final String message, final Entity entity, final float volume, final float pitch) {
  34.         super(message, volume, pitch);
  35.         this.attach(entity);
  36.     }
  37.  
  38.     public CaptionWorld(final String message, final ISound sound, final float volume, final float pitch) {
  39.         super(message, volume, pitch);
  40.         this.attach(sound);
  41.     }
  42.  
  43.     public void attach(final Entity entity) {
  44.         if (this.entity != null || entity == null)
  45.             return;
  46.         this.entity = entity;
  47.         posX = entity.posX;
  48.         posY = entity.posY;
  49.         posZ = entity.posZ;
  50.         prevPosX = entity.prevPosX;
  51.         prevPosY = entity.prevPosY;
  52.         prevPosZ = entity.prevPosZ;
  53.         size = entity.height + 0.5;
  54.     }
  55.  
  56.     public void attach(final ISound sound) {
  57.         if (sound == null)
  58.             return;
  59.         this.sound = sound;
  60.         posX = sound.getXPosF();
  61.         posY = sound.getYPosF();
  62.         posZ = sound.getZPosF();
  63.     }
  64.  
  65.     @Override
  66.     public int compareTo(final Caption o) {
  67.         if (o instanceof CaptionWorld)
  68.             return (int) (getDistanceTo((CaptionWorld) o) * 10000f);
  69.         return key.compareTo(o.key);
  70.     }
  71.  
  72.     public boolean equalTo(final CaptionWorld caption) {
  73.         if (!nameEquals(caption.key))
  74.             return false;
  75.         return getDistanceTo(caption) <= 0.1;
  76.     }
  77.  
  78.     public double getDistanceTo(final CaptionWorld caption) {
  79.         return getDistanceTo(caption.posX, caption.posY, caption.posZ);
  80.     }
  81.  
  82.     public double getDistanceTo(final double posX, final double posY, final double posZ) {
  83.         final double distanceX = this.posX - posX;
  84.         final double distanceY = this.posY - posY;
  85.         final double distanceZ = this.posZ - posZ;
  86.         return Math.sqrt(distanceX * distanceX + distanceY * distanceY + distanceZ * distanceZ);
  87.     }
  88.  
  89.     public double getDistanceTo(final Entity entity) {
  90.         return getDistanceTo(entity.posX, entity.posY, entity.posZ);
  91.     }
  92.  
  93.     public double getDistanceToPlayer() {
  94.         return getDistanceTo(Minecraft.getMinecraft().thePlayer);
  95.     }
  96.  
  97.     public float getScale() {
  98.         if (isEntity()) {
  99.             float out = (float) (size / 2);
  100.             if (out < 1)
  101.                 out = 1;
  102.             return out;
  103.         }
  104.         return 1;
  105.     }
  106.  
  107.     public boolean isEntity() {
  108.         return entity != null;
  109.     }
  110.  
  111.     public boolean isSound() {
  112.         return sound != null;
  113.     }
  114.  
  115.     public boolean isWithin(final CaptionWorld caption, final double distance) {
  116.         return entity != null && getDistanceTo(caption) <= distance;
  117.     }
  118.  
  119.     public boolean isWithin(final Entity entity, final double distance) {
  120.         return entity != null && getDistanceTo(entity) <= distance;
  121.     }
  122.  
  123.     public boolean isWithinPlayer(final double distance) {
  124.         return Minecraft.getMinecraft().thePlayer != null || getDistanceToPlayer() <= distance;
  125.     }
  126.  
  127.     @Override
  128.     public boolean tick() {
  129.         if (isEntity() && entity.isDead)
  130.             entity = null;
  131.         updatePos();
  132.         return super.tick();
  133.     }
  134.  
  135.     private void updatePos() {
  136.         if (isEntity()) {
  137.             prevPosX = entity.prevPosX;
  138.             prevPosY = entity.prevPosY;
  139.             prevPosZ = entity.prevPosZ;
  140.             posX = entity.posX;
  141.             posY = entity.posY;
  142.             posZ = entity.posZ;
  143.         } else if (isSound()) {
  144.             prevPosX = posX;
  145.             prevPosY = posY;
  146.             prevPosZ = posZ;
  147.             posX = sound.getXPosF();
  148.             posY = sound.getYPosF();
  149.             posZ = sound.getZPosF();
  150.         } else {
  151.             prevPosX = posX;
  152.             prevPosY = posY;
  153.             prevPosZ = posZ;
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement