Skip to content

Commit

Permalink
Minor decompression pattern refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Sep 11, 2023
1 parent a134766 commit d83ae8e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Objects;

import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

import appeng.api.crafting.IPatternDetails;
Expand All @@ -11,17 +12,21 @@

public class DecompressionPattern implements IPatternDetails {
private final AEItemKey definition;
private final AEItemKey compressed;
private final AEItemKey decompressed;
private final AEItemKey base;
private final AEItemKey variant;
private final int factor;
private final boolean toCompress;

public DecompressionPattern(ItemStack stack) {
this(Objects.requireNonNull(AEItemKey.of(stack)));
}

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

compressed = DecompressionPatternEncoding.getCompressed(tag);
decompressed = DecompressionPatternEncoding.getDecompressed(tag);
base = DecompressionPatternEncoding.getBase(tag);
variant = DecompressionPatternEncoding.getVariant(tag);
factor = DecompressionPatternEncoding.getFactor(tag);
toCompress = DecompressionPatternEncoding.getToCompress(tag);
}
Expand All @@ -33,13 +38,12 @@ public AEItemKey getDefinition() {

@Override
public IInput[] getInputs() {
return new IInput[] {toCompress ? new Input(decompressed, factor) : new Input(compressed, 1)};
return new IInput[] {toCompress ? new Input(base, factor) : new Input(variant, 1)};
}

@Override
public GenericStack[] getOutputs() {
return new GenericStack[] {toCompress ? new GenericStack(compressed, 1) : new GenericStack(decompressed, factor)
};
return new GenericStack[] {toCompress ? new GenericStack(variant, 1) : new GenericStack(base, factor)};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@

import appeng.api.stacks.AEItemKey;

import gripe._90.megacells.util.CompressionVariant;

public class DecompressionPatternEncoding {
private static final String NBT_COMPRESSED = "compressed";
private static final String NBT_DECOMPRESSED = "decompressed";
private static final String NBT_BASE = "base";
private static final String NBT_VARIANT = "variant";
private static final String NBT_FACTOR = "factor";
private static final String NBT_TO_COMPRESS = "toCompress";

public static AEItemKey getCompressed(CompoundTag nbt) {
Objects.requireNonNull(nbt, "Pattern must have a compressed tag.");
return AEItemKey.fromTag(nbt.getCompound(NBT_COMPRESSED));
public static AEItemKey getBase(CompoundTag nbt) {
Objects.requireNonNull(nbt, "Pattern must have a base tag.");
return AEItemKey.fromTag(nbt.getCompound(NBT_BASE));
}

public static AEItemKey getDecompressed(CompoundTag nbt) {
Objects.requireNonNull(nbt, "Pattern must have a decompressed tag.");
return AEItemKey.fromTag(nbt.getCompound(NBT_DECOMPRESSED));
public static AEItemKey getVariant(CompoundTag nbt) {
Objects.requireNonNull(nbt, "Pattern must have a variant tag.");
return AEItemKey.fromTag(nbt.getCompound(NBT_VARIANT));
}

public static int getFactor(CompoundTag nbt) {
Expand All @@ -32,11 +34,10 @@ public static boolean getToCompress(CompoundTag nbt) {
return nbt.getBoolean(NBT_TO_COMPRESS);
}

public static void encode(
CompoundTag tag, AEItemKey compressed, AEItemKey decompressed, int factor, boolean toCompress) {
tag.put(NBT_COMPRESSED, compressed.toTag());
tag.put(NBT_DECOMPRESSED, decompressed.toTag());
tag.putInt(NBT_FACTOR, factor);
public static void encode(CompoundTag tag, AEItemKey base, CompressionVariant variant, boolean toCompress) {
tag.put(NBT_VARIANT, variant.item().toTag());
tag.put(NBT_BASE, base.toTag());
tag.putInt(NBT_FACTOR, variant.factor());
tag.putBoolean(NBT_TO_COMPRESS, toCompress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
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;
Expand Down Expand Up @@ -89,9 +88,8 @@ private Set<IPatternDetails> generatePatterns(BulkCellInventory cell) {
var pattern = new ItemStack(MEGAItems.DECOMPRESSION_PATTERN);
var decompressed = decompressionChain.get(decompressionChain.indexOf(variant) + 1);

DecompressionPatternEncoding.encode(
pattern.getOrCreateTag(), variant.item(), decompressed.item(), variant.factor(), false);
patterns.add(new DecompressionPattern(AEItemKey.of(pattern.getItem(), pattern.getTag())));
DecompressionPatternEncoding.encode(pattern.getOrCreateTag(), decompressed.item(), variant, false);
patterns.add(new DecompressionPattern(pattern));
}

var compressionChain = fullChain.subList(decompressionChain.size() - 1, fullChain.size());
Expand All @@ -104,9 +102,8 @@ private Set<IPatternDetails> generatePatterns(BulkCellInventory cell) {
var pattern = new ItemStack(MEGAItems.DECOMPRESSION_PATTERN);
var decompressed = compressionChain.get(compressionChain.indexOf(variant) - 1);

DecompressionPatternEncoding.encode(
pattern.getOrCreateTag(), variant.item(), decompressed.item(), variant.factor(), true);
patterns.add(new DecompressionPattern(AEItemKey.of(pattern.getItem(), pattern.getTag())));
DecompressionPatternEncoding.encode(pattern.getOrCreateTag(), decompressed.item(), variant, true);
patterns.add(new DecompressionPattern(pattern));
}

return patterns;
Expand Down

0 comments on commit d83ae8e

Please sign in to comment.