mirror of
https://github.com/architectury/architectury-api.git
synced 2026-03-30 05:05:19 -05:00
Clean up some classes
This commit is contained in:
@@ -77,10 +77,10 @@ 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.<br>
|
||||
* <b>Please properly close this stream.</b> Failure to do so will result in a potential
|
||||
* <b>Please properly close this resource.</b> Failure to do so will result in a potential
|
||||
* crash in conflicting transactions.<br><br>
|
||||
* You can easily ensure that this is closed by using try-with-resources, otherwise
|
||||
* you will have to manually close the stream by calling {@link Stream#close()}.
|
||||
* you will have to manually close the stream by calling {@link ResourceView#close()}.
|
||||
*
|
||||
* @param index the index of the resource
|
||||
* @return the resource in the given index
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
package dev.architectury.transfer;
|
||||
package dev.architectury.transfer.access;
|
||||
|
||||
/**
|
||||
* The base interface for all lookup accesses.
|
||||
@@ -20,7 +20,6 @@
|
||||
package dev.architectury.transfer.access;
|
||||
|
||||
import dev.architectury.impl.transfer.access.BlockLookupAccessImpl;
|
||||
import dev.architectury.transfer.ApiLookupAccess;
|
||||
|
||||
/**
|
||||
* An API lookup for blocks.
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
package dev.architectury.transfer.access;
|
||||
|
||||
import dev.architectury.impl.transfer.access.ItemLookupAccessImpl;
|
||||
import dev.architectury.transfer.ApiLookupAccess;
|
||||
|
||||
public interface ItemLookupAccess<T, Context> extends ApiLookupAccess<T, ItemLookup<T, Context>, ItemLookupRegistration<T, Context>>, ItemLookup<T, Context>, ItemLookupRegistration<T, Context> {
|
||||
static <T, Context> ItemLookupAccess<T, Context> create() {
|
||||
|
||||
@@ -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.access;
|
||||
|
||||
import dev.architectury.injectables.annotations.ExpectPlatform;
|
||||
import net.minecraft.core.Direction;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class PlatformLookup {
|
||||
public static <O, T> void attachBlock(BlockLookupAccess<T, Direction> access, Object lookup, Function<O, T> wrapper, BiFunction<T, Direction, O> unwrapper) {
|
||||
attachBlockQuery(access, lookup, wrapper);
|
||||
attachBlockRegistration(access, lookup, unwrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches a block query to the given lookup.<br><br>
|
||||
* Lookup accepts {@code Capability<O>} on Forge.<br>
|
||||
* Lookup accepts {@code BlockApiLookup<O, Direction>} on Fabric.
|
||||
* <br><br>
|
||||
* This method allows the lookup to <b>read</b> the lookup object, while converting
|
||||
* it to the platform-less abstracted type {@code T}.
|
||||
*
|
||||
* @param access the access to attach to
|
||||
* @param lookup the platform lookup object
|
||||
* @param wrapper the wrapper function, to convert the platform object to the abstracted object
|
||||
* @param <O> the platform object type
|
||||
* @param <T> the abstracted object type
|
||||
*/
|
||||
@ExpectPlatform
|
||||
public static <O, T> void attachBlockQuery(BlockLookupAccess<T, Direction> access, Object lookup, Function<O, T> wrapper) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches a block registration handler to the given lookup.<br><br>
|
||||
* Lookup accepts {@code Capability<O>} on Forge.<br>
|
||||
* Lookup accepts {@code BlockApiLookup<O, Direction>} on Fabric.
|
||||
* <br><br>
|
||||
* This method allows other mods on the platform to look up the abstracted object, by
|
||||
* converting the abstracted object to the platform object.
|
||||
*
|
||||
* @param access the access to attach to
|
||||
* @param lookup the platform lookup object
|
||||
* @param unwrapper the unwrapper function, to convert the abstracted object to the platform object
|
||||
* @param <O> the platform object type
|
||||
* @param <T> the abstracted object type
|
||||
*/
|
||||
@ExpectPlatform
|
||||
public static <O, T> void attachBlockRegistration(BlockLookupAccess<T, Direction> access, Object lookup, BiFunction<T, Direction, O> unwrapper) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
@@ -20,8 +20,8 @@
|
||||
package dev.architectury.transfer.energy;
|
||||
|
||||
import dev.architectury.injectables.annotations.ExpectPlatform;
|
||||
import dev.architectury.transfer.TransferHandler;
|
||||
import dev.architectury.transfer.access.BlockLookupAccess;
|
||||
import dev.architectury.transfer.access.PlatformLookup;
|
||||
import dev.architectury.transfer.wrapper.single.SingleTransferHandler;
|
||||
import net.minecraft.core.Direction;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -30,17 +30,17 @@ public class EnergyTransfer {
|
||||
public static final BlockLookupAccess<SingleTransferHandler<Long>, Direction> BLOCK = BlockLookupAccess.create();
|
||||
|
||||
static {
|
||||
init();
|
||||
PlatformLookup.attachBlock(BLOCK, platformBlockLookup(), EnergyTransfer::wrap, (handler, direction) -> unwrap(handler));
|
||||
}
|
||||
|
||||
@ExpectPlatform
|
||||
private static void init() {
|
||||
private static Object platformBlockLookup() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps a platform-specific item transfer handler into the architectury transfer handler.
|
||||
* This accepts {@code IEnergyStorage} on Forge.
|
||||
* Wraps a platform-specific item transfer handler into the architectury transfer handler.<br><br>
|
||||
* This accepts {@code IEnergyStorage} on Forge.<br>
|
||||
* This accepts {@code EnergyStorage} on Fabric.
|
||||
*
|
||||
* @param object the handler to wrap
|
||||
@@ -52,4 +52,10 @@ public class EnergyTransfer {
|
||||
public static SingleTransferHandler<Long> wrap(@Nullable Object object) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
@ExpectPlatform
|
||||
@Nullable
|
||||
public static Object unwrap(@Nullable SingleTransferHandler<Long> handler) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,15 +23,11 @@ import dev.architectury.fluid.FluidStack;
|
||||
import dev.architectury.injectables.annotations.ExpectPlatform;
|
||||
import dev.architectury.transfer.TransferHandler;
|
||||
import dev.architectury.transfer.access.BlockLookupAccess;
|
||||
import dev.architectury.transfer.access.ItemLookupAccess;
|
||||
import dev.architectury.transfer.access.PlatformLookup;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class FluidTransfer {
|
||||
private FluidTransfer() {
|
||||
}
|
||||
|
||||
/**
|
||||
* A lookup access for fluid transfer handlers, the direction context is
|
||||
* only required on fabric.
|
||||
@@ -46,17 +42,17 @@ public class FluidTransfer {
|
||||
// public static final ItemLookupAccess<TransferHandler<FluidStack>, TransferHandler<ItemStack>> ITEM = ItemLookupAccess.create();
|
||||
|
||||
static {
|
||||
init();
|
||||
PlatformLookup.attachBlock(BLOCK, platformBlockLookup(), FluidTransfer::wrap, (handler, direction) -> unwrap(handler));
|
||||
}
|
||||
|
||||
@ExpectPlatform
|
||||
private static void init() {
|
||||
private static Object platformBlockLookup() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps a platform-specific fluid transfer handler into the architectury transfer handler.
|
||||
* This accepts {@code IFluidHandler} on Forge.
|
||||
* Wraps a platform-specific fluid transfer handler into the architectury transfer handler.<br><br>
|
||||
* This accepts {@code IFluidHandler} on Forge.<br>
|
||||
* This accepts {@code Storage<FluidVariant>} on Fabric.
|
||||
*
|
||||
* @param object the handler to wrap
|
||||
@@ -68,4 +64,10 @@ public class FluidTransfer {
|
||||
public static TransferHandler<FluidStack> wrap(@Nullable Object object) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
@ExpectPlatform
|
||||
@Nullable
|
||||
public static Object unwrap(@Nullable TransferHandler<FluidStack> handler) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,15 +21,19 @@ package dev.architectury.transfer.fluid;
|
||||
|
||||
import dev.architectury.fluid.FluidStack;
|
||||
import dev.architectury.transfer.TransferHandler;
|
||||
import dev.architectury.transfer.view.VariantView;
|
||||
|
||||
public interface FluidTransferHandler extends TransferHandler<FluidStack> {
|
||||
/**
|
||||
* This is a convenience class that implements methods for {@link FluidStack}s.
|
||||
*/
|
||||
public interface FluidTransferHandler extends FluidTransferView, TransferHandler<FluidStack>, VariantView<FluidStack> {
|
||||
@Override
|
||||
default FluidStack blank() {
|
||||
return FluidStack.empty();
|
||||
default long getAmount(FluidStack resource) {
|
||||
return resource.getAmount();
|
||||
}
|
||||
|
||||
@Override
|
||||
default FluidStack copyWithAmount(FluidStack resource, long amount) {
|
||||
return resource.copyWithAmount(amount);
|
||||
default boolean isSameVariant(FluidStack first, FluidStack second) {
|
||||
return first.isFluidEqual(second) && first.isTagEqual(second);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,8 +21,9 @@ package dev.architectury.transfer.fluid;
|
||||
|
||||
import dev.architectury.fluid.FluidStack;
|
||||
import dev.architectury.transfer.ResourceView;
|
||||
import dev.architectury.transfer.TransferView;
|
||||
|
||||
public interface FluidResourceView extends ResourceView<FluidStack> {
|
||||
public interface FluidTransferView extends TransferView<FluidStack> {
|
||||
@Override
|
||||
default FluidStack blank() {
|
||||
return FluidStack.empty();
|
||||
@@ -1,31 +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.fluid.wrapper;
|
||||
|
||||
import dev.architectury.fluid.FluidStack;
|
||||
import dev.architectury.transfer.fluid.FluidTransferHandler;
|
||||
import dev.architectury.transfer.wrapper.single.BaseSingleTransferHandler;
|
||||
|
||||
public interface BaseSingleFluidTransferHandler extends BaseSingleTransferHandler<FluidStack>, FluidTransferHandler, FluidVariantView {
|
||||
@Override
|
||||
default FluidStack copy(FluidStack resource) {
|
||||
return resource.copy();
|
||||
}
|
||||
}
|
||||
@@ -20,8 +20,13 @@
|
||||
package dev.architectury.transfer.fluid.wrapper;
|
||||
|
||||
import dev.architectury.fluid.FluidStack;
|
||||
import dev.architectury.transfer.TransferHandler;
|
||||
import dev.architectury.transfer.fluid.FluidTransferHandler;
|
||||
import dev.architectury.transfer.wrapper.CombinedTransferHandler;
|
||||
|
||||
public interface CombinedFluidTransferHandler extends CombinedTransferHandler<FluidStack>, FluidTransferHandler, FluidVariantView {
|
||||
/**
|
||||
* A {@link TransferHandler} that combines multiple {@link TransferHandler}s.<br>
|
||||
* This is a convenience class that implements methods for {@link FluidStack}s.
|
||||
*/
|
||||
public interface CombinedFluidTransferHandler extends CombinedTransferHandler<FluidStack>, FluidTransferHandler {
|
||||
}
|
||||
|
||||
@@ -1,35 +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.fluid.wrapper;
|
||||
|
||||
import dev.architectury.fluid.FluidStack;
|
||||
import dev.architectury.transfer.view.VariantView;
|
||||
|
||||
public interface FluidVariantView extends VariantView<FluidStack> {
|
||||
@Override
|
||||
default long getAmount(FluidStack resource) {
|
||||
return resource.getAmount();
|
||||
}
|
||||
|
||||
@Override
|
||||
default boolean isSameVariant(FluidStack first, FluidStack second) {
|
||||
return first.isFluidEqual(second) && first.isTagEqual(second);
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ package dev.architectury.transfer.item;
|
||||
import dev.architectury.injectables.annotations.ExpectPlatform;
|
||||
import dev.architectury.transfer.TransferHandler;
|
||||
import dev.architectury.transfer.access.BlockLookupAccess;
|
||||
import dev.architectury.transfer.access.PlatformLookup;
|
||||
import dev.architectury.transfer.item.wrapper.ContainerTransferHandler;
|
||||
import dev.architectury.transfer.item.wrapper.WorldlyContainerTransferHandler;
|
||||
import net.minecraft.core.Direction;
|
||||
@@ -31,20 +32,30 @@ import net.minecraft.world.item.ItemStack;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ItemTransfer {
|
||||
/**
|
||||
* A lookup access for item transfer handlers, the direction context is
|
||||
* only required on fabric.
|
||||
* <p>
|
||||
* This is the equivalent to getting the item transfer handler for a
|
||||
* block entity on Forge, or the storage with the lookup api on Fabric.
|
||||
* <p>
|
||||
* There are performance implications for using the architectury lookups,
|
||||
* please keep your implementations as simple as possible.
|
||||
*/
|
||||
public static final BlockLookupAccess<TransferHandler<ItemStack>, Direction> BLOCK = BlockLookupAccess.create();
|
||||
|
||||
static {
|
||||
init();
|
||||
PlatformLookup.attachBlock(BLOCK, platformBlockLookup(), ItemTransfer::wrap, (handler, direction) -> unwrap(handler));
|
||||
}
|
||||
|
||||
@ExpectPlatform
|
||||
private static void init() {
|
||||
private static Object platformBlockLookup() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps a platform-specific item transfer handler into the architectury transfer handler.
|
||||
* This accepts {@code IItemHandler} on Forge.
|
||||
* Wraps a platform-specific item transfer handler into the architectury transfer handler.<br><br>
|
||||
* This accepts {@code IItemHandler} on Forge.<br>
|
||||
* This accepts {@code Storage<ItemVariant>} on Fabric.
|
||||
*
|
||||
* @param object the handler to wrap
|
||||
@@ -64,4 +75,10 @@ public class ItemTransfer {
|
||||
|
||||
return new ContainerTransferHandler(container);
|
||||
}
|
||||
|
||||
@ExpectPlatform
|
||||
@Nullable
|
||||
public static Object unwrap(@Nullable TransferHandler<ItemStack> handler) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,16 +21,27 @@ package dev.architectury.transfer.item;
|
||||
|
||||
import dev.architectury.hooks.item.ItemStackHooks;
|
||||
import dev.architectury.transfer.TransferHandler;
|
||||
import dev.architectury.transfer.view.VariantView;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface ItemTransferHandler extends TransferHandler<ItemStack> {
|
||||
/**
|
||||
* This is a convenience class that implements methods for {@link ItemStack}s.
|
||||
*/
|
||||
public interface ItemTransferHandler extends ItemTransferView, TransferHandler<ItemStack>, VariantView<ItemStack> {
|
||||
@Override
|
||||
default ItemStack blank() {
|
||||
return ItemStack.EMPTY;
|
||||
default long getAmount(ItemStack resource) {
|
||||
return resource.getCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
default ItemStack copyWithAmount(ItemStack resource, long amount) {
|
||||
return ItemStackHooks.copyWithCount(resource, amount);
|
||||
@Nullable
|
||||
default Long getCapacityNullable(ItemStack resource) {
|
||||
return (long) resource.getMaxStackSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
default boolean isSameVariant(ItemStack first, ItemStack second) {
|
||||
return ItemStackHooks.isStackable(first, second);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,10 +20,10 @@
|
||||
package dev.architectury.transfer.item;
|
||||
|
||||
import dev.architectury.hooks.item.ItemStackHooks;
|
||||
import dev.architectury.transfer.ResourceView;
|
||||
import dev.architectury.transfer.TransferView;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
public interface ItemResourceView extends ResourceView<ItemStack> {
|
||||
public interface ItemTransferView extends TransferView<ItemStack> {
|
||||
@Override
|
||||
default ItemStack blank() {
|
||||
return ItemStack.EMPTY;
|
||||
@@ -1,36 +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.item.wrapper;
|
||||
|
||||
import dev.architectury.transfer.item.ItemTransferHandler;
|
||||
import dev.architectury.transfer.wrapper.single.BaseSingleTransferHandler;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
public interface BaseSingleItemTransferHandler extends BaseSingleTransferHandler<ItemStack>, ItemTransferHandler, ItemVariantView {
|
||||
@Override
|
||||
default ItemStack copy(ItemStack resource) {
|
||||
return resource.copy();
|
||||
}
|
||||
|
||||
@Override
|
||||
default long getCapacity(ItemStack resource) {
|
||||
return resource.getMaxStackSize();
|
||||
}
|
||||
}
|
||||
@@ -19,9 +19,14 @@
|
||||
|
||||
package dev.architectury.transfer.item.wrapper;
|
||||
|
||||
import dev.architectury.transfer.TransferHandler;
|
||||
import dev.architectury.transfer.item.ItemTransferHandler;
|
||||
import dev.architectury.transfer.wrapper.CombinedTransferHandler;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
public interface CombinedItemTransferHandler extends CombinedTransferHandler<ItemStack>, ItemTransferHandler, ItemVariantView {
|
||||
/**
|
||||
* A {@link TransferHandler} that combines multiple {@link TransferHandler}s.<br>
|
||||
* This is a convenience class that implements methods for {@link ItemStack}s.
|
||||
*/
|
||||
public interface CombinedItemTransferHandler extends CombinedTransferHandler<ItemStack>, ItemTransferHandler {
|
||||
}
|
||||
|
||||
@@ -19,26 +19,30 @@
|
||||
|
||||
package dev.architectury.transfer.item.wrapper;
|
||||
|
||||
import dev.architectury.transfer.TransferHandler;
|
||||
import dev.architectury.transfer.item.ItemTransferHandler;
|
||||
import dev.architectury.transfer.wrapper.CombinedSingleTransferHandler;
|
||||
import dev.architectury.transfer.wrapper.single.BaseSingleTransferHandler;
|
||||
import dev.architectury.transfer.wrapper.single.SingleTransferHandler;
|
||||
import net.minecraft.world.Container;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
import java.util.AbstractList;
|
||||
import java.util.List;
|
||||
|
||||
public class ContainerTransferHandler implements CombinedItemTransferHandler {
|
||||
public class ContainerTransferHandler implements CombinedItemTransferHandler, CombinedSingleTransferHandler<ItemStack> {
|
||||
protected final Container container;
|
||||
private Iterable<TransferHandler<ItemStack>> handlers = null;
|
||||
private List<SingleTransferHandler<ItemStack>> handlers = null;
|
||||
|
||||
public ContainerTransferHandler(Container container) {
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
protected Iterable<TransferHandler<ItemStack>> createHandlers() {
|
||||
protected List<SingleTransferHandler<ItemStack>> createHandlers() {
|
||||
return new Handlers();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<TransferHandler<ItemStack>> getHandlers() {
|
||||
public List<SingleTransferHandler<ItemStack>> getParts() {
|
||||
if (handlers == null) {
|
||||
handlers = createHandlers();
|
||||
}
|
||||
@@ -46,13 +50,13 @@ public class ContainerTransferHandler implements CombinedItemTransferHandler {
|
||||
return handlers;
|
||||
}
|
||||
|
||||
protected TransferHandler<ItemStack> asTransfer(int index) {
|
||||
protected SingleTransferHandler<ItemStack> asTransfer(int index) {
|
||||
return new SlotTransferHandler(container, index);
|
||||
}
|
||||
|
||||
protected class Handlers extends AbstractList<TransferHandler<ItemStack>> {
|
||||
protected class Handlers extends AbstractList<SingleTransferHandler<ItemStack>> {
|
||||
@Override
|
||||
public TransferHandler<ItemStack> get(int index) {
|
||||
public SingleTransferHandler<ItemStack> get(int index) {
|
||||
if (index < 0 || index >= size()) {
|
||||
throw new IndexOutOfBoundsException("Index " + index + " is out of bounds for size " + size());
|
||||
}
|
||||
@@ -65,7 +69,7 @@ public class ContainerTransferHandler implements CombinedItemTransferHandler {
|
||||
}
|
||||
}
|
||||
|
||||
protected static class SlotTransferHandler implements BaseSingleItemTransferHandler {
|
||||
protected static class SlotTransferHandler implements BaseSingleTransferHandler<ItemStack>, ItemTransferHandler {
|
||||
protected final Container container;
|
||||
protected final int index;
|
||||
|
||||
@@ -88,7 +92,7 @@ public class ContainerTransferHandler implements CombinedItemTransferHandler {
|
||||
public long getCapacity() {
|
||||
return Math.min(container.getMaxStackSize(), getResource().getMaxStackSize());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
|
||||
@@ -1,36 +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.item.wrapper;
|
||||
|
||||
import dev.architectury.hooks.item.ItemStackHooks;
|
||||
import dev.architectury.transfer.view.VariantView;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
public interface ItemVariantView extends VariantView<ItemStack> {
|
||||
@Override
|
||||
default long getAmount(ItemStack resource) {
|
||||
return resource.getCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
default boolean isSameVariant(ItemStack first, ItemStack second) {
|
||||
return ItemStackHooks.isStackable(first, second);
|
||||
}
|
||||
}
|
||||
@@ -19,13 +19,14 @@
|
||||
|
||||
package dev.architectury.transfer.item.wrapper;
|
||||
|
||||
import dev.architectury.transfer.TransferHandler;
|
||||
import dev.architectury.transfer.wrapper.FilteringTransferHandler;
|
||||
import dev.architectury.transfer.wrapper.single.FilteringSingleTransferHandler;
|
||||
import dev.architectury.transfer.wrapper.single.SingleTransferHandler;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.world.WorldlyContainer;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class WorldlyContainerTransferHandler extends ContainerTransferHandler {
|
||||
protected final Direction direction;
|
||||
@@ -36,13 +37,13 @@ public class WorldlyContainerTransferHandler extends ContainerTransferHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Iterable<TransferHandler<ItemStack>> createHandlers() {
|
||||
protected List<SingleTransferHandler<ItemStack>> createHandlers() {
|
||||
WorldlyContainer container = (WorldlyContainer) this.container;
|
||||
int[] slots = container.getSlotsForFace(this.direction);
|
||||
TransferHandler<ItemStack>[] handlers = new TransferHandler[slots.length];
|
||||
SingleTransferHandler<ItemStack>[] handlers = new SingleTransferHandler[slots.length];
|
||||
for (int i = 0; i < slots.length; ++i) {
|
||||
int index = i;
|
||||
handlers[i] = FilteringTransferHandler.of(new SlotTransferHandler(container, slots[i]),
|
||||
handlers[i] = FilteringSingleTransferHandler.of(new SlotTransferHandler(container, slots[i]),
|
||||
stack -> container.canPlaceItemThroughFace(index, stack, direction),
|
||||
stack -> container.canTakeItemThroughFace(index, stack, direction));
|
||||
}
|
||||
|
||||
@@ -19,8 +19,15 @@
|
||||
|
||||
package dev.architectury.transfer.view;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface VariantView<T> {
|
||||
long getAmount(T resource);
|
||||
|
||||
@Nullable
|
||||
default Long getCapacityNullable(T resource) {
|
||||
return null;
|
||||
}
|
||||
|
||||
boolean isSameVariant(T first, T second);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import dev.architectury.transfer.ResourceView;
|
||||
import dev.architectury.transfer.TransferHandler;
|
||||
import dev.architectury.transfer.wrapper.single.SingleTransferHandler;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* A {@link TransferHandler} that combines multiple {@link SingleTransferHandler}s.<br>
|
||||
* This is faster than using {@link CombinedTransferHandler} directly, as the size of
|
||||
* each {@link SingleTransferHandler} is known in advance.
|
||||
*
|
||||
* @param <T> the type of resource
|
||||
*/
|
||||
public interface CombinedSingleTransferHandler<T> extends CombinedTransferHandler<T> {
|
||||
@Override
|
||||
default Iterable<TransferHandler<T>> getHandlers() {
|
||||
return (Iterable<TransferHandler<T>>) (Iterable<? super SingleTransferHandler<T>>) getParts();
|
||||
}
|
||||
|
||||
List<SingleTransferHandler<T>> getParts();
|
||||
|
||||
@Override
|
||||
default Stream<ResourceView<T>> getContents() {
|
||||
return (Stream<ResourceView<T>>) (Stream<? super SingleTransferHandler<T>>) getParts().stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
default int getContentsSize() {
|
||||
return getParts().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
default ResourceView<T> getContent(int index) {
|
||||
return getParts().get(index);
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,11 @@ import java.util.Collection;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* A {@link TransferHandler} that combines multiple {@link TransferHandler}s.
|
||||
*
|
||||
* @param <T> the type of resource
|
||||
*/
|
||||
public interface CombinedTransferHandler<T> extends TransferHandler<T>, VariantView<T> {
|
||||
Iterable<TransferHandler<T>> getHandlers();
|
||||
|
||||
@@ -38,6 +43,7 @@ public interface CombinedTransferHandler<T> extends TransferHandler<T>, VariantV
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
default int getContentsSize() {
|
||||
int size = 0;
|
||||
for (TransferHandler<T> handler : getHandlers()) {
|
||||
@@ -47,6 +53,7 @@ public interface CombinedTransferHandler<T> extends TransferHandler<T>, VariantV
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
default ResourceView<T> getContent(int index) {
|
||||
if (index < 0) {
|
||||
throw new IllegalArgumentException("Index must be non-negative");
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
package dev.architectury.transfer.wrapper.single;
|
||||
|
||||
import dev.architectury.transfer.TransferAction;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface BaseSingleTransferHandler<T> extends SingleTransferHandler<T> {
|
||||
@Override
|
||||
@@ -34,9 +35,9 @@ public interface BaseSingleTransferHandler<T> extends SingleTransferHandler<T> {
|
||||
|
||||
void setResource(T resource);
|
||||
|
||||
T copy(T resource);
|
||||
|
||||
long getCapacity(T resource);
|
||||
default T copy(T resource) {
|
||||
return copyWithAmount(resource, getAmount(resource));
|
||||
}
|
||||
|
||||
@Override
|
||||
default long insert(T toInsert, TransferAction action) {
|
||||
@@ -44,8 +45,10 @@ public interface BaseSingleTransferHandler<T> extends SingleTransferHandler<T> {
|
||||
long currentAmount = getAmount(resource);
|
||||
boolean isEmpty = currentAmount <= 0;
|
||||
if ((isEmpty || isSameVariant(resource, toInsert)) && canInsert(toInsert)) {
|
||||
long slotSpace = isEmpty ? getCapacity(toInsert) : getCapacity() - currentAmount;
|
||||
long inserted = Math.min(slotSpace, getAmount(toInsert));
|
||||
@Nullable
|
||||
Long slotSpace = isEmpty ? getCapacityNullable(toInsert) : Long.valueOf(getCapacity() - currentAmount);
|
||||
long toInsertAmount = getAmount(toInsert);
|
||||
long inserted = slotSpace == null ? toInsertAmount : Math.min(slotSpace, toInsertAmount);
|
||||
|
||||
if (inserted > 0) {
|
||||
if (isEmpty) {
|
||||
|
||||
@@ -24,16 +24,42 @@ import dev.architectury.transfer.TransferAction;
|
||||
import dev.architectury.transfer.wrapper.ForwardingTransferHandler;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public interface ForwardingSingleTransferHandler<T> extends ForwardingTransferHandler<T>, ResourceView<T> {
|
||||
public interface ForwardingSingleTransferHandler<T> extends SingleTransferHandler<T>, ForwardingTransferHandler<T>, ResourceView<T> {
|
||||
@Override
|
||||
SingleTransferHandler<T> forwardingTo();
|
||||
|
||||
@Override
|
||||
default Stream<ResourceView<T>> getContents() {
|
||||
return forwardingTo().getContents();
|
||||
}
|
||||
|
||||
@Override
|
||||
default int getContentsSize() {
|
||||
return forwardingTo().getContentsSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
default ResourceView<T> getContent(int index) {
|
||||
return forwardingTo().getContent(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
default T extract(Predicate<T> toExtract, long maxAmount, TransferAction action) {
|
||||
return ForwardingTransferHandler.super.extract(toExtract, maxAmount, action);
|
||||
}
|
||||
|
||||
@Override
|
||||
default long getAmount(T resource) {
|
||||
return forwardingTo().getAmount(resource);
|
||||
}
|
||||
|
||||
@Override
|
||||
default boolean isSameVariant(T first, T second) {
|
||||
return forwardingTo().isSameVariant(first, second);
|
||||
}
|
||||
|
||||
@Override
|
||||
default T getResource() {
|
||||
return forwardingTo().getResource();
|
||||
|
||||
@@ -26,6 +26,13 @@ import dev.architectury.transfer.view.VariantView;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* A {@link TransferHandler} that only has one slot, this is also
|
||||
* an implementation of {@link ResourceView}.
|
||||
*
|
||||
* @param <T> the type of resource
|
||||
* @see BaseSingleTransferHandler for a simple implementation
|
||||
*/
|
||||
public interface SingleTransferHandler<T> extends TransferHandler<T>, ResourceView<T>, ModifiableView<T>, VariantView<T> {
|
||||
@Override
|
||||
default Stream<ResourceView<T>> getContents() {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.access.fabric;
|
||||
|
||||
import dev.architectury.transfer.access.BlockLookupAccess;
|
||||
import dev.architectury.transfer.fabric.BlockApiLookupWrapper;
|
||||
import dev.architectury.transfer.fabric.FabricBlockLookupRegistration;
|
||||
import net.fabricmc.fabric.api.lookup.v1.block.BlockApiLookup;
|
||||
import net.minecraft.core.Direction;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class PlatformLookupImpl {
|
||||
public static <O, T> void attachBlockQuery(BlockLookupAccess<T, Direction> access, Object lookup, Function<O, T> wrapper) {
|
||||
if (!(lookup instanceof BlockApiLookup)) {
|
||||
throw new IllegalArgumentException("Lookup must be an instance of BlockApiLookup on Fabric!");
|
||||
}
|
||||
access.addQueryHandler(new BlockApiLookupWrapper<>((BlockApiLookup<O, Direction>) lookup, wrapper));
|
||||
}
|
||||
|
||||
public static <O, T> void attachBlockRegistration(BlockLookupAccess<T, Direction> access, Object lookup, BiFunction<T, Direction, O> unwrapper) {
|
||||
if (!(lookup instanceof BlockApiLookup)) {
|
||||
throw new IllegalArgumentException("Lookup must be an instance of BlockApiLookup on Fabric!");
|
||||
}
|
||||
access.addRegistrationHandler(FabricBlockLookupRegistration.create((BlockApiLookup<O, Direction>) lookup, unwrapper));
|
||||
}
|
||||
}
|
||||
@@ -20,10 +20,7 @@
|
||||
package dev.architectury.transfer.energy.fabric;
|
||||
|
||||
import dev.architectury.transfer.TransferAction;
|
||||
import dev.architectury.transfer.energy.EnergyTransfer;
|
||||
import dev.architectury.transfer.energy.EnergyTransferHandler;
|
||||
import dev.architectury.transfer.fabric.BlockApiLookupWrapper;
|
||||
import dev.architectury.transfer.fabric.FabricBlockLookupRegistration;
|
||||
import dev.architectury.transfer.wrapper.single.SingleTransferHandler;
|
||||
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;
|
||||
import net.fabricmc.fabric.api.transfer.v1.transaction.TransactionContext;
|
||||
@@ -44,7 +41,7 @@ public class EnergyTransferImpl {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static EnergyStorage unwrap(@Nullable SingleTransferHandler<Long> handler) {
|
||||
public static Object unwrap(@Nullable SingleTransferHandler<Long> handler) {
|
||||
if (handler == null) return null;
|
||||
|
||||
if (handler instanceof FabricTransferHandler) {
|
||||
@@ -54,9 +51,8 @@ public class EnergyTransferImpl {
|
||||
}
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
EnergyTransfer.BLOCK.addQueryHandler(new BlockApiLookupWrapper<>(EnergyStorage.SIDED, EnergyTransferImpl::wrap));
|
||||
EnergyTransfer.BLOCK.addRegistrationHandler(FabricBlockLookupRegistration.create(EnergyStorage.SIDED, EnergyTransferImpl::unwrap));
|
||||
public static Object platformBlockLookup() {
|
||||
return EnergyStorage.SIDED;
|
||||
}
|
||||
|
||||
private static class FabricTransferHandler implements EnergyTransferHandler {
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
|
||||
package dev.architectury.transfer.fabric;
|
||||
|
||||
import dev.architectury.transfer.TransferHandler;
|
||||
import dev.architectury.transfer.access.BlockLookup;
|
||||
import net.fabricmc.fabric.api.lookup.v1.block.BlockApiLookup;
|
||||
import net.minecraft.core.BlockPos;
|
||||
@@ -30,7 +29,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class BlockApiLookupWrapper<T, F, C, H extends TransferHandler<T>> implements BlockLookup<H, C> {
|
||||
public class BlockApiLookupWrapper<F, C, H> implements BlockLookup<H, C> {
|
||||
private final BlockApiLookup<F, C> lookup;
|
||||
private final Function<@Nullable F, @Nullable H> wrapper;
|
||||
|
||||
|
||||
@@ -27,18 +27,19 @@ import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class FabricBlockLookupRegistration<T, A, Context> implements BlockLookupRegistration<T, Context> {
|
||||
private final Function<T, A> unwrapper;
|
||||
private final BiFunction<T, Context, A> unwrapper;
|
||||
private final BlockApiLookup<A, Context> lookup;
|
||||
|
||||
public static <T, A, Context> FabricBlockLookupRegistration<T, A, Context> create(BlockApiLookup<A, Context> lookup, Function<T, A> unwrapper) {
|
||||
public static <T, A, Context> FabricBlockLookupRegistration<T, A, Context> create(BlockApiLookup<A, Context> lookup, BiFunction<T, Context, A> unwrapper) {
|
||||
return new FabricBlockLookupRegistration<>(unwrapper, lookup);
|
||||
}
|
||||
|
||||
private FabricBlockLookupRegistration(Function<T, A> unwrapper, BlockApiLookup<A, Context> lookup) {
|
||||
private FabricBlockLookupRegistration(BiFunction<T, Context, A> unwrapper, BlockApiLookup<A, Context> lookup) {
|
||||
this.unwrapper = unwrapper;
|
||||
this.lookup = lookup;
|
||||
}
|
||||
@@ -47,7 +48,7 @@ public class FabricBlockLookupRegistration<T, A, Context> implements BlockLookup
|
||||
return (level, pos, state, blockEntity, context) -> {
|
||||
Function<Context, T> function = provider.get(level, pos, state, blockEntity);
|
||||
if (function != null) {
|
||||
return unwrapper.apply(function.apply(context));
|
||||
return unwrapper.apply(function.apply(context), context);
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -63,7 +64,7 @@ public class FabricBlockLookupRegistration<T, A, Context> implements BlockLookup
|
||||
|
||||
Function<Context, T> function = provider.get(blockEntity.getLevel(), blockEntity.getBlockPos(), blockEntity.getBlockState(), (B) blockEntity);
|
||||
if (function != null) {
|
||||
return unwrapper.apply(function.apply(context));
|
||||
return unwrapper.apply(function.apply(context), context);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -22,11 +22,8 @@ package dev.architectury.transfer.fluid.fabric;
|
||||
import dev.architectury.fluid.FluidStack;
|
||||
import dev.architectury.hooks.fluid.fabric.FluidStackHooksFabric;
|
||||
import dev.architectury.transfer.TransferHandler;
|
||||
import dev.architectury.transfer.fabric.BlockApiLookupWrapper;
|
||||
import dev.architectury.transfer.fabric.FabricBlockLookupRegistration;
|
||||
import dev.architectury.transfer.fabric.FabricStorageTransferHandler;
|
||||
import dev.architectury.transfer.fabric.TransferHandlerStorage;
|
||||
import dev.architectury.transfer.fluid.FluidTransfer;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidStorage;
|
||||
import net.fabricmc.fabric.api.transfer.v1.fluid.FluidVariant;
|
||||
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
|
||||
@@ -58,7 +55,7 @@ public class FluidTransferImpl {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Storage<FluidVariant> unwrap(@Nullable TransferHandler<FluidStack> handler) {
|
||||
public static Object unwrap(@Nullable TransferHandler<FluidStack> handler) {
|
||||
if (handler == null) return null;
|
||||
|
||||
if (handler instanceof FabricStorageTransferHandler) {
|
||||
@@ -68,11 +65,7 @@ public class FluidTransferImpl {
|
||||
}
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
FluidTransfer.BLOCK.addQueryHandler(new BlockApiLookupWrapper<>(FluidStorage.SIDED, FluidTransferImpl::wrap));
|
||||
FluidTransfer.BLOCK.addRegistrationHandler(FabricBlockLookupRegistration.create(FluidStorage.SIDED, FluidTransferImpl::unwrap));
|
||||
// FluidTransfer.ITEM.addQueryHandler((stack, context) -> {
|
||||
// return wrap(FluidStorage.ITEM.find(stack, fromTransfer(stack, context)));
|
||||
// });
|
||||
public static Object platformBlockLookup() {
|
||||
return FluidStorage.SIDED;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,11 +21,8 @@ package dev.architectury.transfer.item.fabric;
|
||||
|
||||
import dev.architectury.hooks.item.ItemStackHooks;
|
||||
import dev.architectury.transfer.TransferHandler;
|
||||
import dev.architectury.transfer.fabric.BlockApiLookupWrapper;
|
||||
import dev.architectury.transfer.fabric.FabricBlockLookupRegistration;
|
||||
import dev.architectury.transfer.fabric.FabricStorageTransferHandler;
|
||||
import dev.architectury.transfer.fabric.TransferHandlerStorage;
|
||||
import dev.architectury.transfer.item.ItemTransfer;
|
||||
import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage;
|
||||
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
|
||||
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
|
||||
@@ -60,7 +57,7 @@ public class ItemTransferImpl {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Storage<ItemVariant> unwrap(@Nullable TransferHandler<ItemStack> handler) {
|
||||
public static Object unwrap(@Nullable TransferHandler<ItemStack> handler) {
|
||||
if (handler == null) return null;
|
||||
|
||||
if (handler instanceof FabricStorageTransferHandler) {
|
||||
@@ -70,8 +67,7 @@ public class ItemTransferImpl {
|
||||
}
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
ItemTransfer.BLOCK.addQueryHandler(new BlockApiLookupWrapper<>(ItemStorage.SIDED, ItemTransferImpl::wrap));
|
||||
ItemTransfer.BLOCK.addRegistrationHandler(FabricBlockLookupRegistration.create(ItemStorage.SIDED, ItemTransferImpl::unwrap));
|
||||
public static Object platformBlockLookup() {
|
||||
return ItemStorage.SIDED;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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.access.forge;
|
||||
|
||||
import dev.architectury.transfer.access.BlockLookup;
|
||||
import dev.architectury.transfer.access.BlockLookupAccess;
|
||||
import dev.architectury.transfer.forge.ForgeBlockLookupRegistration;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class PlatformLookupImpl {
|
||||
public static <O, T> void attachBlockQuery(BlockLookupAccess<T, Direction> access, Object lookup, Function<O, T> wrapper) {
|
||||
if (!(lookup instanceof Capability)) {
|
||||
throw new IllegalArgumentException("Lookup must be an instance of Capability on Forge!");
|
||||
}
|
||||
access.addQueryHandler(instantiateBlockLookup((Capability<O>) lookup, wrapper));
|
||||
}
|
||||
|
||||
public static <O, T> void attachBlockRegistration(BlockLookupAccess<T, Direction> access, Object lookup, BiFunction<T, Direction, O> unwrapper) {
|
||||
if (!(lookup instanceof Capability)) {
|
||||
throw new IllegalArgumentException("Lookup must be an instance of Capability on Forge!");
|
||||
}
|
||||
access.addRegistrationHandler(ForgeBlockLookupRegistration.create((Capability<O>) lookup,
|
||||
(level, pos, state, blockEntity) -> unwrapper));
|
||||
}
|
||||
|
||||
public static <O, T> BlockLookup<T, Direction> instantiateBlockLookup(Capability<O> capability, Function<O, T> wrapper) {
|
||||
return new BlockLookup<T, Direction>() {
|
||||
@Override
|
||||
@Nullable
|
||||
public T get(Level level, BlockPos pos, Direction direction) {
|
||||
return get(level, pos, level.getBlockState(pos), null, direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public T get(Level level, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, Direction direction) {
|
||||
Block block = state.getBlock();
|
||||
O handler = null;
|
||||
if (state.hasBlockEntity()) {
|
||||
if (blockEntity == null) {
|
||||
blockEntity = level.getBlockEntity(pos);
|
||||
}
|
||||
if (blockEntity != null) {
|
||||
handler = blockEntity.getCapability(capability, direction).resolve().orElse(null);
|
||||
}
|
||||
}
|
||||
return wrapper.apply(handler);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -20,17 +20,8 @@
|
||||
package dev.architectury.transfer.energy.forge;
|
||||
|
||||
import dev.architectury.transfer.TransferAction;
|
||||
import dev.architectury.transfer.access.BlockLookup;
|
||||
import dev.architectury.transfer.energy.EnergyTransfer;
|
||||
import dev.architectury.transfer.energy.EnergyTransferHandler;
|
||||
import dev.architectury.transfer.forge.ForgeBlockLookupRegistration;
|
||||
import dev.architectury.transfer.wrapper.single.SingleTransferHandler;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraftforge.energy.CapabilityEnergy;
|
||||
import net.minecraftforge.energy.IEnergyStorage;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -47,36 +38,19 @@ public class EnergyTransferImpl {
|
||||
}
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
EnergyTransfer.BLOCK.addQueryHandler(instantiateBlockLookup());
|
||||
EnergyTransfer.BLOCK.addRegistrationHandler(ForgeBlockLookupRegistration.create(CapabilityEnergy.ENERGY,
|
||||
(level, pos, state, blockEntity) -> (direction, handler) -> new ArchEnergyStorage(handler)));
|
||||
@Nullable
|
||||
private static Object unwrap(@Nullable SingleTransferHandler<Long> handler) {
|
||||
if (handler == null) return null;
|
||||
|
||||
if (handler instanceof ForgeTransferHandler) {
|
||||
return ((ForgeTransferHandler) handler).getStorage();
|
||||
} else {
|
||||
return new ArchEnergyStorage(handler);
|
||||
}
|
||||
}
|
||||
|
||||
public static BlockLookup<SingleTransferHandler<Long>, Direction> instantiateBlockLookup() {
|
||||
return new BlockLookup<SingleTransferHandler<Long>, Direction>() {
|
||||
@Override
|
||||
@Nullable
|
||||
public SingleTransferHandler<Long> get(Level level, BlockPos pos, Direction direction) {
|
||||
return get(level, pos, level.getBlockState(pos), null, direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public SingleTransferHandler<Long> get(Level level, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, Direction direction) {
|
||||
Block block = state.getBlock();
|
||||
IEnergyStorage handler = null;
|
||||
if (state.hasBlockEntity()) {
|
||||
if (blockEntity == null) {
|
||||
blockEntity = level.getBlockEntity(pos);
|
||||
}
|
||||
if (blockEntity != null) {
|
||||
handler = blockEntity.getCapability(CapabilityEnergy.ENERGY, direction).resolve().orElse(null);
|
||||
}
|
||||
}
|
||||
return wrap(handler);
|
||||
}
|
||||
};
|
||||
public static Object platformBlockLookup() {
|
||||
return CapabilityEnergy.ENERGY;
|
||||
}
|
||||
|
||||
private static class ForgeTransferHandler implements EnergyTransferHandler {
|
||||
@@ -86,6 +60,10 @@ public class EnergyTransferImpl {
|
||||
this.storage = storage;
|
||||
}
|
||||
|
||||
public IEnergyStorage getStorage() {
|
||||
return storage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getResource() {
|
||||
return (long) storage.getEnergyStored();
|
||||
|
||||
@@ -27,10 +27,9 @@ import dev.architectury.transfer.TransferAction;
|
||||
import dev.architectury.transfer.TransferHandler;
|
||||
import dev.architectury.transfer.access.BlockLookup;
|
||||
import dev.architectury.transfer.access.ItemLookup;
|
||||
import dev.architectury.transfer.fluid.FluidResourceView;
|
||||
import dev.architectury.transfer.fluid.FluidTransfer;
|
||||
import dev.architectury.transfer.fluid.FluidTransferHandler;
|
||||
import dev.architectury.transfer.forge.ForgeBlockLookupRegistration;
|
||||
import dev.architectury.transfer.fluid.FluidTransferView;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
@@ -51,11 +50,9 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Objects;
|
||||
import java.util.Spliterators;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
public class FluidTransferImpl {
|
||||
@Nullable
|
||||
@@ -69,13 +66,20 @@ public class FluidTransferImpl {
|
||||
}
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
@Nullable
|
||||
public static Object unwrap(@Nullable TransferHandler<FluidStack> handler) {
|
||||
if (handler == null) return null;
|
||||
|
||||
if (handler instanceof ForgeTransferHandler) {
|
||||
return ((ForgeTransferHandler) handler).getHandler();
|
||||
} else {
|
||||
return new ArchFluidHandler(handler);
|
||||
}
|
||||
}
|
||||
|
||||
public static Object platformBlockLookup() {
|
||||
FluidTransfer.BLOCK.addQueryHandler(instantiateBlockLookup());
|
||||
FluidTransfer.BLOCK.addRegistrationHandler(ForgeBlockLookupRegistration.create(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY,
|
||||
(level, pos, state, blockEntity) -> (direction, handler) -> new ArchFluidHandler(handler)));
|
||||
// FluidTransfer.ITEM.addQueryHandler(instantiateItemLookup());
|
||||
// FluidTransfer.ITEM.addRegistrationHandler(ForgeItemLookupRegistration.create(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY,
|
||||
// stack -> (direction, handler) -> new ArchFluidHandlerItem(handler, stack)));
|
||||
return CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY;
|
||||
}
|
||||
|
||||
public static BlockLookup<TransferHandler<FluidStack>, Direction> instantiateBlockLookup() {
|
||||
@@ -95,13 +99,6 @@ public class FluidTransferImpl {
|
||||
handler = new FluidBlockWrapper((IFluidBlock) block, level, pos);
|
||||
} else if (block instanceof BucketPickup) {
|
||||
handler = new BucketPickupHandlerWrapper((BucketPickup) block, level, pos);
|
||||
} else if (state.hasBlockEntity()) {
|
||||
if (blockEntity == null) {
|
||||
blockEntity = level.getBlockEntity(pos);
|
||||
}
|
||||
if (blockEntity != null) {
|
||||
handler = blockEntity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, direction).resolve().orElse(null);
|
||||
}
|
||||
}
|
||||
return wrap(handler);
|
||||
}
|
||||
@@ -120,6 +117,7 @@ public class FluidTransferImpl {
|
||||
|
||||
public static class ArchFluidHandler implements IFluidHandler {
|
||||
private static final Predicate<FluidStack> TRUE = stack -> true;
|
||||
|
||||
private TransferHandler<FluidStack> handler;
|
||||
|
||||
public ArchFluidHandler(TransferHandler<FluidStack> handler) {
|
||||
@@ -195,6 +193,10 @@ public class FluidTransferImpl {
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
public IFluidHandler getHandler() {
|
||||
return handler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<ResourceView<FluidStack>> getContents() {
|
||||
return Streams.stream(new Itr());
|
||||
@@ -281,7 +283,7 @@ public class FluidTransferImpl {
|
||||
}
|
||||
}
|
||||
|
||||
private class ForgeResourceView implements FluidResourceView {
|
||||
private class ForgeResourceView implements ResourceView<FluidStack>, FluidTransferView {
|
||||
int index;
|
||||
|
||||
public ForgeResourceView(int index) {
|
||||
|
||||
@@ -39,7 +39,7 @@ import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
|
||||
public interface ForgeBlockLookupRegistration<T, Cap> extends BlockLookupRegistration<T, Direction> {
|
||||
static <T, Cap> ForgeBlockLookupRegistration<T, Cap> create(Capability<Cap> capability, BlockAccessProvider<BiFunction<Direction, T, Cap>, BlockEntity> transformer) {
|
||||
static <T, Cap> ForgeBlockLookupRegistration<T, Cap> create(Capability<Cap> capability, BlockAccessProvider<BiFunction<T, Direction, Cap>, BlockEntity> transformer) {
|
||||
return new ForgeBlockLookupRegistration<T, Cap>() {
|
||||
@Override
|
||||
public Capability<Cap> getCapability() {
|
||||
@@ -48,7 +48,7 @@ public interface ForgeBlockLookupRegistration<T, Cap> extends BlockLookupRegistr
|
||||
|
||||
@Override
|
||||
public Cap from(Level level, BlockPos blockPos, BlockState blockState, BlockEntity blockEntity, Direction direction, T handler) {
|
||||
return transformer.get(level, blockPos, blockState, blockEntity).apply(direction, handler);
|
||||
return transformer.get(level, blockPos, blockState, blockEntity).apply(handler, direction);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -24,10 +24,8 @@ import dev.architectury.transfer.ResourceView;
|
||||
import dev.architectury.transfer.TransferAction;
|
||||
import dev.architectury.transfer.TransferHandler;
|
||||
import dev.architectury.transfer.access.BlockLookup;
|
||||
import dev.architectury.transfer.forge.ForgeBlockLookupRegistration;
|
||||
import dev.architectury.transfer.item.ItemResourceView;
|
||||
import dev.architectury.transfer.item.ItemTransfer;
|
||||
import dev.architectury.transfer.item.ItemTransferHandler;
|
||||
import dev.architectury.transfer.item.ItemTransferView;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
@@ -58,10 +56,19 @@ public class ItemTransferImpl {
|
||||
}
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
ItemTransfer.BLOCK.addQueryHandler(instantiateBlockLookup());
|
||||
ItemTransfer.BLOCK.addRegistrationHandler(ForgeBlockLookupRegistration.create(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY,
|
||||
(level, pos, state, blockEntity) -> (direction, handler) -> new ArchItemHandler(handler)));
|
||||
@Nullable
|
||||
public static Object unwrap(@Nullable TransferHandler<ItemStack> handler) {
|
||||
if (handler == null) return null;
|
||||
|
||||
if (handler instanceof ForgeTransferHandler) {
|
||||
return ((ForgeTransferHandler) handler).getHandler();
|
||||
} else {
|
||||
return new ArchItemHandler(handler);
|
||||
}
|
||||
}
|
||||
|
||||
public static Object platformBlockLookup() {
|
||||
return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY;
|
||||
}
|
||||
|
||||
public static BlockLookup<TransferHandler<ItemStack>, Direction> instantiateBlockLookup() {
|
||||
@@ -133,7 +140,7 @@ public class ItemTransferImpl {
|
||||
@Override
|
||||
public boolean isItemValid(int index, @NotNull ItemStack stack) {
|
||||
ItemStack content;
|
||||
|
||||
|
||||
try (var resource = handler.getContent(index)) {
|
||||
content = resource.getResource();
|
||||
}
|
||||
@@ -148,6 +155,10 @@ public class ItemTransferImpl {
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
public IItemHandler getHandler() {
|
||||
return handler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<ResourceView<ItemStack>> getContents() {
|
||||
return IntStream.range(0, handler.getSlots()).mapToObj(ForgeResourceView::new);
|
||||
@@ -224,7 +235,7 @@ public class ItemTransferImpl {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private class ForgeResourceView implements ItemResourceView {
|
||||
private class ForgeResourceView implements ResourceView<ItemStack>, ItemTransferView {
|
||||
int index;
|
||||
|
||||
public ForgeResourceView(int index) {
|
||||
|
||||
Reference in New Issue
Block a user