Skip to content

Commit

Permalink
Re-implement old "limiter" for bulk cell compression, but on a per-ce…
Browse files Browse the repository at this point in the history
…ll basis
  • Loading branch information
62832 committed Nov 18, 2024
1 parent d536180 commit 3168ea1
Show file tree
Hide file tree
Showing 22 changed files with 356 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.21.1 2024-11-17T14:07:31.916554252 Language
79f2bdffe2659a931e47c3e340144ada20c70b08 assets/megacells/lang/en_us.json
// 1.21.1 2024-11-18T23:07:51.796909718 Language
9e9924e9ebb1aaf3dced54bdb41bfadab31c7fae assets/megacells/lang/en_us.json
2 changes: 2 additions & 0 deletions src/generated/resources/assets/megacells/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"gui.tooltips.megacells.ALot": "A lot.",
"gui.tooltips.megacells.AcceleratorThreads": "Provides 4 co-processing threads per block.",
"gui.tooltips.megacells.Compression": "Compression: %s",
"gui.tooltips.megacells.CompressionCutoff": "Bulk Compression Cutoff",
"gui.tooltips.megacells.Contains": "Contains: %s",
"gui.tooltips.megacells.Cutoff": "Cutoff: %s",
"gui.tooltips.megacells.Disabled": "Disabled",
"gui.tooltips.megacells.Empty": "Empty",
"gui.tooltips.megacells.Enabled": "Enabled",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package gripe._90.megacells.client.screen;

import java.util.ArrayList;
import java.util.List;

import org.jetbrains.annotations.Nullable;

import net.minecraft.network.chat.Component;
import net.minecraft.world.item.Item;

import appeng.client.gui.Icon;
import appeng.client.gui.widgets.IconButton;

import gripe._90.megacells.definition.MEGATranslations;
import gripe._90.megacells.misc.CompressionChain;

public class CompressionCutoffButton extends IconButton {
private Item item;

public CompressionCutoffButton(OnPress onPress) {
super(onPress);
}

public void setItem(CompressionChain.Variant variant) {
item = variant.item().getItem();
}

@Override
protected Icon getIcon() {
return null;
}

@Nullable
@Override
protected Item getItemOverlay() {
return item;
}

@Override
public List<Component> getTooltipMessage() {
var message = new ArrayList<Component>();
message.add(MEGATranslations.CompressionCutoff.text());

if (item != null) {
message.add(item.getDescription());
}

return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import appeng.core.definitions.AEItems;
import appeng.core.localization.GuiText;

import gripe._90.megacells.item.cell.BulkCellInventory;
import gripe._90.megacells.item.cell.BulkCellItem;
import gripe._90.megacells.menu.PortableCellWorkbenchMenu;

/**
Expand All @@ -33,6 +35,7 @@
public class PortableCellWorkbenchScreen extends UpgradeableScreen<PortableCellWorkbenchMenu> {
private final ToggleButton copyMode;
private final SettingToggleButton<FuzzyMode> fuzzyMode;
private final CompressionCutoffButton compressionCutoff;

public PortableCellWorkbenchScreen(
PortableCellWorkbenchMenu menu, Inventory playerInventory, Component title, ScreenStyle style) {
Expand All @@ -49,6 +52,7 @@ public PortableCellWorkbenchScreen(
GuiText.CopyMode.text(),
GuiText.CopyModeDesc.text(),
act -> menu.nextWorkBenchCopyMode()));
compressionCutoff = addToLeftToolbar(new CompressionCutoffButton(button -> menu.mega$nextCompressionLimit()));
}

@Override
Expand All @@ -57,6 +61,15 @@ protected void updateBeforeRender() {
copyMode.setState(menu.getCopyMode() == CopyMode.CLEAR_ON_REMOVE);
fuzzyMode.set(menu.getFuzzyMode());
fuzzyMode.setVisibility(menu.getUpgrades().isInstalled(AEItems.FUZZY_CARD));

if (BulkCellItem.HANDLER.getCellInventory(menu.getHost().mega$getContainedStack(), null)
instanceof BulkCellInventory bulkCell) {
compressionCutoff.setVisibility(bulkCell.isCompressionEnabled()
&& !bulkCell.getCompressionChain().isEmpty());
compressionCutoff.setItem(bulkCell.getCompressionChain().get(bulkCell.getCompressionCutoff() - 1));
} else {
compressionCutoff.setVisibility(false);
}
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public final class MEGAComponents {

public static final DataComponentType<AEKey> BULK_CELL_ITEM =
register("bulk_item", AEKey.CODEC, AEKey.STREAM_CODEC);
public static final DataComponentType<Integer> BULK_CELL_COMPRESSION_CUTOFF =
register("compression_cutoff", Codec.INT, ByteBufCodecs.VAR_INT);

private static final Codec<BigInteger> BIG_INTEGER_CODEC = Codec.STRING.comapFlatMap(
str -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ public enum MEGATranslations implements LocalizationEnum {
AcceleratorThreads("Provides 4 co-processing threads per block.", Type.TOOLTIP),
ALot("A lot.", Type.TOOLTIP),
Compression("Compression: %s", Type.TOOLTIP),
CompressionCutoff("Bulk Compression Cutoff", Type.TOOLTIP),
Contains("Contains: %s", Type.TOOLTIP),
Disabled("Disabled", Type.TOOLTIP),
Empty("Empty", Type.TOOLTIP),
Enabled("Enabled", Type.TOOLTIP),
FilterChemicalUnsupported("Filter chemical unsupported!", Type.TOOLTIP),
Cutoff("Cutoff: %s", Type.TOOLTIP),
MismatchedFilter("Mismatched filter!", Type.TOOLTIP),
ModName("MEGA Cells", Type.GUI),
NotInstalled("%s not installed.", Type.TOOLTIP),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ protected void saveChanges() {
if (container != null) {
container.saveChanges();
} else {
// if there is no ISaveProvider, store to NBT immediately
persist();
}
}
Expand Down
71 changes: 51 additions & 20 deletions src/main/java/gripe/_90/megacells/item/cell/BulkCellInventory.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package gripe._90.megacells.item.cell;

import static gripe._90.megacells.definition.MEGAItems.COMPRESSION_CARD;

import java.math.BigInteger;
import java.util.Collections;
import java.util.Set;
Expand All @@ -22,6 +20,7 @@
import appeng.api.storage.cells.StorageCell;

import gripe._90.megacells.definition.MEGAComponents;
import gripe._90.megacells.definition.MEGAItems;
import gripe._90.megacells.misc.CompressionChain;
import gripe._90.megacells.misc.CompressionService;
import gripe._90.megacells.misc.DecompressionPattern;
Expand All @@ -37,9 +36,10 @@ 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;
private final int compressionCutoff;
private Set<IPatternDetails> decompressionPatterns;

private boolean isPersisted = true;

Expand All @@ -53,10 +53,9 @@ public class BulkCellInventory implements StorageCell {
storedItem = (AEItemKey) stack.get(MEGAComponents.BULK_CELL_ITEM);
unitCount = stack.getOrDefault(MEGAComponents.BULK_CELL_UNIT_COUNT, BigInteger.ZERO);

compressionEnabled = cell.getUpgrades(stack).isInstalled(COMPRESSION_CARD);
compressionEnabled = cell.getUpgrades(stack).isInstalled(MEGAItems.COMPRESSION_CARD);
compressionChain = CompressionService.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 All @@ -67,6 +66,15 @@ public class BulkCellInventory implements StorageCell {
unitCount = unitCount.multiply(unitFactor).divide(recordedFactor);
saveChanges();
}

int recordedCutoff = stack.getOrDefault(MEGAComponents.BULK_CELL_COMPRESSION_CUTOFF, compressionChain.size());

if (recordedCutoff > compressionChain.size()) {
stack.remove(MEGAComponents.BULK_CELL_COMPRESSION_CUTOFF);
compressionCutoff = compressionChain.size();
} else {
compressionCutoff = recordedCutoff;
}
}

private long clampedLong(BigInteger toClamp, long limit) {
Expand Down Expand Up @@ -102,31 +110,43 @@ public boolean isCompressionEnabled() {
return compressionEnabled;
}

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

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

var decompressionChain = new CompressionChain();
decompressionChain.addAll(compressionChain);
Collections.reverse(decompressionChain);
if (decompressionPatterns == null) {
var decompressionChain = compressionChain.limited(compressionCutoff);
Collections.reverse(decompressionChain);

var patterns = new ObjectLinkedOpenHashSet<IPatternDetails>();
decompressionPatterns = new ObjectLinkedOpenHashSet<>();

for (var variant : decompressionChain) {
if (variant == decompressionChain.getLast()) {
continue;
for (var variant : decompressionChain) {
if (variant == decompressionChain.getLast()) {
continue;
}

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

var decompressed = decompressionChain.get(decompressionChain.indexOf(variant) + 1);
patterns.add(new DecompressionPattern(decompressed.item(), variant));
var remainingChain = compressionChain.subList(decompressionChain.size() - 1, compressionChain.size());

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

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

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

@Override
Expand Down Expand Up @@ -213,7 +233,6 @@ private void saveChanges() {
if (container != null) {
container.saveChanges();
} else {
// if there is no ISaveProvider, store to NBT immediately
persist();
}
}
Expand All @@ -237,12 +256,24 @@ public void persist() {
isPersisted = true;
}

public void setCompressionCutoff(int cutoff) {
if (cutoff == compressionChain.size()) {
stack.remove(MEGAComponents.BULK_CELL_COMPRESSION_CUTOFF);
} else {
stack.set(MEGAComponents.BULK_CELL_COMPRESSION_CUTOFF, cutoff);
}
}

public int getCompressionCutoff() {
return compressionCutoff;
}

@Override
public void getAvailableStacks(KeyCounter out) {
if (storedItem != null) {
if (compressionEnabled && storedItem.equals(filterItem) && !compressionChain.isEmpty()) {
var count = unitCount;
var chain = compressionChain.lastMultiplierSwapped();
var chain = compressionChain.limited(compressionCutoff).lastMultiplierSwapped();

for (var variant : chain) {
var compressionFactor = BigInteger.valueOf(variant.factor());
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/gripe/_90/megacells/item/cell/BulkCellItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ public void appendHoverText(ItemStack is, TooltipContext context, List<Component
inv.isCompressionEnabled()
? MEGATranslations.Enabled.text().withStyle(ChatFormatting.GREEN)
: MEGATranslations.Disabled.text().withStyle(ChatFormatting.RED))));

if (inv.isCompressionEnabled()
&& inv.getCompressionCutoff() < inv.getCompressionChain().size()) {
lines.add(Tooltips.of(MEGATranslations.Cutoff.text(inv.getCompressionChain()
.get(inv.getCompressionCutoff() - 1)
.item()
.getDisplayName())));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class PortableCellWorkbenchInventory extends AppEngInternalInventory impl

private final GenericStackInv config =
new GenericStackInv(this::onConfigChanged, GenericStackInv.Mode.CONFIG_TYPES, 63);
private final IConfigManager manager = IConfigManager.builder(this::saveChanges)
.registerSetting(Settings.COPY_MODE, CopyMode.CLEAR_ON_REMOVE)
.build();

public PortableCellWorkbenchInventory(ItemStack stack) {
super(null, 1, 1, new Filter());
Expand All @@ -56,9 +59,7 @@ GenericStackInv getConfig() {
}

IConfigManager getConfigManager() {
return IConfigManager.builder(stack)
.registerSetting(Settings.COPY_MODE, CopyMode.CLEAR_ON_REMOVE)
.build();
return manager;
}

private ConfigInventory getCellConfigInventory() {
Expand Down Expand Up @@ -98,9 +99,10 @@ private void onConfigChanged() {
saveChanges();
}

private void saveChanges() {
void saveChanges() {
stack.set(DataComponents.CONTAINER, toItemContainerContents());
stack.set(AEComponents.EXPORTED_CONFIG_INV, config.toList());
stack.set(AEComponents.EXPORTED_SETTINGS, manager.exportSettings());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public Optional<TooltipComponent> getTooltipImage(@NotNull ItemStack stack) {
}
}

return Optional.of(new PortableCellWorkbenchTooltipComponent(shownConfig, host.getContainedStack(), hasMore));
return Optional.of(
new PortableCellWorkbenchTooltipComponent(shownConfig, host.mega$getContainedStack(), hasMore));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,27 @@
import appeng.items.contents.StackDependentSupplier;
import appeng.menu.locator.ItemMenuHostLocator;

import gripe._90.megacells.misc.CellWorkbenchHost;

/**
* See {@link appeng.blockentity.misc.CellWorkbenchBlockEntity}
*/
public class PortableCellWorkbenchMenuHost extends ItemMenuHost<PortableCellWorkbenchItem>
implements ISegmentedInventory, IConfigurableObject, IConfigInvHost {
implements ISegmentedInventory, IConfigurableObject, IConfigInvHost, CellWorkbenchHost {
private final Supplier<PortableCellWorkbenchInventory> cellInv =
new StackDependentSupplier<>(this::getItemStack, PortableCellWorkbenchInventory::new);

public PortableCellWorkbenchMenuHost(PortableCellWorkbenchItem item, Player player, ItemMenuHostLocator locator) {
super(item, player, locator);
}

@Override
public ICellWorkbenchItem getCell() {
return cellInv.get().getCell();
}

ItemStack getContainedStack() {
@Override
public ItemStack mega$getContainedStack() {
return cellInv.get().getStackInSlot(0);
}

Expand All @@ -52,6 +56,11 @@ public IUpgradeInventory getCellUpgrades() {
return cellInv.get().getCellUpgrades();
}

@Override
public void saveChanges() {
cellInv.get().saveChanges();
}

@Override
public InternalInventory getSubInventory(ResourceLocation id) {
return id.equals(ISegmentedInventory.CELLS) ? cellInv.get() : null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package gripe._90.megacells.menu;

public interface CompressionCutoffHost {
String ACTION_SET_COMPRESSION_LIMIT = "openCompressionLimitMenu";

void mega$nextCompressionLimit();
}
Loading

0 comments on commit 3168ea1

Please sign in to comment.