Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ private static void persistBaby(LivingEntity child, LivingEntity mother, LivingE
HusbandryHarvest.prepareNewAnimal(baby, child.getType());
repository.upsertAnimal(baby);
HusbandryEntities.putLoaded(baby);
HusbandryGrowth.applyMaturity(child, baby, now);
}

private static void clearLove(Entity entity) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package net.tfminecraft.cooking.husbandry;

import org.bukkit.entity.Ageable;
import org.bukkit.entity.Breedable;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;

public final class HusbandryGrowth {

public enum ModelAge {
BABY,
ADULT
}

private HusbandryGrowth() {}

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

/** Baby model until husbandry maturity. A missing record is already grown. */
public static ModelAge modelAge(HusbandryAnimal animal, long nowMillis) {
return isMature(animal, nowMillis) ? ModelAge.ADULT : ModelAge.BABY;
}

public static long computeMatureAt(EntityType type, long bornAtMillis) {
return bornAtMillis + HusbandryConfig.growUpSeconds(type) * 1000L;
}

public static void applyMaturity(LivingEntity entity, HusbandryAnimal animal, long nowMillis) {
if (animal == null || !isMature(animal, nowMillis)) {
if (animal == null || entity == null) {
return;
}
if (modelAge(animal, nowMillis) == ModelAge.BABY) {
if (entity instanceof Breedable breedable) {
holdBaby(breedable);
}
return;
}
if (animal.matureAt() != null) {
animal.setMatureAt(null);
}
if (entity instanceof Breedable breedable) {
releaseAdult(breedable);
return;
}
if (entity instanceof Ageable ageable && !ageable.isAdult()) {
ageable.setAdult();
}
}

private static void holdBaby(Breedable breedable) {
if (breedable.isAdult()) {
breedable.setBaby();
}
if (!breedable.getAgeLock()) {
breedable.setAgeLock(true);
}
}

private static void releaseAdult(Breedable breedable) {
if (breedable.getAgeLock()) {
breedable.setAgeLock(false);
}
if (!breedable.isAdult()) {
breedable.setAdult();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.tfminecraft.cooking.husbandry;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.UUID;

import org.junit.jupiter.api.Test;

class HusbandryGrowthTest {

@Test
void immatureRecordKeepsTheBabyModel() {
HusbandryAnimal animal = new HusbandryAnimal(UUID.randomUUID(), "COW", "Calf");
long now = 1_000_000L;
animal.setMatureAt(now + 60_000L);
assertEquals(HusbandryGrowth.ModelAge.BABY, HusbandryGrowth.modelAge(animal, now));
}

@Test
void dueAndStoredAdultsUseTheAdultModel() {
long now = 5_000L;
HusbandryAnimal due = new HusbandryAnimal(UUID.randomUUID(), "CHICKEN", "Chick");
due.setMatureAt(now);
assertEquals(HusbandryGrowth.ModelAge.ADULT, HusbandryGrowth.modelAge(due, now));

HusbandryAnimal adult = new HusbandryAnimal(UUID.randomUUID(), "COW", "Cow");
adult.setMatureAt(null);
assertEquals(HusbandryGrowth.ModelAge.ADULT, HusbandryGrowth.modelAge(adult, now));
assertEquals(HusbandryGrowth.ModelAge.ADULT, HusbandryGrowth.modelAge(null, now));
}
}