diff --git a/src/main/java/codechicken/nei/recipe/AutoCraftingManager.java b/src/main/java/codechicken/nei/recipe/AutoCraftingManager.java index d56ef894..6c929398 100644 --- a/src/main/java/codechicken/nei/recipe/AutoCraftingManager.java +++ b/src/main/java/codechicken/nei/recipe/AutoCraftingManager.java @@ -50,9 +50,19 @@ public class AutoCraftingManager { long multiplier = entry.getValue(); while (multiplier > 0 && !interrupted(guiContainer)) { - sleepInterruptibly(throttle.nextDelayMs(entry.getKey()), guiContainer); - if (interrupted(guiContainer) || !handler.craft(guiContainer, 1)) break; - multiplier -= 1; + final CraftRampThrottle.Tick tick = throttle.next(entry.getKey()); + sleepInterruptibly(tick.delayMs, guiContainer); + if (interrupted(guiContainer)) break; + + boolean crafted = false; + for (int i = 0; i < tick.crafts && multiplier > 0 + && !interrupted(guiContainer); i++) { + if (!handler.craft(guiContainer, 1)) break; + multiplier -= 1; + crafted = true; + } + + if (!crafted) break; // output couldn't be taken (e.g. inventory full) } craft = multiplier != entry.getValue(); diff --git a/src/main/java/codechicken/nei/recipe/CraftRampThrottle.java b/src/main/java/codechicken/nei/recipe/CraftRampThrottle.java index 378240c2..9fcc9f28 100644 --- a/src/main/java/codechicken/nei/recipe/CraftRampThrottle.java +++ b/src/main/java/codechicken/nei/recipe/CraftRampThrottle.java @@ -1,33 +1,48 @@ package codechicken.nei.recipe; -import java.util.Objects; +import java.util.HashMap; +import java.util.Map; import codechicken.nei.recipe.Recipe.RecipeId; /** * Per-craft delay ramp for auto-crafting. First craft of a recipe is instant, - * then the delay decays geometrically toward a floor. Changing the recipe - * resets the ramp. Pure logic; does not sleep. + * then the delay decays geometrically toward a floor. Each recipe keeps its own + * ramp for the run, so switching away and back resumes rather than restarts. At + * floor (max) speed crafts are batched. Pure logic; does not sleep. */ public class CraftRampThrottle { public static final long START_DELAY_MS = 500L; // delay before the 2nd craft public static final long FLOOR_DELAY_MS = 50L; // fastest allowed (cap) public static final double DECAY = 0.85D; + public static final int BULK_CRAFTS = 8; // crafts per tick once maxed out - private RecipeId current; - private long delayMs; + /** Delay to wait before a craft, and how many crafts that tick covers. */ + public static final class Tick { - /** Delay to wait before the next craft of {@code id}; 0 for the first / after a change. */ - public long nextDelayMs(RecipeId id) { - if (!Objects.equals(id, this.current)) { - this.current = id; - this.delayMs = START_DELAY_MS; - return 0L; + public final long delayMs; + public final int crafts; + + Tick(long delayMs, int crafts) { + this.delayMs = delayMs; + this.crafts = crafts; + } + } + + private final Map delays = new HashMap<>(); + + /** Next tick for {@code id}: instant single craft the first time, then a decaying delay. */ + public Tick next(RecipeId id) { + final Long stored = this.delays.get(id); + + if (stored == null) { + this.delays.put(id, START_DELAY_MS); + return new Tick(0L, 1); } - final long delay = this.delayMs; - this.delayMs = Math.max(FLOOR_DELAY_MS, Math.round(this.delayMs * DECAY)); - return delay; + final long delay = stored; + this.delays.put(id, Math.max(FLOOR_DELAY_MS, Math.round(delay * DECAY))); + return new Tick(delay, delay == FLOOR_DELAY_MS ? BULK_CRAFTS : 1); } } diff --git a/src/test/java/codechicken/nei/recipe/CraftRampThrottleTest.java b/src/test/java/codechicken/nei/recipe/CraftRampThrottleTest.java index e4b048a9..1855d6e5 100644 --- a/src/test/java/codechicken/nei/recipe/CraftRampThrottleTest.java +++ b/src/test/java/codechicken/nei/recipe/CraftRampThrottleTest.java @@ -6,16 +6,20 @@ import static org.mockito.Mockito.mock; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import codechicken.nei.recipe.CraftRampThrottle.Tick; import codechicken.nei.recipe.Recipe.RecipeId; class CraftRampThrottleTest { @Test - @DisplayName("first craft of a recipe is instant") + @DisplayName("first craft of a recipe is instant and single") void firstCraftInstant() { CraftRampThrottle throttle = new CraftRampThrottle(); RecipeId id = mock(RecipeId.class); - assertEquals(0L, throttle.nextDelayMs(id)); + + Tick tick = throttle.next(id); + assertEquals(0L, tick.delayMs); + assertEquals(1, tick.crafts); } @Test @@ -24,29 +28,55 @@ class CraftRampThrottleTest { CraftRampThrottle throttle = new CraftRampThrottle(); RecipeId id = mock(RecipeId.class); - assertEquals(0L, throttle.nextDelayMs(id)); // craft 1: instant - assertEquals(500L, throttle.nextDelayMs(id)); // craft 2: START - assertEquals(425L, throttle.nextDelayMs(id)); // 500 * 0.85 - assertEquals(361L, throttle.nextDelayMs(id)); // round(425 * 0.85) + assertEquals(0L, throttle.next(id).delayMs); // craft 1: instant + assertEquals(500L, throttle.next(id).delayMs); // craft 2: START + assertEquals(425L, throttle.next(id).delayMs); // 500 * 0.85 + assertEquals(361L, throttle.next(id).delayMs); // round(425 * 0.85) - long last = 361L; + Tick last = null; for (int i = 0; i < 50; i++) { - last = throttle.nextDelayMs(id); + last = throttle.next(id); } - assertEquals(CraftRampThrottle.FLOOR_DELAY_MS, last); // clamped at floor + assertEquals(CraftRampThrottle.FLOOR_DELAY_MS, last.delayMs); // clamped at floor } @Test - @DisplayName("changing recipe resets the ramp to instant") - void recipeChangeResets() { + @DisplayName("at floor speed, crafts happen in bulk") + void bulkAtFloorSpeed() { + CraftRampThrottle throttle = new CraftRampThrottle(); + RecipeId id = mock(RecipeId.class); + + Tick tick = throttle.next(id); + for (int i = 0; i < 50; i++) { + tick = throttle.next(id); + } + + assertEquals(CraftRampThrottle.FLOOR_DELAY_MS, tick.delayMs); + assertEquals(CraftRampThrottle.BULK_CRAFTS, tick.crafts); // batched once maxed out + } + + @Test + @DisplayName("crafts stay single while still ramping") + void singleWhileRamping() { + CraftRampThrottle throttle = new CraftRampThrottle(); + RecipeId id = mock(RecipeId.class); + + assertEquals(1, throttle.next(id).crafts); // instant + assertEquals(1, throttle.next(id).crafts); // 500 + assertEquals(1, throttle.next(id).crafts); // 425 + } + + @Test + @DisplayName("each recipe keeps its own ramp; returning resumes, not resets") + void perRecipeMemoryResumes() { CraftRampThrottle throttle = new CraftRampThrottle(); RecipeId a = mock(RecipeId.class); RecipeId b = mock(RecipeId.class); - assertEquals(0L, throttle.nextDelayMs(a)); // a craft 1 - assertEquals(500L, throttle.nextDelayMs(a)); // a craft 2 - assertEquals(0L, throttle.nextDelayMs(b)); // switch -> instant - assertEquals(500L, throttle.nextDelayMs(b)); // b craft 2 - assertEquals(0L, throttle.nextDelayMs(a)); // switch back -> instant again + assertEquals(0L, throttle.next(a).delayMs); // a craft 1 + assertEquals(500L, throttle.next(a).delayMs); // a craft 2 + assertEquals(0L, throttle.next(b).delayMs); // b starts fresh + assertEquals(425L, throttle.next(a).delayMs); // a resumes where it left off + assertEquals(500L, throttle.next(b).delayMs); // b resumes its own ramp } }