Skip to content

Commit

Permalink
Allow decompression module to decompress immediately to the smallest …
Browse files Browse the repository at this point in the history
…variant
  • Loading branch information
62832 committed Sep 1, 2023
1 parent ee2e0f7 commit 3cf62bb
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@

public class DecompressionPattern implements IPatternDetails {
private final AEItemKey definition;
private final AEItemKey input;
private final IInput[] inputs;
private final GenericStack[] outputs;
private final AEItemKey compressed;
private final long count;
private final AEItemKey decompressed;
private final int factor;

public DecompressionPattern(AEItemKey definition) {
this.definition = definition;
var tag = Objects.requireNonNull(definition.getTag());

this.input = DecompressionPatternEncoding.getCompressed(tag);
this.compressed = DecompressionPatternEncoding.getCompressed(tag);
this.count = DecompressionPatternEncoding.getCount(tag);

var decompressed = DecompressionPatternEncoding.getDecompressed(tag);
var factor = DecompressionPatternEncoding.getFactor(tag);
var output = decompressed.toStack(factor);

this.inputs = new IInput[] {new Input()};
this.outputs = new GenericStack[] {GenericStack.fromItemStack(output)};
this.decompressed = DecompressionPatternEncoding.getDecompressed(tag);
this.factor = DecompressionPatternEncoding.getFactor(tag);
}

@Override
Expand All @@ -36,12 +34,12 @@ public AEItemKey getDefinition() {

@Override
public IInput[] getInputs() {
return inputs;
return new IInput[] {new Input(compressed, count)};
}

@Override
public GenericStack[] getOutputs() {
return outputs;
return new GenericStack[] {new GenericStack(decompressed, count * factor)};
}

@Override
Expand All @@ -56,15 +54,15 @@ public int hashCode() {
return definition.hashCode();
}

private class Input implements IInput {
private record Input(AEItemKey input, long multiplier) implements IInput {
@Override
public GenericStack[] getPossibleInputs() {
return new GenericStack[] {new GenericStack(input, 1)};
}

@Override
public long getMultiplier() {
return 1;
return multiplier;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class DecompressionPatternEncoding {
private static final String NBT_COMPRESSED = "compressed";
private static final String NBT_DECOMPRESSED = "decompressed";
private static final String NBT_COUNT = "count";
private static final String NBT_FACTOR = "factor";

public static AEItemKey getCompressed(CompoundTag nbt) {
Expand All @@ -21,14 +22,20 @@ public static AEItemKey getDecompressed(CompoundTag nbt) {
return AEItemKey.fromTag(nbt.getCompound(NBT_DECOMPRESSED));
}

public static long getCount(CompoundTag nbt) {
Objects.requireNonNull(nbt, "Pattern must have a count tag.");
return nbt.getLong(NBT_COUNT);
}

public static int getFactor(CompoundTag nbt) {
Objects.requireNonNull(nbt, "Pattern must have a factor tag.");
return nbt.getInt(NBT_FACTOR);
}

public static void encode(CompoundTag tag, AEItemKey compressed, AEItemKey decompressed, int factor) {
public static void encode(CompoundTag tag, AEItemKey compressed, AEItemKey decompressed, long count, int factor) {
tag.put(NBT_COMPRESSED, compressed.toTag());
tag.put(NBT_DECOMPRESSED, decompressed.toTag());
tag.putLong(NBT_COUNT, count);
tag.putInt(NBT_FACTOR, factor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,69 +9,88 @@

import net.minecraft.world.item.ItemStack;

import appeng.api.crafting.IPatternDetails;
import appeng.api.implementations.blockentities.IChestOrDrive;
import appeng.api.networking.IGridNode;
import appeng.api.networking.IGridService;
import appeng.api.networking.IGridServiceProvider;
import appeng.api.networking.crafting.ICraftingProvider;
import appeng.api.stacks.AEItemKey;

import gripe._90.megacells.definition.MEGAItems;
import gripe._90.megacells.item.cell.BulkCellInventory;
import gripe._90.megacells.util.CompressionChain;
import gripe._90.megacells.item.part.DecompressionModulePart;

public class DecompressionService implements IGridService, IGridServiceProvider {
private final Set<CompressionChain> decompressionChains = new ObjectLinkedOpenHashSet<>();
private final List<IChestOrDrive> cellHosts = new ObjectArrayList<>();
private final List<IPatternDetails> patterns = new ObjectArrayList<>();
private final List<DecompressionModulePart> modules = new ObjectArrayList<>();

@Override
public void addNode(IGridNode node) {
if (node.getOwner() instanceof IChestOrDrive cellHost) {
cellHosts.add(cellHost);
}

if (node.getOwner() instanceof DecompressionModulePart module) {
modules.add(module);
}
}

@Override
public void removeNode(IGridNode node) {
if (node.getOwner() instanceof IChestOrDrive cellHost) {
cellHosts.remove(cellHost);
}

if (node.getOwner() instanceof DecompressionModulePart module) {
modules.remove(module);
}
}

@Override
public void onServerStartTick() {
decompressionChains.clear();
patterns.clear();

for (var cellHost : cellHosts) {
for (int i = 0; i < cellHost.getCellCount(); i++) {
var cell = cellHost.getOriginalCellInventory(i);

if (cell instanceof BulkCellInventory bulkCell && bulkCell.isCompressionEnabled()) {
decompressionChains.add(bulkCell.getDecompressionChain());
patterns.addAll(generatePatterns(bulkCell));
}
}
}

if (!modules.isEmpty()) {
ICraftingProvider.requestUpdate(modules.get(0).getMainNode());
}
}

public Set<CompressionChain> getDecompressionChains() {
return Collections.unmodifiableSet(decompressionChains);
public List<IPatternDetails> getPatterns() {
return Collections.unmodifiableList(patterns);
}

public Set<AEItemKey> getDecompressionPatterns(CompressionChain chain) {
var patterns = new ObjectLinkedOpenHashSet<AEItemKey>();
private Set<IPatternDetails> generatePatterns(BulkCellInventory cell) {
var patterns = new ObjectLinkedOpenHashSet<IPatternDetails>();
var chain = cell.getDecompressionChain();
var count = 1;

for (var variant : chain) {
if (variant == chain.last()) {
continue;
}

var pattern = new ItemStack(MEGAItems.DECOMPRESSION_PATTERN);
var patternItem = new ItemStack(MEGAItems.DECOMPRESSION_PATTERN);
var decompressed = chain.get(chain.indexOf(variant) + 1);

DecompressionPatternEncoding.encode(
pattern.getOrCreateTag(), variant.item(), decompressed.item(), variant.factor());
patterns.add(AEItemKey.of(pattern));
patternItem.getOrCreateTag(), variant.item(), decompressed.item(), count, variant.factor());
var pattern = new DecompressionPattern(AEItemKey.of(patternItem.getItem(), patternItem.getTag()));
patterns.add(pattern);
count *= variant.factor();
}

return Collections.unmodifiableSet(patterns);
return patterns;
}
}
Loading

0 comments on commit 3cf62bb

Please sign in to comment.