Make ResourceView implement Closeable

This commit is contained in:
shedaniel
2022-02-04 03:12:38 +08:00
parent a274b7c631
commit 5a9fca2207
8 changed files with 66 additions and 11 deletions

View File

@@ -19,6 +19,7 @@
package dev.architectury.transfer;
import java.io.Closeable;
import java.util.function.Predicate;
/**
@@ -26,7 +27,7 @@ import java.util.function.Predicate;
*
* @param <T> the type of resource
*/
public interface ResourceView<T> extends TransferView<T> {
public interface ResourceView<T> extends TransferView<T>, Closeable {
/**
* Returns the resource that this view represents.
* The returned resource is <b>immutable</b>.
@@ -50,4 +51,7 @@ public interface ResourceView<T> extends TransferView<T> {
return blank();
}
@Override
void close();
}

View File

@@ -74,7 +74,9 @@ public interface TransferHandler<T> extends TransferView<T> {
/**
* Returns the resource in a particular index.
* This may be extremely expensive to compute, avoid if you can.
* This may be extremely expensive to compute, avoid if you can.<br>
* <b>Please properly close this stream.</b> Failure to do so will result in a potential
* crash in conflicting transactions.
*
* @param index the index of the resource
* @return the resource in the given index

View File

@@ -88,5 +88,9 @@ public class ContainerTransferHandler implements CombinedItemTransferHandler {
public long getCapacity() {
return Math.min(container.getMaxStackSize(), getResource().getMaxStackSize());
}
@Override
public void close() {
}
}
}

View File

@@ -42,6 +42,11 @@ public interface FilteringResourceView<T> extends ForwardingResourceView<T>, Ext
public boolean canExtract(T toExtract) {
return canExtract.test(toExtract);
}
@Override
public void close() {
delegate.close();
}
};
}

View File

@@ -75,9 +75,8 @@ public class FabricStorageTransferHandler<F, S> implements TransferHandler<S> {
if (storage instanceof InventoryStorage) {
return new FabricStorageResourceView((StorageView<F>) ((InventoryStorage) storage).getSlots().get(index));
}
try (Transaction transaction = Transaction.openNested(this.transaction)) {
return new FabricStorageResourceView(Iterables.get(storage.iterable(transaction), index));
}
Transaction transaction = Transaction.openNested(this.transaction);
return new FabricStorageResourceView(Iterables.get(storage.iterable(transaction), index), transaction);
}
@Override
@@ -176,9 +175,16 @@ public class FabricStorageTransferHandler<F, S> implements TransferHandler<S> {
private class FabricStorageResourceView implements ResourceView<S> {
private final StorageView<F> view;
@Nullable
private final Transaction transaction;
private FabricStorageResourceView(StorageView<F> view) {
this(view, null);
}
private FabricStorageResourceView(StorageView<F> view, @Nullable Transaction transaction) {
this.view = view;
this.transaction = transaction;
}
@Override
@@ -226,6 +232,13 @@ public class FabricStorageTransferHandler<F, S> implements TransferHandler<S> {
public void loadState(Object state) {
throw new UnsupportedOperationException();
}
@Override
public void close() {
if (this.transaction != null) {
this.transaction.close();
}
}
}
public static class TypeAdapter<F, S> {

View File

@@ -189,5 +189,9 @@ public class FabricContainerItemTransferHandler implements TransferHandler<ItemS
public void loadState(Object state) {
throw new UnsupportedOperationException();
}
@Override
public void close() {
}
}
}

View File

@@ -133,17 +133,24 @@ public class FluidTransferImpl {
@NotNull
@Override
public net.minecraftforge.fluids.FluidStack getFluidInTank(int index) {
return FluidStackHooksForge.toForge(handler.getContent(index).getResource());
try (var resource = handler.getContent(index)) {
return FluidStackHooksForge.toForge(resource.getResource());
}
}
@Override
public int getTankCapacity(int index) {
return (int) handler.getContent(index).getCapacity();
try (var resource = handler.getContent(index)) {
return (int) resource.getCapacity();
}
}
@Override
public boolean isFluidValid(int index, @NotNull net.minecraftforge.fluids.FluidStack stack) {
FluidStack content = handler.getContent(index).getResource();
FluidStack content;
try (var resource = handler.getContent(index)) {
content = resource.getResource();
}
return content.getFluid() == stack.getFluid() && Objects.equals(content.getTag(), stack.getTag());
}
@@ -305,6 +312,10 @@ public class FluidTransferImpl {
public void loadState(Object state) {
throw new UnsupportedOperationException();
}
@Override
public void close() {
}
}
}

View File

@@ -106,12 +106,16 @@ public class ItemTransferImpl {
@NotNull
@Override
public ItemStack getStackInSlot(int index) {
return handler.getContent(index).getResource();
try (var resource = handler.getContent(index)) {
return resource.getResource();
}
}
@Override
public int getSlotLimit(int index) {
return (int) handler.getContent(index).getCapacity();
try (var resource = handler.getContent(index)) {
return (int) resource.getCapacity();
}
}
@NotNull
@@ -128,7 +132,11 @@ public class ItemTransferImpl {
@Override
public boolean isItemValid(int index, @NotNull ItemStack stack) {
ItemStack content = handler.getContent(index).getResource();
ItemStack content;
try (var resource = handler.getContent(index)) {
content = resource.getResource();
}
return content.getItem() == stack.getItem() && Objects.equals(content.getTag(), stack.getTag());
}
}
@@ -247,6 +255,10 @@ public class ItemTransferImpl {
public void loadState(Object state) {
throw new UnsupportedOperationException();
}
@Override
public void close() {
}
}
}
}