Advertisement
Camellias_

Untitled

Jul 4th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. package com.bewitchment.api.registry.entity;
  2.  
  3. import com.bewitchment.api.capability.magicpower.MagicPower;
  4.  
  5. import net.minecraft.block.state.IBlockState;
  6. import net.minecraft.entity.Entity;
  7. import net.minecraft.entity.EntityLivingBase;
  8. import net.minecraft.entity.MoverType;
  9. import net.minecraft.entity.player.EntityPlayer;
  10. import net.minecraft.inventory.InventoryHelper;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.nbt.NBTTagCompound;
  13. import net.minecraft.util.DamageSource;
  14. import net.minecraft.util.EnumActionResult;
  15. import net.minecraft.util.EnumHand;
  16. import net.minecraft.util.math.BlockPos;
  17. import net.minecraft.util.math.Vec3d;
  18. import net.minecraft.world.World;
  19. import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
  20.  
  21. @SuppressWarnings({"NullableProblems"})
  22. public abstract class EntityBroom extends Entity {
  23.     private ItemStack item;
  24.     private boolean canFly;
  25.    
  26.     public EntityBroom(World world) {
  27.         super(world);
  28.         setSize(0.7f, 0.7f);
  29.         setNoGravity(true);
  30.     }
  31.    
  32.     @Override
  33.     public EnumActionResult applyPlayerInteraction(EntityPlayer player, Vec3d vec, EnumHand hand) {
  34.         if (!player.isRiding() && !player.isSneaking()) {
  35.             player.rotationYaw = rotationYaw;
  36.             player.rotationPitch = rotationPitch;
  37.             player.startRiding(this);
  38.             return EnumActionResult.SUCCESS;
  39.         }
  40.         return super.applyPlayerInteraction(player, vec, hand);
  41.     }
  42.    
  43.     @Override
  44.     public Entity getControllingPassenger() {
  45.         return getPassengers().isEmpty() ? null : getPassengers().get(0);
  46.     }
  47.    
  48.     @Override
  49.     public boolean attackEntityFrom(DamageSource source, float amount) {
  50.         if (getControllingPassenger() == null && source.getTrueSource() instanceof EntityPlayer) {
  51.             if (!world.isRemote) {
  52.                 InventoryHelper.spawnItemStack(world, posX, posY, posZ, item.copy());
  53.             }
  54.             setDead();
  55.             return true;
  56.         }
  57.         return super.attackEntityFrom(source, amount);
  58.     }
  59.    
  60.     @Override
  61.     public boolean canBeCollidedWith() {
  62.         return !isDead;
  63.     }
  64.    
  65.     @Override
  66.     public boolean canBePushed() {
  67.         return canBeCollidedWith();
  68.     }
  69.    
  70.     @Override
  71.     public boolean processInitialInteract(EntityPlayer player, EnumHand hand) {
  72.         if (item == null) {
  73.             item = player.getHeldItem(hand).splitStack(1);
  74.             return true;
  75.         }
  76.         return super.processInitialInteract(player, hand);
  77.     }
  78.    
  79.     @Override
  80.     public double getMountedYOffset() {
  81.         return 0.4;
  82.     }
  83.    
  84.     @Override
  85.     public void onUpdate() {
  86.         super.onUpdate();
  87.         float friction = 0.98f;
  88.         if (onGround) friction = 0.4f;
  89.         motionX *= friction;
  90.         motionZ *= friction;
  91.         Entity rider = getControllingPassenger();
  92.         if (rider instanceof EntityPlayer) {
  93.             boolean jump = getJump((EntityPlayer) rider);
  94.             if (rider.ticksExisted % 20 == 0 && (jump || !onGround)) canFly = MagicPower.attemptDrain(null, (EntityPlayer) rider, getMagicCost());
  95.             if (canFly) {
  96.                 float speed = getSpeed();
  97.                 float maxSpeed = getMaxSpeed();
  98.                 if (Math.abs(motionX) < maxSpeed) motionX += rider.motionX * speed;
  99.                 if (Math.abs(motionZ) < maxSpeed) motionZ += rider.motionZ * speed;
  100.                 rotationYaw = rider.rotationYaw;
  101.                 if (jump && motionY < 1) motionY += (0.1f + getThrust());
  102.             }
  103.         }
  104.         if (collidedHorizontally) {
  105.             motionX = 0;
  106.             motionZ = 0;
  107.         }
  108.         motionY -= 0.1f;
  109.         motionY *= 0.75f;
  110.         move(MoverType.SELF, motionX, motionY, motionZ);
  111.     }
  112.    
  113.     @Override
  114.     protected void updateFallState(double y, boolean onGround, IBlockState state, BlockPos pos) {
  115.     }
  116.    
  117.     @Override
  118.     protected void entityInit() {
  119.     }
  120.    
  121.     @Override
  122.     protected void writeEntityToNBT(NBTTagCompound tag) {
  123.         if (item != null) tag.setTag("item", item.serializeNBT());
  124.     }
  125.    
  126.     @Override
  127.     protected void readEntityFromNBT(NBTTagCompound tag) {
  128.         item = tag.hasKey("item") ? new ItemStack(tag.getCompoundTag("item")) : ItemStack.EMPTY;
  129.     }
  130.    
  131.     protected abstract float getSpeed();
  132.    
  133.     protected abstract float getMaxSpeed();
  134.    
  135.     protected abstract float getThrust();
  136.    
  137.     protected abstract int getMagicCost();
  138.    
  139.     public void dismount() {
  140.         /*if (!world.isRemote) {
  141.             if (getRidingEntity() != null && this.getControllingPassenger() instanceof EntityPlayer) {
  142.                 EntityPlayer player = (EntityPlayer) getControllingPassenger();
  143.                 Util.giveItem(player, item.copy());
  144.             }
  145.             else InventoryHelper.spawnItemStack(world, posX, posY, posZ, item.copy());
  146.         }
  147.         setDead();*/
  148.     }
  149.    
  150.     private static boolean getJump(EntityLivingBase rider) throws IllegalArgumentException {
  151.         return ObfuscationReflectionHelper.getPrivateValue(EntityLivingBase.class, rider, "isJumping", "field_70703_bu");
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement