Skip to content

Commit

Permalink
Add Slot locking and unlocking for better automation closes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
ProjectET committed Jul 30, 2024
1 parent 69e5de5 commit f4a56bb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public class BEAdvancedInscriber extends AENetworkPowerBlockEntity implements IG
private final int maxProcessingTime = 100;
private boolean working;

public boolean topLock;

public boolean botLock;

private final Map<InternalInventory, ItemStack> lastStacks = new IdentityHashMap<>(Map.of(
topItemHandler, ItemStack.EMPTY,
botItemHandler, ItemStack.EMPTY,
Expand All @@ -93,6 +97,9 @@ public BEAdvancedInscriber(BlockPos pos, BlockState state) {
this.sideItemHandlerExtern = new FilteredInternalInventory(this.sideItemHandler, filter);

this.combinedExtInventory = new CombinedInternalInventory(topItemHandlerExtern, botItemHandlerExtern, sideItemHandlerExtern);

topLock = true;
botLock = true;
}

@Override
Expand Down Expand Up @@ -148,6 +155,14 @@ protected boolean readFromStream(FriendlyByteBuf data) {
return c;
}

public void toggleTopLock() {
topLock = !topLock;
}

public void toggleBotLock() {
botLock = !botLock;
}

@Override
protected void writeToStream(FriendlyByteBuf data) {
super.writeToStream(data);
Expand Down Expand Up @@ -189,12 +204,16 @@ public InscriberRecipe getTask() {
public void saveAdditional(CompoundTag data) {
super.saveAdditional(data);
this.upgrades.writeToNBT(data, "upgrades");
data.putBoolean("topLock", topLock);
data.putBoolean("botLock", botLock);
}

@Override
public void loadTag(CompoundTag data) {
super.loadTag(data);
this.upgrades.readFromNBT(data, "upgrades");
topLock = data.getBoolean("topLock");
botLock = data.getBoolean("botLock");
}

@Override
Expand Down Expand Up @@ -299,6 +318,12 @@ public int getProcessingTime() {
public class FilteredInventory implements IAEItemFilter {
@Override
public boolean allowExtract(InternalInventory inv, int slot, int amount) {
if(inv == topItemHandler) {
return !topLock && !sideItemHandler.getStackInSlot(1).isEmpty() && sideItemHandler.getStackInSlot(0).isEmpty();
}
else if(inv == botItemHandler) {
return !botLock && !sideItemHandler.getStackInSlot(1).isEmpty() && sideItemHandler.getStackInSlot(0).isEmpty();
}
return slot == 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public class AdvancedInscriberMenu extends UpgradeableMenu<BEAdvancedInscriber>
@GuiSync(3)
public int processingTime = -1;

@GuiSync(7)
public boolean topLock;
@GuiSync(8)
public boolean botLock;

public AdvancedInscriberMenu(int syncId, Inventory playerInventory, BEAdvancedInscriber advancedInscriber) {
super(ADVANCED_INSCRIBER_SHT, syncId, playerInventory, advancedInscriber);
world = playerInventory.player.level;
Expand All @@ -55,13 +60,18 @@ public AdvancedInscriberMenu(int syncId, Inventory playerInventory, BEAdvancedIn

var output = new OutputSlot(inventory, 3, null);
this.addSlot(output, SlotSemantics.MACHINE_OUTPUT);

registerClientAction("toggleTopLock", this::toggleTopLock);
registerClientAction("toggleBotLock", this::toggleBotLock);
}

@Override
protected void standardDetectAndSendChanges() {
if (isServerSide()) {
this.maxProcessingTime = getHost().getMaxProcessingTime();
this.processingTime = getHost().getProcessingTime();
this.botLock = getHost().botLock;
this.topLock = getHost().topLock;
}
super.standardDetectAndSendChanges();
}
Expand Down Expand Up @@ -113,4 +123,21 @@ public int getMaxProgress() {
public boolean stillValid(Player player) {
return true;
}

public void toggleBotLock() {
if (isClientSide()) {
sendClientAction("toggleBotLock");
} else {
getHost().toggleBotLock();
}
}

public void toggleTopLock() {
if (isClientSide()) {
sendClientAction("toggleTopLock");
} else {
getHost().toggleTopLock();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
package io.github.projectet.ae2things.gui.advancedInscriber;

import appeng.client.gui.Icon;
import appeng.client.gui.implementations.UpgradeableScreen;
import appeng.client.gui.style.ScreenStyle;
import appeng.client.gui.widgets.ProgressBar;
import appeng.client.gui.widgets.ToggleButton;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Inventory;

public class AdvancedInscriberRootPanel extends UpgradeableScreen<AdvancedInscriberMenu> {

private final ProgressBar pb;

private final ToggleButton topLock;

private final ToggleButton botLock;


public AdvancedInscriberRootPanel(AdvancedInscriberMenu menu, Inventory playerInventory, Component title, ScreenStyle style) {
super(menu, playerInventory, title, style);
this.pb = new ProgressBar(this.menu, style.getImage("progressBar"), ProgressBar.Direction.VERTICAL);
widgets.add("progressBar", this.pb);
this.topLock = new ToggleButton(Icon.LOCKED, Icon.UNLOCKED, a -> menu.toggleTopLock());
this.botLock = new ToggleButton(Icon.LOCKED, Icon.UNLOCKED, a -> menu.toggleBotLock());
widgets.add("topLock", topLock);
widgets.add("botLock", botLock);
}

@Override
protected void updateBeforeRender() {
super.updateBeforeRender();

topLock.setState(menu.topLock);
botLock.setState(menu.botLock);

int progress = this.menu.getCurrentProgress() * 100 / this.menu.getMaxProgress();
this.pb.setFullMsg(Component.literal(progress + "%"));
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/assets/ae2/screens/advanced_inscriber.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
"progressBar": {
"left": 135,
"top": 39
},
"topLock": {
"left": 22,
"top": 16
},
"botLock": {
"left": 22,
"top": 62
}
}
}

0 comments on commit f4a56bb

Please sign in to comment.