Advertisement
Guest User

CustomEntity

a guest
Jan 12th, 2021
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. package com.example.examplemod.entity;
  2.  
  3. import com.example.examplemod.custom.CustomBreakBlockGoal;
  4. import com.example.examplemod.custom.CustomSitGoal;
  5. import net.minecraft.block.Blocks;
  6. import net.minecraft.entity.*;
  7. import net.minecraft.entity.ai.attributes.AttributeModifierMap;
  8. import net.minecraft.entity.ai.attributes.Attributes;
  9. import net.minecraft.entity.ai.goal.*;
  10. import net.minecraft.entity.passive.TameableEntity;
  11. import net.minecraft.entity.player.PlayerEntity;
  12. import net.minecraft.network.datasync.DataParameter;
  13. import net.minecraft.network.datasync.DataSerializers;
  14. import net.minecraft.network.datasync.EntityDataManager;
  15. import net.minecraft.util.ActionResultType;
  16. import net.minecraft.util.Hand;
  17. import net.minecraft.util.SoundCategory;
  18. import net.minecraft.util.SoundEvents;
  19. import net.minecraft.util.math.BlockPos;
  20. import net.minecraft.util.math.vector.Vector3d;
  21. import net.minecraft.world.IWorld;
  22. import net.minecraft.world.World;
  23. import net.minecraft.world.server.ServerWorld;
  24. import software.bernie.geckolib3.core.IAnimatable;
  25. import software.bernie.geckolib3.core.PlayState;
  26. import software.bernie.geckolib3.core.builder.AnimationBuilder;
  27. import software.bernie.geckolib3.core.controller.AnimationController;
  28. import software.bernie.geckolib3.core.event.predicate.AnimationEvent;
  29. import software.bernie.geckolib3.core.manager.AnimationData;
  30. import software.bernie.geckolib3.core.manager.AnimationFactory;
  31. import javax.annotation.Nullable;
  32.  
  33. public class TestEntity extends TameableEntity implements IAnimatable {
  34.  
  35. private AnimationFactory factory = new AnimationFactory(this);
  36. public boolean flag = false;
  37.  
  38. public TestEntity(EntityType<? extends TameableEntity> type, World worldIn)
  39. {
  40. super(type, worldIn);
  41. this.enablePersistence();
  42. this.setHealth(this.getMaxHealth());
  43. this.getNavigator().setCanSwim(true);
  44. this.recalculateSize();
  45. this.ignoreFrustumCheck = true;
  46. }
  47.  
  48. public static AttributeModifierMap.MutableAttribute getAttributes() {
  49. return MobEntity.func_233666_p_()
  50. .createMutableAttribute(Attributes.MAX_HEALTH, 100.0D)
  51. .createMutableAttribute(Attributes.MOVEMENT_SPEED, 0.4D);
  52. }
  53.  
  54. @Override
  55. public ActionResultType func_230254_b_(PlayerEntity player, Hand hand) {
  56. ActionResultType result = super.func_230254_b_(player, hand);
  57.  
  58. if(player.world.isRemote){
  59. if(hand == Hand.MAIN_HAND){
  60. flag = !flag;
  61. result = ActionResultType.SUCCESS;
  62. }
  63. }
  64.  
  65. return result;
  66. }
  67.  
  68. @Override
  69. protected void registerGoals() {
  70. this.goalSelector.addGoal(1, new SwimGoal(this));
  71. this.goalSelector.addGoal(2, new CustomSitGoal(this));
  72. this.goalSelector.addGoal(3, new AttackHive(this, 0.6D, 3));
  73. this.goalSelector.addGoal(4, new WaterAvoidingRandomWalkingGoal(this, 0.4D));
  74. this.goalSelector.addGoal(5, new LookRandomlyGoal(this));
  75. this.goalSelector.addGoal(6, new PanicGoal(this, 0.6D));
  76. }
  77.  
  78. private <E extends IAnimatable> PlayState predicate(AnimationEvent<E> event) {
  79.  
  80. if(flag){
  81. event.getController().setAnimation(new AnimationBuilder().addAnimation("seating", true));
  82. } else {
  83. if (!(event.getLimbSwingAmount() > -0.15F && event.getLimbSwingAmount() < 0.15F)){
  84. event.getController().setAnimation(new AnimationBuilder().addAnimation("walk", true));
  85. }
  86. else {
  87. event.getController().setAnimation(new AnimationBuilder().addAnimation("idle", true));
  88. }
  89. }
  90.  
  91. return PlayState.CONTINUE;
  92. }
  93.  
  94. @Override
  95. public void registerControllers(AnimationData data)
  96. {
  97. data.addAnimationController(new AnimationController(this, "controller", 0, this::predicate));
  98. }
  99.  
  100. @Override
  101. public AnimationFactory getFactory()
  102. {
  103. return this.factory;
  104. }
  105.  
  106. @Nullable
  107. @Override
  108. public AgeableEntity func_241840_a(ServerWorld p_241840_1_, AgeableEntity p_241840_2_) {
  109. return null;
  110. }
  111.  
  112. class AttackHive extends CustomBreakBlockGoal {
  113. AttackHive(CreatureEntity creatureIn, double speed, int yMax) {
  114. super(Blocks.BEEHIVE, creatureIn, speed, yMax);
  115. }
  116.  
  117. public void playBreakingSound(IWorld worldIn, BlockPos pos) {
  118. worldIn.playSound((PlayerEntity)null, pos, SoundEvents.ENTITY_ZOMBIE_DESTROY_EGG, SoundCategory.HOSTILE, 0.5F, 0.9F + TestEntity.this.rand.nextFloat() * 0.2F);
  119. }
  120.  
  121. public void playBrokenSound(World worldIn, BlockPos pos) {
  122. worldIn.playSound((PlayerEntity)null, pos, SoundEvents.ENTITY_TURTLE_EGG_BREAK, SoundCategory.BLOCKS, 0.7F, 0.9F + worldIn.rand.nextFloat() * 0.2F);
  123. }
  124.  
  125. public double getTargetDistanceSq() {
  126. return 1.6D;
  127. }
  128. }
  129.  
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement