diff --git a/src/main/java/vazkii/quark/content/experimental/module/VillagerRerollingReworkModule.java b/src/main/java/vazkii/quark/content/experimental/module/VillagerRerollingReworkModule.java index 032b2ffd0d..0fc738c3aa 100644 --- a/src/main/java/vazkii/quark/content/experimental/module/VillagerRerollingReworkModule.java +++ b/src/main/java/vazkii/quark/content/experimental/module/VillagerRerollingReworkModule.java @@ -22,6 +22,7 @@ public class VillagerRerollingReworkModule extends QuarkModule { public static final String TAG_VILLAGER_SEED = "quark:MerchantInitialSeed"; + public static final String TAG_ITEMS_REROLLED_TODAY = "quark:RerolledItemsToday"; public static final String TAG_TRADE_TIER = "quark:tier"; public static boolean staticEnabled; @@ -42,6 +43,13 @@ public class VillagerRerollingReworkModule extends QuarkModule { "Trades earlier in the list will restock first.") public static int maximumRestocksPerDay = 3; + @Config(description = "If enabled, villagers will reroll when they restock, rather than when they begin work for the day.\n" + + "If disabled, players can prevent rerolling by ensuring the villager isn't out of stock on their last restock of the day.") + public static boolean rerollOnAnyRestock = false; + + @Config(description = "If enabled, villagers will be able to reroll any trade that has been used AT ALL since the last restock.") + public static boolean rerollEvenIfNotOutOfStock = false; + @Override public void configChanged() { staticEnabled = enabled; @@ -69,17 +77,23 @@ public void keepSeedOnConversion(LivingConversionEvent.Post event) { } } + public static void clearRerolls(Villager villager) { + villager.getPersistentData().remove(TAG_ITEMS_REROLLED_TODAY); + } + public static void attemptToReroll(Villager villager) { if (!staticEnabled || maximumRestocksPerDay == 0 || chanceToRerollWhenRestocking == 0) return; - int restocks = 0; + int restocks = villager.getPersistentData().getInt(TAG_ITEMS_REROLLED_TODAY); + if (restocks >= maximumRestocksPerDay && maximumRestocksPerDay > 0) + return; MerchantOffers offers = villager.getOffers(); for (int i = 0; i < offers.size(); i++) { MerchantOffer offer = offers.get(i); - if (offer.isOutOfStock()) { + if ((rerollEvenIfNotOutOfStock && offer.getUses() > 0) || offer.isOutOfStock()) { MerchantOffer rerolled = attemptToReroll(villager, offer); if (rerolled != null) { @@ -108,6 +122,9 @@ public static void attemptToReroll(Villager villager) { } } } + + if (maximumRestocksPerDay > 0) + villager.getPersistentData().putInt(TAG_ITEMS_REROLLED_TODAY, restocks); } public static MerchantOffer attemptToReroll(Villager villager, MerchantOffer original) { diff --git a/src/main/java/vazkii/quark/mixin/VillagerMixin.java b/src/main/java/vazkii/quark/mixin/VillagerMixin.java index 0fa4fb867e..858f4c68d4 100644 --- a/src/main/java/vazkii/quark/mixin/VillagerMixin.java +++ b/src/main/java/vazkii/quark/mixin/VillagerMixin.java @@ -13,6 +13,15 @@ public class VillagerMixin { @Inject(method = "resetNumberOfRestocks", at = @At("TAIL")) public void resetRestocks(CallbackInfo ci) { Villager villager = (Villager) (Object) this; - VillagerRerollingReworkModule.attemptToReroll(villager); + VillagerRerollingReworkModule.clearRerolls(villager); + if (!VillagerRerollingReworkModule.rerollOnAnyRestock) + VillagerRerollingReworkModule.attemptToReroll(villager); + } + + @Inject(method = "restock", at = @At("TAIL")) + public void restock(CallbackInfo ci) { + Villager villager = (Villager) (Object) this; + if (VillagerRerollingReworkModule.rerollOnAnyRestock) + VillagerRerollingReworkModule.attemptToReroll(villager); } }