Guest User

Untitled

a guest
Apr 21st, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.58 KB | Gaming | 0 0
  1. /**
  2.  * Test mob class made for testing the logic of the DwellerEntity mob.
  3.  *
  4.  */
  5. public class BoxEntity extends PathfinderMob  implements IAnimatable, VibrationListener.VibrationListenerConfig {
  6.     private AnimationFactory animationFactory = new AnimationFactory(this);
  7.     private final DynamicGameEventListener<VibrationListener> dynamicGameEventListener;
  8.     public static final EntityDataAccessor<Boolean> SYNC_DATA = SynchedEntityData.defineId(BoxEntity.class, EntityDataSerializers.BOOLEAN);
  9.     private Rotation rot;
  10.     private boolean angry = false;
  11.  
  12.     public static AttributeSupplier setAttributes() {
  13.         return Monster.createMobAttributes()
  14.                 .add(Attributes.MAX_HEALTH, 30.0D)
  15.                 .add(Attributes.MOVEMENT_SPEED, 0.3f)
  16.                 .add(Attributes.FOLLOW_RANGE, 12.0D)
  17.                 .add(Attributes.ATTACK_DAMAGE, 5.0f)
  18.                 .add(Attributes.ATTACK_KNOCKBACK, 0.3f)
  19.                 .build();
  20.     }
  21.  
  22.     public BoxEntity(EntityType<? extends PathfinderMob> pEntityType, Level pLevel) {
  23.         super(pEntityType, pLevel);
  24.         this.dynamicGameEventListener = new DynamicGameEventListener<>(new VibrationListener(new EntityPositionSource(this, this.getEyeHeight()), 10, this, (VibrationListener.ReceivingEvent)null, 0.0F, 0));
  25.         this.setPathfindingMalus(BlockPathTypes.WATER, -1.0F);
  26.         this.setPathfindingMalus(BlockPathTypes.DAMAGE_FIRE, -1.0F);
  27.         this.xpReward = 3;
  28.     }
  29.  
  30.     protected void registerGoals() {
  31.         this.goalSelector.addGoal(4, new LookAtPlayerGoal(this, Player.class, 20));
  32.         this.goalSelector.addGoal(1, new MeleeAttackGoal(this, 1.0f, false));
  33.         this.goalSelector.addGoal(4, new RandomLookAroundGoal(this));
  34.         this.goalSelector.addGoal(3, new WaterAvoidingRandomStrollGoal(this, 1.0f, 0.0f));
  35.         this.goalSelector.addGoal(0, new LeapAtTargetGoal(this, 0.3f));
  36.  
  37.         this.targetSelector.addGoal(0, new HurtByTargetGoal(this));
  38.     }
  39.  
  40.     public void tick() {
  41.         Level level = this.level;
  42.         if (level instanceof ServerLevel serverlevel) {
  43.             this.dynamicGameEventListener.getListener().tick(serverlevel);
  44.         }
  45.         if (this.getTarget() == null) {
  46.             angry = false;
  47.         }
  48.         else angry = true;
  49.  
  50.         this.setClimbing(this.horizontalCollision);
  51.  
  52.         super.tick();
  53.     }
  54.  
  55.  
  56.     private <E extends IAnimatable> PlayState attackPredicate(AnimationEvent event) {
  57.  
  58.         if (this.swinging && event.getController().getAnimationState().equals(AnimationState.Stopped)) {
  59.             event.getController().markNeedsReload();
  60.             event.getController().setAnimation(new AnimationBuilder()
  61.                     .addAnimation("animation.box.attack", false));
  62.             this.swinging = false;
  63.         }
  64.         return PlayState.CONTINUE;
  65.     }
  66.  
  67.     private <E extends IAnimatable> PlayState predicate(AnimationEvent<E> event) {
  68.         if (event.isMoving()) {
  69.             event.getController().setAnimation(new AnimationBuilder()
  70.                     .addAnimation("animation.box.walk", true));
  71.             return PlayState.CONTINUE;
  72.         }
  73.  
  74.         event.getController().setAnimation(new AnimationBuilder()
  75.                 .addAnimation("animation.box.idle", true));
  76.         return PlayState.CONTINUE;
  77.     }
  78.  
  79.  
  80.     @Override
  81.     public boolean isPushable() {
  82.         return true;
  83.     }
  84.  
  85.     @Override
  86.     public void registerControllers(AnimationData data) {
  87.         data.addAnimationController(new AnimationController(this, "controller",
  88.                 0, this::predicate));
  89.         data.addAnimationController(new AnimationController(this, "attackController",
  90.                 0, this::attackPredicate));
  91.     }
  92.  
  93.     public TagKey<GameEvent> getListenableEvents() {
  94.         return GameEventTags.WARDEN_CAN_LISTEN;
  95.     }
  96.  
  97.     @Override
  98.     public AnimationFactory getFactory() {
  99.         return this.animationFactory;
  100.     }
  101.  
  102.     protected void playStepSound(BlockPos blockPos, BlockState blockIn) {
  103.         this.playSound(SoundEvents.GRASS_STEP, 0.3f, 1.0f);
  104.     }
  105.  
  106.     protected SoundEvent getAmbientSound() {
  107.         return SoundEvents.GRASS_STEP;
  108.     }
  109.  
  110.     protected SoundEvent getHurtSound(DamageSource dmgSrc) {
  111.         return SoundEvents.GRASS_HIT;
  112.     }
  113.  
  114.     protected SoundEvent getDeathSound() {
  115.         return SoundEvents.GRASS_BREAK;
  116.     }
  117.  
  118.     protected float getVolume() {
  119.         return 1.0f;
  120.     }
  121.  
  122.     @Contract("null->false")
  123.     public boolean canTargetEntity(@javax.annotation.Nullable Entity entity) {
  124.         if (entity instanceof LivingEntity livingentity) {
  125.             if (this.level == entity.level && EntitySelector.NO_CREATIVE_OR_SPECTATOR.test(entity) && !this.isAlliedTo(entity) && livingentity.getType() != EntityType.ARMOR_STAND && livingentity.getType() != EntityType.WARDEN && !livingentity.isInvulnerable() && !livingentity.isDeadOrDying() && this.level.getWorldBorder().isWithinBounds(livingentity.getBoundingBox())) {
  126.                 if (!(entity instanceof BoxEntity)) return true;
  127.             }
  128.         }
  129.  
  130.         return false;
  131.     }
  132.  
  133.     @Override
  134.     public boolean shouldListen(ServerLevel pLevel, GameEventListener pListener, BlockPos pPos, GameEvent pEvent, GameEvent.Context pContext) {
  135.         if (!this.isNoAi() && !this.isDeadOrDying() && !this.getBrain().hasMemoryValue(MemoryModuleType.VIBRATION_COOLDOWN) && pLevel.getWorldBorder().isWithinBounds(pPos) && !this.isRemoved() && this.level == pLevel) {
  136.             Entity entity = pContext.sourceEntity();
  137.             if (entity instanceof LivingEntity) {
  138.                 LivingEntity livingentity = (LivingEntity)entity;
  139.                 if (!this.canTargetEntity(livingentity)) {
  140.                     return false;
  141.                 }
  142.             }
  143.  
  144.             return true;
  145.         } else {
  146.             return false;
  147.         }
  148.     }
  149.  
  150.     @Override
  151.     public void updateDynamicGameEventListener(BiConsumer<DynamicGameEventListener<?>, ServerLevel> serverLevel) {
  152.         Level level = this.level;
  153.         if (level instanceof ServerLevel serverlevel) {
  154.             serverLevel.accept(this.dynamicGameEventListener, serverlevel);
  155.         }
  156.  
  157.     }
  158.  
  159.     @Override
  160.     public void onSignalReceive(ServerLevel level, GameEventListener listener, BlockPos pos, GameEvent event, @Nullable Entity entity, @Nullable Entity entity2, float distance) {
  161.         System.out.println("[DEBUG] event: " + event.toString());
  162.         System.out.println("[DEBUG] entity: " + entity.toString());
  163.         System.out.println("[DEBUG] distance: " + distance);
  164.         if (entity instanceof LivingEntity && entity.isAlive() && !(entity instanceof BoxEntity)) {
  165.             this.setTarget((LivingEntity) entity);
  166.         }
  167.     }
  168.  
  169.     // Climbing stuff
  170.  
  171.     @Override
  172.     protected PathNavigation createNavigation(Level pLevel) {
  173.         return new WallClimberNavigation(this, pLevel);
  174.     }
  175.  
  176.     public boolean isAngry() {
  177.         return this.angry;
  178.     }
  179.  
  180.     public boolean isClimbing() {
  181.         // System.out.println((this.entityData.get(DATA_FLAGS_ID) & 1) != 0);
  182.         // System.out.println(this.climbing);
  183.         System.out.println(this.entityData.get(SYNC_DATA));
  184.         return this.entityData.get(SYNC_DATA);
  185.     }
  186.  
  187.     public boolean onClimbable() {
  188.         return this.isClimbing();
  189.     }
  190.  
  191.     @Override
  192.     protected void defineSynchedData() {
  193.         super.defineSynchedData();
  194.         this.entityData.define(SYNC_DATA, false);
  195.     }
  196.  
  197.     public void setClimbing(boolean pClimbing) {
  198.         boolean data = this.entityData.get(SYNC_DATA);
  199.         this.entityData.set(SYNC_DATA, pClimbing);
  200.     }
  201. }
  202.  
Advertisement
Add Comment
Please, Sign In to add comment