-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
BatEntity.java
72 lines (62 loc) · 2.68 KB
/
BatEntity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package software.bernie.example.entity;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import software.bernie.geckolib.animatable.GeoEntity;
import software.bernie.geckolib.constant.DefaultAnimations;
import software.bernie.geckolib.core.animatable.GeoAnimatable;
import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.core.animation.AnimatableManager;
import software.bernie.geckolib.core.animation.AnimationController;
import software.bernie.geckolib.util.ClientUtils;
import software.bernie.geckolib.util.GeckoLibUtil;
/**
* Example {@link GeoAnimatable} implementation of an entity
* @see software.bernie.example.client.renderer.entity.BatRenderer
* @see software.bernie.example.client.model.entity.BatModel
*/
public class BatEntity extends PathfinderMob implements GeoEntity {
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
private boolean isFlying = false;
public BatEntity(EntityType<? extends PathfinderMob> type, Level worldIn) {
super(type, worldIn);
}
// Have the bat look at the player
@Override
protected void registerGoals() {
this.goalSelector.addGoal(6, new LookAtPlayerGoal(this, Player.class, 12.0F));
super.registerGoals();
}
// Adds a right-click toggle that turns on/off its animating pose
@Override
public InteractionResult interactAt(Player player, Vec3 hitPos, InteractionHand hand) {
if (hand == InteractionHand.MAIN_HAND)
this.isFlying = !this.isFlying;
return super.interactAt(player, hitPos, hand);
}
@Override
public void registerControllers(AnimatableManager.ControllerRegistrar controllers) {
controllers.add(
// Add our flying animation controller
new AnimationController<>(this, 10, state -> state.setAndContinue(this.isFlying ? DefaultAnimations.FLY : DefaultAnimations.IDLE))
// Handle the custom instruction keyframe that is part of our animation json
.setCustomInstructionKeyframeHandler(state -> {
Player player = ClientUtils.getClientPlayer();
if (player != null)
player.displayClientMessage(Component.literal("KeyFraming"), true);
}),
// Add our generic living animation controller
DefaultAnimations.genericLivingController(this)
);
}
@Override
public AnimatableInstanceCache getAnimatableInstanceCache() {
return this.cache;
}
}