Guest User

Untitled

a guest
Sep 14th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.02 KB | None | 0 0
  1.  
  2. package net.secknv.scomp.item;
  3.  
  4. import net.minecraft.entity.Entity;
  5. import net.minecraft.entity.EntityLivingBase;
  6. import net.minecraft.entity.item.EntityItemFrame;
  7. import net.minecraft.entity.player.EntityPlayer;
  8. import net.minecraft.item.IItemPropertyGetter;
  9. import net.minecraft.item.Item;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.nbt.NBTTagCompound;
  12. import net.minecraft.util.EnumActionResult;
  13. import net.minecraft.util.EnumFacing;
  14. import net.minecraft.util.EnumHand;
  15. import net.minecraft.util.ResourceLocation;
  16. import net.minecraft.util.math.BlockPos;
  17. import net.minecraft.util.math.MathHelper;
  18. import net.minecraft.util.text.TextComponentString;
  19. import net.minecraft.world.World;
  20. import net.secknv.scomp.block.BlockCoil;
  21. import net.secknv.scomp.reference.Reference;
  22.  
  23. import javax.annotation.Nullable;
  24. import java.util.List;
  25.  
  26. public class ItemResonantCompass extends Item {
  27.  
  28.     private String name = "resonant_compass";
  29.  
  30.     public ItemResonantCompass() {
  31.         super();
  32.         this.setRegistryName(name);
  33.         this.setUnlocalizedName(Reference.MOD_ID + "." + name);
  34.         this.maxStackSize = 1;
  35.  
  36.         this.addPropertyOverride(new ResourceLocation("scomp", "scomp:angle"), new IItemPropertyGetter() {
  37.  
  38.             double rotation;
  39.             double rota;
  40.             long lastUpdateTick;
  41.  
  42.             public float apply(ItemStack stack, @Nullable World worldIn, @Nullable EntityLivingBase entityIn) {
  43.  
  44.                 if (entityIn == null && !stack.isOnItemFrame()) {
  45.  
  46.                     return 0.0F;
  47.                 }
  48.                 else {
  49.  
  50.                     //true if compass in player's hand
  51.                     boolean flag = entityIn != null;
  52.  
  53.                     //entity becomes either the player(EntityLivingBase) or the ItemFrame(EntityItemFrame)
  54.                     Entity entity = flag ? entityIn : stack.getItemFrame();
  55.  
  56.                     if (worldIn == null) {
  57.  
  58.                         worldIn = entity.worldObj;
  59.                     }
  60.  
  61.                     //this is the float returned
  62.                     double d0;
  63.  
  64.                     if (worldIn.provider.isSurfaceWorld()) {
  65.  
  66.                         double d1 = flag ? (double)entity.rotationYaw : this.getFrameRotation((EntityItemFrame)entity);
  67.                         d1 = (double) MathHelper.positiveModulo( (float)d1 / 360F, 1.0F );
  68.                         //this adds an offset because of how compass textures are ordered.
  69.                         d0 = 0.5D - d1;
  70.                     }
  71.                     else {
  72.  
  73.                         d0 = Math.random();
  74.                     }
  75.  
  76.                     if (flag) {
  77.  
  78.                         d0 = this.wobble(worldIn, d0);
  79.                     }
  80.  
  81.                     System.out.println(MathHelper.positiveModulo((float)d0, 1.0F));
  82.                     return MathHelper.positiveModulo((float)d0, 1.0F);
  83.                 }
  84.             }
  85.  
  86.             //vanilla wobble method
  87.             private double wobble(World worldIn, double num) {
  88.  
  89.                 if (worldIn.getTotalWorldTime() != this.lastUpdateTick) {
  90.  
  91.                     this.lastUpdateTick = worldIn.getTotalWorldTime();
  92.                     double d0 = num - this.rotation;
  93.                     d0 = (double)MathHelper.positiveModulo( (float)d0 + 0.5F, 1.0F ) - 0.5D;
  94.                     this.rota += d0 * 0.1D;
  95.                     this.rota *= 0.8D;
  96.                     this.rotation += this.rota;
  97.                     this.rotation = (double)MathHelper.positiveModulo( (float)this.rotation, 1.0F );
  98.                 }
  99.  
  100.                 return this.rotation;
  101.             }
  102.  
  103.             private double getFrameRotation(EntityItemFrame entFrame) {
  104.  
  105.                 return (double)MathHelper.clampAngle(180 + entFrame.facingDirection.getHorizontalIndex() * 90);
  106.             }
  107.         });
  108.     }
  109.  
  110.     /*
  111.    
  112.     @Override
  113.     public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced) {
  114.         if (!(stack.getTagCompound() == null)) {
  115.             if(stack.getTagCompound().hasKey("Description")) {
  116.                 tooltip.add(stack.getTagCompound().getString("Description"));
  117.             }
  118.         }
  119.     }
  120.  
  121.  
  122.     @Override
  123.     public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
  124.  
  125.         if (stack.getTagCompound() == null)
  126.         {
  127.             stack.setTagCompound(new NBTTagCompound());
  128.         }
  129.  
  130.         if (worldIn.getBlockState(pos).getBlock() instanceof BlockCoil) {
  131.             if (!worldIn.isRemote) {
  132.                 String desc = "Resonating, x" + pos.getX() + " y" + pos.getY() + " z" + pos.getZ();
  133.                 playerIn.addChatMessage(new TextComponentString("Resonanting with the coil at x: " + pos.getX() + " y: " + pos.getY() + " z: " + pos.getZ()));
  134.                 stack.getTagCompound().setString("Description", desc);
  135.             }
  136.         }
  137.  
  138.         return EnumActionResult.SUCCESS;
  139.     }
  140.     */
  141. }
Add Comment
Please, Sign In to add comment