-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transfer API: Add slotted storage and non-empty iterator (#2908)
* Transfer API: Add non-empty iterator * Add SlottedStorage * Add StorageUtil.extractAny * Undeprecate ContainerItemContext.withInitial * Add licenses * Revert "Undeprecate ContainerItemContext.withInitial" This reverts commit dcf123e. * Tweaks * Make SlottedStorage#getSlots return a view, remove useless field, add UnmodifiableView annotations * Remove useless @inheritdoc * Fix infinite loop in the tests
- Loading branch information
1 parent
b5a544a
commit f8daae0
Showing
11 changed files
with
333 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...sfer-api-v1/src/main/java/net/fabricmc/fabric/api/transfer/v1/storage/SlottedStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.fabric.api.transfer.v1.storage; | ||
|
||
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. | ||
* | ||
* <p>Please note that some storages may not implement this interface. | ||
* It is up to the storage implementation to decide whether to implement this interface or not. | ||
* Checking whether a storage is slotted can be done using {@code instanceof}. | ||
* | ||
* @param <T> The type of the stored resources. | ||
* | ||
* <b>Experimental feature</b>, we reserve the right to remove or change it without further notice. | ||
* The transfer API is a complex addition, and we want to be able to correct possible design mistakes. | ||
*/ | ||
@ApiStatus.Experimental | ||
public interface SlottedStorage<T> extends Storage<T> { | ||
/** | ||
* Retrieve the number of slots in this storage. | ||
*/ | ||
int getSlotCount(); | ||
|
||
/** | ||
* Retrieve a specific slot of this storage. | ||
* | ||
* @throws IndexOutOfBoundsException If the slot index is out of bounds. | ||
*/ | ||
SingleSlotStorage<T> getSlot(int slot); | ||
|
||
/** | ||
* 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() { | ||
return TransferApiImpl.makeListView(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...rc/main/java/net/fabricmc/fabric/api/transfer/v1/storage/base/CombinedSlottedStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.fabricmc.fabric.api.transfer.v1.storage.base; | ||
|
||
import java.util.List; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
import net.fabricmc.fabric.api.transfer.v1.storage.SlottedStorage; | ||
import net.fabricmc.fabric.api.transfer.v1.storage.Storage; | ||
|
||
/** | ||
* A {@link Storage} wrapping multiple slotted storages. | ||
* Same as {@link CombinedStorage}, but for {@link SlottedStorage}s. | ||
* | ||
* @param <T> The type of the stored resources. | ||
* @param <S> The class of every part. {@code ? extends Storage<T>} can be used if the parts are of different types. | ||
* | ||
* <b>Experimental feature</b>, we reserve the right to remove or change it without further notice. | ||
* The transfer API is a complex addition, and we want to be able to correct possible design mistakes. | ||
*/ | ||
@ApiStatus.Experimental | ||
public class CombinedSlottedStorage<T, S extends SlottedStorage<T>> extends CombinedStorage<T, S> implements SlottedStorage<T> { | ||
public CombinedSlottedStorage(List<S> parts) { | ||
super(parts); | ||
} | ||
|
||
@Override | ||
public int getSlotCount() { | ||
int count = 0; | ||
|
||
for (S part : parts) { | ||
count += part.getSlotCount(); | ||
} | ||
|
||
return count; | ||
} | ||
|
||
@Override | ||
public SingleSlotStorage<T> getSlot(int slot) { | ||
int updatedSlot = slot; | ||
|
||
for (SlottedStorage<T> part : parts) { | ||
if (updatedSlot < part.getSlotCount()) { | ||
return part.getSlot(updatedSlot); | ||
} | ||
|
||
updatedSlot -= part.getSlotCount(); | ||
} | ||
|
||
throw new IndexOutOfBoundsException("Slot " + slot + " is out of bounds. This storage has size " + getSlotCount()); | ||
} | ||
} |
Oops, something went wrong.