Advertisement
Camellias_

Untitled

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