From 4984df233055a2617c0395109dc9425b2527ac4a Mon Sep 17 00:00:00 2001 From: MattiDragon Date: Mon, 29 Jan 2024 14:07:23 +0200 Subject: [PATCH] Make drawers slotted storages --- changelog/2.1.2+1.20.4.md | 2 ++ gradle.properties | 2 +- .../CompactingDrawerBlockEntityRenderer.java | 2 +- .../block/CompactingDrawerBlock.java | 4 +-- .../storage/CombinedDrawerStorage.java | 35 ++++++++++++------- .../storage/CompactingDrawerStorage.java | 35 +++++++++++-------- 6 files changed, 50 insertions(+), 30 deletions(-) create mode 100644 changelog/2.1.2+1.20.4.md diff --git a/changelog/2.1.2+1.20.4.md b/changelog/2.1.2+1.20.4.md new file mode 100644 index 0000000..b9b425e --- /dev/null +++ b/changelog/2.1.2+1.20.4.md @@ -0,0 +1,2 @@ +* Make drawers slotted storages + * This improves mod compatibility \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 88ba816..c608408 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/src/client/java/io/github/mattidragon/extendeddrawers/client/renderer/CompactingDrawerBlockEntityRenderer.java b/src/client/java/io/github/mattidragon/extendeddrawers/client/renderer/CompactingDrawerBlockEntityRenderer.java index 65ce6db..a9e4bba 100644 --- a/src/client/java/io/github/mattidragon/extendeddrawers/client/renderer/CompactingDrawerBlockEntityRenderer.java +++ b/src/client/java/io/github/mattidragon/extendeddrawers/client/renderer/CompactingDrawerBlockEntityRenderer.java @@ -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); diff --git a/src/main/java/io/github/mattidragon/extendeddrawers/block/CompactingDrawerBlock.java b/src/main/java/io/github/mattidragon/extendeddrawers/block/CompactingDrawerBlock.java index 2a2454e..b3f2989 100644 --- a/src/main/java/io/github/mattidragon/extendeddrawers/block/CompactingDrawerBlock.java +++ b/src/main/java/io/github/mattidragon/extendeddrawers/block/CompactingDrawerBlock.java @@ -65,7 +65,7 @@ public void appendTooltip(ItemStack stack, @Nullable BlockView world, List 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(); @@ -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--) { diff --git a/src/main/java/io/github/mattidragon/extendeddrawers/storage/CombinedDrawerStorage.java b/src/main/java/io/github/mattidragon/extendeddrawers/storage/CombinedDrawerStorage.java index 8babef9..8702e06 100644 --- a/src/main/java/io/github/mattidragon/extendeddrawers/storage/CombinedDrawerStorage.java +++ b/src/main/java/io/github/mattidragon/extendeddrawers/storage/CombinedDrawerStorage.java @@ -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; @@ -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 { - private DrawerSlot[] slots; +public class CombinedDrawerStorage implements SlottedStorage { + 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; } @@ -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; } @@ -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; } @@ -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; } @@ -79,8 +82,16 @@ public long extract(ItemVariant resource, long maxAmount, TransactionContext tra @Override public @NotNull Iterator> iterator() { - return Iterators.forArray(slots); + return Iterators.forArray(sortedSlots); } -} + @Override + public int getSlotCount() { + return unsortedSlots.length; + } + @Override + public SingleSlotStorage getSlot(int slot) { + return unsortedSlots[slot]; + } +} diff --git a/src/main/java/io/github/mattidragon/extendeddrawers/storage/CompactingDrawerStorage.java b/src/main/java/io/github/mattidragon/extendeddrawers/storage/CompactingDrawerStorage.java index c66543a..24c48db 100644 --- a/src/main/java/io/github/mattidragon/extendeddrawers/storage/CompactingDrawerStorage.java +++ b/src/main/java/io/github/mattidragon/extendeddrawers/storage/CompactingDrawerStorage.java @@ -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; @@ -26,7 +27,7 @@ import java.util.NoSuchElementException; import java.util.stream.Stream; -public final class CompactingDrawerStorage extends SnapshotParticipant implements DrawerStorage { +public final class CompactingDrawerStorage extends SnapshotParticipant implements DrawerStorage, SlottedStorage { private final CompactingDrawerBlockEntity owner; private final Settings settings; private ItemVariant item = ItemVariant.blank(); @@ -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]; @@ -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; } @@ -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; } @@ -132,8 +133,14 @@ public Iterator> nonEmptyIterator() { return new StorageIterator(); } + @Override + public int getSlotCount() { + return getActiveSlotCount(); + } + + @Override public Slot getSlot(int index) { - return getSlots()[index]; + return getSlotArray()[index]; } @Override @@ -163,15 +170,15 @@ 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; @@ -179,13 +186,13 @@ public int getActiveSlotCount() { 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; @@ -276,7 +283,7 @@ public boolean hasNext() { @Override public Slot next() { - return getSlots()[index--]; + return getSlotArray()[index--]; } } @@ -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; @@ -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--]; } }