Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.64 KB | None | 0 0
  1. public class EntityTameableSpider extends EntityHorse
  2. {
  3.     private static final UUID ARMOR_MODIFIER_UUID = UUID.fromString("556E1665-8B10-40C8-8F9D-CF9B1667F295");
  4.     private static final DataParameter<Byte> CLIMBING = EntityDataManager.<Byte>createKey(EntityTameableSpider.class, DataSerializers.BYTE);
  5.     private static final DataParameter<Integer> SPIDER_ARMOR = EntityDataManager.<Integer>createKey(EntityTameableSpider.class, DataSerializers.VARINT);
  6.     private static final DataParameter<ItemStack> SPIDER_ARMOR_STACK = EntityDataManager.<ItemStack>createKey(EntityTameableSpider.class, DataSerializers.ITEM_STACK);
  7.  
  8.     public EntityTameableSpider(World worldIn)
  9.     {
  10.         super(worldIn);
  11.         this.setSize(1.4F, 0.9F);
  12.         this.stepHeight = 1.0F;
  13.         this.initHorseChest();
  14.     }
  15.    
  16.     public SpiderArmorType getSpiderArmorType(ItemStack stack)
  17.     {
  18.         return SpiderArmorType.getByItem(stack.getItem());
  19.     }
  20.    
  21.     @Override
  22.     public void onUpdate()
  23.     {
  24.         super.onUpdate();
  25.        
  26.         if (!this.world.isRemote)
  27.         {
  28.             this.setBesideClimbableBlock(this.collidedHorizontally);
  29.         }
  30.        
  31.         if (this.world.isRemote && this.dataManager.isDirty())
  32.         {
  33.             this.dataManager.setClean();
  34.         }
  35.         ItemStack armor = this.horseChest.getStackInSlot(1);
  36.         if (isArmor(armor)) armor.getItem().onHorseArmorTick(world, this, armor);
  37.     }
  38.  
  39.     @Override
  40.     protected void initEntityAI()
  41.     {
  42.         this.tasks.addTask(0, new EntityAISwimming(this));
  43.         this.tasks.addTask(1, new EntityAIPanic(this, 0.95D));
  44.         this.tasks.addTask(1, new EntityAIRunAroundLikeCrazy(this, 0.95D));
  45.         this.tasks.addTask(6, new EntityAIWanderAvoidWater(this, 0.8D));
  46.         this.tasks.addTask(7, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));
  47.         this.tasks.addTask(8, new EntityAILookIdle(this));
  48.     }
  49.    
  50.     protected PathNavigate createNavigator(World worldIn)
  51.     {
  52.         return new PathNavigateClimber(this, worldIn);
  53.     }
  54.    
  55.     @Override
  56.     protected void entityInit()
  57.     {
  58.         super.entityInit();
  59.         this.dataManager.register(SPIDER_ARMOR, Integer.valueOf(SpiderArmorType.NONE.getOrdinal()));
  60.         this.dataManager.register(SPIDER_ARMOR_STACK, ItemStack.EMPTY);
  61.         this.dataManager.register(CLIMBING, Byte.valueOf((byte)0));
  62.     }
  63.    
  64.     public boolean isOnLadder()
  65.     {
  66.         return this.isBesideClimbableBlock();
  67.     }
  68.  
  69.     public void setInWeb() {}
  70.  
  71.     public EnumCreatureAttribute getCreatureAttribute()
  72.     {
  73.         return EnumCreatureAttribute.ARTHROPOD;
  74.     }
  75.  
  76.     public boolean isPotionApplicable(PotionEffect potioneffectIn)
  77.     {
  78.         return potioneffectIn.getPotion() == MobEffects.POISON ? false : super.isPotionApplicable(potioneffectIn);
  79.     }
  80.  
  81.     public boolean isBesideClimbableBlock()
  82.     {
  83.         return (((Byte)this.dataManager.get(CLIMBING)).byteValue() & 1) != 0;
  84.     }
  85.  
  86.     public void setBesideClimbableBlock(boolean climbing)
  87.     {
  88.         byte b0 = ((Byte)this.dataManager.get(CLIMBING)).byteValue();
  89.  
  90.         if (climbing)
  91.         {
  92.             b0 = (byte)(b0 | 1);
  93.         }
  94.         else
  95.         {
  96.             b0 = (byte)(b0 & -2);
  97.         }
  98.  
  99.         this.dataManager.set(CLIMBING, Byte.valueOf(b0));
  100.     }
  101.  
  102.     @Override
  103.     public void writeEntityToNBT(NBTTagCompound compound)
  104.     {
  105.         super.writeEntityToNBT(compound);
  106.  
  107.         if (!this.horseChest.getStackInSlot(1).isEmpty())
  108.         {
  109.             compound.setTag("ArmorItem", this.horseChest.getStackInSlot(1).writeToNBT(new NBTTagCompound()));
  110.         }
  111.     }
  112.  
  113.     @Override
  114.     public void readEntityFromNBT(NBTTagCompound compound)
  115.     {
  116.         super.readEntityFromNBT(compound);
  117.  
  118.         if (compound.hasKey("ArmorItem", 10))
  119.         {
  120.             ItemStack itemstack = new ItemStack(compound.getCompoundTag("ArmorItem"));
  121.  
  122.             if (!itemstack.isEmpty() && isArmor(itemstack))
  123.             {
  124.                 this.horseChest.setInventorySlotContents(1, itemstack);
  125.             }
  126.         }
  127.  
  128.         this.updateHorseSlots();
  129.     }
  130.  
  131.     @Override
  132.     public void setHorseVariant(int variant) {}
  133.  
  134.     @Override
  135.     public int getHorseVariant()
  136.     {
  137.         return 0;
  138.     }
  139.  
  140.     protected void updateHorseSlots()
  141.     {
  142.         super.updateHorseSlots();
  143.         this.setSpiderArmorStack(this.horseChest.getStackInSlot(1));
  144.     }
  145.    
  146.     public ItemStack getArmor()
  147.     {
  148.         return this.dataManager.get(SPIDER_ARMOR_STACK);
  149.     }
  150.  
  151.     public void setSpiderArmorStack(ItemStack itemStackIn)
  152.     {
  153.         SpiderArmorType spiderArmorType = SpiderArmorType.getByItemStack(itemStackIn);
  154.         this.dataManager.set(SPIDER_ARMOR, Integer.valueOf(spiderArmorType.getOrdinal()));
  155.         this.dataManager.set(SPIDER_ARMOR_STACK, itemStackIn);
  156.  
  157.         if (!this.world.isRemote)
  158.         {
  159.             this.getEntityAttribute(SharedMonsterAttributes.ARMOR).removeModifier(ARMOR_MODIFIER_UUID);
  160.             int i = spiderArmorType.getProtection();
  161.  
  162.             if (i != 0)
  163.             {
  164.                 this.getEntityAttribute(SharedMonsterAttributes.ARMOR).applyModifier((new AttributeModifier(ARMOR_MODIFIER_UUID, "Spider armor bonus", (double)i, 0)).setSaved(false));
  165.             }
  166.         }
  167.     }
  168.  
  169.     public SpiderArmorType getSpiderArmorType()
  170.     {
  171.         SpiderArmorType armor = SpiderArmorType.getByItemStack(this.dataManager.get(SPIDER_ARMOR_STACK));
  172.         if (armor == SpiderArmorType.NONE) armor = SpiderArmorType.getByOrdinal(this.dataManager.get(SPIDER_ARMOR));
  173.         return armor;
  174.     }
  175.  
  176.     @Override
  177.     public void onInventoryChanged(IInventory invBasic)
  178.     {
  179.         SpiderArmorType spiderArmorType = this.getSpiderArmorType();
  180.         super.onInventoryChanged(invBasic);
  181.         SpiderArmorType spiderArmorType1 = this.getSpiderArmorType();
  182.  
  183.         if (this.ticksExisted > 20 && spiderArmorType != spiderArmorType1 && spiderArmorType1 != SpiderArmorType.NONE)
  184.         {
  185.             this.playSound(SoundEvents.ENTITY_HORSE_ARMOR, 0.5F, 1.0F);
  186.         }
  187.     }
  188.  
  189.     @Override
  190.     protected void playGallopSound(SoundType p_190680_1_) {}
  191.  
  192.     @Override
  193.     protected void applyEntityAttributes()
  194.     {
  195.         super.applyEntityAttributes();
  196.         this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(16.0D);
  197.         this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(this.getModifiedMovementSpeed());
  198.         this.getEntityAttribute(JUMP_STRENGTH).setBaseValue(this.getModifiedJumpStrength());
  199.     }
  200.  
  201.     @Override
  202.     protected SoundEvent getAmbientSound()
  203.     {
  204.         return SoundEvents.ENTITY_SPIDER_AMBIENT;
  205.     }
  206.    
  207.     @Override
  208.     protected SoundEvent getHurtSound(DamageSource damageSourceIn)
  209.     {
  210.         return SoundEvents.ENTITY_SPIDER_HURT;
  211.     }
  212.    
  213.     @Override
  214.     protected SoundEvent getDeathSound()
  215.     {
  216.         return SoundEvents.ENTITY_SPIDER_DEATH;
  217.     }
  218.    
  219.     @Override
  220.     protected void playStepSound(BlockPos pos, Block blockIn)
  221.     {
  222.         this.playSound(SoundEvents.ENTITY_SPIDER_STEP, 0.15F, 1.0F);
  223.     }
  224.  
  225.     @Override
  226.     @Nullable
  227.     protected ResourceLocation getLootTable()
  228.     {
  229.         return LootTableList.ENTITIES_SPIDER;
  230.     }
  231.  
  232.     @Override
  233.     protected SoundEvent getAngrySound()
  234.     {
  235.         super.getAngrySound();
  236.         return null;
  237.     }
  238.  
  239.     public boolean processInteract(EntityPlayer player, EnumHand hand)
  240.     {
  241.         ItemStack itemstack = player.getHeldItem(hand);
  242.        
  243.         if (this.isTame() && player.isSneaking())
  244.         {
  245.             this.openGUI(player);
  246.             return true;
  247.         }
  248.  
  249.         if (this.isBeingRidden())
  250.         {
  251.             return super.processInteract(player, hand);
  252.         }    
  253.  
  254.         if (!itemstack.isEmpty())
  255.         {
  256.             if (this.getHealth() < this.getMaxHealth() && itemstack.getItem() instanceof ItemFood && this.isTame())
  257.             {
  258.                 ItemFood food = (ItemFood) itemstack.getItem();
  259.                
  260.                 if (!player.capabilities.isCreativeMode)
  261.                 {
  262.                     itemstack.shrink(1);
  263.                 }
  264.                
  265.                 for (int i = 0; i < 7; ++i)
  266.                 {
  267.                     double d0 = this.rand.nextGaussian() * 0.02D;
  268.                     double d1 = this.rand.nextGaussian() * 0.02D;
  269.                     double d2 = this.rand.nextGaussian() * 0.02D;
  270.                     this.world.spawnParticle(EnumParticleTypes.VILLAGER_HAPPY, this.posX + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, this.posY + 0.5D + (double)(this.rand.nextFloat() * this.height), this.posZ + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, d0, d1, d2);
  271.                 }
  272.                
  273.                 this.heal(food.getHealAmount(itemstack));
  274.                
  275.                 return true;
  276.             }
  277.             else if (!this.isTame() && itemstack.getItem() == Items.NETHER_WART)
  278.             {
  279.                 if (!player.capabilities.isCreativeMode)
  280.                 {
  281.                     itemstack.shrink(1);
  282.                 }
  283.                
  284.                 if (this.rand.nextInt(3) == 1)
  285.                 {
  286.                     this.setTamedBy(player);
  287.                    
  288.                     this.playTameEffect(true);
  289.                 }
  290.                 else
  291.                 {
  292.                     this.playTameEffect(false);
  293.                 }
  294.                
  295.                 return true;
  296.             }
  297.  
  298.             if (itemstack.interactWithEntity(player, this, hand))
  299.             {
  300.                 return true;
  301.             }
  302.  
  303.             boolean flag1 = SpiderArmorType.getByItemStack(itemstack) != SpiderArmorType.NONE;
  304.             boolean flag2 = !this.isHorseSaddled() && itemstack.getItem() == Items.SADDLE;
  305.  
  306.             if (flag1 || flag2)
  307.             {
  308.                 this.openGUI(player);
  309.                 return true;
  310.             }
  311.         }
  312.  
  313.            this.mountTo(player);
  314.            return true;
  315.     }
  316.    
  317.     @Override
  318.     public void fall(float distance, float damageMultiplier)
  319.     {
  320.         int i = MathHelper.ceil((distance * 0.5F - 3.0F) * damageMultiplier);
  321.  
  322.         if (i > 0)
  323.         {
  324.             this.attackEntityFrom(DamageSource.FALL, (float)i);
  325.  
  326.             if (this.isBeingRidden())
  327.             {
  328.                 for (Entity entity : this.getRecursivePassengers())
  329.                 {
  330.                     entity.attackEntityFrom(DamageSource.FALL, (float)i);
  331.                 }
  332.             }
  333.  
  334.             IBlockState iblockstate = this.world.getBlockState(new BlockPos(this.posX, this.posY - 0.2D - (double)this.prevRotationYaw, this.posZ));
  335.             Block block = iblockstate.getBlock();
  336.  
  337.             if (iblockstate.getMaterial() != Material.AIR && !this.isSilent())
  338.             {
  339.                 SoundType soundtype = block.getSoundType();
  340.                 this.world.playSound((EntityPlayer)null, this.posX, this.posY, this.posZ, soundtype.getStepSound(), this.getSoundCategory(), soundtype.getVolume() * 0.5F, soundtype.getPitch() * 0.75F);
  341.             }
  342.         }
  343.     }
  344.    
  345.     @Override
  346.     public double getMountedYOffset()
  347.     {
  348.         return (double)(this.height * 0.5F);
  349.     }
  350.    
  351.     @Override
  352.     protected void mountTo(EntityPlayer player)
  353.     {
  354.         if (this.isTame())
  355.         {
  356.             player.rotationYaw = this.rotationYaw;
  357.             player.rotationPitch = this.rotationPitch;
  358.             this.setEatingHaystack(false);
  359.             this.setRearing(false);
  360.  
  361.             if (!this.world.isRemote)
  362.             {
  363.                 player.startRiding(this);
  364.             }
  365.         }
  366.     }
  367.    
  368.     protected void playTameEffect(boolean play)
  369.     {
  370.         EnumParticleTypes enumparticletypes = EnumParticleTypes.HEART;
  371.  
  372.         if (!play)
  373.         {
  374.             enumparticletypes = EnumParticleTypes.SMOKE_NORMAL;
  375.         }
  376.  
  377.         for (int i = 0; i < 7; ++i)
  378.         {
  379.             double d0 = this.rand.nextGaussian() * 0.02D;
  380.             double d1 = this.rand.nextGaussian() * 0.02D;
  381.             double d2 = this.rand.nextGaussian() * 0.02D;
  382.             this.world.spawnParticle(enumparticletypes, this.posX + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, this.posY + 0.5D + (double)(this.rand.nextFloat() * this.height), this.posZ + (double)(this.rand.nextFloat() * this.width * 2.0F) - (double)this.width, d0, d1, d2);
  383.         }
  384.     }
  385.    
  386.     @Override
  387.     protected boolean handleEating(EntityPlayer player, ItemStack stack)
  388.     {
  389.         return false;
  390.     }
  391.    
  392.     @Override
  393.     public void setRearing(boolean rearing) {}
  394.    
  395.     @Override
  396.     public void setBreeding(boolean breeding) {}
  397.    
  398.     @Override
  399.     public void setTemper(int temperIn) {}
  400.    
  401.     @Override
  402.     @Nullable
  403.     protected AbstractHorse getClosestHorse(Entity entityIn, double distance)
  404.     {
  405.         return null;
  406.     }
  407.  
  408.     @Override
  409.     public boolean canMateWith(EntityAnimal otherAnimal)
  410.     {
  411.         return false;
  412.     }
  413.    
  414.     @Override
  415.     public EntityAgeable createChild(EntityAgeable ageable)
  416.     {
  417.         return null;
  418.     }
  419.  
  420.     public boolean wearsArmor()
  421.     {
  422.         return true;
  423.     }
  424.  
  425.     @Override
  426.     public boolean isArmor(ItemStack stack)
  427.     {
  428.         return SpiderArmorType.isSpiderArmor(stack);
  429.     }
  430.  
  431.     @Override
  432.     @Nullable
  433.     public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata)
  434.     {
  435.         livingdata = super.onInitialSpawn(difficulty, livingdata);
  436.  
  437.         return livingdata;
  438.     }
  439.    
  440.     @Override
  441.     public void setHorseArmorStack(ItemStack itemStackIn) {}
  442.    
  443.     @Override
  444.     public HorseArmorType getHorseArmorType()
  445.     {
  446.         return null;
  447.     }
  448. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement