-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…eading Autocrafting threading
- Loading branch information
Showing
152 changed files
with
2,212 additions
and
2,013 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
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
37 changes: 6 additions & 31 deletions
37
...rafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/Ingredient.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 |
---|---|---|
@@ -1,40 +1,15 @@ | ||
package com.refinedmods.refinedstorage.api.autocrafting; | ||
|
||
import com.refinedmods.refinedstorage.api.core.CoreValidations; | ||
import com.refinedmods.refinedstorage.api.resource.ResourceKey; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class Ingredient { | ||
private final long amount; | ||
private final List<ResourceKey> inputs; | ||
|
||
public Ingredient(final long amount, final List<? extends ResourceKey> inputs) { | ||
public record Ingredient(long amount, List<ResourceKey> inputs) { | ||
public Ingredient(final long amount, final List<ResourceKey> inputs) { | ||
CoreValidations.validateLargerThanZero(amount, "Amount must be larger than zero"); | ||
CoreValidations.validateNotEmpty(inputs, "Inputs cannot be empty"); | ||
this.amount = amount; | ||
this.inputs = Collections.unmodifiableList(inputs); | ||
} | ||
|
||
public boolean isEmpty() { | ||
return inputs.isEmpty(); | ||
} | ||
|
||
public int size() { | ||
return inputs.size(); | ||
} | ||
|
||
public long getAmount() { | ||
return amount; | ||
} | ||
|
||
public ResourceKey get(final int index) { | ||
return inputs.get(index); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Ingredient{" | ||
+ "amount=" + amount | ||
+ ", inputs=" + inputs | ||
+ '}'; | ||
this.inputs = List.copyOf(inputs); | ||
} | ||
} |
18 changes: 8 additions & 10 deletions
18
...tocrafting-api/src/main/java/com/refinedmods/refinedstorage/api/autocrafting/Pattern.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 |
---|---|---|
@@ -1,20 +1,18 @@ | ||
package com.refinedmods.refinedstorage.api.autocrafting; | ||
|
||
import com.refinedmods.refinedstorage.api.core.CoreValidations; | ||
import com.refinedmods.refinedstorage.api.resource.ResourceAmount; | ||
import com.refinedmods.refinedstorage.api.resource.ResourceKey; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
@API(status = API.Status.STABLE, since = "2.0.0-milestone.4.6") | ||
public interface Pattern { | ||
Set<ResourceKey> getInputResources(); | ||
|
||
Set<ResourceKey> getOutputResources(); | ||
|
||
List<Ingredient> getIngredients(); | ||
|
||
List<ResourceAmount> getOutputs(); | ||
public record Pattern(List<Ingredient> ingredients, List<ResourceAmount> outputs) { | ||
public Pattern(final List<Ingredient> ingredients, final List<ResourceAmount> outputs) { | ||
CoreValidations.validateNotEmpty(ingredients, "Ingredients cannot be empty"); | ||
CoreValidations.validateNotEmpty(outputs, "Outputs cannot be empty"); | ||
this.ingredients = List.copyOf(ingredients); | ||
this.outputs = List.copyOf(outputs); | ||
} | ||
} |
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
57 changes: 41 additions & 16 deletions
57
.../src/main/java/com/refinedmods/refinedstorage/api/autocrafting/PatternRepositoryImpl.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 |
---|---|---|
@@ -1,62 +1,87 @@ | ||
package com.refinedmods.refinedstorage.api.autocrafting; | ||
|
||
import com.refinedmods.refinedstorage.api.resource.ResourceAmount; | ||
import com.refinedmods.refinedstorage.api.resource.ResourceKey; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.PriorityQueue; | ||
import java.util.Set; | ||
|
||
public class PatternRepositoryImpl implements PatternRepository { | ||
private final Set<Pattern> patterns = new HashSet<>(); | ||
private final Map<ResourceKey, List<Pattern>> patternsByOutput = new HashMap<>(); | ||
private final Set<Pattern> patternsView = Collections.unmodifiableSet(patterns); | ||
private final Map<ResourceKey, PriorityQueue<PatternHolder>> patternsByOutput = new HashMap<>(); | ||
private final Set<ResourceKey> outputs = new HashSet<>(); | ||
private final Set<ResourceKey> outputsView = Collections.unmodifiableSet(outputs); | ||
|
||
@Override | ||
public void add(final Pattern pattern) { | ||
public void add(final Pattern pattern, final int priority) { | ||
patterns.add(pattern); | ||
outputs.addAll(pattern.getOutputResources()); | ||
for (final ResourceKey output : pattern.getOutputResources()) { | ||
patternsByOutput.computeIfAbsent(output, k -> new ArrayList<>()).add(pattern); | ||
pattern.outputs().forEach(output -> outputs.add(output.resource())); | ||
for (final ResourceAmount output : pattern.outputs()) { | ||
patternsByOutput.computeIfAbsent(output.resource(), k -> new PriorityQueue<>( | ||
Comparator.comparingInt(PatternHolder::priority).reversed() | ||
)).add(new PatternHolder(pattern, priority)); | ||
} | ||
} | ||
|
||
@Override | ||
public void update(final Pattern pattern, final int priority) { | ||
for (final ResourceAmount output : pattern.outputs()) { | ||
final PriorityQueue<PatternHolder> holders = patternsByOutput.get(output.resource()); | ||
if (holders == null) { | ||
continue; | ||
} | ||
holders.removeIf(holder -> holder.pattern.equals(pattern)); | ||
holders.add(new PatternHolder(pattern, priority)); | ||
} | ||
} | ||
|
||
@Override | ||
public void remove(final Pattern pattern) { | ||
patterns.remove(pattern); | ||
for (final ResourceKey output : pattern.getOutputResources()) { | ||
final List<Pattern> byOutput = patternsByOutput.get(output); | ||
if (byOutput == null) { | ||
for (final ResourceAmount output : pattern.outputs()) { | ||
final PriorityQueue<PatternHolder> holders = patternsByOutput.get(output.resource()); | ||
if (holders == null) { | ||
continue; | ||
} | ||
byOutput.remove(pattern); | ||
if (byOutput.isEmpty()) { | ||
patternsByOutput.remove(output); | ||
holders.removeIf(holder -> holder.pattern.equals(pattern)); | ||
if (holders.isEmpty()) { | ||
patternsByOutput.remove(output.resource()); | ||
} | ||
final boolean noOtherPatternHasThisOutput = patterns.stream() | ||
.noneMatch(otherPattern -> otherPattern.getOutputResources().contains(output)); | ||
.noneMatch(otherPattern -> otherPattern.outputs().stream() | ||
.anyMatch(o -> o.resource().equals(output.resource()))); | ||
if (noOtherPatternHasThisOutput) { | ||
outputs.remove(output); | ||
outputs.remove(output.resource()); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public List<Pattern> getByOutput(final ResourceKey output) { | ||
return patternsByOutput.getOrDefault(output, Collections.emptyList()); | ||
final PriorityQueue<PatternHolder> holders = patternsByOutput.get(output); | ||
if (holders == null) { | ||
return Collections.emptyList(); | ||
} | ||
return holders.stream().map(holder -> holder.pattern).toList(); | ||
} | ||
|
||
@Override | ||
public Set<ResourceKey> getOutputs() { | ||
return outputs; | ||
return outputsView; | ||
} | ||
|
||
@Override | ||
public Set<Pattern> getAll() { | ||
return patternsView; | ||
} | ||
|
||
private record PatternHolder(Pattern pattern, int priority) { | ||
} | ||
} |
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
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.