diff --git a/src/generated/resources/.cache/89b86ab0e66f527166d98df92ddbcf5416ed58f6 b/src/generated/resources/.cache/89b86ab0e66f527166d98df92ddbcf5416ed58f6 index daf08e0a..62986d33 100644 --- a/src/generated/resources/.cache/89b86ab0e66f527166d98df92ddbcf5416ed58f6 +++ b/src/generated/resources/.cache/89b86ab0e66f527166d98df92ddbcf5416ed58f6 @@ -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 diff --git a/src/generated/resources/assets/megacells/lang/en_us.json b/src/generated/resources/assets/megacells/lang/en_us.json index c92b2973..b149e7fa 100644 --- a/src/generated/resources/assets/megacells/lang/en_us.json +++ b/src/generated/resources/assets/megacells/lang/en_us.json @@ -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", diff --git a/src/main/java/gripe/_90/megacells/client/screen/CompressionCutoffButton.java b/src/main/java/gripe/_90/megacells/client/screen/CompressionCutoffButton.java new file mode 100644 index 00000000..6bc774de --- /dev/null +++ b/src/main/java/gripe/_90/megacells/client/screen/CompressionCutoffButton.java @@ -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 getTooltipMessage() { + var message = new ArrayList(); + message.add(MEGATranslations.CompressionCutoff.text()); + + if (item != null) { + message.add(item.getDescription()); + } + + return message; + } +} diff --git a/src/main/java/gripe/_90/megacells/client/screen/PortableCellWorkbenchScreen.java b/src/main/java/gripe/_90/megacells/client/screen/PortableCellWorkbenchScreen.java index 35a64aac..f5c0de27 100644 --- a/src/main/java/gripe/_90/megacells/client/screen/PortableCellWorkbenchScreen.java +++ b/src/main/java/gripe/_90/megacells/client/screen/PortableCellWorkbenchScreen.java @@ -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; /** @@ -33,6 +35,7 @@ public class PortableCellWorkbenchScreen extends UpgradeableScreen { private final ToggleButton copyMode; private final SettingToggleButton fuzzyMode; + private final CompressionCutoffButton compressionCutoff; public PortableCellWorkbenchScreen( PortableCellWorkbenchMenu menu, Inventory playerInventory, Component title, ScreenStyle style) { @@ -49,6 +52,7 @@ public PortableCellWorkbenchScreen( GuiText.CopyMode.text(), GuiText.CopyModeDesc.text(), act -> menu.nextWorkBenchCopyMode())); + compressionCutoff = addToLeftToolbar(new CompressionCutoffButton(button -> menu.mega$nextCompressionLimit())); } @Override @@ -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 diff --git a/src/main/java/gripe/_90/megacells/definition/MEGAComponents.java b/src/main/java/gripe/_90/megacells/definition/MEGAComponents.java index 930d1abc..bbb48423 100644 --- a/src/main/java/gripe/_90/megacells/definition/MEGAComponents.java +++ b/src/main/java/gripe/_90/megacells/definition/MEGAComponents.java @@ -25,6 +25,8 @@ public final class MEGAComponents { public static final DataComponentType BULK_CELL_ITEM = register("bulk_item", AEKey.CODEC, AEKey.STREAM_CODEC); + public static final DataComponentType BULK_CELL_COMPRESSION_CUTOFF = + register("compression_cutoff", Codec.INT, ByteBufCodecs.VAR_INT); private static final Codec BIG_INTEGER_CODEC = Codec.STRING.comapFlatMap( str -> { diff --git a/src/main/java/gripe/_90/megacells/definition/MEGATranslations.java b/src/main/java/gripe/_90/megacells/definition/MEGATranslations.java index 53b23f59..70e6fcf6 100644 --- a/src/main/java/gripe/_90/megacells/definition/MEGATranslations.java +++ b/src/main/java/gripe/_90/megacells/definition/MEGATranslations.java @@ -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), diff --git a/src/main/java/gripe/_90/megacells/integration/appmek/RadioactiveCellInventory.java b/src/main/java/gripe/_90/megacells/integration/appmek/RadioactiveCellInventory.java index 5b554b26..69a57553 100644 --- a/src/main/java/gripe/_90/megacells/integration/appmek/RadioactiveCellInventory.java +++ b/src/main/java/gripe/_90/megacells/integration/appmek/RadioactiveCellInventory.java @@ -161,7 +161,6 @@ protected void saveChanges() { if (container != null) { container.saveChanges(); } else { - // if there is no ISaveProvider, store to NBT immediately persist(); } } diff --git a/src/main/java/gripe/_90/megacells/item/cell/BulkCellInventory.java b/src/main/java/gripe/_90/megacells/item/cell/BulkCellInventory.java index 1a689f72..1f073dd1 100644 --- a/src/main/java/gripe/_90/megacells/item/cell/BulkCellInventory.java +++ b/src/main/java/gripe/_90/megacells/item/cell/BulkCellInventory.java @@ -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; @@ -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; @@ -37,9 +36,10 @@ public class BulkCellInventory implements StorageCell { private final boolean compressionEnabled; private final CompressionChain compressionChain; - private final Set decompressionPatterns; private BigInteger unitCount; private final BigInteger unitFactor; + private final int compressionCutoff; + private Set decompressionPatterns; private boolean isPersisted = true; @@ -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 @@ -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) { @@ -102,31 +110,43 @@ public boolean isCompressionEnabled() { return compressionEnabled; } - public Set getDecompressionPatterns() { - return decompressionPatterns; + public CompressionChain getCompressionChain() { + return compressionChain; } - private Set generateDecompressionPatterns() { + public Set 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(); + 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 @@ -213,7 +233,6 @@ private void saveChanges() { if (container != null) { container.saveChanges(); } else { - // if there is no ISaveProvider, store to NBT immediately persist(); } } @@ -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()); diff --git a/src/main/java/gripe/_90/megacells/item/cell/BulkCellItem.java b/src/main/java/gripe/_90/megacells/item/cell/BulkCellItem.java index b014aa03..0ba60a5b 100644 --- a/src/main/java/gripe/_90/megacells/item/cell/BulkCellItem.java +++ b/src/main/java/gripe/_90/megacells/item/cell/BulkCellItem.java @@ -87,6 +87,14 @@ public void appendHoverText(ItemStack is, TooltipContext context, List getTooltipImage(@NotNull ItemStack stack) { } } - return Optional.of(new PortableCellWorkbenchTooltipComponent(shownConfig, host.getContainedStack(), hasMore)); + return Optional.of( + new PortableCellWorkbenchTooltipComponent(shownConfig, host.mega$getContainedStack(), hasMore)); } } diff --git a/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchMenuHost.java b/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchMenuHost.java index e96357fd..eada9789 100644 --- a/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchMenuHost.java +++ b/src/main/java/gripe/_90/megacells/item/cell/PortableCellWorkbenchMenuHost.java @@ -18,11 +18,13 @@ 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 - implements ISegmentedInventory, IConfigurableObject, IConfigInvHost { + implements ISegmentedInventory, IConfigurableObject, IConfigInvHost, CellWorkbenchHost { private final Supplier cellInv = new StackDependentSupplier<>(this::getItemStack, PortableCellWorkbenchInventory::new); @@ -30,11 +32,13 @@ public PortableCellWorkbenchMenuHost(PortableCellWorkbenchItem item, Player play super(item, player, locator); } + @Override public ICellWorkbenchItem getCell() { return cellInv.get().getCell(); } - ItemStack getContainedStack() { + @Override + public ItemStack mega$getContainedStack() { return cellInv.get().getStackInSlot(0); } @@ -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; diff --git a/src/main/java/gripe/_90/megacells/menu/CompressionCutoffHost.java b/src/main/java/gripe/_90/megacells/menu/CompressionCutoffHost.java new file mode 100644 index 00000000..8a01b6d4 --- /dev/null +++ b/src/main/java/gripe/_90/megacells/menu/CompressionCutoffHost.java @@ -0,0 +1,7 @@ +package gripe._90.megacells.menu; + +public interface CompressionCutoffHost { + String ACTION_SET_COMPRESSION_LIMIT = "openCompressionLimitMenu"; + + void mega$nextCompressionLimit(); +} diff --git a/src/main/java/gripe/_90/megacells/menu/PortableCellWorkbenchMenu.java b/src/main/java/gripe/_90/megacells/menu/PortableCellWorkbenchMenu.java index 087ded63..545fbd4f 100644 --- a/src/main/java/gripe/_90/megacells/menu/PortableCellWorkbenchMenu.java +++ b/src/main/java/gripe/_90/megacells/menu/PortableCellWorkbenchMenu.java @@ -30,13 +30,15 @@ import appeng.util.inv.SupplierInternalInventory; import gripe._90.megacells.definition.MEGAMenus; +import gripe._90.megacells.item.cell.BulkCellInventory; +import gripe._90.megacells.item.cell.BulkCellItem; import gripe._90.megacells.item.cell.PortableCellWorkbenchMenuHost; /** * See {@link appeng.menu.implementations.CellWorkbenchMenu} */ public class PortableCellWorkbenchMenu extends UpgradeableMenu - implements IPartitionSlotHost { + implements IPartitionSlotHost, CompressionCutoffHost { @GuiSync(2) public CopyMode copyMode = CopyMode.CLEAR_ON_REMOVE; @@ -46,18 +48,18 @@ public PortableCellWorkbenchMenu(int id, Inventory ip, PortableCellWorkbenchMenu registerClientAction(CellWorkbenchMenu.ACTION_PARTITION, this::partition); registerClientAction(CellWorkbenchMenu.ACTION_CLEAR, this::clear); registerClientAction(CellWorkbenchMenu.ACTION_SET_FUZZY_MODE, FuzzyMode.class, this::setCellFuzzyMode); + registerClientAction(CompressionCutoffHost.ACTION_SET_COMPRESSION_LIMIT, this::mega$nextCompressionLimit); } public void setCellFuzzyMode(FuzzyMode fuzzyMode) { if (isClientSide()) { sendClientAction(CellWorkbenchMenu.ACTION_SET_FUZZY_MODE, fuzzyMode); - return; - } - - var cell = getHost().getCell(); + } else { + var cell = getHost().getCell(); - if (cell != null) { - cell.setFuzzyMode(getWorkbenchItem(), fuzzyMode); + if (cell != null) { + cell.setFuzzyMode(getWorkbenchItem(), fuzzyMode); + } } } @@ -69,6 +71,20 @@ public void nextWorkBenchCopyMode() { } } + public void mega$nextCompressionLimit() { + if (isClientSide()) { + sendClientAction(CompressionCutoffHost.ACTION_SET_COMPRESSION_LIMIT); + } else { + if (BulkCellItem.HANDLER.getCellInventory(getHost().mega$getContainedStack(), null) + instanceof BulkCellInventory bulkCell) { + var currentLimit = bulkCell.getCompressionCutoff(); + bulkCell.setCompressionCutoff( + currentLimit == 1 ? bulkCell.getCompressionChain().size() : currentLimit - 1); + getHost().saveChanges(); + } + } + } + private CopyMode getWorkBenchCopyMode() { return getHost().getConfigManager().getSetting(Settings.COPY_MODE); } @@ -148,27 +164,26 @@ public void clear() { public void partition() { if (isClientSide()) { sendClientAction(CellWorkbenchMenu.ACTION_PARTITION); - return; - } - - var inv = getConfigInventory(); - var is = getWorkbenchItem(); + } else { + var inv = getConfigInventory(); + var is = getWorkbenchItem(); - var cellInv = StorageCells.getCellInventory(is, null); + var cellInv = StorageCells.getCellInventory(is, null); - if (cellInv != null) { - var it = Iterators.transform(cellInv.getAvailableStacks().iterator(), Map.Entry::getKey); + if (cellInv != null) { + var it = Iterators.transform(cellInv.getAvailableStacks().iterator(), Map.Entry::getKey); - for (var x = 0; x < inv.size(); x++) { - if (it.hasNext()) { - inv.setStack(x, new GenericStack(it.next(), 0)); - } else { - inv.setStack(x, null); + for (var x = 0; x < inv.size(); x++) { + if (it.hasNext()) { + inv.setStack(x, new GenericStack(it.next(), 0)); + } else { + inv.setStack(x, null); + } } } - } - broadcastChanges(); + broadcastChanges(); + } } private GenericStackInv getConfigInventory() { diff --git a/src/main/java/gripe/_90/megacells/misc/CellWorkbenchHost.java b/src/main/java/gripe/_90/megacells/misc/CellWorkbenchHost.java new file mode 100644 index 00000000..b4a57c57 --- /dev/null +++ b/src/main/java/gripe/_90/megacells/misc/CellWorkbenchHost.java @@ -0,0 +1,13 @@ +package gripe._90.megacells.misc; + +import net.minecraft.world.item.ItemStack; + +import appeng.api.storage.cells.ICellWorkbenchItem; + +public interface CellWorkbenchHost { + ICellWorkbenchItem getCell(); + + ItemStack mega$getContainedStack(); + + void saveChanges(); +} diff --git a/src/main/java/gripe/_90/megacells/misc/CompressionChain.java b/src/main/java/gripe/_90/megacells/misc/CompressionChain.java index a2e7a1f6..ed57bb7d 100644 --- a/src/main/java/gripe/_90/megacells/misc/CompressionChain.java +++ b/src/main/java/gripe/_90/megacells/misc/CompressionChain.java @@ -45,6 +45,12 @@ public CompressionChain lastMultiplierSwapped() { return chain; } + public CompressionChain limited(int limit) { + var chain = new CompressionChain(); + chain.addAll(subList(0, limit)); + return chain; + } + public record Variant(AEItemKey item, int factor) { Variant(Item item, int factor) { this(AEItemKey.of(item), factor); diff --git a/src/main/java/gripe/_90/megacells/misc/DecompressionPattern.java b/src/main/java/gripe/_90/megacells/misc/DecompressionPattern.java index 7ec6c1de..c67bc178 100644 --- a/src/main/java/gripe/_90/megacells/misc/DecompressionPattern.java +++ b/src/main/java/gripe/_90/megacells/misc/DecompressionPattern.java @@ -24,16 +24,18 @@ public class DecompressionPattern implements IPatternDetails { private final AEItemKey base; private final AEItemKey variant; private final int factor; + private final boolean toCompress; - public DecompressionPattern(AEItemKey base, CompressionChain.Variant variant) { + public DecompressionPattern(AEItemKey base, CompressionChain.Variant variant, boolean toCompress) { this.base = base; this.variant = variant.item(); this.factor = variant.factor(); + this.toCompress = toCompress; var definition = new ItemStack(MEGAItems.SKY_STEEL_INGOT); definition.set( MEGAComponents.ENCODED_DECOMPRESSION_PATTERN, - new Encoded(base.toStack(), variant.item().toStack(), variant.factor())); + new Encoded(base.toStack(), variant.item().toStack(), variant.factor(), toCompress)); this.definition = AEItemKey.of(definition); } @@ -44,12 +46,12 @@ public AEItemKey getDefinition() { @Override public IInput[] getInputs() { - return new IInput[] {new Input(variant)}; + return new IInput[] {toCompress ? new Input(base, factor) : new Input(variant, 1)}; } @Override public List getOutputs() { - return List.of(new GenericStack(base, factor)); + return List.of(toCompress ? new GenericStack(variant, 1) : new GenericStack(base, factor)); } @Override @@ -64,7 +66,7 @@ public int hashCode() { return definition.hashCode(); } - private record Input(AEItemKey input) implements IInput { + private record Input(AEItemKey input, int factor) implements IInput { @Override public GenericStack[] getPossibleInputs() { return new GenericStack[] {new GenericStack(input, 1)}; @@ -72,7 +74,7 @@ public GenericStack[] getPossibleInputs() { @Override public long getMultiplier() { - return 1; + return factor; } @Override @@ -86,11 +88,12 @@ public AEKey getRemainingKey(AEKey template) { } } - public record Encoded(ItemStack base, ItemStack variant, int factor) { + public record Encoded(ItemStack base, ItemStack variant, int factor, boolean toCompress) { public static final Codec CODEC = RecordCodecBuilder.create(builder -> builder.group( ItemStack.CODEC.fieldOf("base").forGetter(Encoded::base), ItemStack.CODEC.fieldOf("variant").forGetter(Encoded::variant), - Codec.INT.fieldOf("factor").forGetter(Encoded::factor)) + Codec.INT.fieldOf("factor").forGetter(Encoded::factor), + Codec.BOOL.fieldOf("toCompress").forGetter(Encoded::toCompress)) .apply(builder, Encoded::new)); public static final StreamCodec STREAM_CODEC = StreamCodec.composite( @@ -100,6 +103,8 @@ public record Encoded(ItemStack base, ItemStack variant, int factor) { Encoded::variant, ByteBufCodecs.VAR_INT, Encoded::factor, + ByteBufCodecs.BOOL, + Encoded::toCompress, Encoded::new); } } diff --git a/src/main/java/gripe/_90/megacells/mixin/CellWorkbenchBlockEntityMixin.java b/src/main/java/gripe/_90/megacells/mixin/CellWorkbenchBlockEntityMixin.java new file mode 100644 index 00000000..876c5abc --- /dev/null +++ b/src/main/java/gripe/_90/megacells/mixin/CellWorkbenchBlockEntityMixin.java @@ -0,0 +1,24 @@ +package gripe._90.megacells.mixin; + +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +import net.minecraft.world.item.ItemStack; + +import appeng.blockentity.misc.CellWorkbenchBlockEntity; +import appeng.util.inv.AppEngInternalInventory; + +import gripe._90.megacells.misc.CellWorkbenchHost; + +@Mixin(CellWorkbenchBlockEntity.class) +public abstract class CellWorkbenchBlockEntityMixin implements CellWorkbenchHost { + @Shadow + @Final + private AppEngInternalInventory cell; + + @Override + public ItemStack mega$getContainedStack() { + return cell.getStackInSlot(0); + } +} diff --git a/src/main/java/gripe/_90/megacells/mixin/CellWorkbenchMenuMixin.java b/src/main/java/gripe/_90/megacells/mixin/CellWorkbenchMenuMixin.java new file mode 100644 index 00000000..b5d80582 --- /dev/null +++ b/src/main/java/gripe/_90/megacells/mixin/CellWorkbenchMenuMixin.java @@ -0,0 +1,46 @@ +package gripe._90.megacells.mixin; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.inventory.MenuType; + +import appeng.blockentity.misc.CellWorkbenchBlockEntity; +import appeng.menu.implementations.CellWorkbenchMenu; +import appeng.menu.implementations.UpgradeableMenu; + +import gripe._90.megacells.item.cell.BulkCellInventory; +import gripe._90.megacells.item.cell.BulkCellItem; +import gripe._90.megacells.menu.CompressionCutoffHost; +import gripe._90.megacells.misc.CellWorkbenchHost; + +@Mixin(CellWorkbenchMenu.class) +public abstract class CellWorkbenchMenuMixin extends UpgradeableMenu + implements CompressionCutoffHost { + public CellWorkbenchMenuMixin(MenuType menuType, int id, Inventory ip, CellWorkbenchBlockEntity host) { + super(menuType, id, ip, host); + } + + @Inject(method = "", at = @At("RETURN")) + private void registerAction(int id, Inventory ip, CellWorkbenchBlockEntity te, CallbackInfo ci) { + registerClientAction(CompressionCutoffHost.ACTION_SET_COMPRESSION_LIMIT, this::mega$nextCompressionLimit); + } + + @Override + public void mega$nextCompressionLimit() { + if (isClientSide()) { + sendClientAction(CompressionCutoffHost.ACTION_SET_COMPRESSION_LIMIT); + } else { + if (BulkCellItem.HANDLER.getCellInventory(((CellWorkbenchHost) getHost()).mega$getContainedStack(), null) + instanceof BulkCellInventory bulkCell) { + var currentLimit = bulkCell.getCompressionCutoff(); + bulkCell.setCompressionCutoff( + currentLimit == 1 ? bulkCell.getCompressionChain().size() : currentLimit - 1); + getHost().saveChanges(); + } + } + } +} diff --git a/src/main/java/gripe/_90/megacells/mixin/client/CellWorkbenchScreenMixin.java b/src/main/java/gripe/_90/megacells/mixin/client/CellWorkbenchScreenMixin.java new file mode 100644 index 00000000..dee8e3b5 --- /dev/null +++ b/src/main/java/gripe/_90/megacells/mixin/client/CellWorkbenchScreenMixin.java @@ -0,0 +1,51 @@ +package gripe._90.megacells.mixin.client; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import net.minecraft.network.chat.Component; +import net.minecraft.world.entity.player.Inventory; + +import appeng.client.gui.AEBaseScreen; +import appeng.client.gui.implementations.CellWorkbenchScreen; +import appeng.client.gui.style.ScreenStyle; +import appeng.menu.implementations.CellWorkbenchMenu; + +import gripe._90.megacells.client.screen.CompressionCutoffButton; +import gripe._90.megacells.item.cell.BulkCellInventory; +import gripe._90.megacells.item.cell.BulkCellItem; +import gripe._90.megacells.menu.CompressionCutoffHost; +import gripe._90.megacells.misc.CellWorkbenchHost; + +@Mixin(CellWorkbenchScreen.class) +public abstract class CellWorkbenchScreenMixin extends AEBaseScreen { + @Unique + private CompressionCutoffButton mega$compressionCutoff; + + public CellWorkbenchScreenMixin( + CellWorkbenchMenu menu, Inventory playerInventory, Component title, ScreenStyle style) { + super(menu, playerInventory, title, style); + } + + @Inject(method = "", at = @At("RETURN")) + private void initCutoffButton( + CellWorkbenchMenu menu, Inventory playerInventory, Component title, ScreenStyle style, CallbackInfo ci) { + mega$compressionCutoff = addToLeftToolbar( + new CompressionCutoffButton(button -> ((CompressionCutoffHost) menu).mega$nextCompressionLimit())); + } + + @Inject(method = "updateBeforeRender", at = @At("RETURN")) + private void updateCutoffButton(CallbackInfo ci) { + if (BulkCellItem.HANDLER.getCellInventory(((CellWorkbenchHost) menu.getHost()).mega$getContainedStack(), null) + instanceof BulkCellInventory bulkCell) { + mega$compressionCutoff.setVisibility(bulkCell.isCompressionEnabled() + && !bulkCell.getCompressionChain().isEmpty()); + mega$compressionCutoff.setItem(bulkCell.getCompressionChain().get(bulkCell.getCompressionCutoff() - 1)); + } else { + mega$compressionCutoff.setVisibility(false); + } + } +} diff --git a/src/main/resources/assets/ae2/screens/megacells/portable_cell_workbench.json b/src/main/resources/assets/ae2/screens/megacells/portable_cell_workbench.json index f4beaa57..6f36329d 100644 --- a/src/main/resources/assets/ae2/screens/megacells/portable_cell_workbench.json +++ b/src/main/resources/assets/ae2/screens/megacells/portable_cell_workbench.json @@ -1,24 +1,5 @@ { - "includes": [ - "../common/common.json", - "../common/player_inventory.json", - "../common/toolbox.json" - ], - "background": { - "texture": "guis/cellworkbench.png", - "srcRect": [0, 0, 176, 253] - }, - "slots": { - "STORAGE_CELL": { - "left": 152, - "top": 8 - }, - "CONFIG": { - "left": 8, - "top": 29, - "grid": "BREAK_AFTER_9COLS" - } - }, + "includes": ["../cell_workbench.json"], "text": { "dialog_title": { "text": { diff --git a/src/main/resources/megacells.mixins.json b/src/main/resources/megacells.mixins.json index 1d869cee..3ef1dff0 100644 --- a/src/main/resources/megacells.mixins.json +++ b/src/main/resources/megacells.mixins.json @@ -4,10 +4,16 @@ "package": "gripe._90.megacells.mixin", "mixins": [ "ItemEntityMixin", - "ItemMenuHostMixin", - "PatternProviderMenuAccessor" + "PatternProviderMenuAccessor", + "CellWorkbenchBlockEntityMixin", + "CellWorkbenchMenuMixin", + "ItemMenuHostMixin" + ], + "client": [ + "client.BuiltInModelHooksAccessor", + "client.CellWorkbenchScreenMixin", + "client.ModelBakeryMixin" ], - "client": ["client.BuiltInModelHooksAccessor", "client.ModelBakeryMixin"], "injectors": { "defaultRequire": 1 }