diff --git a/common/src/main/java/dev/architectury/transfer/TransferHandler.java b/common/src/main/java/dev/architectury/transfer/TransferHandler.java index e6e6e663..d13ae29b 100644 --- a/common/src/main/java/dev/architectury/transfer/TransferHandler.java +++ b/common/src/main/java/dev/architectury/transfer/TransferHandler.java @@ -77,10 +77,10 @@ public interface TransferHandler extends TransferView { /** * Returns the resource in a particular index. * This may be extremely expensive to compute, avoid if you can.
- * Please properly close this stream. Failure to do so will result in a potential + * Please properly close this resource. Failure to do so will result in a potential * crash in conflicting transactions.

* 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 diff --git a/common/src/main/java/dev/architectury/transfer/ApiLookupAccess.java b/common/src/main/java/dev/architectury/transfer/access/ApiLookupAccess.java similarity index 97% rename from common/src/main/java/dev/architectury/transfer/ApiLookupAccess.java rename to common/src/main/java/dev/architectury/transfer/access/ApiLookupAccess.java index 36110a33..5a908214 100644 --- a/common/src/main/java/dev/architectury/transfer/ApiLookupAccess.java +++ b/common/src/main/java/dev/architectury/transfer/access/ApiLookupAccess.java @@ -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. diff --git a/common/src/main/java/dev/architectury/transfer/access/BlockLookupAccess.java b/common/src/main/java/dev/architectury/transfer/access/BlockLookupAccess.java index 78de182a..2c1a68b0 100644 --- a/common/src/main/java/dev/architectury/transfer/access/BlockLookupAccess.java +++ b/common/src/main/java/dev/architectury/transfer/access/BlockLookupAccess.java @@ -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. diff --git a/common/src/main/java/dev/architectury/transfer/access/ItemLookupAccess.java b/common/src/main/java/dev/architectury/transfer/access/ItemLookupAccess.java index 29ce7b45..d1c5c934 100644 --- a/common/src/main/java/dev/architectury/transfer/access/ItemLookupAccess.java +++ b/common/src/main/java/dev/architectury/transfer/access/ItemLookupAccess.java @@ -20,7 +20,6 @@ package dev.architectury.transfer.access; import dev.architectury.impl.transfer.access.ItemLookupAccessImpl; -import dev.architectury.transfer.ApiLookupAccess; public interface ItemLookupAccess extends ApiLookupAccess, ItemLookupRegistration>, ItemLookup, ItemLookupRegistration { static ItemLookupAccess create() { diff --git a/common/src/main/java/dev/architectury/transfer/access/PlatformLookup.java b/common/src/main/java/dev/architectury/transfer/access/PlatformLookup.java new file mode 100644 index 00000000..4d5fcb71 --- /dev/null +++ b/common/src/main/java/dev/architectury/transfer/access/PlatformLookup.java @@ -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 void attachBlock(BlockLookupAccess access, Object lookup, Function wrapper, BiFunction unwrapper) { + attachBlockQuery(access, lookup, wrapper); + attachBlockRegistration(access, lookup, unwrapper); + } + + /** + * Attaches a block query to the given lookup.

+ * Lookup accepts {@code Capability} on Forge.
+ * Lookup accepts {@code BlockApiLookup} on Fabric. + *

+ * This method allows the lookup to read 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 the platform object type + * @param the abstracted object type + */ + @ExpectPlatform + public static void attachBlockQuery(BlockLookupAccess access, Object lookup, Function wrapper) { + throw new AssertionError(); + } + + /** + * Attaches a block registration handler to the given lookup.

+ * Lookup accepts {@code Capability} on Forge.
+ * Lookup accepts {@code BlockApiLookup} on Fabric. + *

+ * 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 the platform object type + * @param the abstracted object type + */ + @ExpectPlatform + public static void attachBlockRegistration(BlockLookupAccess access, Object lookup, BiFunction unwrapper) { + throw new AssertionError(); + } +} diff --git a/common/src/main/java/dev/architectury/transfer/energy/EnergyTransfer.java b/common/src/main/java/dev/architectury/transfer/energy/EnergyTransfer.java index ba8fc301..f9c92f8c 100644 --- a/common/src/main/java/dev/architectury/transfer/energy/EnergyTransfer.java +++ b/common/src/main/java/dev/architectury/transfer/energy/EnergyTransfer.java @@ -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, 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.

+ * This accepts {@code IEnergyStorage} on Forge.
* This accepts {@code EnergyStorage} on Fabric. * * @param object the handler to wrap @@ -52,4 +52,10 @@ public class EnergyTransfer { public static SingleTransferHandler wrap(@Nullable Object object) { throw new AssertionError(); } + + @ExpectPlatform + @Nullable + public static Object unwrap(@Nullable SingleTransferHandler handler) { + throw new AssertionError(); + } } diff --git a/common/src/main/java/dev/architectury/transfer/fluid/FluidTransfer.java b/common/src/main/java/dev/architectury/transfer/fluid/FluidTransfer.java index 1e93a2be..f62d7938 100644 --- a/common/src/main/java/dev/architectury/transfer/fluid/FluidTransfer.java +++ b/common/src/main/java/dev/architectury/transfer/fluid/FluidTransfer.java @@ -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> 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.

+ * This accepts {@code IFluidHandler} on Forge.
* This accepts {@code Storage} on Fabric. * * @param object the handler to wrap @@ -68,4 +64,10 @@ public class FluidTransfer { public static TransferHandler wrap(@Nullable Object object) { throw new AssertionError(); } + + @ExpectPlatform + @Nullable + public static Object unwrap(@Nullable TransferHandler handler) { + throw new AssertionError(); + } } diff --git a/common/src/main/java/dev/architectury/transfer/fluid/FluidTransferHandler.java b/common/src/main/java/dev/architectury/transfer/fluid/FluidTransferHandler.java index 78517dc4..134ce36e 100644 --- a/common/src/main/java/dev/architectury/transfer/fluid/FluidTransferHandler.java +++ b/common/src/main/java/dev/architectury/transfer/fluid/FluidTransferHandler.java @@ -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 { +/** + * This is a convenience class that implements methods for {@link FluidStack}s. + */ +public interface FluidTransferHandler extends FluidTransferView, TransferHandler, VariantView { @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); } } diff --git a/common/src/main/java/dev/architectury/transfer/fluid/FluidResourceView.java b/common/src/main/java/dev/architectury/transfer/fluid/FluidTransferView.java similarity index 91% rename from common/src/main/java/dev/architectury/transfer/fluid/FluidResourceView.java rename to common/src/main/java/dev/architectury/transfer/fluid/FluidTransferView.java index 7b440313..50eaaad5 100644 --- a/common/src/main/java/dev/architectury/transfer/fluid/FluidResourceView.java +++ b/common/src/main/java/dev/architectury/transfer/fluid/FluidTransferView.java @@ -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 { +public interface FluidTransferView extends TransferView { @Override default FluidStack blank() { return FluidStack.empty(); diff --git a/common/src/main/java/dev/architectury/transfer/fluid/wrapper/BaseSingleFluidTransferHandler.java b/common/src/main/java/dev/architectury/transfer/fluid/wrapper/BaseSingleFluidTransferHandler.java deleted file mode 100644 index a10f583f..00000000 --- a/common/src/main/java/dev/architectury/transfer/fluid/wrapper/BaseSingleFluidTransferHandler.java +++ /dev/null @@ -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, FluidTransferHandler, FluidVariantView { - @Override - default FluidStack copy(FluidStack resource) { - return resource.copy(); - } -} diff --git a/common/src/main/java/dev/architectury/transfer/fluid/wrapper/CombinedFluidTransferHandler.java b/common/src/main/java/dev/architectury/transfer/fluid/wrapper/CombinedFluidTransferHandler.java index 78752232..e1cc8e12 100644 --- a/common/src/main/java/dev/architectury/transfer/fluid/wrapper/CombinedFluidTransferHandler.java +++ b/common/src/main/java/dev/architectury/transfer/fluid/wrapper/CombinedFluidTransferHandler.java @@ -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, FluidTransferHandler, FluidVariantView { +/** + * A {@link TransferHandler} that combines multiple {@link TransferHandler}s.
+ * This is a convenience class that implements methods for {@link FluidStack}s. + */ +public interface CombinedFluidTransferHandler extends CombinedTransferHandler, FluidTransferHandler { } diff --git a/common/src/main/java/dev/architectury/transfer/fluid/wrapper/FluidVariantView.java b/common/src/main/java/dev/architectury/transfer/fluid/wrapper/FluidVariantView.java deleted file mode 100644 index 3bab5297..00000000 --- a/common/src/main/java/dev/architectury/transfer/fluid/wrapper/FluidVariantView.java +++ /dev/null @@ -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 { - @Override - default long getAmount(FluidStack resource) { - return resource.getAmount(); - } - - @Override - default boolean isSameVariant(FluidStack first, FluidStack second) { - return first.isFluidEqual(second) && first.isTagEqual(second); - } -} diff --git a/common/src/main/java/dev/architectury/transfer/item/ItemTransfer.java b/common/src/main/java/dev/architectury/transfer/item/ItemTransfer.java index 627ec0f6..85ac01eb 100644 --- a/common/src/main/java/dev/architectury/transfer/item/ItemTransfer.java +++ b/common/src/main/java/dev/architectury/transfer/item/ItemTransfer.java @@ -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. + *

+ * 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. + *

+ * There are performance implications for using the architectury lookups, + * please keep your implementations as simple as possible. + */ public static final BlockLookupAccess, 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.

+ * This accepts {@code IItemHandler} on Forge.
* This accepts {@code Storage} 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 handler) { + throw new AssertionError(); + } } diff --git a/common/src/main/java/dev/architectury/transfer/item/ItemTransferHandler.java b/common/src/main/java/dev/architectury/transfer/item/ItemTransferHandler.java index 143dd8ec..e5838633 100644 --- a/common/src/main/java/dev/architectury/transfer/item/ItemTransferHandler.java +++ b/common/src/main/java/dev/architectury/transfer/item/ItemTransferHandler.java @@ -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 { +/** + * This is a convenience class that implements methods for {@link ItemStack}s. + */ +public interface ItemTransferHandler extends ItemTransferView, TransferHandler, VariantView { @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); } } diff --git a/common/src/main/java/dev/architectury/transfer/item/ItemResourceView.java b/common/src/main/java/dev/architectury/transfer/item/ItemTransferView.java similarity index 91% rename from common/src/main/java/dev/architectury/transfer/item/ItemResourceView.java rename to common/src/main/java/dev/architectury/transfer/item/ItemTransferView.java index d320f5fa..9c2bffda 100644 --- a/common/src/main/java/dev/architectury/transfer/item/ItemResourceView.java +++ b/common/src/main/java/dev/architectury/transfer/item/ItemTransferView.java @@ -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 { +public interface ItemTransferView extends TransferView { @Override default ItemStack blank() { return ItemStack.EMPTY; diff --git a/common/src/main/java/dev/architectury/transfer/item/wrapper/BaseSingleItemTransferHandler.java b/common/src/main/java/dev/architectury/transfer/item/wrapper/BaseSingleItemTransferHandler.java deleted file mode 100644 index 794c2894..00000000 --- a/common/src/main/java/dev/architectury/transfer/item/wrapper/BaseSingleItemTransferHandler.java +++ /dev/null @@ -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, ItemTransferHandler, ItemVariantView { - @Override - default ItemStack copy(ItemStack resource) { - return resource.copy(); - } - - @Override - default long getCapacity(ItemStack resource) { - return resource.getMaxStackSize(); - } -} diff --git a/common/src/main/java/dev/architectury/transfer/item/wrapper/CombinedItemTransferHandler.java b/common/src/main/java/dev/architectury/transfer/item/wrapper/CombinedItemTransferHandler.java index fdec00cf..e5112038 100644 --- a/common/src/main/java/dev/architectury/transfer/item/wrapper/CombinedItemTransferHandler.java +++ b/common/src/main/java/dev/architectury/transfer/item/wrapper/CombinedItemTransferHandler.java @@ -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, ItemTransferHandler, ItemVariantView { +/** + * A {@link TransferHandler} that combines multiple {@link TransferHandler}s.
+ * This is a convenience class that implements methods for {@link ItemStack}s. + */ +public interface CombinedItemTransferHandler extends CombinedTransferHandler, ItemTransferHandler { } diff --git a/common/src/main/java/dev/architectury/transfer/item/wrapper/ContainerTransferHandler.java b/common/src/main/java/dev/architectury/transfer/item/wrapper/ContainerTransferHandler.java index 7462fc2c..cf53de9a 100644 --- a/common/src/main/java/dev/architectury/transfer/item/wrapper/ContainerTransferHandler.java +++ b/common/src/main/java/dev/architectury/transfer/item/wrapper/ContainerTransferHandler.java @@ -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 { protected final Container container; - private Iterable> handlers = null; + private List> handlers = null; public ContainerTransferHandler(Container container) { this.container = container; } - protected Iterable> createHandlers() { + protected List> createHandlers() { return new Handlers(); } @Override - public Iterable> getHandlers() { + public List> getParts() { if (handlers == null) { handlers = createHandlers(); } @@ -46,13 +50,13 @@ public class ContainerTransferHandler implements CombinedItemTransferHandler { return handlers; } - protected TransferHandler asTransfer(int index) { + protected SingleTransferHandler asTransfer(int index) { return new SlotTransferHandler(container, index); } - protected class Handlers extends AbstractList> { + protected class Handlers extends AbstractList> { @Override - public TransferHandler get(int index) { + public SingleTransferHandler 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, 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() { } diff --git a/common/src/main/java/dev/architectury/transfer/item/wrapper/ItemVariantView.java b/common/src/main/java/dev/architectury/transfer/item/wrapper/ItemVariantView.java deleted file mode 100644 index 405d31d3..00000000 --- a/common/src/main/java/dev/architectury/transfer/item/wrapper/ItemVariantView.java +++ /dev/null @@ -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 { - @Override - default long getAmount(ItemStack resource) { - return resource.getCount(); - } - - @Override - default boolean isSameVariant(ItemStack first, ItemStack second) { - return ItemStackHooks.isStackable(first, second); - } -} diff --git a/common/src/main/java/dev/architectury/transfer/item/wrapper/WorldlyContainerTransferHandler.java b/common/src/main/java/dev/architectury/transfer/item/wrapper/WorldlyContainerTransferHandler.java index 242872c9..9c3d41fb 100644 --- a/common/src/main/java/dev/architectury/transfer/item/wrapper/WorldlyContainerTransferHandler.java +++ b/common/src/main/java/dev/architectury/transfer/item/wrapper/WorldlyContainerTransferHandler.java @@ -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> createHandlers() { + protected List> createHandlers() { WorldlyContainer container = (WorldlyContainer) this.container; int[] slots = container.getSlotsForFace(this.direction); - TransferHandler[] handlers = new TransferHandler[slots.length]; + SingleTransferHandler[] 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)); } diff --git a/common/src/main/java/dev/architectury/transfer/view/VariantView.java b/common/src/main/java/dev/architectury/transfer/view/VariantView.java index af2721a5..9ff58cf6 100644 --- a/common/src/main/java/dev/architectury/transfer/view/VariantView.java +++ b/common/src/main/java/dev/architectury/transfer/view/VariantView.java @@ -19,8 +19,15 @@ package dev.architectury.transfer.view; +import org.jetbrains.annotations.Nullable; + public interface VariantView { long getAmount(T resource); + @Nullable + default Long getCapacityNullable(T resource) { + return null; + } + boolean isSameVariant(T first, T second); } diff --git a/common/src/main/java/dev/architectury/transfer/wrapper/CombinedSingleTransferHandler.java b/common/src/main/java/dev/architectury/transfer/wrapper/CombinedSingleTransferHandler.java new file mode 100644 index 00000000..4973138f --- /dev/null +++ b/common/src/main/java/dev/architectury/transfer/wrapper/CombinedSingleTransferHandler.java @@ -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.
+ * This is faster than using {@link CombinedTransferHandler} directly, as the size of + * each {@link SingleTransferHandler} is known in advance. + * + * @param the type of resource + */ +public interface CombinedSingleTransferHandler extends CombinedTransferHandler { + @Override + default Iterable> getHandlers() { + return (Iterable>) (Iterable>) getParts(); + } + + List> getParts(); + + @Override + default Stream> getContents() { + return (Stream>) (Stream>) getParts().stream(); + } + + @Override + @Deprecated + default int getContentsSize() { + return getParts().size(); + } + + @Override + @Deprecated + default ResourceView getContent(int index) { + return getParts().get(index); + } +} diff --git a/common/src/main/java/dev/architectury/transfer/wrapper/CombinedTransferHandler.java b/common/src/main/java/dev/architectury/transfer/wrapper/CombinedTransferHandler.java index ded2059f..239e9adf 100644 --- a/common/src/main/java/dev/architectury/transfer/wrapper/CombinedTransferHandler.java +++ b/common/src/main/java/dev/architectury/transfer/wrapper/CombinedTransferHandler.java @@ -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 the type of resource + */ public interface CombinedTransferHandler extends TransferHandler, VariantView { Iterable> getHandlers(); @@ -38,6 +43,7 @@ public interface CombinedTransferHandler extends TransferHandler, VariantV } @Override + @Deprecated default int getContentsSize() { int size = 0; for (TransferHandler handler : getHandlers()) { @@ -47,6 +53,7 @@ public interface CombinedTransferHandler extends TransferHandler, VariantV } @Override + @Deprecated default ResourceView getContent(int index) { if (index < 0) { throw new IllegalArgumentException("Index must be non-negative"); diff --git a/common/src/main/java/dev/architectury/transfer/wrapper/single/BaseSingleTransferHandler.java b/common/src/main/java/dev/architectury/transfer/wrapper/single/BaseSingleTransferHandler.java index 23fed42c..8cb58e5b 100644 --- a/common/src/main/java/dev/architectury/transfer/wrapper/single/BaseSingleTransferHandler.java +++ b/common/src/main/java/dev/architectury/transfer/wrapper/single/BaseSingleTransferHandler.java @@ -20,6 +20,7 @@ package dev.architectury.transfer.wrapper.single; import dev.architectury.transfer.TransferAction; +import org.jetbrains.annotations.Nullable; public interface BaseSingleTransferHandler extends SingleTransferHandler { @Override @@ -34,9 +35,9 @@ public interface BaseSingleTransferHandler extends SingleTransferHandler { 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 extends SingleTransferHandler { 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) { diff --git a/common/src/main/java/dev/architectury/transfer/wrapper/single/ForwardingSingleTransferHandler.java b/common/src/main/java/dev/architectury/transfer/wrapper/single/ForwardingSingleTransferHandler.java index 901da8de..eef7c94c 100644 --- a/common/src/main/java/dev/architectury/transfer/wrapper/single/ForwardingSingleTransferHandler.java +++ b/common/src/main/java/dev/architectury/transfer/wrapper/single/ForwardingSingleTransferHandler.java @@ -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 extends ForwardingTransferHandler, ResourceView { +public interface ForwardingSingleTransferHandler extends SingleTransferHandler, ForwardingTransferHandler, ResourceView { @Override SingleTransferHandler forwardingTo(); + @Override + default Stream> getContents() { + return forwardingTo().getContents(); + } + + @Override + default int getContentsSize() { + return forwardingTo().getContentsSize(); + } + + @Override + default ResourceView getContent(int index) { + return forwardingTo().getContent(index); + } + @Override default T extract(Predicate 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(); diff --git a/common/src/main/java/dev/architectury/transfer/wrapper/single/SingleTransferHandler.java b/common/src/main/java/dev/architectury/transfer/wrapper/single/SingleTransferHandler.java index b7ce0bee..1bd939db 100644 --- a/common/src/main/java/dev/architectury/transfer/wrapper/single/SingleTransferHandler.java +++ b/common/src/main/java/dev/architectury/transfer/wrapper/single/SingleTransferHandler.java @@ -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 the type of resource + * @see BaseSingleTransferHandler for a simple implementation + */ public interface SingleTransferHandler extends TransferHandler, ResourceView, ModifiableView, VariantView { @Override default Stream> getContents() { diff --git a/fabric/src/main/java/dev/architectury/transfer/access/fabric/PlatformLookupImpl.java b/fabric/src/main/java/dev/architectury/transfer/access/fabric/PlatformLookupImpl.java new file mode 100644 index 00000000..e4d66d23 --- /dev/null +++ b/fabric/src/main/java/dev/architectury/transfer/access/fabric/PlatformLookupImpl.java @@ -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 void attachBlockQuery(BlockLookupAccess access, Object lookup, Function wrapper) { + if (!(lookup instanceof BlockApiLookup)) { + throw new IllegalArgumentException("Lookup must be an instance of BlockApiLookup on Fabric!"); + } + access.addQueryHandler(new BlockApiLookupWrapper<>((BlockApiLookup) lookup, wrapper)); + } + + public static void attachBlockRegistration(BlockLookupAccess access, Object lookup, BiFunction unwrapper) { + if (!(lookup instanceof BlockApiLookup)) { + throw new IllegalArgumentException("Lookup must be an instance of BlockApiLookup on Fabric!"); + } + access.addRegistrationHandler(FabricBlockLookupRegistration.create((BlockApiLookup) lookup, unwrapper)); + } +} diff --git a/fabric/src/main/java/dev/architectury/transfer/energy/fabric/EnergyTransferImpl.java b/fabric/src/main/java/dev/architectury/transfer/energy/fabric/EnergyTransferImpl.java index 42634294..4a2eb6ca 100644 --- a/fabric/src/main/java/dev/architectury/transfer/energy/fabric/EnergyTransferImpl.java +++ b/fabric/src/main/java/dev/architectury/transfer/energy/fabric/EnergyTransferImpl.java @@ -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 handler) { + public static Object unwrap(@Nullable SingleTransferHandler 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 { diff --git a/fabric/src/main/java/dev/architectury/transfer/fabric/BlockApiLookupWrapper.java b/fabric/src/main/java/dev/architectury/transfer/fabric/BlockApiLookupWrapper.java index 6952dbc3..577f2a0a 100644 --- a/fabric/src/main/java/dev/architectury/transfer/fabric/BlockApiLookupWrapper.java +++ b/fabric/src/main/java/dev/architectury/transfer/fabric/BlockApiLookupWrapper.java @@ -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> implements BlockLookup { +public class BlockApiLookupWrapper implements BlockLookup { private final BlockApiLookup lookup; private final Function<@Nullable F, @Nullable H> wrapper; diff --git a/fabric/src/main/java/dev/architectury/transfer/fabric/FabricBlockLookupRegistration.java b/fabric/src/main/java/dev/architectury/transfer/fabric/FabricBlockLookupRegistration.java index 64dc16a0..3cb59aea 100644 --- a/fabric/src/main/java/dev/architectury/transfer/fabric/FabricBlockLookupRegistration.java +++ b/fabric/src/main/java/dev/architectury/transfer/fabric/FabricBlockLookupRegistration.java @@ -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 implements BlockLookupRegistration { - private final Function unwrapper; + private final BiFunction unwrapper; private final BlockApiLookup lookup; - public static FabricBlockLookupRegistration create(BlockApiLookup lookup, Function unwrapper) { + public static FabricBlockLookupRegistration create(BlockApiLookup lookup, BiFunction unwrapper) { return new FabricBlockLookupRegistration<>(unwrapper, lookup); } - private FabricBlockLookupRegistration(Function unwrapper, BlockApiLookup lookup) { + private FabricBlockLookupRegistration(BiFunction unwrapper, BlockApiLookup lookup) { this.unwrapper = unwrapper; this.lookup = lookup; } @@ -47,7 +48,7 @@ public class FabricBlockLookupRegistration implements BlockLookup return (level, pos, state, blockEntity, context) -> { Function 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 implements BlockLookup Function 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; diff --git a/fabric/src/main/java/dev/architectury/transfer/fluid/fabric/FluidTransferImpl.java b/fabric/src/main/java/dev/architectury/transfer/fluid/fabric/FluidTransferImpl.java index 79f0a502..695458b4 100644 --- a/fabric/src/main/java/dev/architectury/transfer/fluid/fabric/FluidTransferImpl.java +++ b/fabric/src/main/java/dev/architectury/transfer/fluid/fabric/FluidTransferImpl.java @@ -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 unwrap(@Nullable TransferHandler handler) { + public static Object unwrap(@Nullable TransferHandler 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; } } diff --git a/fabric/src/main/java/dev/architectury/transfer/item/fabric/ItemTransferImpl.java b/fabric/src/main/java/dev/architectury/transfer/item/fabric/ItemTransferImpl.java index c26d49ca..d367a501 100644 --- a/fabric/src/main/java/dev/architectury/transfer/item/fabric/ItemTransferImpl.java +++ b/fabric/src/main/java/dev/architectury/transfer/item/fabric/ItemTransferImpl.java @@ -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 unwrap(@Nullable TransferHandler handler) { + public static Object unwrap(@Nullable TransferHandler 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; } } diff --git a/forge/src/main/java/dev/architectury/transfer/access/forge/PlatformLookupImpl.java b/forge/src/main/java/dev/architectury/transfer/access/forge/PlatformLookupImpl.java new file mode 100644 index 00000000..697cee76 --- /dev/null +++ b/forge/src/main/java/dev/architectury/transfer/access/forge/PlatformLookupImpl.java @@ -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 void attachBlockQuery(BlockLookupAccess access, Object lookup, Function wrapper) { + if (!(lookup instanceof Capability)) { + throw new IllegalArgumentException("Lookup must be an instance of Capability on Forge!"); + } + access.addQueryHandler(instantiateBlockLookup((Capability) lookup, wrapper)); + } + + public static void attachBlockRegistration(BlockLookupAccess access, Object lookup, BiFunction unwrapper) { + if (!(lookup instanceof Capability)) { + throw new IllegalArgumentException("Lookup must be an instance of Capability on Forge!"); + } + access.addRegistrationHandler(ForgeBlockLookupRegistration.create((Capability) lookup, + (level, pos, state, blockEntity) -> unwrapper)); + } + + public static BlockLookup instantiateBlockLookup(Capability capability, Function wrapper) { + return new BlockLookup() { + @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); + } + }; + } +} diff --git a/forge/src/main/java/dev/architectury/transfer/energy/forge/EnergyTransferImpl.java b/forge/src/main/java/dev/architectury/transfer/energy/forge/EnergyTransferImpl.java index 2190e2e0..7fb46359 100644 --- a/forge/src/main/java/dev/architectury/transfer/energy/forge/EnergyTransferImpl.java +++ b/forge/src/main/java/dev/architectury/transfer/energy/forge/EnergyTransferImpl.java @@ -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 handler) { + if (handler == null) return null; + + if (handler instanceof ForgeTransferHandler) { + return ((ForgeTransferHandler) handler).getStorage(); + } else { + return new ArchEnergyStorage(handler); + } } - public static BlockLookup, Direction> instantiateBlockLookup() { - return new BlockLookup, Direction>() { - @Override - @Nullable - public SingleTransferHandler get(Level level, BlockPos pos, Direction direction) { - return get(level, pos, level.getBlockState(pos), null, direction); - } - - @Override - @Nullable - public SingleTransferHandler 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(); diff --git a/forge/src/main/java/dev/architectury/transfer/fluid/forge/FluidTransferImpl.java b/forge/src/main/java/dev/architectury/transfer/fluid/forge/FluidTransferImpl.java index f5ad35da..e31c02a1 100644 --- a/forge/src/main/java/dev/architectury/transfer/fluid/forge/FluidTransferImpl.java +++ b/forge/src/main/java/dev/architectury/transfer/fluid/forge/FluidTransferImpl.java @@ -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 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, 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 TRUE = stack -> true; + private TransferHandler handler; public ArchFluidHandler(TransferHandler handler) { @@ -195,6 +193,10 @@ public class FluidTransferImpl { this.handler = handler; } + public IFluidHandler getHandler() { + return handler; + } + @Override public Stream> getContents() { return Streams.stream(new Itr()); @@ -281,7 +283,7 @@ public class FluidTransferImpl { } } - private class ForgeResourceView implements FluidResourceView { + private class ForgeResourceView implements ResourceView, FluidTransferView { int index; public ForgeResourceView(int index) { diff --git a/forge/src/main/java/dev/architectury/transfer/forge/ForgeBlockLookupRegistration.java b/forge/src/main/java/dev/architectury/transfer/forge/ForgeBlockLookupRegistration.java index 21ccc64f..d8f69412 100644 --- a/forge/src/main/java/dev/architectury/transfer/forge/ForgeBlockLookupRegistration.java +++ b/forge/src/main/java/dev/architectury/transfer/forge/ForgeBlockLookupRegistration.java @@ -39,7 +39,7 @@ import java.util.function.BiFunction; import java.util.function.Function; public interface ForgeBlockLookupRegistration extends BlockLookupRegistration { - static ForgeBlockLookupRegistration create(Capability capability, BlockAccessProvider, BlockEntity> transformer) { + static ForgeBlockLookupRegistration create(Capability capability, BlockAccessProvider, BlockEntity> transformer) { return new ForgeBlockLookupRegistration() { @Override public Capability getCapability() { @@ -48,7 +48,7 @@ public interface ForgeBlockLookupRegistration 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); } }; } diff --git a/forge/src/main/java/dev/architectury/transfer/item/forge/ItemTransferImpl.java b/forge/src/main/java/dev/architectury/transfer/item/forge/ItemTransferImpl.java index 45293cc9..86ee6190 100644 --- a/forge/src/main/java/dev/architectury/transfer/item/forge/ItemTransferImpl.java +++ b/forge/src/main/java/dev/architectury/transfer/item/forge/ItemTransferImpl.java @@ -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 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, 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> 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, ItemTransferView { int index; public ForgeResourceView(int index) {