22
33import jimenezli .neuro21 .entity .ai .goal .NeurosamaFamilyHurtByTargetGoal ;
44import jimenezli .neuro21 .handler .ItemHandler ;
5+ import net .minecraft .nbt .CompoundTag ;
6+ import net .minecraft .network .chat .Component ;
7+ import net .minecraft .network .syncher .EntityDataAccessor ;
8+ import net .minecraft .network .syncher .EntityDataSerializers ;
9+ import net .minecraft .network .syncher .SynchedEntityData ;
510import net .minecraft .sounds .SoundEvent ;
611import net .minecraft .sounds .SoundEvents ;
712import net .minecraft .world .InteractionHand ;
1924import net .minecraft .world .level .Level ;
2025
2126public class IronCowEntity extends Cow {
27+ private int nextMilkTicks ;
28+ private static final EntityDataAccessor <Integer > DATA_NEXT_MILK_TICKS ;
29+
2230 /**
2331 * I didn't override the breed method so iron cow's offsprings are normal cows.
2432 */
2533 public IronCowEntity (EntityType <? extends Cow > entityType , Level level ) {
2634 super (entityType , level );
35+ this .nextMilkTicks = 0 ;
36+ }
37+
38+ protected void defineSynchedData () {
39+ super .defineSynchedData ();
40+ this .entityData .define (DATA_NEXT_MILK_TICKS , 0 );
41+ }
42+
43+ static {
44+ DATA_NEXT_MILK_TICKS = SynchedEntityData .defineId (IronCowEntity .class , EntityDataSerializers .INT );
2745 }
2846
2947 public static AttributeSupplier .Builder createAttributes () {
@@ -34,9 +52,22 @@ public static AttributeSupplier.Builder createAttributes() {
3452 * Warning! if you attack the cow, the Neuro-sama family will attack you!
3553 */
3654 public void registerGoals () {
55+ super .registerGoals ();
3756 this .targetSelector .addGoal (1 , new NeurosamaFamilyHurtByTargetGoal (this ));
3857 }
3958
59+ public void readAdditionalSaveData (CompoundTag compoundTag ) {
60+ super .readAdditionalSaveData (compoundTag );
61+ if (compoundTag .contains ("NextMilkTicks" )) {
62+ this .nextMilkTicks = compoundTag .getInt ("NextMilkTicks" );
63+ }
64+ }
65+
66+ public void addAdditionalSaveData (CompoundTag compoundTag ) {
67+ super .addAdditionalSaveData (compoundTag );
68+ compoundTag .putInt ("NextMilkTicks" , this .nextMilkTicks );
69+ }
70+
4071 @ Override
4172 protected SoundEvent getHurtSound (DamageSource damageSource ) {
4273 return SoundEvents .IRON_GOLEM_HURT ;
@@ -47,15 +78,31 @@ protected SoundEvent getDeathSound() {
4778 return SoundEvents .IRON_GOLEM_DEATH ;
4879 }
4980
81+ public void aiStep () {
82+ super .aiStep ();
83+ if (!this .level .isClientSide && this .isAlive () && !this .isBaby ()) {
84+ this .entityData .set (DATA_NEXT_MILK_TICKS , --nextMilkTicks );
85+ }
86+ }
87+
5088 public InteractionResult mobInteract (Player player , InteractionHand interactionHand ) {
5189 ItemStack itemStack = player .getItemInHand (interactionHand );
5290 if (itemStack .is (Items .BUCKET ) && !this .isBaby ()) {
53- player .playSound (SoundEvents .COW_MILK , 1.0F , 1.0F );
54- ItemStack itemStack2 = ItemUtils .createFilledResult (itemStack , player , new ItemStack (ItemHandler .IRONMILK .get ()));
55- player .setItemInHand (interactionHand , itemStack2 );
56- return InteractionResult .sidedSuccess (this .level .isClientSide );
57- } else {
58- return super .mobInteract (player , interactionHand );
91+ if (this .entityData .get (DATA_NEXT_MILK_TICKS ) < 0 ) {
92+ if (!this .level .isClientSide ) {
93+ nextMilkTicks = 6000 ;
94+ } else {
95+ this .entityData .set (DATA_NEXT_MILK_TICKS , 6000 );
96+ }
97+ player .playSound (SoundEvents .COW_MILK , 1.0F , 1.0F );
98+ ItemStack itemStack2 = ItemUtils .createFilledResult (itemStack , player , new ItemStack (ItemHandler .IRONMILK .get ()));
99+ player .setItemInHand (interactionHand , itemStack2 );
100+ return InteractionResult .sidedSuccess (this .level .isClientSide );
101+ } else {
102+ player .displayClientMessage (Component .translatable ("entity.neuro21.iron_cow.cooldown" , Integer .valueOf (this .entityData .get (DATA_NEXT_MILK_TICKS ) / 1200 + 1 ).toString ()), true );
103+ return InteractionResult .PASS ;
104+ }
59105 }
106+ return super .mobInteract (player , interactionHand );
60107 }
61108}
0 commit comments