Add insert to ResourceView, this is partially supported

This commit is contained in:
shedaniel
2022-02-05 20:18:25 +08:00
parent 52aa329597
commit aa454ee525
18 changed files with 312 additions and 137 deletions

View File

@@ -63,11 +63,28 @@ public interface ResourceView<T> extends TransferView<T>, Closeable {
@Override
void close();
@Override
default ResourceView<T> unmodifiable() {
return filter(Predicates.alwaysFalse());
}
@Override
default ResourceView<T> onlyInsert() {
return filter(Predicates.alwaysTrue(), Predicates.alwaysFalse());
}
@Override
default ResourceView<T> onlyExtract() {
return filter(Predicates.alwaysFalse(), Predicates.alwaysTrue());
}
@Override
default ResourceView<T> filter(Predicate<T> filter) {
return FilteringResourceView.of(this, filter);
return filter(filter, filter);
}
@Override
default ResourceView<T> filter(Predicate<T> insert, Predicate<T> extract) {
return FilteringResourceView.of(this, insert, extract);
}
}

View File

@@ -107,13 +107,18 @@ public interface TransferHandler<T> extends TransferView<T> {
}
/**
* Inserts the given resource into the handler, returning the amount that was inserted.
* Inserts the given resource into a given resource index, returning the amount that was inserted.
*
* @param index the index of the resource
* @param toInsert the resource to insert
* @param action whether to simulate or actually insert the resource
* @return the amount that was inserted
*/
long insert(T toInsert, TransferAction action);
default long insert(int index, T toInsert, TransferAction action) {
try (ResourceView<T> resource = getContent(index)) {
return resource.insert(toInsert, action);
}
}
/**
* Extracts the given resource from a given resource index, returning the stack that was extracted.
@@ -158,22 +163,27 @@ public interface TransferHandler<T> extends TransferView<T> {
}
}
@Override
default TransferHandler<T> unmodifiable() {
return filter(Predicates.alwaysFalse());
}
@Override
default TransferHandler<T> onlyInsert() {
return filter(Predicates.alwaysTrue(), Predicates.alwaysFalse());
}
@Override
default TransferHandler<T> onlyExtract() {
return filter(Predicates.alwaysFalse(), Predicates.alwaysTrue());
}
@Override
default TransferHandler<T> filter(Predicate<T> filter) {
return filter(filter, filter);
}
@Override
default TransferHandler<T> filter(Predicate<T> insert, Predicate<T> extract) {
return FilteringTransferHandler.of(this, insert, extract);
}

View File

@@ -20,11 +20,22 @@
package dev.architectury.transfer;
import com.google.common.base.Predicates;
import dev.architectury.transfer.wrapper.filtering.FilteringTransferHandler;
import dev.architectury.transfer.wrapper.filtering.FilteringTransferView;
import org.jetbrains.annotations.ApiStatus;
import java.util.function.Predicate;
public interface TransferView<T> {
/**
* Inserts the given resource into the handler, returning the amount that was inserted.
*
* @param toInsert the resource to insert
* @param action whether to simulate or actually insert the resource
* @return the amount that was inserted
*/
long insert(T toInsert, TransferAction action);
/**
* Extracts the given resource from the handler, returning the stack that was extracted.
*
@@ -93,4 +104,24 @@ public interface TransferView<T> {
*/
@ApiStatus.OverrideOnly
void loadState(Object state);
default TransferView<T> unmodifiable() {
return filter(Predicates.alwaysFalse());
}
default TransferView<T> onlyInsert() {
return filter(Predicates.alwaysTrue(), Predicates.alwaysFalse());
}
default TransferView<T> onlyExtract() {
return filter(Predicates.alwaysFalse(), Predicates.alwaysTrue());
}
default TransferView<T> filter(Predicate<T> filter) {
return filter(filter, filter);
}
default TransferView<T> filter(Predicate<T> insert, Predicate<T> extract) {
return FilteringTransferView.of(this, insert, extract);
}
}

View File

@@ -1,3 +1,22 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021, 2022 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package dev.architectury.transfer.item.util;
import dev.architectury.transfer.ResourceView;

View File

@@ -1,26 +0,0 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021, 2022 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package dev.architectury.transfer.view;
public interface ExtractableView<T> {
default boolean canExtract(T toExtract) {
return true;
}
}

View File

@@ -19,8 +19,12 @@
package dev.architectury.transfer.view;
public interface ModifiableView<T> extends ExtractableView<T> {
public interface ModifiableView<T> {
default boolean canInsert(T toInsert) {
return true;
}
default boolean canExtract(T toExtract) {
return true;
}
}

View File

@@ -21,13 +21,12 @@ package dev.architectury.transfer.wrapper.filtering;
import dev.architectury.transfer.ResourceView;
import dev.architectury.transfer.TransferAction;
import dev.architectury.transfer.view.ExtractableView;
import dev.architectury.transfer.wrapper.forwarding.ForwardingResourceView;
import java.util.function.Predicate;
public interface FilteringResourceView<T> extends ForwardingResourceView<T>, ExtractableView<T> {
static <T> FilteringResourceView<T> of(ResourceView<T> delegate, Predicate<T> canExtract) {
public interface FilteringResourceView<T> extends ForwardingResourceView<T>, FilteringTransferView<T> {
static <T> FilteringResourceView<T> of(ResourceView<T> delegate, Predicate<T> canInsert, Predicate<T> canExtract) {
return new FilteringResourceView<T>() {
@Override
public ResourceView<T> forwardingTo() {
@@ -35,26 +34,19 @@ public interface FilteringResourceView<T> extends ForwardingResourceView<T>, Ext
}
@Override
public boolean canExtract(T toExtract) {
return canExtract.test(toExtract);
public boolean canInsert(T toInsert) {
return canInsert.test(toInsert);
}
@Override
public void close() {
delegate.close();
public boolean canExtract(T toExtract) {
return canExtract.test(toExtract);
}
};
}
@Override
boolean canExtract(T toExtract);
@Override
default T extract(T toExtract, TransferAction action) {
if (canExtract(toExtract)) {
return ForwardingResourceView.super.extract(toExtract, action);
} else {
return blank();
}
default T extract(Predicate<T> toExtract, long maxAmount, TransferAction action) {
return ForwardingResourceView.super.extract(toExtract, maxAmount, action);
}
}

View File

@@ -25,7 +25,7 @@ import dev.architectury.transfer.wrapper.single.SingleTransferHandler;
import java.util.function.Predicate;
public interface FilteringSingleTransferHandler<T> extends FilteringTransferHandler<T>, ForwardingSingleTransferHandler<T> {
public interface FilteringSingleTransferHandler<T> extends ForwardingSingleTransferHandler<T>, FilteringTransferHandler<T> {
static <T> FilteringSingleTransferHandler<T> of(SingleTransferHandler<T> delegate, Predicate<T> canInsert, Predicate<T> canExtract) {
return new FilteringSingleTransferHandler<T>() {
@Override

View File

@@ -20,14 +20,12 @@
package dev.architectury.transfer.wrapper.filtering;
import dev.architectury.transfer.ResourceView;
import dev.architectury.transfer.TransferAction;
import dev.architectury.transfer.TransferHandler;
import dev.architectury.transfer.view.ModifiableView;
import dev.architectury.transfer.wrapper.forwarding.ForwardingTransferHandler;
import java.util.function.Predicate;
public interface FilteringTransferHandler<T> extends ForwardingTransferHandler<T>, ModifiableView<T> {
public interface FilteringTransferHandler<T> extends ForwardingTransferHandler<T>, FilteringTransferView<T> {
static <T> FilteringTransferHandler<T> of(TransferHandler<T> delegate, Predicate<T> canInsert, Predicate<T> canExtract) {
return new FilteringTransferHandler<T>() {
@Override
@@ -47,31 +45,8 @@ public interface FilteringTransferHandler<T> extends ForwardingTransferHandler<T
};
}
@Override
default long insert(T toInsert, TransferAction action) {
if (canInsert(toInsert)) {
return ForwardingTransferHandler.super.insert(toInsert, action);
} else {
return 0;
}
}
@Override
default T extract(T toExtract, TransferAction action) {
if (canExtract(toExtract)) {
return ForwardingTransferHandler.super.extract(toExtract, action);
} else {
return blank();
}
}
@Override
default T extract(Predicate<T> toExtract, long maxAmount, TransferAction action) {
return ForwardingTransferHandler.super.extract(toExtract.and(this::canExtract), maxAmount, action);
}
@Override
default ResourceView<T> forwardResource(ResourceView<T> resource) {
return FilteringResourceView.of(ForwardingTransferHandler.super.forwardResource(resource), this::canExtract);
return FilteringResourceView.of(ForwardingTransferHandler.super.forwardResource(resource), this::canInsert, this::canExtract);
}
}

View File

@@ -0,0 +1,71 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021, 2022 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package dev.architectury.transfer.wrapper.filtering;
import dev.architectury.transfer.TransferAction;
import dev.architectury.transfer.TransferView;
import dev.architectury.transfer.view.ModifiableView;
import dev.architectury.transfer.wrapper.forwarding.ForwardingTransferView;
import java.util.function.Predicate;
public interface FilteringTransferView<T> extends ForwardingTransferView<T>, ModifiableView<T> {
static <T> FilteringTransferView<T> of(TransferView<T> delegate, Predicate<T> canInsert, Predicate<T> canExtract) {
return new FilteringTransferView<T>() {
@Override
public TransferView<T> forwardingTo() {
return delegate;
}
@Override
public boolean canInsert(T toInsert) {
return canExtract.test(toInsert);
}
@Override
public boolean canExtract(T toExtract) {
return canExtract.test(toExtract);
}
};
}
@Override
default long insert(T toInsert, TransferAction action) {
if (canInsert(toInsert)) {
return ForwardingTransferView.super.insert(toInsert, action);
} else {
return 0;
}
}
@Override
default T extract(T toExtract, TransferAction action) {
if (canExtract(toExtract)) {
return ForwardingTransferView.super.extract(toExtract, action);
} else {
return blank();
}
}
@Override
default T extract(Predicate<T> toExtract, long maxAmount, TransferAction action) {
return ForwardingTransferView.super.extract(toExtract.and(this::canExtract), maxAmount, action);
}
}

View File

@@ -22,7 +22,10 @@ package dev.architectury.transfer.wrapper.forwarding;
import dev.architectury.transfer.ResourceView;
import dev.architectury.transfer.TransferAction;
public interface ForwardingResourceView<T> extends ResourceView<T> {
import java.util.function.Predicate;
public interface ForwardingResourceView<T> extends ResourceView<T>, ForwardingTransferView<T> {
@Override
ResourceView<T> forwardingTo();
@Override
@@ -36,28 +39,8 @@ public interface ForwardingResourceView<T> extends ResourceView<T> {
}
@Override
default T extract(T toExtract, TransferAction action) {
return forwardingTo().extract(toExtract, action);
}
@Override
default T blank() {
return forwardingTo().blank();
}
@Override
default T copyWithAmount(T resource, long amount) {
return forwardingTo().copyWithAmount(resource, amount);
}
@Override
default Object saveState() {
return forwardingTo().saveState();
}
@Override
default void loadState(Object state) {
forwardingTo().loadState(state);
default T extract(Predicate<T> toExtract, long maxAmount, TransferAction action) {
return ResourceView.super.extract(toExtract, maxAmount, action);
}
@Override

View File

@@ -20,13 +20,12 @@
package dev.architectury.transfer.wrapper.forwarding;
import dev.architectury.transfer.ResourceView;
import dev.architectury.transfer.TransferAction;
import dev.architectury.transfer.TransferHandler;
import java.util.function.Predicate;
import java.util.stream.Stream;
public interface ForwardingTransferHandler<T> extends TransferHandler<T> {
public interface ForwardingTransferHandler<T> extends TransferHandler<T>, ForwardingTransferView<T> {
@Override
TransferHandler<T> forwardingTo();
default ResourceView<T> forwardResource(ResourceView<T> resource) {
@@ -49,39 +48,4 @@ public interface ForwardingTransferHandler<T> extends TransferHandler<T> {
default ResourceView<T> getContent(int index) {
return forwardResource(forwardingTo().getContent(index));
}
@Override
default long insert(T toInsert, TransferAction action) {
return forwardingTo().insert(toInsert, action);
}
@Override
default T extract(T toExtract, TransferAction action) {
return forwardingTo().extract(toExtract, action);
}
@Override
default T extract(Predicate<T> toExtract, long maxAmount, TransferAction action) {
return forwardingTo().extract(toExtract, maxAmount, action);
}
@Override
default T blank() {
return forwardingTo().blank();
}
@Override
default T copyWithAmount(T resource, long amount) {
return forwardingTo().copyWithAmount(resource, amount);
}
@Override
default Object saveState() {
return forwardingTo().saveState();
}
@Override
default void loadState(Object state) {
forwardingTo().loadState(state);
}
}

View File

@@ -0,0 +1,64 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021, 2022 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package dev.architectury.transfer.wrapper.forwarding;
import dev.architectury.transfer.TransferAction;
import dev.architectury.transfer.TransferView;
import java.util.function.Predicate;
public interface ForwardingTransferView<T> extends TransferView<T> {
TransferView<T> forwardingTo();
@Override
default long insert(T toInsert, TransferAction action) {
return forwardingTo().insert(toInsert, action);
}
@Override
default T extract(Predicate<T> toExtract, long maxAmount, TransferAction action) {
return forwardingTo().extract(toExtract, maxAmount, action);
}
@Override
default T extract(T toExtract, TransferAction action) {
return forwardingTo().extract(toExtract, action);
}
@Override
default T blank() {
return forwardingTo().blank();
}
@Override
default T copyWithAmount(T resource, long amount) {
return forwardingTo().copyWithAmount(resource, amount);
}
@Override
default Object saveState() {
return forwardingTo().saveState();
}
@Override
default void loadState(Object state) {
forwardingTo().loadState(state);
}
}

View File

@@ -1,3 +1,22 @@
/*
* This file is part of architectury.
* Copyright (C) 2020, 2021, 2022 architectury
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package dev.architectury.utils;
public class Amount {

View File

@@ -202,6 +202,26 @@ public class FabricStorageTransferHandler<F, S> implements TransferHandler<S> {
return FabricStorageTransferHandler.this.copyWithAmount(resource, amount);
}
@Override
public long insert(S toInsert, TransferAction action) {
if (view instanceof Storage) {
long inserted;
try (Transaction nested = Transaction.openNested(this.transaction)) {
inserted = ((Storage<F>) this.view).insert(toFabric(toInsert), getAmount(toInsert), nested);
if (action == TransferAction.ACT) {
nested.commit();
}
}
return inserted;
}
// impossible to insert to a fabric storage view with an index
return 0;
}
@Override
public S extract(S toExtract, TransferAction action) {
if (isEmpty(toExtract)) return blank();

View File

@@ -161,6 +161,22 @@ public class FabricContainerItemTransferHandler implements TransferHandler<ItemS
return ItemStackHooks.copyWithCount(resource, amount);
}
@Override
public long insert(ItemStack toInsert, TransferAction action) {
if (toInsert.isEmpty()) return 0;
long inserted;
try (Transaction nested = Transaction.openNested(FabricContainerItemTransferHandler.this.transaction)) {
inserted = this.storage.insert(ItemVariant.of(toInsert), toInsert.getCount(), nested);
if (action == TransferAction.ACT) {
nested.commit();
}
}
return inserted;
}
@Override
public ItemStack extract(ItemStack toExtract, TransferAction action) {
if (toExtract.isEmpty()) return blank();

View File

@@ -289,6 +289,12 @@ public class FluidTransferImpl {
return handler.getTankCapacity(index);
}
@Override
public long insert(FluidStack toInsert, TransferAction action) {
// impossible to insert to a forge handler with an index
return 0;
}
@Override
public FluidStack extract(FluidStack toExtract, TransferAction action) {
// impossible to extract from a forge handler with an index

View File

@@ -145,9 +145,10 @@ public class ItemTransferImpl {
@Override
public long insert(ItemStack toInsert, TransferAction action) {
if (toInsert.isEmpty()) return 0;
int toInsertCount = toInsert.getCount();
ItemStack left = ItemHandlerHelper.insertItemStacked(handler, toInsert, action == TransferAction.SIMULATE);
return toInsertCount - left.getCount();
ItemStack remaining = ItemHandlerHelper.insertItemStacked(handler, toInsert, action == TransferAction.SIMULATE);
return toInsertCount - remaining.getCount();
}
@Override
@@ -222,6 +223,15 @@ public class ItemTransferImpl {
return handler.getSlotLimit(index);
}
@Override
public long insert(ItemStack toInsert, TransferAction action) {
if (toInsert.isEmpty()) return 0;
int toInsertCount = toInsert.getCount();
ItemStack remaining = handler.insertItem(index, toInsert, action == TransferAction.SIMULATE);
return toInsertCount - remaining.getCount();
}
@Override
public ItemStack extract(ItemStack toExtract, TransferAction action) {
if (toExtract.isEmpty()) return blank();