|
| 1 | +package net.tfminecraft.cooking.husbandry; |
| 2 | + |
| 3 | +import java.util.UUID; |
| 4 | +import java.util.concurrent.ConcurrentHashMap; |
| 5 | + |
| 6 | +import org.bukkit.inventory.ItemStack; |
| 7 | +import org.bukkit.inventory.meta.ItemMeta; |
| 8 | +import org.bukkit.persistence.PersistentDataType; |
| 9 | + |
| 10 | +import net.tfminecraft.cooking.utils.Keys; |
| 11 | +import net.tfminecraft.cooking.utils.QualityUtils; |
| 12 | + |
| 13 | +/** |
| 14 | + * Breeding-food stars scale the gene bonus above the parent average. |
| 15 | + * Five stars, universal feed, and items with no cooking quality keep the full bonus. |
| 16 | + */ |
| 17 | +public final class HusbandryFeedQuality { |
| 18 | + |
| 19 | + public static final double FULL_SCALE = 1.0; |
| 20 | + private static final long OFFER_WINDOW_MILLIS = 2_000L; |
| 21 | + private static final long LOVE_WINDOW_MILLIS = 45_000L; |
| 22 | + |
| 23 | + private static final ConcurrentHashMap<UUID, TimedScale> OFFERS = new ConcurrentHashMap<>(); |
| 24 | + private static final ConcurrentHashMap<UUID, TimedScale> FEEDS = new ConcurrentHashMap<>(); |
| 25 | + |
| 26 | + private HusbandryFeedQuality() {} |
| 27 | + |
| 28 | + public static double scaleForStars(int stars) { |
| 29 | + int clamped = Math.max(1, Math.min(5, stars)); |
| 30 | + return clamped / 5.0; |
| 31 | + } |
| 32 | + |
| 33 | + public static double clampScale(double scale) { |
| 34 | + if (Double.isNaN(scale)) { |
| 35 | + return FULL_SCALE; |
| 36 | + } |
| 37 | + return Math.max(0, Math.min(FULL_SCALE, scale)); |
| 38 | + } |
| 39 | + |
| 40 | + public static double combine(Double left, Double right) { |
| 41 | + if (left == null && right == null) { |
| 42 | + return FULL_SCALE; |
| 43 | + } |
| 44 | + if (left == null) { |
| 45 | + return clampScale(right); |
| 46 | + } |
| 47 | + if (right == null) { |
| 48 | + return clampScale(left); |
| 49 | + } |
| 50 | + return clampScale((left + right) / 2.0); |
| 51 | + } |
| 52 | + |
| 53 | + public static double scaleOf(ItemStack stack) { |
| 54 | + if (isUniversalFeed(stack)) { |
| 55 | + return FULL_SCALE; |
| 56 | + } |
| 57 | + Integer stars = starsOrNull(stack); |
| 58 | + if (stars == null) { |
| 59 | + return FULL_SCALE; |
| 60 | + } |
| 61 | + return scaleForStars(stars); |
| 62 | + } |
| 63 | + |
| 64 | + public static void offer(UUID animalId, double scale, long nowMillis) { |
| 65 | + if (animalId == null) { |
| 66 | + return; |
| 67 | + } |
| 68 | + OFFERS.put(animalId, new TimedScale(clampScale(scale), nowMillis)); |
| 69 | + } |
| 70 | + |
| 71 | + public static void discardOffer(UUID animalId) { |
| 72 | + if (animalId != null) { |
| 73 | + OFFERS.remove(animalId); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public static void commitOffer(UUID animalId, long nowMillis) { |
| 78 | + if (animalId == null) { |
| 79 | + return; |
| 80 | + } |
| 81 | + TimedScale offer = OFFERS.remove(animalId); |
| 82 | + if (offer == null || nowMillis - offer.atMillis > OFFER_WINDOW_MILLIS) { |
| 83 | + return; |
| 84 | + } |
| 85 | + remember(animalId, offer.scale, nowMillis); |
| 86 | + } |
| 87 | + |
| 88 | + public static void remember(UUID animalId, double scale, long nowMillis) { |
| 89 | + if (animalId == null) { |
| 90 | + return; |
| 91 | + } |
| 92 | + FEEDS.put(animalId, new TimedScale(clampScale(scale), nowMillis)); |
| 93 | + } |
| 94 | + |
| 95 | + public static double consumeBreedingScale(UUID mother, UUID father, ItemStack bredWith, long nowMillis) { |
| 96 | + Double motherScale = take(mother, nowMillis); |
| 97 | + Double fatherScale = take(father, nowMillis); |
| 98 | + if (motherScale == null && fatherScale == null) { |
| 99 | + return scaleOf(bredWith); |
| 100 | + } |
| 101 | + return combine(motherScale, fatherScale); |
| 102 | + } |
| 103 | + |
| 104 | + public static double takePair(UUID mother, UUID father, long nowMillis) { |
| 105 | + return combine(take(mother, nowMillis), take(father, nowMillis)); |
| 106 | + } |
| 107 | + |
| 108 | + static void clear() { |
| 109 | + OFFERS.clear(); |
| 110 | + FEEDS.clear(); |
| 111 | + } |
| 112 | + |
| 113 | + private static Double take(UUID animalId, long nowMillis) { |
| 114 | + if (animalId == null) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + TimedScale feed = FEEDS.remove(animalId); |
| 118 | + if (feed == null || nowMillis - feed.atMillis > LOVE_WINDOW_MILLIS) { |
| 119 | + return null; |
| 120 | + } |
| 121 | + return feed.scale; |
| 122 | + } |
| 123 | + |
| 124 | + private static boolean isUniversalFeed(ItemStack stack) { |
| 125 | + if (stack == null || stack.getType().isAir()) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + try { |
| 129 | + return HusbandryItems.matches(stack, HusbandryConfig.feedItem()); |
| 130 | + } catch (RuntimeException ignored) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private static Integer starsOrNull(ItemStack stack) { |
| 136 | + if (stack == null || stack.getType().isAir() || !stack.hasItemMeta()) { |
| 137 | + return null; |
| 138 | + } |
| 139 | + ItemMeta meta = stack.getItemMeta(); |
| 140 | + if (meta == null) { |
| 141 | + return null; |
| 142 | + } |
| 143 | + Integer stored = meta.getPersistentDataContainer().get(Keys.QUALITY, PersistentDataType.INTEGER); |
| 144 | + if (stored == null) { |
| 145 | + return null; |
| 146 | + } |
| 147 | + return QualityUtils.clamp(stored); |
| 148 | + } |
| 149 | + |
| 150 | + private record TimedScale(double scale, long atMillis) {} |
| 151 | +} |
0 commit comments