Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public ConfigureThresholdSwitchPacket(BlockPos pos, int offBelow, int onAbove, b
this.invert = invert;
this.inStacks = inStacks;
}

public ConfigureThresholdSwitchPacket(FriendlyByteBuf buffer) {
super(buffer);
}

@Override
protected void readSettings(FriendlyByteBuf buffer) {
offBelow = buffer.readInt();
Expand All @@ -45,7 +45,7 @@ protected void applySettings(ThresholdSwitchBlockEntity be) {
be.offWhenBelow = offBelow;
be.onWhenAbove = onAbove;
be.setInverted(invert);
be.inStacks = inStacks;
be.setInStacks(inStacks);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.simibubi.create.content.redstone.thresholdSwitch;

import java.util.List;
import java.util.Optional;

import com.simibubi.create.compat.thresholdSwitch.FunctionalStorage;
import com.simibubi.create.compat.thresholdSwitch.SophisticatedStorage;
Expand Down Expand Up @@ -123,7 +124,6 @@ public int getMaxLevel() {
}

public void updateCurrentLevel() {
boolean changed = false;
int prevLevel = currentLevel;
int prevMaxLevel = currentMaxLevel;

Expand All @@ -137,66 +137,72 @@ public void updateCurrentLevel() {
currentMinLevel = observable.getMinValue();
currentLevel = observable.getCurrentValue();
currentMaxLevel = observable.getMaxValue();

/*} else if (StorageDrawers.isDrawer(targetBlockEntity) && observedInventory.hasInventory()) {
currentMinLevel = 0;
currentLevel = StorageDrawers.getItemCount(observedInventory.getInventory(), filtering);
currentMaxLevel = StorageDrawers.getTotalStorageSpace(observedInventory.getInventory());
*/

} else if (observedInventory.hasInventory() || observedTank.hasInventory()) {
} else if (observedInventory.hasInventory()) {
currentMinLevel = 0;
currentLevel = 0;
currentMaxLevel = 0;

if (observedInventory.hasInventory()) {

// Item inventory
IItemHandler inv = observedInventory.getInventory();
if (invVersionTracker.stillWaiting(inv)) {
currentLevel = prevLevel;
currentMaxLevel = prevMaxLevel;

} else {
invVersionTracker.awaitNewVersion(inv);
for (int slot = 0; slot < inv.getSlots(); slot++) {
ItemStack stackInSlot = inv.getStackInSlot(slot);
IItemHandler inv = observedInventory.getInventory();
if (invVersionTracker.stillWaiting(inv)) {
currentLevel = prevLevel;
currentMaxLevel = prevMaxLevel;
} else {
invVersionTracker.awaitNewVersion(inv);
Optional<ThresholdSwitchCompat> compatibilityHandler = COMPAT.stream()
.filter(compat -> compat.isFromThisMod(targetBlockEntity))
.findFirst();

for (int slot = 0; slot < inv.getSlots(); slot++) {
ItemStack stackInSlot = inv.getStackInSlot(slot);

long space;
if (compatibilityHandler.isPresent()){
space = compatibilityHandler.get().getSpaceInSlot(inv, slot);
} else {
space = Math.min(stackInSlot.getMaxStackSize(), inv.getSlotLimit(slot));
}

int finalSlot = slot;
long space = COMPAT
.stream()
.filter(compat -> compat.isFromThisMod(targetBlockEntity))
.map(compat -> compat.getSpaceInSlot(inv, finalSlot))
.findFirst()
.orElseGet(() -> (long) Math.min(stackInSlot.getMaxStackSize(), inv.getSlotLimit(finalSlot)));
if (space == 0) continue;

int count = stackInSlot.getCount();
if (space == 0)
continue;
if (inStacks) {
// When counting in stacks, each slot has 1 unit of capacity
currentMaxLevel += 1;

if (filtering.test(stackInSlot)) {
// only count full stacks as 1, partial stacks as 0
if (stackInSlot.getCount() == space) {
currentLevel += 1;
}
}
} else {
// When counting individual items, use actual item capacity
currentMaxLevel += space;
if (filtering.test(stackInSlot))
currentLevel += count;

// Count items that pass the filter
if (filtering.test(stackInSlot)) {
currentLevel += stackInSlot.getCount();
}
}
}
}
} else if (observedTank.hasInventory()) {
currentMinLevel = 0;
currentLevel = 0;
currentMaxLevel = 0;

if (observedTank.hasInventory()) {
// Fluid inventory
IFluidHandler tank = observedTank.getInventory();
for (int slot = 0; slot < tank.getTanks(); slot++) {
FluidStack stackInSlot = tank.getFluidInTank(slot);
int space = tank.getTankCapacity(slot);
int count = stackInSlot.getAmount();
if (space == 0)
continue;

currentMaxLevel += space;
if (filtering.test(stackInSlot))
currentLevel += count;
// Fluid inventory
IFluidHandler tank = observedTank.getInventory();
for (int slot = 0; slot < tank.getTanks(); slot++) {
FluidStack stackInSlot = tank.getFluidInTank(slot);
int space = tank.getTankCapacity(slot);
int count = stackInSlot.getAmount();
if (space == 0) continue;

currentMaxLevel += space;
if (filtering.test(stackInSlot)) {
currentLevel += count;
}
}

} else {
// No compatible inventories found
currentMinLevel = -1;
Expand All @@ -213,7 +219,8 @@ public void updateCurrentLevel() {
}

currentLevel = Mth.clamp(currentLevel, currentMinLevel, currentMaxLevel);
changed = currentLevel != prevLevel;
boolean levelChanged = currentLevel != prevLevel;
boolean anyChanged = levelChanged || currentMaxLevel != prevMaxLevel || currentMinLevel != prevLevel;

boolean previouslyPowered = redstoneState;
if (redstoneState && currentLevel <= offWhenBelow)
Expand All @@ -232,10 +239,11 @@ else if (!redstoneState && currentLevel >= onWhenAbove)
if (update)
scheduleBlockTick();

if (changed || update) {
if (levelChanged || update)
DisplayLinkBlock.notifyGatherers(level, worldPosition);

if (anyChanged)
notifyUpdate();
}
}

private boolean isSuitableInventory(BlockEntity be) {
Expand Down Expand Up @@ -347,4 +355,14 @@ public void setInverted(boolean inverted) {
this.inverted = inverted;
updatePowerAfterDelay();
}

public void setInStacks(Boolean value) {
// When configuration values such as inStacks are changed from the screen,
// ensure that the next tick recalculates the inventory even if its version
// has not changed yet.
inStacks = value;
if (invVersionTracker != null)
invVersionTracker.reset();
updateCurrentLevel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ protected void init() {
.forOptions(List.of(CreateLang.translateDirect("schedule.condition.threshold.items"),
CreateLang.translateDirect("schedule.condition.threshold.stacks")))
.titled(CreateLang.translateDirect("schedule.condition.threshold.item_measure"))
.calling((state) -> send(blockEntity.isInverted()))
.setState(blockEntity.inStacks ? 1 : 0);

offBelow = new ScrollInput(x + 48, y + 47, 1, 18)
Expand All @@ -78,7 +79,6 @@ protected void init() {

if (onAbove.getState() / valueStep == 0 && state / valueStep == 0)
return;

if (onAbove.getState() / valueStep <= state / valueStep) {
onAbove.setState((state + valueStep) / valueStep * valueStep);
onAbove.onChanged();
Expand Down Expand Up @@ -152,19 +152,16 @@ protected void renderWindow(GuiGraphics graphics, int mouseX, int mouseY, float
inputBg.render(graphics, x + 44, y + 21);
inputBg.render(graphics, x + 44, y + 21 + 24);

int valueStep = 1;
int valueStep = typeOfCurrentTarget == ThresholdType.FLUID ? 1000 : 1;
boolean stacks = inStacks.getState() == 1;
if (typeOfCurrentTarget == ThresholdType.FLUID)
valueStep = 1000;

if (forItems) {
Component suffix =
inStacks.getState() == 0 ? CreateLang.translateDirect("schedule.condition.threshold.items")
: CreateLang.translateDirect("schedule.condition.threshold.stacks");
valueStep = inStacks.getState() == 0 ? 1 : 64;
stacks
? CreateLang.translateDirect("schedule.condition.threshold.stacks")
: CreateLang.translateDirect("schedule.condition.threshold.items");
graphics.drawString(font, suffix, x + 105, y + 28, 0xFFFFFFFF, true);
graphics.drawString(font, suffix, x + 105, y + 28 + 24, 0xFFFFFFFF, true);

}

graphics.drawString(font,
Expand Down Expand Up @@ -327,13 +324,9 @@ private void updateInputBoxes() {
}

private int getValueStep() {
boolean stacks = inStacks.getState() == 1;
int valueStep = 1;
if (blockEntity.getTypeOfCurrentTarget() == ThresholdType.FLUID)
valueStep = 1000;
else if (stacks)
valueStep = 64;
return valueStep;
return 1000;
return 1;
}

@Override
Expand Down