From 7df55416a8370c22e1bed109460f8991c0a9419d Mon Sep 17 00:00:00 2001 From: Bruno Ploumhans Date: Fri, 31 Mar 2023 19:31:51 +0200 Subject: [PATCH] Make SlottedStorage#getSlots return a view, remove useless field, add UnmodifiableView annotations --- .../v1/context/ContainerItemContext.java | 2 ++ .../transfer/v1/item/InventoryStorage.java | 2 ++ .../transfer/v1/storage/SlottedStorage.java | 21 +++++++++-------- .../fabric/impl/transfer/TransferApiImpl.java | 23 +++++++++++++++---- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/context/ContainerItemContext.java b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/context/ContainerItemContext.java index d17bc12677f..af7da921ec1 100644 --- a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/context/ContainerItemContext.java +++ b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/context/ContainerItemContext.java @@ -20,6 +20,7 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.UnmodifiableView; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; @@ -293,5 +294,6 @@ default long exchange(ItemVariant newVariant, long maxAmount, TransactionContext * * @return An unmodifiable list containing additional slots of this context. If no additional slot is available, the list is empty. */ + @UnmodifiableView List> getAdditionalSlots(); } diff --git a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/item/InventoryStorage.java b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/item/InventoryStorage.java index 71884323b18..4446388c807 100644 --- a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/item/InventoryStorage.java +++ b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/item/InventoryStorage.java @@ -21,6 +21,7 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.UnmodifiableView; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.inventory.Inventory; @@ -70,6 +71,7 @@ static InventoryStorage of(Inventory inventory, @Nullable Direction direction) { * Each wrapper corresponds to a single slot in the inventory. */ @Override + @UnmodifiableView List> getSlots(); /** diff --git a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/storage/SlottedStorage.java b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/storage/SlottedStorage.java index cbd136682fb..bb8cc60f905 100644 --- a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/storage/SlottedStorage.java +++ b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/storage/SlottedStorage.java @@ -19,8 +19,11 @@ import java.util.List; import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.UnmodifiableView; import net.fabricmc.fabric.api.transfer.v1.storage.base.SingleSlotStorage; +import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext; +import net.fabricmc.fabric.impl.transfer.TransferApiImpl; /** * A {@link Storage} implementation made of indexed slots. @@ -49,16 +52,16 @@ public interface SlottedStorage extends Storage { SingleSlotStorage getSlot(int slot); /** - * Retrieve all the slots of this storage. The list must not be modified. + * Retrieve a list containing all the slots of this storage. The list must not be modified. + * + *

This function can be used to interface with code that requires a slot list, + * for example {@link StorageUtil#insertStacking} or {@link ContainerItemContext#getAdditionalSlots()}. + * + *

It is guaranteed that calling this function is fast. + * The default implementation returns a view over the storage that delegates to {@link #getSlotCount} and {@link #getSlot}. */ + @UnmodifiableView default List> getSlots() { - int slotCount = getSlotCount(); - SingleSlotStorage[] slots = new SingleSlotStorage[slotCount]; - - for (int i = 0; i < slotCount; i++) { - slots[i] = getSlot(i); - } - - return List.of(slots); + return TransferApiImpl.makeListView(this); } } diff --git a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/impl/transfer/TransferApiImpl.java b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/impl/transfer/TransferApiImpl.java index 170194f8268..04e0fa51b96 100644 --- a/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/impl/transfer/TransferApiImpl.java +++ b/fabric-transfer-api-v1/src/main/java/net/fabricmc/fabric/impl/transfer/TransferApiImpl.java @@ -16,16 +16,20 @@ package net.fabricmc.fabric.impl.transfer; +import java.util.AbstractList; import java.util.Collections; import java.util.Iterator; +import java.util.List; import java.util.NoSuchElementException; import java.util.concurrent.atomic.AtomicLong; import org.slf4j.LoggerFactory; import org.slf4j.Logger; +import net.fabricmc.fabric.api.transfer.v1.storage.SlottedStorage; import net.fabricmc.fabric.api.transfer.v1.storage.Storage; 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; public class TransferApiImpl { @@ -64,11 +68,6 @@ public long getVersion() { } }; - /** - * Not null when writing to an inventory in a transaction, null otherwise. - */ - public static final ThreadLocal SUPPRESS_SPECIAL_LOGIC = new ThreadLocal<>(); - public static Iterator singletonIterator(T it) { return new Iterator() { boolean hasNext = true; @@ -125,4 +124,18 @@ public StorageView next() { } }; } + + public static List> makeListView(SlottedStorage storage) { + return new AbstractList<>() { + @Override + public SingleSlotStorage get(int index) { + return storage.getSlot(index); + } + + @Override + public int size() { + return storage.getSlotCount(); + } + }; + } }