Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.25 KB | None | 0 0
  1. public class ItemWayfinderCompass extends Item {
  2.     public int dimension;
  3.     private int posX, posZ;
  4.  
  5.     public ItemWayfinderCompass(Properties properties) {
  6.         super(properties);
  7.         this.addPropertyOverride(new ResourceLocation("angle"), new IItemPropertyGetter() {
  8.             @OnlyIn(Dist.CLIENT)
  9.             double rotation, rota;
  10.             @OnlyIn(Dist.CLIENT)
  11.             long lastUpdateTick;
  12.            
  13.             @OnlyIn(Dist.CLIENT)
  14.             public float call(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
  15.                 if (entityIn == null && !stack.isOnItemFrame()) {
  16.                     return 0.0F;
  17.                 } else {
  18.                     boolean flag = entityIn != null;
  19.                     Entity entity = (Entity)(flag ? entityIn : stack.getItemFrame());
  20.  
  21.                     if (worldIn == null) {
  22.                         worldIn = entity.world;
  23.                     }
  24.                     double d0 = 0;
  25.                    
  26.                     NBTTagCompound tags = stack.getTag();
  27.                     if (tags != null && tags.hasKey("dimension")) {
  28.                         dimension = stack.getTag().getInt("dimension");
  29.                        
  30.                         if (worldIn.getDimension().getType().getId() == dimension) {
  31.                             double d1 = flag ? (double)entity.rotationYaw : this.getFrameRotation((EntityItemFrame)entity);
  32.                             d1 = MathHelper.positiveModulo(d1 / 360.0D, 1.0D);
  33.                             double d2 = this.getSpawnToAngle(worldIn, entity) / (Math.PI * 2D);
  34.                             d0 = 0.5D - (d1 - 0.25D - d2);
  35.                         } else {
  36.                             d0 = Math.random();
  37.                         }
  38.                        
  39.                         if (flag) {
  40.                             d0 = this.wobble(worldIn, d0);
  41.                         }
  42.                         return MathHelper.positiveModulo((float)d0, 1.0F);
  43.                     } else {
  44.                         return (float) MathHelper.positiveModulo((float)d0 * Math.random() * 5, 1.0F);
  45.                     }
  46.                 }
  47.             }
  48.             @OnlyIn(Dist.CLIENT)
  49.             private double wobble(World worldIn, double p_185093_2_) {
  50.                 if (worldIn.getGameTime() != this.lastUpdateTick) {
  51.                     this.lastUpdateTick = worldIn.getGameTime();
  52.                     double d0 = p_185093_2_ - this.rotation;
  53.                     d0 = MathHelper.positiveModulo(d0 + 0.5D, 1.0D) - 0.5D;
  54.                     this.rota += d0 * 0.1D;
  55.                     this.rota *= 0.8D;
  56.                     this.rotation = MathHelper.positiveModulo(this.rotation + this.rota, 1.0D);
  57.                 }
  58.                 return this.rotation;
  59.             }
  60.            
  61.             @OnlyIn(Dist.CLIENT)
  62.             private double getFrameRotation(EntityItemFrame p_185094_1_) {
  63.                 return (double)MathHelper.wrapDegrees(180 + p_185094_1_.facingDirection.getHorizontalIndex() * 90);
  64.             }
  65.            
  66.             @OnlyIn(Dist.CLIENT)
  67.             private double getSpawnToAngle(World p_185092_1_, Entity p_185092_2_) {
  68.                 if(p_185092_1_ != null && p_185092_2_ instanceof EntityPlayer) {
  69.                     EntityPlayer player = (EntityPlayer)p_185092_2_;
  70.                     ItemStack itemstack = player.inventory.getCurrentItem();
  71.                     if (!itemstack.isEmpty() && itemstack.getItem() == TLSItems.WAYFINDER_COMPASS && itemstack.hasTag() && itemstack.getTag().hasKey("dimension")) {
  72.                         posX = itemstack.getTag().getInt("posX");
  73.                         posZ = itemstack.getTag().getInt("posZ");
  74.                     }
  75.                 }
  76.                 return Math.atan2((double)posZ - p_185092_2_.posZ, (double)posX - p_185092_2_.posX);
  77.             }
  78.         });
  79.     }
  80.    
  81.     @OnlyIn(Dist.CLIENT)
  82.     public void addInformation(ItemStack stack, @Nullable World worldIn, List<ITextComponent> list, ITooltipFlag flagIn) {
  83.         if(!stack.hasTag()) {
  84.             stack.setTag(new NBTTagCompound());
  85.         }
  86.         if(stack.hasTag() && stack.getTag().hasKey("dimension")) {
  87.             list.add(new TextComponentTranslation("tooltip.thelostsea.dimension ", new Object() + stack.getTag().getString("dim_name")));
  88.             list.add(new TextComponentTranslation("tooltip.thelostsea.x ", new Object() + String.valueOf(stack.getTag().getInt("posX"))));
  89.             list.add(new TextComponentTranslation("tooltip.thelostsea.y ", new Object() + String.valueOf(stack.getTag().getInt("posY"))));
  90.         }
  91.     }
  92.    
  93.     @Override
  94.     public EnumActionResult onItemUse(ItemUseContext context) {
  95.         EntityPlayer player = context.getPlayer();
  96.         BlockPos pos = context.getPos();
  97.         ItemStack stack = player.getHeldItemMainhand();
  98.        
  99.         NBTTagCompound tags = stack.getTag();
  100.         if(tags == null) {
  101.             tags = new NBTTagCompound();
  102.         }
  103.        
  104.         Block block = context.getWorld().getBlockState(context.getPos()).getBlock();
  105.         if (context.getPlayer().isSneaking()) {
  106.             if (!context.getWorld().isRemote && block != null) {
  107.                 tags.setString("dim_name", player.getEntityWorld().getDimension().getType().getRegistryName().toString());
  108.                 tags.setInt("dimension", player.dimension.getId());
  109.                 tags.setInt("posX", pos.getX());
  110.                 tags.setInt("posZ", pos.getZ());
  111.                 stack.setTag(tags);
  112.                 return EnumActionResult.SUCCESS;
  113.             }
  114.         }
  115.         return EnumActionResult.FAIL;
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement