Skip to content

Commit

Permalink
Allow compression cutoff button to cycle backwards when right-clicked
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 committed Jan 22, 2025
1 parent fb96610 commit 21b51fe
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,30 @@

import org.jetbrains.annotations.Nullable;

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

import appeng.client.gui.AEBaseScreen;
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 final Handler onPress;
private Item item;

public CompressionCutoffButton(OnPress onPress) {
super(onPress);
public CompressionCutoffButton(Handler onPress) {
super(btn -> {
if (btn instanceof CompressionCutoffButton cutoff) {
if (Minecraft.getInstance().screen instanceof AEBaseScreen<?> screen) {
cutoff.onPress.handle(cutoff, screen.isHandlingRightClick());
}
}
});
this.onPress = onPress;
}

public void setItem(CompressionChain.Variant variant) {
Expand Down Expand Up @@ -47,4 +57,9 @@ public List<Component> getTooltipMessage() {

return message;
}

@FunctionalInterface
public interface Handler {
void handle(CompressionCutoffButton button, boolean backwards);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public PortableCellWorkbenchScreen(
GuiText.CopyMode.text(),
GuiText.CopyModeDesc.text(),
act -> menu.nextWorkBenchCopyMode()));
compressionCutoff = addToLeftToolbar(new CompressionCutoffButton(button -> menu.mega$nextCompressionLimit()));
compressionCutoff = addToLeftToolbar(
new CompressionCutoffButton((button, backwards) -> menu.mega$nextCompressionLimit(backwards)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
public interface CompressionCutoffHost {
String ACTION_SET_COMPRESSION_LIMIT = "openCompressionLimitMenu";

void mega$nextCompressionLimit();
void mega$nextCompressionLimit(boolean backwards);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ 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(ACTION_SET_COMPRESSION_LIMIT, this::mega$nextCompressionLimit);
registerClientAction(ACTION_SET_COMPRESSION_LIMIT, Boolean.class, this::mega$nextCompressionLimit);
}

public void setCellFuzzyMode(FuzzyMode fuzzyMode) {
Expand All @@ -73,15 +73,21 @@ public void nextWorkBenchCopyMode() {
}

@Override
public void mega$nextCompressionLimit() {
public void mega$nextCompressionLimit(boolean backwards) {
if (isClientSide()) {
sendClientAction(ACTION_SET_COMPRESSION_LIMIT);
sendClientAction(ACTION_SET_COMPRESSION_LIMIT, backwards);
} 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);
var limit = bulkCell.getCompressionCutoff();

if (backwards) {
limit = limit == bulkCell.getCompressionChain().size() ? 1 : limit + 1;
} else {
limit = limit == 1 ? bulkCell.getCompressionChain().size() : limit - 1;
}

bulkCell.setCompressionCutoff(limit);
getHost().saveChanges();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,25 @@ public CellWorkbenchMenuMixin(MenuType<?> menuType, int id, Inventory ip, CellWo

@Inject(method = "<init>", at = @At("RETURN"))
private void registerAction(int id, Inventory ip, CellWorkbenchBlockEntity te, CallbackInfo ci) {
registerClientAction(ACTION_SET_COMPRESSION_LIMIT, this::mega$nextCompressionLimit);
registerClientAction(ACTION_SET_COMPRESSION_LIMIT, Boolean.class, this::mega$nextCompressionLimit);
}

@Override
public void mega$nextCompressionLimit() {
public void mega$nextCompressionLimit(boolean backwards) {
if (isClientSide()) {
sendClientAction(ACTION_SET_COMPRESSION_LIMIT);
sendClientAction(ACTION_SET_COMPRESSION_LIMIT, backwards);
} 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);
var limit = bulkCell.getCompressionCutoff();

if (backwards) {
limit = limit == bulkCell.getCompressionChain().size() ? 1 : limit + 1;
} else {
limit = limit == 1 ? bulkCell.getCompressionChain().size() : limit - 1;
}

bulkCell.setCompressionCutoff(limit);
getHost().saveChanges();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public CellWorkbenchScreenMixin(
@Inject(method = "<init>", 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()));
mega$compressionCutoff = addToLeftToolbar(new CompressionCutoffButton(
(button, backwards) -> ((CompressionCutoffHost) menu).mega$nextCompressionLimit(backwards)));
}

@Inject(method = "updateBeforeRender", at = @At("RETURN"))
Expand Down

0 comments on commit 21b51fe

Please sign in to comment.