-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalize Encoded Pattern transformers
Fixes transformations done by Programming Circuit Card (and future cards) not being saved
- Loading branch information
Showing
11 changed files
with
399 additions
and
80 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
36 changes: 36 additions & 0 deletions
36
src/main/java/co/neeve/nae2/common/crafting/patterntransform/PatternTransform.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,36 @@ | ||
package co.neeve.nae2.common.crafting.patterntransform; | ||
|
||
import appeng.api.networking.crafting.ICraftingMedium; | ||
import appeng.api.networking.crafting.ICraftingPatternDetails; | ||
import co.neeve.nae2.common.crafting.patterntransform.transformers.IPatternTransformer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class PatternTransform { | ||
protected final static List<IPatternTransformer> transformers = new ArrayList<>(); | ||
|
||
public static void registerTransformer(IPatternTransformer transformer) { | ||
transformers.add(transformer); | ||
} | ||
|
||
public static ICraftingPatternDetails transform(ICraftingMedium medium, ICraftingPatternDetails pattern) { | ||
final var originalInputs = pattern.getInputs(); | ||
final var originalOutputs = pattern.getOutputs(); | ||
var inputs = originalInputs; | ||
var outputs = originalOutputs; | ||
|
||
for (var transformer : transformers) { | ||
if (!transformer.shouldTransform(medium, pattern)) continue; | ||
|
||
inputs = transformer.transformInputs(medium, pattern, inputs); | ||
outputs = transformer.transformOutputs(medium, pattern, outputs); | ||
} | ||
|
||
if (!Arrays.equals(inputs, originalInputs) || !Arrays.equals(outputs, originalOutputs)) { | ||
return new PatternTransformWrapper(pattern, inputs, outputs); | ||
} | ||
return pattern; | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
src/main/java/co/neeve/nae2/common/crafting/patterntransform/PatternTransformWrapper.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,118 @@ | ||
package co.neeve.nae2.common.crafting.patterntransform; | ||
|
||
import appeng.api.networking.crafting.ICraftingPatternDetails; | ||
import appeng.api.storage.data.IAEItemStack; | ||
import net.minecraft.inventory.InventoryCrafting; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.world.World; | ||
|
||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class PatternTransformWrapper implements ICraftingPatternDetails { | ||
private final IAEItemStack[] condensedInputs; | ||
private final IAEItemStack[] condensedOutputs; | ||
private final ICraftingPatternDetails delegate; | ||
private final IAEItemStack[] inputs; | ||
private final IAEItemStack[] outputs; | ||
|
||
public PatternTransformWrapper(ICraftingPatternDetails delegate, IAEItemStack[] inputs, IAEItemStack[] outputs) { | ||
this.delegate = delegate; | ||
this.inputs = inputs; | ||
this.outputs = outputs; | ||
|
||
this.condensedInputs = new IAEItemStack[inputs.length]; | ||
var offset = 0; | ||
|
||
IAEItemStack io; | ||
Iterator<IAEItemStack> iterator; | ||
for (iterator = Arrays.stream(inputs).iterator(); iterator.hasNext(); ++offset) { | ||
io = iterator.next(); | ||
this.condensedInputs[offset] = io; | ||
} | ||
|
||
offset = 0; | ||
this.condensedOutputs = new IAEItemStack[outputs.length]; | ||
|
||
for (iterator = Arrays.stream(outputs).iterator(); iterator.hasNext(); ++offset) { | ||
io = iterator.next(); | ||
this.condensedOutputs[offset] = io; | ||
} | ||
} | ||
|
||
@Override | ||
public ItemStack getPattern() {return this.delegate.getPattern();} | ||
|
||
@Override | ||
public boolean isValidItemForSlot(int i, ItemStack itemStack, World world) { | ||
return this.delegate.isValidItemForSlot(i, | ||
itemStack, | ||
world); | ||
} | ||
|
||
@Override | ||
public boolean isCraftable() {return this.delegate.isCraftable();} | ||
|
||
@Override | ||
public boolean canSubstitute() {return this.delegate.canSubstitute();} | ||
|
||
@Override | ||
public List<IAEItemStack> getSubstituteInputs(int slot) {return this.delegate.getSubstituteInputs(slot);} | ||
|
||
@Override | ||
public ItemStack getOutput(InventoryCrafting inventoryCrafting, World world) { | ||
return this.delegate.getOutput(inventoryCrafting, | ||
world); | ||
} | ||
|
||
@Override | ||
public int getPriority() {return this.delegate.getPriority();} | ||
|
||
@Override | ||
public void setPriority(int i) {this.delegate.setPriority(i);} | ||
|
||
@Override | ||
public IAEItemStack[] getCondensedInputs() { | ||
return this.condensedInputs; | ||
} | ||
|
||
@Override | ||
public IAEItemStack[] getInputs() { | ||
return this.inputs; | ||
} | ||
|
||
@Override | ||
public IAEItemStack[] getCondensedOutputs() { | ||
return this.condensedOutputs; | ||
} | ||
|
||
@Override | ||
public IAEItemStack[] getOutputs() { | ||
return this.outputs; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof PatternTransformWrapper that)) return false; | ||
return Arrays.equals(this.condensedInputs, that.condensedInputs) && Arrays.equals(this.condensedOutputs, | ||
that.condensedOutputs) && Objects.equals(this.delegate, that.delegate) && Arrays.equals(this.inputs, | ||
that.inputs) && Arrays.equals(this.outputs, that.outputs); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
var result = Objects.hash(this.delegate); | ||
result = 31 * result + Arrays.hashCode(this.condensedInputs); | ||
result = 31 * result + Arrays.hashCode(this.condensedOutputs); | ||
result = 31 * result + Arrays.hashCode(this.inputs); | ||
result = 31 * result + Arrays.hashCode(this.outputs); | ||
return result; | ||
} | ||
|
||
public ICraftingPatternDetails getDelegate() { | ||
return this.delegate; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...nae2/common/crafting/patterntransform/transformers/GregTechCircuitPatternTransformer.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,57 @@ | ||
package co.neeve.nae2.common.crafting.patterntransform.transformers; | ||
|
||
import appeng.api.implementations.IUpgradeableHost; | ||
import appeng.api.networking.crafting.ICraftingMedium; | ||
import appeng.api.networking.crafting.ICraftingPatternDetails; | ||
import appeng.api.storage.data.IAEItemStack; | ||
import co.neeve.nae2.common.interfaces.IExtendedUpgradeInventory; | ||
import co.neeve.nae2.common.registration.definitions.Upgrades; | ||
import gregtech.api.recipes.ingredients.IntCircuitIngredient; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
public class GregTechCircuitPatternTransformer implements IPatternTransformer { | ||
public static Optional<Integer> getCircuitValueFromDetails(ICraftingPatternDetails details) { | ||
var optCircuit = Arrays.stream(details.getInputs()) | ||
.filter(Objects::nonNull) | ||
.filter(ais -> IntCircuitIngredient.isIntegratedCircuit(ais.createItemStack())) | ||
.findFirst(); | ||
|
||
if (!optCircuit.isPresent()) return Optional.empty(); | ||
|
||
var circuit = optCircuit.get(); | ||
var config = IntCircuitIngredient.getCircuitConfiguration(circuit.createItemStack()); | ||
return Optional.of(config); | ||
} | ||
|
||
@NotNull | ||
protected static IAEItemStack[] filterCircuitsOut(IAEItemStack[] inputs) { | ||
if (inputs == null) return null; | ||
|
||
return Arrays.stream(inputs) | ||
.filter(Objects::nonNull) | ||
.filter(x -> !IntCircuitIngredient.isIntegratedCircuit(x.createItemStack())) | ||
.toArray(IAEItemStack[]::new); | ||
} | ||
|
||
@Override | ||
public boolean shouldTransform(ICraftingMedium medium, ICraftingPatternDetails details) { | ||
if (details.isCraftable()) return false; | ||
|
||
var optCircuit = getCircuitValueFromDetails(details); | ||
if (!optCircuit.isPresent()) return false; | ||
|
||
return medium instanceof IUpgradeableHost upgradeableHost | ||
&& upgradeableHost.getInventoryByName("upgrades") instanceof IExtendedUpgradeInventory naeUpgrades | ||
&& naeUpgrades.getInstalledUpgrades(Upgrades.UpgradeType.GREGTECH_CIRCUIT) > 0; | ||
} | ||
|
||
@Override | ||
public IAEItemStack[] transformInputs(ICraftingMedium medium, ICraftingPatternDetails details, | ||
IAEItemStack[] inputs) { | ||
return filterCircuitsOut(inputs); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...java/co/neeve/nae2/common/crafting/patterntransform/transformers/IPatternTransformer.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,19 @@ | ||
package co.neeve.nae2.common.crafting.patterntransform.transformers; | ||
|
||
import appeng.api.networking.crafting.ICraftingMedium; | ||
import appeng.api.networking.crafting.ICraftingPatternDetails; | ||
import appeng.api.storage.data.IAEItemStack; | ||
|
||
public interface IPatternTransformer { | ||
default IAEItemStack[] transformInputs(ICraftingMedium medium, ICraftingPatternDetails details, | ||
IAEItemStack[] inputs) { | ||
return inputs; | ||
} | ||
|
||
default IAEItemStack[] transformOutputs(ICraftingMedium medium, ICraftingPatternDetails details, | ||
IAEItemStack[] outputs) { | ||
return outputs; | ||
} | ||
|
||
boolean shouldTransform(ICraftingMedium medium, ICraftingPatternDetails details); | ||
} |
56 changes: 0 additions & 56 deletions
56
src/main/java/co/neeve/nae2/common/helpers/GregCircuitCraftingDetailsWrapper.java
This file was deleted.
Oops, something went wrong.
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.