Skip to content

Commit

Permalink
Backport 1.20 compression rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Oct 2, 2023
1 parent ad70a70 commit e64fb0b
Show file tree
Hide file tree
Showing 19 changed files with 549 additions and 512 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,33 @@

import java.util.Objects;

import net.minecraft.core.NonNullList;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

import appeng.api.crafting.IPatternDetails;
import appeng.api.stacks.AEItemKey;
import appeng.api.stacks.AEKey;
import appeng.api.stacks.GenericStack;
import appeng.items.AEBaseItem;

public class MEGADecompressionPattern implements IPatternDetails {
public class DecompressionPattern implements IPatternDetails {
private final AEItemKey definition;
private final AEItemKey input;
private final IInput[] inputs;
private final GenericStack[] outputs;
private final AEItemKey base;
private final AEItemKey variant;
private final int factor;
private final boolean toCompress;

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

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

this.input = DecompressionPatternEncoding.getCompressed(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)};
base = DecompressionPatternEncoding.getBase(tag);
variant = DecompressionPatternEncoding.getVariant(tag);
factor = DecompressionPatternEncoding.getFactor(tag);
toCompress = DecompressionPatternEncoding.getToCompress(tag);
}

@Override
Expand All @@ -40,35 +38,35 @@ public AEItemKey getDefinition() {

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

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

@Override
public boolean equals(Object obj) {
return obj != null
&& obj.getClass() == getClass()
&& ((MEGADecompressionPattern) obj).definition.equals(definition);
&& ((DecompressionPattern) obj).definition.equals(definition);
}

@Override
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 All @@ -81,15 +79,4 @@ public AEKey getRemainingKey(AEKey template) {
return null;
}
}

public static class Item extends AEBaseItem {
public Item(Properties properties) {
super(properties);
}

@Override
public void fillItemCategory(CreativeModeTab tab, NonNullList<ItemStack> list) {
// Don't show in creative mode, this isn't meant to be used as an item anyway
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,38 @@

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) {
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) {
tag.put(NBT_COMPRESSED, compressed.toTag());
tag.put(NBT_DECOMPRESSED, decompressed.toTag());
tag.putInt(NBT_FACTOR, factor);
public static boolean getToCompress(CompoundTag nbt) {
Objects.requireNonNull(nbt, "Pattern must have a toCompress tag.");
return nbt.getBoolean(NBT_TO_COMPRESS);
}

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
@@ -0,0 +1,39 @@
package gripe._90.megacells.crafting;

import org.jetbrains.annotations.Nullable;

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

import appeng.api.stacks.AEItemKey;
import appeng.crafting.pattern.EncodedPatternItem;

import gripe._90.megacells.MEGACells;

public class DecompressionPatternItem extends EncodedPatternItem {
public DecompressionPatternItem(Properties properties) {
super(properties);
}

@Nullable
@Override
public DecompressionPattern decode(ItemStack stack, Level level, boolean tryRecovery) {
return decode(AEItemKey.of(stack), level);
}

@Nullable
@Override
public DecompressionPattern decode(AEItemKey what, Level level) {
if (what == null || !(what.hasTag())) {
return null;
}

try {
return new DecompressionPattern(what);
} catch (Exception e) {
MEGACells.LOGGER.warn(
"Could not decode an invalid decompression pattern %s: %s".formatted(what.getTag(), e));
return null;
}
}
}
Loading

0 comments on commit e64fb0b

Please sign in to comment.