Skip to content

Commit

Permalink
Make SlottedStorage#getSlots return a view, remove useless field, add…
Browse files Browse the repository at this point in the history
… UnmodifiableView annotations
  • Loading branch information
Bruno Ploumhans committed Mar 31, 2023
1 parent f416451 commit 7df5541
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<SingleSlotStorage<ItemVariant>> getAdditionalSlots();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<SingleSlotStorage<ItemVariant>> getSlots();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -49,16 +52,16 @@ public interface SlottedStorage<T> extends Storage<T> {
SingleSlotStorage<T> getSlot(int slot);

/**
* Retrieve all the slots of this storage. <b>The list must not be modified.</b>
* Retrieve a list containing all the slots of this storage. <b>The list must not be modified.</b>
*
* <p>This function can be used to interface with code that requires a slot list,
* for example {@link StorageUtil#insertStacking} or {@link ContainerItemContext#getAdditionalSlots()}.
*
* <p>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<SingleSlotStorage<T>> getSlots() {
int slotCount = getSlotCount();
SingleSlotStorage<T>[] slots = new SingleSlotStorage[slotCount];

for (int i = 0; i < slotCount; i++) {
slots[i] = getSlot(i);
}

return List.of(slots);
return TransferApiImpl.makeListView(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -64,11 +68,6 @@ public long getVersion() {
}
};

/**
* Not null when writing to an inventory in a transaction, null otherwise.
*/
public static final ThreadLocal<Object> SUPPRESS_SPECIAL_LOGIC = new ThreadLocal<>();

public static <T> Iterator<T> singletonIterator(T it) {
return new Iterator<T>() {
boolean hasNext = true;
Expand Down Expand Up @@ -125,4 +124,18 @@ public StorageView<T> next() {
}
};
}

public static <T> List<SingleSlotStorage<T>> makeListView(SlottedStorage<T> storage) {
return new AbstractList<>() {
@Override
public SingleSlotStorage<T> get(int index) {
return storage.getSlot(index);
}

@Override
public int size() {
return storage.getSlotCount();
}
};
}
}

0 comments on commit 7df5541

Please sign in to comment.