Skip to content

Commit

Permalink
Generalize Encoded Pattern transformers
Browse files Browse the repository at this point in the history
Fixes transformations done by Programming Circuit Card (and future cards) not being saved
  • Loading branch information
NotMyWing committed May 24, 2024
1 parent 886b08a commit 5d70065
Show file tree
Hide file tree
Showing 11 changed files with 399 additions and 80 deletions.
3 changes: 3 additions & 0 deletions src/main/java/co/neeve/nae2/NAE2MixinPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public List<String> getMixinConfigs() {

var mixins = new ArrayList<String>();

// Core.
mixins.add("mixins.nae2.json");

for (var feature : Features.values()) {
if (!feature.isEnabled()) continue;

Expand Down
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;
}
}
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;
}
}
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);
}
}
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);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import appeng.core.features.DamagedItemDefinition;
import appeng.core.features.IStackSrc;
import co.neeve.nae2.Tags;
import co.neeve.nae2.common.crafting.patterntransform.PatternTransform;
import co.neeve.nae2.common.crafting.patterntransform.transformers.GregTechCircuitPatternTransformer;
import co.neeve.nae2.common.features.IFeature;
import co.neeve.nae2.common.features.subfeatures.UpgradeFeatures;
import co.neeve.nae2.common.items.NAEBaseItemUpgrade;
Expand Down Expand Up @@ -68,6 +70,8 @@ public Upgrades(Registry registry) {

this.gregtechCircuit = this.createUpgrade(this.upgrade, UpgradeType.GREGTECH_CIRCUIT);
if (this.gregtechCircuit.isEnabled()) {
PatternTransform.registerTransformer(new GregTechCircuitPatternTransformer());

registry.addBootstrapComponent((IPostInitComponent) r -> {
var definitions = Api.INSTANCE.definitions();
final IBlocks blocks = definitions.blocks();
Expand Down
Loading

0 comments on commit 5d70065

Please sign in to comment.