Skip to content

Commit

Permalink
Make drawers slotted storages
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiDragon committed Jan 29, 2024
1 parent 7edb95c commit 4984df2
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 30 deletions.
2 changes: 2 additions & 0 deletions changelog/2.1.2+1.20.4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Make drawers slotted storages
* This improves mod compatibility
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ minecraft_version=1.20.4
yarn_mappings=1.20.4+build.1
loader_version=0.15.1

mod_version=2.1.1
mod_version=2.1.2
maven_group=io.github.mattidragon
archives_base_name=ExtendedDrawers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void render(CompactingDrawerBlockEntity drawer, float tickDelta, MatrixSt

renderIcons(drawer, matrices, vertexConsumers, light, overlay);

var slots = drawer.storage.getActiveSlots();
var slots = drawer.storage.getActiveSlotArray();

if (slots.length >= 1) { // Top slot
matrices.translate(0, 0.25, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void appendTooltip(ItemStack stack, @Nullable BlockView world, List<Text>
drawer.readNbt(nbt);
var storage = drawer.storage;

var list = Arrays.stream(storage.getActiveSlots())
var list = Arrays.stream(storage.getActiveSlotArray())
.map(slot -> new ResourceAmount<>(slot.getResource(), slot.getTrueAmount()))
.filter(resource -> !resource.resource().isBlank())
.toList();
Expand All @@ -86,7 +86,7 @@ public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockSt
if (!state.isOf(newState.getBlock())) {
var drawer = getBlockEntity(world, pos);
if (drawer != null && ExtendedDrawers.CONFIG.get().misc().drawersDropContentsOnBreak()) {
var slots = drawer.storage.getSlots();
var slots = drawer.storage.getSlotArray();
var amount = drawer.storage.getTrueAmount();
// Iterate slots in reverse order
for (int i = slots.length - 1; i >= 0; i--) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import com.google.common.collect.Iterators;
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.SlottedStorage;
import net.fabricmc.fabric.api.transfer.v1.storage.StoragePreconditions;
import net.fabricmc.fabric.api.transfer.v1.storage.StorageView;
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage;
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
import org.jetbrains.annotations.NotNull;

Expand All @@ -16,22 +17,24 @@
* Stores slots in a fixed size array. The storage array is copied on iteration to avoid issues with mods that iterate storages outside transactions, because we need to sort slots.
* This class also don't need to handle multi-view storages since each slot is a single view, so it's simpler.
*/
public class CombinedDrawerStorage implements Storage<ItemVariant> {
private DrawerSlot[] slots;
public class CombinedDrawerStorage implements SlottedStorage<ItemVariant> {
private DrawerSlot[] sortedSlots;
private final DrawerSlot[] unsortedSlots;

public CombinedDrawerStorage(DrawerSlot[] slots) {
this.slots = Arrays.copyOf(slots, slots.length);
this.unsortedSlots = slots;
this.sortedSlots = Arrays.copyOf(slots, slots.length);
}

public void sort() {
// We copy the slot array so that currently active iterators don't break
slots = Arrays.copyOf(slots, slots.length);
Arrays.sort(slots);
sortedSlots = Arrays.copyOf(sortedSlots, sortedSlots.length);
Arrays.sort(sortedSlots);
}

@Override
public boolean supportsInsertion() {
for (DrawerSlot part : slots) {
for (DrawerSlot part : sortedSlots) {
if (part.supportsInsertion()) {
return true;
}
Expand All @@ -45,7 +48,7 @@ public long insert(ItemVariant resource, long maxAmount, TransactionContext tran
StoragePreconditions.notNegative(maxAmount);
long amount = 0;

for (DrawerSlot part : slots) {
for (DrawerSlot part : sortedSlots) {
amount += part.insert(resource, maxAmount - amount, transaction);
if (amount == maxAmount) break;
}
Expand All @@ -55,7 +58,7 @@ public long insert(ItemVariant resource, long maxAmount, TransactionContext tran

@Override
public boolean supportsExtraction() {
for (DrawerSlot part : slots) {
for (DrawerSlot part : sortedSlots) {
if (part.supportsExtraction()) {
return true;
}
Expand All @@ -69,7 +72,7 @@ public long extract(ItemVariant resource, long maxAmount, TransactionContext tra
StoragePreconditions.notNegative(maxAmount);
long amount = 0;

for (DrawerSlot part : slots) {
for (DrawerSlot part : sortedSlots) {
amount += part.extract(resource, maxAmount - amount, transaction);
if (amount == maxAmount) break;
}
Expand All @@ -79,8 +82,16 @@ public long extract(ItemVariant resource, long maxAmount, TransactionContext tra

@Override
public @NotNull Iterator<StorageView<ItemVariant>> iterator() {
return Iterators.forArray(slots);
return Iterators.forArray(sortedSlots);
}
}

@Override
public int getSlotCount() {
return unsortedSlots.length;
}

@Override
public SingleSlotStorage<ItemVariant> getSlot(int slot) {
return unsortedSlots[slot];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.github.mattidragon.extendeddrawers.compacting.CompressionRecipeManager;
import io.github.mattidragon.extendeddrawers.misc.ItemUtils;
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
import net.fabricmc.fabric.api.transfer.v1.storage.SlottedStorage;
import net.fabricmc.fabric.api.transfer.v1.storage.StoragePreconditions;
import net.fabricmc.fabric.api.transfer.v1.storage.StorageView;
import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage;
Expand All @@ -26,7 +27,7 @@
import java.util.NoSuchElementException;
import java.util.stream.Stream;

public final class CompactingDrawerStorage extends SnapshotParticipant<CompactingDrawerStorage.Snapshot> implements DrawerStorage {
public final class CompactingDrawerStorage extends SnapshotParticipant<CompactingDrawerStorage.Snapshot> implements DrawerStorage, SlottedStorage<ItemVariant> {
private final CompactingDrawerBlockEntity owner;
private final Settings settings;
private ItemVariant item = ItemVariant.blank();
Expand All @@ -42,7 +43,7 @@ public CompactingDrawerStorage(CompactingDrawerBlockEntity owner) {
@Override
public void dumpExcess(World world, BlockPos pos, @Nullable Direction side, @Nullable PlayerEntity player) {
if (amount > getCapacity()) {
var slots = getSlots();
var slots = getSlotArray();
// Iterate slots in reverse order
for (int i = slots.length - 1; i >= 0; i--) {
var slot = slots[i];
Expand Down Expand Up @@ -100,11 +101,11 @@ public long insert(ItemVariant resource, long maxAmount, TransactionContext tran
StoragePreconditions.notBlankNotNegative(resource, maxAmount);
long inserted = 0;

for (var slot : getActiveSlots()) {
for (var slot : getActiveSlotArray()) {
inserted += slot.insert(resource, maxAmount - inserted, transaction);
if (inserted == maxAmount) break;
}
if (Arrays.stream(getActiveSlots()).anyMatch(slot -> slot.item.equals(resource)) && settings.voiding)
if (Arrays.stream(getActiveSlotArray()).anyMatch(slot -> slot.item.equals(resource)) && settings.voiding)
return maxAmount;
return inserted;
}
Expand All @@ -114,7 +115,7 @@ public long extract(ItemVariant resource, long maxAmount, TransactionContext tra
StoragePreconditions.notNegative(maxAmount);
long extracted = 0;

for (var slot : getActiveSlots()) {
for (var slot : getActiveSlotArray()) {
extracted += slot.extract(resource, maxAmount - extracted, transaction);
if (extracted == maxAmount) break;
}
Expand All @@ -132,8 +133,14 @@ public Iterator<StorageView<ItemVariant>> nonEmptyIterator() {
return new StorageIterator();
}

@Override
public int getSlotCount() {
return getActiveSlotCount();
}

@Override
public Slot getSlot(int index) {
return getSlots()[index];
return getSlotArray()[index];
}

@Override
Expand Down Expand Up @@ -163,29 +170,29 @@ public void writeNbt(NbtCompound nbt) {
/**
* Returns all non-blocked slots in a new array
*/
public Slot[] getActiveSlots() {
public Slot[] getActiveSlotArray() {
int count = getActiveSlotCount();
var result = new Slot[count];
System.arraycopy(getSlots(), 0, result, 0, count);
System.arraycopy(getSlotArray(), 0, result, 0, count);
return result;
}

public int getActiveSlotCount() {
var slots = getSlots();
var slots = getSlotArray();
int size;
for (size = 0; size < slots.length; size++) {
if (slots[size].blocked) break;
}
return size;
}

public Slot[] getSlots() {
public Slot[] getSlotArray() {
if (updatePending) updateSlots();
return slots;
}

private int getTotalCompression() {
var slots = getActiveSlots();
var slots = getActiveSlotArray();
if (slots.length == 0) return 1; // Fallback in case something breaks

return slots[slots.length-1].compression;
Expand Down Expand Up @@ -276,7 +283,7 @@ public boolean hasNext() {

@Override
public Slot next() {
return getSlots()[index--];
return getSlotArray()[index--];
}
}

Expand All @@ -289,7 +296,7 @@ public boolean hasNext() {
// Loop over and check for non-empty storage
// No need for range check as the loop doesn't do anything if index is out of bounds
for (int i = index; i >= 0; i--) {
if (!getSlots()[i].isResourceBlank()) return true;
if (!getSlotArray()[i].isResourceBlank()) return true;
}

return false;
Expand All @@ -298,7 +305,7 @@ public boolean hasNext() {
@Override
public Slot next() {
for (; index >= 0; index--) {
if (!getSlots()[index].isResourceBlank()) {
if (!getSlotArray()[index].isResourceBlank()) {
return slots[index--];
}
}
Expand Down

0 comments on commit 4984df2

Please sign in to comment.