-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ability to retrieve the maximum amount craftable
- Loading branch information
1 parent
7320e07
commit d0a11f2
Showing
32 changed files
with
465 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...ava/com/refinedmods/refinedstorage/api/autocrafting/calculation/CalculationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.refinedmods.refinedstorage.api.autocrafting.calculation; | ||
|
||
class CalculationException extends RuntimeException { | ||
protected CalculationException(final String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...finedstorage/api/autocrafting/calculation/MissingResourcesCraftingCalculatorListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.refinedmods.refinedstorage.api.autocrafting.calculation; | ||
|
||
import com.refinedmods.refinedstorage.api.resource.ResourceKey; | ||
|
||
class MissingResourcesCraftingCalculatorListener implements CraftingCalculatorListener<Boolean> { | ||
private boolean missingResources; | ||
|
||
MissingResourcesCraftingCalculatorListener() { | ||
} | ||
|
||
MissingResourcesCraftingCalculatorListener(final boolean missingResources) { | ||
this.missingResources = missingResources; | ||
} | ||
|
||
boolean isMissingResources() { | ||
return missingResources; | ||
} | ||
|
||
@Override | ||
public CraftingCalculatorListener<Boolean> childCalculationStarted() { | ||
return new MissingResourcesCraftingCalculatorListener(missingResources); | ||
} | ||
|
||
@Override | ||
public void childCalculationCompleted(final ResourceKey resource, | ||
final long amount, | ||
final CraftingCalculatorListener<Boolean> childListener) { | ||
missingResources = ((MissingResourcesCraftingCalculatorListener) childListener).missingResources; | ||
} | ||
|
||
@Override | ||
public void ingredientsExhausted(final ResourceKey resource, final long amount) { | ||
missingResources = true; | ||
} | ||
|
||
@Override | ||
public void ingredientExtractedFromStorage(final ResourceKey resource, final long amount) { | ||
// no op | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...refinedstorage/api/autocrafting/calculation/NumberOverflowDuringCalculationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...pi/src/test/java/com/refinedmods/refinedstorage/api/autocrafting/AutocraftingHelpers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.refinedmods.refinedstorage.api.autocrafting; | ||
|
||
import com.refinedmods.refinedstorage.api.core.Action; | ||
import com.refinedmods.refinedstorage.api.resource.ResourceAmount; | ||
import com.refinedmods.refinedstorage.api.storage.EmptyActor; | ||
import com.refinedmods.refinedstorage.api.storage.StorageImpl; | ||
import com.refinedmods.refinedstorage.api.storage.root.RootStorage; | ||
import com.refinedmods.refinedstorage.api.storage.root.RootStorageImpl; | ||
|
||
public final class AutocraftingHelpers { | ||
private AutocraftingHelpers() { | ||
} | ||
|
||
public static RootStorage storage(final ResourceAmount... resourceAmounts) { | ||
final RootStorage storage = new RootStorageImpl(); | ||
storage.addSource(new StorageImpl()); | ||
for (final ResourceAmount resourceAmount : resourceAmounts) { | ||
storage.insert(resourceAmount.resource(), resourceAmount.amount(), Action.EXECUTE, EmptyActor.INSTANCE); | ||
} | ||
return storage; | ||
} | ||
|
||
public static PatternRepository patterns(final Pattern... patterns) { | ||
final PatternRepository patternRepository = new PatternRepositoryImpl(); | ||
for (final Pattern pattern : patterns) { | ||
patternRepository.add(pattern); | ||
} | ||
return patternRepository; | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...m/refinedmods/refinedstorage/api/autocrafting/calculation/CraftingCalculatorImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.refinedmods.refinedstorage.api.autocrafting.calculation; | ||
|
||
import com.refinedmods.refinedstorage.api.autocrafting.PatternRepository; | ||
import com.refinedmods.refinedstorage.api.resource.ResourceAmount; | ||
import com.refinedmods.refinedstorage.api.storage.root.RootStorage; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import static com.refinedmods.refinedstorage.api.autocrafting.AutocraftingHelpers.patterns; | ||
import static com.refinedmods.refinedstorage.api.autocrafting.AutocraftingHelpers.storage; | ||
import static com.refinedmods.refinedstorage.api.autocrafting.PatternBuilder.pattern; | ||
import static com.refinedmods.refinedstorage.api.autocrafting.ResourceFixtures.CRAFTING_TABLE; | ||
import static com.refinedmods.refinedstorage.api.autocrafting.ResourceFixtures.OAK_LOG; | ||
import static com.refinedmods.refinedstorage.api.autocrafting.ResourceFixtures.OAK_PLANKS; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class CraftingCalculatorImplTest { | ||
@Test | ||
void shouldNotFindMaxAmountIfThereAreAlwaysMissingResources() { | ||
// Arrange | ||
final RootStorage storage = storage( | ||
new ResourceAmount(OAK_PLANKS, 1) | ||
); | ||
final PatternRepository patterns = patterns( | ||
pattern() | ||
.ingredient(OAK_LOG, 1) | ||
.output(OAK_PLANKS, 4) | ||
.build(), | ||
pattern() | ||
.ingredient(OAK_PLANKS, 4) | ||
.output(CRAFTING_TABLE, 1) | ||
.build() | ||
); | ||
final CraftingCalculator sut = new CraftingCalculatorImpl(patterns, storage); | ||
|
||
// Act | ||
final long maxAmount = sut.getMaxAmount(CRAFTING_TABLE); | ||
|
||
// Assert | ||
assertThat(maxAmount).isZero(); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(longs = {1L, 2L, 3L, 4L, 5L, 6L, 7L, 64L, 128L}) | ||
void shouldFindMaxAmount(final long amountPossible) { | ||
// Arrange | ||
final RootStorage storage = storage( | ||
new ResourceAmount(OAK_LOG, amountPossible) | ||
); | ||
final PatternRepository patterns = patterns( | ||
pattern() | ||
.ingredient(OAK_LOG, 1) | ||
.output(OAK_PLANKS, 4) | ||
.build(), | ||
pattern() | ||
.ingredient(OAK_PLANKS, 4) | ||
.output(CRAFTING_TABLE, 1) | ||
.build() | ||
); | ||
final CraftingCalculator sut = new CraftingCalculatorImpl(patterns, storage); | ||
|
||
// Act | ||
final long maxAmount = sut.getMaxAmount(CRAFTING_TABLE); | ||
|
||
// Assert | ||
assertThat(maxAmount).isEqualTo(amountPossible); | ||
} | ||
|
||
@Test | ||
void shouldNotFindMaxAmountIfThereIsANumberOverflow() { | ||
// Arrange | ||
final RootStorage storage = storage( | ||
new ResourceAmount(OAK_PLANKS, Long.MAX_VALUE) | ||
); | ||
final PatternRepository patterns = patterns( | ||
pattern() | ||
.ingredient(OAK_LOG, 1) | ||
.output(OAK_PLANKS, 4) | ||
.build(), | ||
pattern() | ||
.ingredient(OAK_PLANKS, 4) | ||
.output(CRAFTING_TABLE, 1) | ||
.build() | ||
); | ||
final CraftingCalculator sut = new CraftingCalculatorImpl(patterns, storage); | ||
|
||
// Act | ||
final long maxAmount = sut.getMaxAmount(CRAFTING_TABLE); | ||
|
||
// Assert | ||
assertThat(maxAmount).isZero(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...c/test/java/com/refinedmods/refinedstorage/api/autocrafting/calculation/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@ParametersAreNonnullByDefault | ||
@FieldsAndMethodsAreNonnullByDefault | ||
package com.refinedmods.refinedstorage.api.autocrafting.calculation; | ||
|
||
import com.refinedmods.refinedstorage.api.core.FieldsAndMethodsAreNonnullByDefault; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.