Skip to content

Commit 9061076

Browse files
committed
🚧 refactor -> AnimUtils for handling idle and walk animations; refactor spawn registration for entities
1 parent f907488 commit 9061076

File tree

5 files changed

+34
-30
lines changed

5 files changed

+34
-30
lines changed

common/src/main/java/org/infernalstudios/enemyexp/content/entity/HaulEntity.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,15 @@
55
import net.minecraft.world.entity.Entity;
66
import net.minecraft.world.entity.EntityType;
77
import net.minecraft.world.entity.LivingEntity;
8-
import net.minecraft.world.entity.SpawnPlacements;
98
import net.minecraft.world.entity.monster.Zombie;
109
import net.minecraft.world.level.Level;
11-
import net.minecraft.world.level.levelgen.Heightmap;
12-
import org.infernalstudios.enemyexp.core.mixin.SpawnPlacementsAccessor;
13-
import org.infernalstudios.enemyexp.setup.EEntities;
1410
import org.jetbrains.annotations.NotNull;
1511

1612
public class HaulEntity extends SprinterEntity {
1713
public HaulEntity(EntityType<? extends Zombie> entityType, Level level) {
1814
super(entityType, level);
1915
}
2016

21-
public static void spawn() {
22-
SpawnPlacementsAccessor.callRegister(EEntities.HAUL.get(), SpawnPlacements.Type.ON_GROUND, Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, EEntities::checkHostileRules);
23-
}
24-
2517
@Override
2618
public boolean doHurtTarget(@NotNull Entity entity) {
2719
boolean flag = super.doHurtTarget(entity);

common/src/main/java/org/infernalstudios/enemyexp/content/entity/SluggerEntity.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
import net.minecraft.world.entity.npc.AbstractVillager;
1616
import net.minecraft.world.entity.player.Player;
1717
import net.minecraft.world.level.Level;
18-
import net.minecraft.world.level.levelgen.Heightmap;
1918
import net.minecraft.world.phys.Vec3;
2019
import net.minecraft.world.damagesource.DamageSource;
2120
import org.infernalstudios.enemyexp.core.mixin.RandomLookAroundGoalAccessor;
22-
import org.infernalstudios.enemyexp.core.mixin.SpawnPlacementsAccessor;
23-
import org.infernalstudios.enemyexp.setup.EEntities;
21+
import org.infernalstudios.enemyexp.core.util.AnimUtils;
2422
import org.jetbrains.annotations.NotNull;
2523
import software.bernie.geckolib.animatable.GeoEntity;
2624
import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache;
@@ -87,10 +85,6 @@ protected void defineSynchedData() {
8785
this.entityData.define(TEXTURE, NORMAL_TEXTURE);
8886
}
8987

90-
public static void spawn() {
91-
SpawnPlacementsAccessor.callRegister(EEntities.SLUGGER.get(), SpawnPlacements.Type.ON_GROUND, Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, EEntities::checkHostileRules);
92-
}
93-
9488
private void lockRotationDuringCharge() {
9589
this.setYRot(this.yRotO);
9690
this.yHeadRot = this.yRotO;
@@ -134,9 +128,7 @@ private PlayState movementPredicate(AnimationState<?> event) {
134128
} else if (chargeTime > 0) {
135129
return event.setAndContinue(RawAnimation.begin().thenLoop("dash"));
136130
}
137-
return !event.isMoving() && event.getLimbSwingAmount() > -0.15F && event.getLimbSwingAmount() < 0.15F
138-
? event.setAndContinue(RawAnimation.begin().thenLoop("idle"))
139-
: event.setAndContinue(RawAnimation.begin().thenLoop("walk"));
131+
return AnimUtils.idleWalkAnimation(event, "idle", "walk");
140132
}
141133

142134
public int getChargeTime() {

common/src/main/java/org/infernalstudios/enemyexp/content/entity/SprinterEntity.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import net.minecraft.world.effect.MobEffects;
1414
import net.minecraft.world.entity.EntityType;
1515
import net.minecraft.world.entity.MobType;
16-
import net.minecraft.world.entity.SpawnPlacements;
1716
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
1817
import net.minecraft.world.entity.ai.attributes.Attributes;
1918
import net.minecraft.world.entity.ai.goal.FloatGoal;
@@ -24,10 +23,8 @@
2423
import net.minecraft.world.entity.monster.Zombie;
2524
import net.minecraft.world.entity.player.Player;
2625
import net.minecraft.world.level.Level;
27-
import net.minecraft.world.level.levelgen.Heightmap;
2826
import org.infernalstudios.enemyexp.EEMod;
29-
import org.infernalstudios.enemyexp.core.mixin.SpawnPlacementsAccessor;
30-
import org.infernalstudios.enemyexp.setup.EEntities;
27+
import org.infernalstudios.enemyexp.core.util.AnimUtils;
3128
import org.jetbrains.annotations.NotNull;
3229
import software.bernie.geckolib.animatable.GeoEntity;
3330
import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache;
@@ -60,10 +57,6 @@ public SprinterEntity(EntityType<? extends Zombie> entityType, Level level) {
6057
this.xpReward = 5;
6158
}
6259

63-
public static void spawn() {
64-
SpawnPlacementsAccessor.callRegister(EEntities.SPRINTER.get(), SpawnPlacements.Type.ON_GROUND, Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, EEntities::checkHostileRules);
65-
}
66-
6760
public static AttributeSupplier.@NotNull Builder createAttributes() {
6861
return Zombie.createAttributes()
6962
.add(Attributes.MOVEMENT_SPEED, 0.39D).add(Attributes.MAX_HEALTH, 10.0D)
@@ -114,7 +107,7 @@ public void registerControllers(AnimatableManager.ControllerRegistrar data) {
114107

115108
private PlayState movementPredicate(AnimationState<?> event) {
116109
if (!this.entityData.get(ANIMATION).equals("undefined")) return PlayState.STOP;
117-
return !event.isMoving() && event.getLimbSwingAmount() > -0.15F && event.getLimbSwingAmount() < 0.15F ? event.setAndContinue(RawAnimation.begin().thenLoop("idle")) : event.setAndContinue(RawAnimation.begin().thenLoop("sprint"));
110+
return AnimUtils.idleWalkAnimation(event, "idle", "sprint");
118111
}
119112

120113
private PlayState procedurePredicate(AnimationState<?> event) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.infernalstudios.enemyexp.core.util;
2+
3+
import software.bernie.geckolib.core.animation.AnimationState;
4+
import software.bernie.geckolib.core.animation.RawAnimation;
5+
import software.bernie.geckolib.core.object.PlayState;
6+
7+
public final class AnimUtils {
8+
9+
/**
10+
* A utility method to handle idle and walk animations based on movement.
11+
*
12+
* @param event The animation state event.
13+
* @param idleAnim The name of the idle animation.
14+
* @param walkAnim The name of the walk animation.
15+
* @return The appropriate PlayState for the animation.
16+
*/
17+
public static PlayState idleWalkAnimation(AnimationState<?> event, String idleAnim, String walkAnim) {
18+
return !event.isMoving() && event.getLimbSwingAmount() > -0.15F && event.getLimbSwingAmount() < 0.15F
19+
? event.setAndContinue(RawAnimation.begin().thenLoop(idleAnim))
20+
: event.setAndContinue(RawAnimation.begin().thenLoop(walkAnim));
21+
}
22+
23+
private AnimUtils() {
24+
}
25+
}

common/src/main/java/org/infernalstudios/enemyexp/setup/EEntities.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
99
import net.minecraft.world.entity.monster.Monster;
1010
import net.minecraft.world.level.ServerLevelAccessor;
11+
import net.minecraft.world.level.levelgen.Heightmap;
1112
import org.infernalstudios.enemyexp.EEMod;
1213
import org.infernalstudios.enemyexp.content.entity.HaulEntity;
1314
import org.infernalstudios.enemyexp.content.entity.SluggerEntity;
1415
import org.infernalstudios.enemyexp.content.entity.SprinterEntity;
1516
import org.infernalstudios.enemyexp.core.DeferredObject;
1617
import org.infernalstudios.enemyexp.core.RegisterFunction;
18+
import org.infernalstudios.enemyexp.core.mixin.SpawnPlacementsAccessor;
1719

1820
import java.util.HashMap;
1921
import java.util.Map;
@@ -36,9 +38,9 @@ public static void register(RegisterFunction<EntityType<?>> function) {
3638
}
3739

3840
public static void registerSpawns() {
39-
SprinterEntity.spawn();
40-
HaulEntity.spawn();
41-
SluggerEntity.spawn();
41+
SpawnPlacementsAccessor.callRegister(EEntities.SPRINTER.get(), SpawnPlacements.Type.ON_GROUND, Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, EEntities::checkHostileRules);
42+
SpawnPlacementsAccessor.callRegister(EEntities.HAUL.get(), SpawnPlacements.Type.ON_GROUND, Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, EEntities::checkHostileRules);
43+
SpawnPlacementsAccessor.callRegister(EEntities.SLUGGER.get(), SpawnPlacements.Type.ON_GROUND, Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, EEntities::checkHostileRules);
4244
}
4345

4446
public static void registerAttributes(BiConsumer<EntityType<? extends LivingEntity>, AttributeSupplier> consumer) {

0 commit comments

Comments
 (0)