Skip to content

Commit 8145e47

Browse files
fix: keep husbandry animals on the baby model until they grow up
Vanilla age turns tracked babies into adults in about twenty minutes, before the husbandry grow-up timer. Lock age until mature_at so the adult model waits for that timer. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 2c230fc commit 8145e47

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

‎src/main/java/net/tfminecraft/cooking/husbandry/HusbandryBreedListener.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ private static void persistBaby(LivingEntity child, LivingEntity mother, LivingE
168168
HusbandryHarvest.prepareNewAnimal(baby, child.getType());
169169
repository.upsertAnimal(baby);
170170
HusbandryEntities.putLoaded(baby);
171+
HusbandryGrowth.applyMaturity(child, baby, now);
171172
}
172173

173174
private static void clearLove(Entity entity) {

‎src/main/java/net/tfminecraft/cooking/husbandry/HusbandryGrowth.java‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package net.tfminecraft.cooking.husbandry;
22

33
import org.bukkit.entity.Ageable;
4+
import org.bukkit.entity.Breedable;
45
import org.bukkit.entity.EntityType;
56
import org.bukkit.entity.LivingEntity;
67

78
public final class HusbandryGrowth {
89

10+
public enum ModelAge {
11+
BABY,
12+
ADULT
13+
}
14+
915
private HusbandryGrowth() {}
1016

1117
public static boolean isMature(HusbandryAnimal animal, long nowMillis) {
@@ -16,19 +22,52 @@ public static boolean isMature(HusbandryAnimal animal, long nowMillis) {
1622
return matureAt == null || nowMillis >= matureAt;
1723
}
1824

25+
/** Baby model until husbandry maturity. A missing record is already grown. */
26+
public static ModelAge modelAge(HusbandryAnimal animal, long nowMillis) {
27+
return isMature(animal, nowMillis) ? ModelAge.ADULT : ModelAge.BABY;
28+
}
29+
1930
public static long computeMatureAt(EntityType type, long bornAtMillis) {
2031
return bornAtMillis + HusbandryConfig.growUpSeconds(type) * 1000L;
2132
}
2233

2334
public static void applyMaturity(LivingEntity entity, HusbandryAnimal animal, long nowMillis) {
24-
if (animal == null || !isMature(animal, nowMillis)) {
35+
if (animal == null || entity == null) {
36+
return;
37+
}
38+
if (modelAge(animal, nowMillis) == ModelAge.BABY) {
39+
if (entity instanceof Breedable breedable) {
40+
holdBaby(breedable);
41+
}
2542
return;
2643
}
2744
if (animal.matureAt() != null) {
2845
animal.setMatureAt(null);
2946
}
47+
if (entity instanceof Breedable breedable) {
48+
releaseAdult(breedable);
49+
return;
50+
}
3051
if (entity instanceof Ageable ageable && !ageable.isAdult()) {
3152
ageable.setAdult();
3253
}
3354
}
55+
56+
private static void holdBaby(Breedable breedable) {
57+
if (breedable.isAdult()) {
58+
breedable.setBaby();
59+
}
60+
if (!breedable.getAgeLock()) {
61+
breedable.setAgeLock(true);
62+
}
63+
}
64+
65+
private static void releaseAdult(Breedable breedable) {
66+
if (breedable.getAgeLock()) {
67+
breedable.setAgeLock(false);
68+
}
69+
if (!breedable.isAdult()) {
70+
breedable.setAdult();
71+
}
72+
}
3473
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package net.tfminecraft.cooking.husbandry;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.UUID;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
class HusbandryGrowthTest {
10+
11+
@Test
12+
void immatureRecordKeepsTheBabyModel() {
13+
HusbandryAnimal animal = new HusbandryAnimal(UUID.randomUUID(), "COW", "Calf");
14+
long now = 1_000_000L;
15+
animal.setMatureAt(now + 60_000L);
16+
assertEquals(HusbandryGrowth.ModelAge.BABY, HusbandryGrowth.modelAge(animal, now));
17+
}
18+
19+
@Test
20+
void dueAndStoredAdultsUseTheAdultModel() {
21+
long now = 5_000L;
22+
HusbandryAnimal due = new HusbandryAnimal(UUID.randomUUID(), "CHICKEN", "Chick");
23+
due.setMatureAt(now);
24+
assertEquals(HusbandryGrowth.ModelAge.ADULT, HusbandryGrowth.modelAge(due, now));
25+
26+
HusbandryAnimal adult = new HusbandryAnimal(UUID.randomUUID(), "COW", "Cow");
27+
adult.setMatureAt(null);
28+
assertEquals(HusbandryGrowth.ModelAge.ADULT, HusbandryGrowth.modelAge(adult, now));
29+
assertEquals(HusbandryGrowth.ModelAge.ADULT, HusbandryGrowth.modelAge(null, now));
30+
}
31+
}

0 commit comments

Comments
 (0)