Skip to content

Commit

Permalink
0.5.1g porting part IV
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Aug 7, 2024
1 parent 3b5f881 commit 7bb541a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package com.simibubi.create.compat.thresholdSwitch;

import com.simibubi.create.compat.Mods;

import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.items.IItemHandler;

public class StorageDrawers implements ThresholdSwitchCompat {

@Override
public boolean isFromThisMod(BlockEntity blockEntity) {
return blockEntity != null && Mods.STORAGEDRAWERS.id()
.equals(blockEntity.getType()
.getRegistryName()
.getNamespace());
}

@Override
public long getSpaceInSlot(IItemHandler inv, int slot) {
if (slot == 0)
return 0;

return inv.getSlotLimit(slot);
}
}
//import com.simibubi.create.compat.Mods;
//
//import net.minecraft.world.level.block.entity.BlockEntity;
//import net.minecraftforge.items.IItemHandler;
//
//public class StorageDrawers implements ThresholdSwitchCompat {
//
// @Override
// public boolean isFromThisMod(BlockEntity blockEntity) {
// return blockEntity != null && Mods.STORAGEDRAWERS.id()
// .equals(blockEntity.getType()
// .getRegistryName()
// .getNamespace());
// }
//
// @Override
// public long getSpaceInSlot(IItemHandler inv, int slot) {
// if (slot == 0)
// return 0;
//
// return inv.getSlotLimit(slot);
// }
//}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.simibubi.create.compat.thresholdSwitch;

import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
import net.fabricmc.fabric.api.transfer.v1.storage.StorageView;
import net.minecraft.world.level.block.entity.BlockEntity;

public interface ThresholdSwitchCompat {

boolean isFromThisMod(BlockEntity blockEntity);

long getSpaceInSlot(Storage<ItemVariant> inv, int slot);
long getSpaceInSlot(StorageView<ItemVariant> inv);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.List;

import com.simibubi.create.compat.thresholdSwitch.StorageDrawers;
import com.simibubi.create.compat.thresholdSwitch.ThresholdSwitchCompat;
import com.simibubi.create.content.redstone.DirectedDirectionalBlock;
import com.simibubi.create.content.redstone.FilteredDetectorFilterSlot;
Expand Down Expand Up @@ -47,10 +46,11 @@ public class ThresholdSwitchBlockEntity extends SmartBlockEntity {
private TankManipulationBehaviour observedTank;
private VersionedInventoryTrackerBehaviour invVersionTracker;

// fabric: not needed, view.getCapacity is pretty much correct for the most part
private static final List<ThresholdSwitchCompat> COMPAT = List.of(
//new FunctionalStorage(),
//new SophisticatedStorage(),
new StorageDrawers()
//new StorageDrawers()
);

public ThresholdSwitchBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
Expand Down Expand Up @@ -128,24 +128,23 @@ public void updateCurrentLevel() {

} else {
invVersionTracker.awaitNewVersion(inv);
for (int slot = 0; slot < inv.getSlots(); slot++) {
ItemStack stackInSlot = inv.getStackInSlot(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)));

int count = stackInSlot.getCount();
if (space == 0)
continue;

totalSpace += 1;
if (filtering.test(stackInSlot))
occupied += count * (1f / space);
try (Transaction t = TransferUtil.getTransaction()) {
for (StorageView<ItemVariant> view : inv.iterable(t)) {
long space = COMPAT
.stream()
.filter(compat -> compat.isFromThisMod(targetBlockEntity))
.map(compat -> compat.getSpaceInSlot(view))
.findFirst()
.orElseGet(() -> Math.min(view.getResource().toStack().getMaxStackSize(), view.getCapacity()));

long count = view.getCapacity();
if (space == 0)
continue;

totalSpace += 1;
if (filtering.test(view.getResource().toStack()))
occupied += count * (1f / space);
}
}
}
}
Expand Down

0 comments on commit 7bb541a

Please sign in to comment.