Skip to content

Commit

Permalink
Pre-compute decompression patterns within bulk cell inventories rathe…
Browse files Browse the repository at this point in the history
…r than at every tick within the decompression service
  • Loading branch information
62832 committed Nov 9, 2023
1 parent 202bcef commit 7d19f3a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import static gripe._90.megacells.definition.MEGAItems.COMPRESSION_CARD;

import java.math.BigInteger;
import java.util.Set;

import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;

import appeng.api.config.Actionable;
import appeng.api.crafting.IPatternDetails;
import appeng.api.networking.security.IActionSource;
import appeng.api.stacks.AEItemKey;
import appeng.api.stacks.AEKey;
Expand All @@ -19,6 +23,7 @@

import gripe._90.megacells.misc.CompressionChain;
import gripe._90.megacells.misc.CompressionService;
import gripe._90.megacells.misc.DecompressionPattern;

public class BulkCellInventory implements StorageCell {
private static final String KEY = "key";
Expand All @@ -35,6 +40,7 @@ public class BulkCellInventory implements StorageCell {

private final boolean compressionEnabled;
private final CompressionChain compressionChain;
private final Set<IPatternDetails> decompressionPatterns;
private BigInteger unitCount;
private final BigInteger unitFactor;

Expand All @@ -45,17 +51,18 @@ public class BulkCellInventory implements StorageCell {
this.container = container;

var cell = (BulkCellItem) stack.getItem();
filterItem = (AEItemKey) cell.getConfigInventory(this.stack).getKey(0);
filterItem = (AEItemKey) cell.getConfigInventory(stack).getKey(0);

storedItem = getTag().contains(KEY) ? AEItemKey.fromTag(getTag().getCompound(KEY)) : null;
unitCount = !getTag().getString(UNIT_COUNT).isEmpty()
? new BigInteger(getTag().getString(UNIT_COUNT))
: BigInteger.ZERO;

compressionEnabled = cell.getUpgrades(this.stack).isInstalled(COMPRESSION_CARD);
compressionEnabled = cell.getUpgrades(stack).isInstalled(COMPRESSION_CARD);
compressionChain = CompressionService.INSTANCE
.getChain(storedItem != null ? storedItem : filterItem)
.orElseGet(CompressionChain::new);
decompressionPatterns = generateDecompressionPatterns();
unitFactor = compressionChain.unitFactor(storedItem != null ? storedItem : filterItem);

// Check newly-calculated factor against what's already recorded in order to adjust for a compression chain that
Expand Down Expand Up @@ -107,8 +114,39 @@ public boolean isCompressionEnabled() {
return compressionEnabled;
}

public CompressionChain getCompressionChain() {
return compressionChain;
public Set<IPatternDetails> getDecompressionPatterns() {
return decompressionPatterns;
}

private Set<IPatternDetails> generateDecompressionPatterns() {
if (!compressionEnabled || compressionChain.isEmpty()) {
return Set.of();
}

var patterns = new ObjectLinkedOpenHashSet<IPatternDetails>();
var decompressionChain = compressionChain.limited().reversed();

for (var variant : decompressionChain) {
if (variant == decompressionChain.get(decompressionChain.size() - 1)) {
continue;
}

var decompressed = decompressionChain.get(decompressionChain.indexOf(variant) + 1);
patterns.add(new DecompressionPattern(decompressed.item(), variant, false));
}

var remainingChain = compressionChain.subList(decompressionChain.size() - 1, compressionChain.size());

for (var variant : remainingChain) {
if (variant == remainingChain.get(0)) {
continue;
}

var decompressed = remainingChain.get(remainingChain.indexOf(variant) - 1);
patterns.add(new DecompressionPattern(decompressed.item(), variant, true));
}

return patterns;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,8 @@ public void appendHoverText(ItemStack is, Level level, @NotNull List<Component>
if (filterItem != null) {
if (storedItem == null) {
lines.add(Tooltips.of(MEGATranslations.PartitionedFor.text(filterItem.getDisplayName())));
} else {
if (!storedItem.equals(filterItem)) {
lines.add(MEGATranslations.MismatchedFilter.text().withStyle(ChatFormatting.DARK_RED));
}
} else if (!storedItem.equals(filterItem)) {
lines.add(MEGATranslations.MismatchedFilter.text().withStyle(ChatFormatting.DARK_RED));
}
} else {
lines.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import java.util.Objects;

import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
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 gripe._90.megacells.definition.MEGAItems;

public class DecompressionPattern implements IPatternDetails {
static final String NBT_BASE = "base";
static final String NBT_VARIANT = "variant";
Expand All @@ -19,23 +21,34 @@ public class DecompressionPattern implements IPatternDetails {
private final AEItemKey definition;
private final AEItemKey base;
private final AEItemKey variant;
private final int factor;
private final byte 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());
base = AEItemKey.fromTag(tag.getCompound(NBT_BASE));
variant = AEItemKey.fromTag(tag.getCompound(NBT_VARIANT));
factor = tag.getInt(NBT_FACTOR);
factor = tag.getByte(NBT_FACTOR);
toCompress = tag.getBoolean(NBT_TO_COMPRESS);
}

public DecompressionPattern(AEItemKey base, CompressionService.Variant variant, boolean toCompress) {
this.base = base;
this.variant = variant.item();
this.factor = variant.factor();
this.toCompress = toCompress;

var tag = new CompoundTag();
tag.put(NBT_BASE, this.base.toTag());
tag.put(NBT_VARIANT, this.variant.toTag());
tag.putByte(NBT_FACTOR, this.factor);
tag.putBoolean(NBT_TO_COMPRESS, this.toCompress);

definition = AEItemKey.of(MEGAItems.DECOMPRESSION_PATTERN, tag);
}

@Override
public AEItemKey getDefinition() {
return definition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import java.util.Collections;
import java.util.List;
import java.util.Set;

import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;

import net.minecraft.nbt.CompoundTag;

Expand All @@ -15,9 +13,7 @@
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.item.part.DecompressionModulePart;

Expand Down Expand Up @@ -56,8 +52,8 @@ public void onServerStartTick() {
for (int i = 0; i < cellHost.getCellCount(); i++) {
var cell = cellHost.getOriginalCellInventory(i);

if (cell instanceof BulkCellInventory bulkCell && bulkCell.isCompressionEnabled()) {
patterns.addAll(generatePatterns(bulkCell));
if (cell instanceof BulkCellInventory bulkCell) {
patterns.addAll(bulkCell.getDecompressionPatterns());
}
}
}
Expand All @@ -70,50 +66,4 @@ public void onServerStartTick() {
public List<IPatternDetails> getPatterns() {
return Collections.unmodifiableList(patterns);
}

private Set<IPatternDetails> generatePatterns(BulkCellInventory cell) {
var fullChain = cell.getCompressionChain();

if (fullChain.isEmpty()) {
return Set.of();
}

var patterns = new ObjectLinkedOpenHashSet<IPatternDetails>();
var decompressionChain = fullChain.limited().reversed();

for (var variant : decompressionChain) {
if (variant == decompressionChain.get(decompressionChain.size() - 1)) {
continue;
}

var pattern = MEGAItems.DECOMPRESSION_PATTERN.stack();
var decompressed = decompressionChain.get(decompressionChain.indexOf(variant) + 1);

encode(pattern.getOrCreateTag(), decompressed.item(), variant, false);
patterns.add(new DecompressionPattern(pattern));
}

var compressionChain = fullChain.subList(decompressionChain.size() - 1, fullChain.size());

for (var variant : compressionChain) {
if (variant == compressionChain.get(0)) {
continue;
}

var pattern = MEGAItems.DECOMPRESSION_PATTERN.stack();
var decompressed = compressionChain.get(compressionChain.indexOf(variant) - 1);

encode(pattern.getOrCreateTag(), decompressed.item(), variant, true);
patterns.add(new DecompressionPattern(pattern));
}

return patterns;
}

private void encode(CompoundTag tag, AEItemKey base, CompressionService.Variant variant, boolean toCompress) {
tag.put(DecompressionPattern.NBT_VARIANT, variant.item().toTag());
tag.put(DecompressionPattern.NBT_BASE, base.toTag());
tag.putInt(DecompressionPattern.NBT_FACTOR, variant.factor());
tag.putBoolean(DecompressionPattern.NBT_TO_COMPRESS, toCompress);
}
}

0 comments on commit 7d19f3a

Please sign in to comment.