Advertisement
Guest User

ModCompass implements IItemPropertyGetter

a guest
Aug 15th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.53 KB | None | 0 0
  1. package net.secknv.nkmod.item;
  2.  
  3. import javax.annotation.Nullable;
  4.  
  5. import net.minecraft.block.Block;
  6. import net.minecraft.entity.Entity;
  7. import net.minecraft.entity.EntityLivingBase;
  8. import net.minecraft.entity.item.EntityItemFrame;
  9. import net.minecraft.entity.player.EntityPlayer;
  10. import net.minecraft.item.IItemPropertyGetter;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.util.math.BlockPos;
  13. import net.minecraft.util.math.MathHelper;
  14. import net.minecraft.world.World;
  15. import net.minecraftforge.fml.relauncher.Side;
  16. import net.minecraftforge.fml.relauncher.SideOnly;
  17. import net.secknv.nkmod.tileentity.TileEntityCoil;
  18.  
  19. public class NkCompass implements IItemPropertyGetter{
  20.  
  21.  
  22.     @SideOnly(Side.CLIENT)
  23.     double rotation;
  24.     @SideOnly(Side.CLIENT)
  25.     double rota;
  26.     @SideOnly(Side.CLIENT)
  27.     long lastUpdateTick;
  28.     @SideOnly(Side.CLIENT)
  29.     public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn)
  30.     {
  31.         if (entityIn == null && !stack.isOnItemFrame())
  32.         {
  33.             return 0.0F;
  34.         }
  35.         else
  36.         {
  37.             boolean flag = entityIn != null;
  38.             Entity entity = (Entity)(flag ? entityIn : stack.getItemFrame());
  39.  
  40.             if (worldIn == null)
  41.             {
  42.                 worldIn = entity.worldObj;
  43.             }
  44.  
  45.             double d0;
  46.  
  47.             if (worldIn.provider.isSurfaceWorld())
  48.             {
  49.                 double d1 = flag ? (double)entity.rotationYaw : this.getFrameRotation((EntityItemFrame)entity);
  50.                 d1 = d1 % 360.0D;
  51.                 double d2 = this.getSpawnToAngle(worldIn, entity);
  52.                 d0 = Math.PI - ((d1 - 90.0D) * 0.01745329238474369D - d2);
  53.             }
  54.             else
  55.             {
  56.                 d0 = Math.random() * (Math.PI * 2D);
  57.             }
  58.  
  59.             if (flag)
  60.             {
  61.                 d0 = this.wobble(worldIn, d0);
  62.             }
  63.  
  64.             float f = (float)(d0 / (Math.PI * 2D));
  65.             return MathHelper.positiveModulo(f, 1.0F);
  66.         }
  67.     }
  68.     @SideOnly(Side.CLIENT)
  69.     private double wobble(World p_185093_1_, double p_185093_2_)
  70.     {
  71.         if (p_185093_1_.getTotalWorldTime() != this.lastUpdateTick)
  72.         {
  73.             this.lastUpdateTick = p_185093_1_.getTotalWorldTime();
  74.             double d0 = p_185093_2_ - this.rotation;
  75.             d0 = d0 % (Math.PI * 2D);
  76.             d0 = MathHelper.clamp_double(d0, -1.0D, 1.0D);
  77.             this.rota += d0 * 0.1D;
  78.             this.rota *= 0.8D;
  79.             this.rotation += this.rota;
  80.         }
  81.  
  82.         return this.rotation;
  83.     }
  84.     @SideOnly(Side.CLIENT)
  85.     private double getFrameRotation(EntityItemFrame p_185094_1_)
  86.     {
  87.         return (double)MathHelper.clampAngle(180 + p_185094_1_.facingDirection.getHorizontalIndex() * 90);
  88.     }
  89.     @SideOnly(Side.CLIENT)
  90.     private double getSpawnToAngle(World worldIn, Entity ent)
  91.     {
  92.         //can I use this to look for nearby Coils?
  93.         //idea is find neraby coil, get it's object and use it's messUpCompass value in the if below
  94.         //worldIn.getEntitiesWithinAABB(EntityPlayer.class, <AABB>)
  95.        
  96.         //first sets blockpos to world spawnpoint
  97.         BlockPos blockpos = worldIn.getSpawnPoint();;
  98.        
  99.         //if true changes the blockpos var used to calculate compass angle to the blockpos of the BlockCoil
  100.         if(nearby_coil_object.messUpCompass){
  101.             blockpos = coil_object.getCoilPosition();
  102.         }
  103.         return Math.atan2((double)blockpos.getZ() - ent.posZ, (double)blockpos.getX() - ent.posX);
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement