Skip to content

Commit

Permalink
rerolling rework now has even harsher options
Browse files Browse the repository at this point in the history
  • Loading branch information
yrsegal committed Oct 13, 2023
1 parent 4ffe8d0 commit 0fea461
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/vazkii/quark/mixin/VillagerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 0fea461

Please sign in to comment.