diff --git a/common/src/main/java/tesseract/TesseractCapUtils.java b/common/src/main/java/tesseract/TesseractCapUtils.java index 98c8490..f921905 100644 --- a/common/src/main/java/tesseract/TesseractCapUtils.java +++ b/common/src/main/java/tesseract/TesseractCapUtils.java @@ -15,45 +15,30 @@ import tesseract.api.item.PlatformItemHandler; import java.util.Optional; +import java.util.ServiceLoader; -public class TesseractCapUtils { - @ExpectPlatform - public static Optional getEnergyHandlerItem(ItemStack stack){ - throw new AssertionError(); - } - - @ExpectPlatform - public static Optional getWrappedEnergyHandlerItem(ItemStack stack){ - throw new AssertionError(); - } - - @ExpectPlatform - public static Optional getEnergyHandler(BlockEntity entity, Direction side){ - throw new AssertionError(); - } - - @ExpectPlatform - public static Optional getHeatHandler(BlockEntity entity, Direction side){ - throw new AssertionError(); - } - - @ExpectPlatform - public static Optional getItemHandler(BlockEntity entity, Direction side){ - throw new AssertionError(); - } - - @ExpectPlatform - public static Optional getFluidHandler(Level level, BlockPos pos, Direction side){ - throw new AssertionError(); - } - - @ExpectPlatform - public static IFluidNode getFluidNode(Level level, long pos, Direction capSide, Runnable capCallback){ - throw new AssertionError(); - } - - @ExpectPlatform - public static IItemNode getItemNode(Level level, long pos, Direction capSide, Runnable capCallback){ - throw new AssertionError(); - } +public interface TesseractCapUtils { + TesseractCapUtils INSTANCE = ServiceLoader.load(TesseractCapUtils.class).findFirst().orElseThrow(() -> new IllegalStateException("No implementation of TesseractPlatformUtils found")); + //public static final TesseractCapUtils INSTANCE = new TesseractCapUtils(); + Optional getEnergyHandlerItem(ItemStack stack); + + Optional getWrappedEnergyHandlerItem(ItemStack stack); + + + Optional getEnergyHandler(BlockEntity entity, Direction side); + + + Optional getHeatHandler(BlockEntity entity, Direction side); + + + Optional getItemHandler(BlockEntity entity, Direction side); + + + Optional getFluidHandler(Level level, BlockPos pos, Direction side); + + + IFluidNode getFluidNode(Level level, long pos, Direction capSide, Runnable capCallback); + + + IItemNode getItemNode(Level level, long pos, Direction capSide, Runnable capCallback); } diff --git a/common/src/main/java/tesseract/api/capability/TesseractFluidCapability.java b/common/src/main/java/tesseract/api/capability/TesseractFluidCapability.java index 107fc3b..0e890c4 100644 --- a/common/src/main/java/tesseract/api/capability/TesseractFluidCapability.java +++ b/common/src/main/java/tesseract/api/capability/TesseractFluidCapability.java @@ -129,7 +129,7 @@ private void transferAroundPipe(FluidTransaction transaction, long pos) { if (this.callback.modify(stack, dir, false, true)) continue; //Check the handler. - var cap = TesseractCapUtils.getFluidHandler(tile.getLevel(), BlockPos.of(Pos.offset(pos, dir)), dir.getOpposite()); + var cap = TesseractCapUtils.INSTANCE.getFluidHandler(tile.getLevel(), BlockPos.of(Pos.offset(pos, dir)), dir.getOpposite()); if (cap.isEmpty()) continue; //Perform insertion, and add to the transaction. var handler = cap.get(); diff --git a/common/src/main/java/tesseract/api/capability/TesseractGTCapability.java b/common/src/main/java/tesseract/api/capability/TesseractGTCapability.java index aaa31bd..21806f5 100644 --- a/common/src/main/java/tesseract/api/capability/TesseractGTCapability.java +++ b/common/src/main/java/tesseract/api/capability/TesseractGTCapability.java @@ -54,7 +54,7 @@ private void transferAroundPipe(GTTransaction transaction, long pos) { BlockEntity otherTile = tile.getLevel().getBlockEntity(BlockPos.of(Pos.offset(pos, dir))); if (otherTile != null) { //Check the handler. - var cap = TesseractCapUtils.getEnergyHandler(otherTile, dir.getOpposite()); + var cap = TesseractCapUtils.INSTANCE.getEnergyHandler(otherTile, dir.getOpposite()); if (cap.isEmpty()) continue; //Perform insertion, and add to the transaction. var handler = cap.get(); diff --git a/common/src/main/java/tesseract/api/capability/TesseractItemCapability.java b/common/src/main/java/tesseract/api/capability/TesseractItemCapability.java index 71093d3..2846f1d 100644 --- a/common/src/main/java/tesseract/api/capability/TesseractItemCapability.java +++ b/common/src/main/java/tesseract/api/capability/TesseractItemCapability.java @@ -89,7 +89,7 @@ private void transferAroundPipe(ItemTransaction transaction, long pos) { BlockEntity otherTile = tile.getLevel().getBlockEntity(BlockPos.of(Pos.offset(pos, dir))); if (otherTile != null) { //Check the handler. - var cap = TesseractCapUtils.getItemHandler(otherTile, dir.getOpposite()); + var cap = TesseractCapUtils.INSTANCE.getItemHandler(otherTile, dir.getOpposite()); if (cap.isEmpty()) continue; //Perform insertion, and add to the transaction. var handler = cap.get(); diff --git a/common/src/main/java/tesseract/api/fluid/IFluidNode.java b/common/src/main/java/tesseract/api/fluid/IFluidNode.java index bd1b2cc..ea4edca 100644 --- a/common/src/main/java/tesseract/api/fluid/IFluidNode.java +++ b/common/src/main/java/tesseract/api/fluid/IFluidNode.java @@ -71,5 +71,5 @@ default FluidHolder getFluidInTank(int tank){ return getFluids().get(tank); } - GraphWrapper.ICapabilityGetter GETTER = (TesseractCapUtils::getFluidNode); + GraphWrapper.ICapabilityGetter GETTER = (TesseractCapUtils.INSTANCE::getFluidNode); } diff --git a/common/src/main/java/tesseract/api/item/IItemNode.java b/common/src/main/java/tesseract/api/item/IItemNode.java index c942dbe..13684bf 100644 --- a/common/src/main/java/tesseract/api/item/IItemNode.java +++ b/common/src/main/java/tesseract/api/item/IItemNode.java @@ -68,5 +68,5 @@ default boolean canInput(ItemStack item, Direction direction) { return true; } - GraphWrapper.ICapabilityGetter GETTER = (TesseractCapUtils::getItemNode); + GraphWrapper.ICapabilityGetter GETTER = (TesseractCapUtils.INSTANCE::getItemNode); } diff --git a/fabric/src/main/java/tesseract/fabric/TesseractCapUtilsImpl.java b/fabric/src/main/java/tesseract/fabric/TesseractCapUtilsImpl.java index 15af5e4..baadc98 100644 --- a/fabric/src/main/java/tesseract/fabric/TesseractCapUtilsImpl.java +++ b/fabric/src/main/java/tesseract/fabric/TesseractCapUtilsImpl.java @@ -23,6 +23,7 @@ import net.minecraft.world.level.block.entity.BlockEntity; import org.jetbrains.annotations.NotNull; import team.reborn.energy.api.EnergyStorage; +import tesseract.TesseractCapUtils; import tesseract.TesseractConfig; import tesseract.api.fabric.TesseractLookups; import tesseract.api.fabric.TileListeners; @@ -37,13 +38,15 @@ import java.util.Optional; -public class TesseractCapUtilsImpl { - public static Optional getEnergyHandlerItem(ItemStack stack){ +public class TesseractCapUtilsImpl implements TesseractCapUtils { + @Override + public Optional getEnergyHandlerItem(ItemStack stack){ IEnergyHandlerItem energyHandler = ContainerItemContext.withInitial(stack).find(TesseractLookups.ENERGY_HANDLER_ITEM); return Optional.ofNullable(energyHandler); } - public static Optional getWrappedEnergyHandlerItem(ItemStack stack){ + @Override + public Optional getWrappedEnergyHandlerItem(ItemStack stack){ IEnergyHandlerItem energyHandler = ContainerItemContext.withInitial(stack).find(TesseractLookups.ENERGY_HANDLER_ITEM); if (energyHandler == null){ EnergyStorage storage = ContainerItemContext.withInitial(stack).find(EnergyStorage.ITEM); @@ -57,7 +60,8 @@ public static Optional getWrappedEnergyHandlerItem(ItemStack return Optional.ofNullable(energyHandler); } - public static Optional getEnergyHandler(@NotNull BlockEntity entity, Direction side){ + @Override + public Optional getEnergyHandler(@NotNull BlockEntity entity, Direction side){ IEnergyHandler energyHandler = TesseractLookups.ENERGY_HANDLER_SIDED.find(entity.getLevel(), entity.getBlockPos(), entity.getBlockState(), entity, side); if (energyHandler == null) { if (FabricLoader.getInstance().isModLoaded("modern_industrialization") && TesseractConfig.ENABLE_MI_COMPAT.get()){ @@ -69,26 +73,30 @@ public static Optional getEnergyHandler(@NotNull BlockEntity ent return Optional.ofNullable(energyHandler); } - public static Optional getHeatHandler(BlockEntity entity, Direction side){ + @Override + public Optional getHeatHandler(BlockEntity entity, Direction side){ IHeatHandler heatHandler = TesseractLookups.HEAT_HANDLER_SIDED.find(entity.getLevel(), entity.getBlockPos(), entity.getBlockState(), entity, side); return Optional.ofNullable(heatHandler); } //TODO figure out better abstraction method - public static Optional getItemHandler(BlockEntity tile, Direction side){ + @Override + public Optional getItemHandler(BlockEntity tile, Direction side){ Storage storage = ItemStorage.SIDED.find(tile.getLevel(), tile.getBlockPos(), tile.getBlockState(), tile, side); //if (storage instanceof IItemHandler itemHandler) return Optional.of(itemHandler); return storage == null ? Optional.empty() : Optional.of(new FabricPlatformItemHandler(storage)); } - public static Optional getFluidHandler(Level level, BlockPos pos, Direction side){ + @Override + public Optional getFluidHandler(Level level, BlockPos pos, Direction side){ BlockEntity blockEntity = level.getBlockEntity(pos); if (blockEntity != null) return FluidHooks.safeGetBlockFluidManager(blockEntity, side); Storage storage = FluidStorage.SIDED.find(level, pos, side); return storage == null ? Optional.empty() : Optional.of(new FabricFluidHandler(storage)); } - public static IItemNode getItemNode(Level level, long pos, Direction capSide, Runnable capCallback){ + @Override + public IItemNode getItemNode(Level level, long pos, Direction capSide, Runnable capCallback){ BlockEntity tile = level.getBlockEntity(BlockPos.of(pos)); if (tile == null) { return null; @@ -105,7 +113,7 @@ public static IItemNode getItemNode(Level level, long pos, Direction capSide, Ru return null; } - private static IEnergyHandler getEnergyStorage(BlockEntity be, Direction direction){ + private IEnergyHandler getEnergyStorage(BlockEntity be, Direction direction){ EnergyStorage storage = EnergyStorage.SIDED.find(be.getLevel(), be.getBlockPos(), be.getBlockState(), be, direction); if (storage == null) return null; if (storage instanceof IEnergyHandler moveable1) return moveable1; @@ -113,7 +121,7 @@ private static IEnergyHandler getEnergyStorage(BlockEntity be, Direction directi return new EnergyTileWrapper(be, storage); } - private static IEnergyHandler getEnergyMoveable(BlockEntity be, Direction direction){ + private IEnergyHandler getEnergyMoveable(BlockEntity be, Direction direction){ EnergyMoveable moveable = EnergyApi.MOVEABLE.find(be.getLevel(), be.getBlockPos(), be.getBlockState(), be, direction); if (moveable == null) return null; if (moveable instanceof IEnergyHandler moveable1) return moveable1; @@ -121,7 +129,8 @@ private static IEnergyHandler getEnergyMoveable(BlockEntity be, Direction direct return new EnergyMoveableWrapper(be, moveable); } - public static IFluidNode getFluidNode(Level level, long pos, Direction capSide, Runnable capCallback){ + @Override + public IFluidNode getFluidNode(Level level, long pos, Direction capSide, Runnable capCallback){ BlockEntity tile = level.getBlockEntity(BlockPos.of(pos)); if (tile == null) { return null; diff --git a/fabric/src/main/java/tesseract/fabric/TesseractPlatformUtilsImpl.java b/fabric/src/main/java/tesseract/fabric/TesseractPlatformUtilsImpl.java index d4162cb..fe7e87a 100644 --- a/fabric/src/main/java/tesseract/fabric/TesseractPlatformUtilsImpl.java +++ b/fabric/src/main/java/tesseract/fabric/TesseractPlatformUtilsImpl.java @@ -27,9 +27,10 @@ @SuppressWarnings("UnstableApiUsage") public class TesseractPlatformUtilsImpl implements TesseractPlatformUtils { + @Override public IGTNode getGTNode(Level level, long pos, Direction direction, Runnable invalidate){ BlockEntity tile = level.getBlockEntity(BlockPos.of(pos)); - Optional capability = TesseractCapUtilsImpl.getEnergyHandler(tile, direction); + Optional capability = TesseractCapUtils.INSTANCE.getEnergyHandler(tile, direction); if (capability.isPresent()) { if (invalidate != null) ((TileListeners)tile).addListener(() -> invalidate.run()); return capability.get(); @@ -37,6 +38,7 @@ public IGTNode getGTNode(Level level, long pos, Direction direction, Runnable in return null; } + @Override public IRFNode getRFNode(Level level, long pos, Direction capSide, Runnable capCallback){ BlockEntity tile = level.getBlockEntity(BlockPos.of(pos)); if (tile == null) { @@ -60,10 +62,11 @@ public IRFNode getRFNode(Level level, long pos, Direction capSide, Runnable capC return null; } + @Override public IHeatNode getHeatNode(Level level, long pos, Direction direction, Runnable invalidate){ BlockEntity tile = level.getBlockEntity(BlockPos.of(pos)); if (tile == null) return null; - Optional capability = TesseractCapUtils.getHeatHandler(tile, direction); + Optional capability = TesseractCapUtils.INSTANCE.getHeatHandler(tile, direction); if (capability.isPresent()) { if (invalidate != null) ((TileListeners)tile).addListener(invalidate); return capability.get(); @@ -71,14 +74,17 @@ public IHeatNode getHeatNode(Level level, long pos, Direction direction, Runnabl return null; } + @Override public boolean isFeCap(Class cap){ return false; } + @Override public boolean isForge(){ return false; } + @Override public ConfigHandler createConfig(Config config){ return CarbonConfig.createConfig(Tesseract.API_ID, config); } diff --git a/fabric/src/main/resources/META-INF/services/tesseract.TesseractCapUtils b/fabric/src/main/resources/META-INF/services/tesseract.TesseractCapUtils new file mode 100644 index 0000000..bf1861d --- /dev/null +++ b/fabric/src/main/resources/META-INF/services/tesseract.TesseractCapUtils @@ -0,0 +1 @@ +tesseract.fabric.TesseractCapUtilsImpl \ No newline at end of file diff --git a/forge/src/main/java/tesseract/forge/TesseractCapUtilsImpl.java b/forge/src/main/java/tesseract/forge/TesseractCapUtilsImpl.java index 52212ce..bca42e5 100644 --- a/forge/src/main/java/tesseract/forge/TesseractCapUtilsImpl.java +++ b/forge/src/main/java/tesseract/forge/TesseractCapUtilsImpl.java @@ -23,6 +23,7 @@ import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandlerModifiable; import net.minecraftforge.items.wrapper.InvWrapper; +import tesseract.TesseractCapUtils; import tesseract.api.fluid.IFluidNode; import tesseract.api.forge.TesseractCaps; import tesseract.api.forge.wrapper.*; @@ -36,12 +37,14 @@ import javax.annotation.Nullable; import java.util.Optional; -public class TesseractCapUtilsImpl { - public static Optional getEnergyHandlerItem(ItemStack stack){ +public class TesseractCapUtilsImpl implements TesseractCapUtils { + @Override + public Optional getEnergyHandlerItem(ItemStack stack){ return stack.getCapability(TesseractCaps.ENERGY_HANDLER_CAPABILITY_ITEM).map(e -> e); } - public static Optional getWrappedEnergyHandlerItem(ItemStack stack){ + @Override + public Optional getWrappedEnergyHandlerItem(ItemStack stack){ IEnergyHandlerItem energyHandler = stack.getCapability(TesseractCaps.ENERGY_HANDLER_CAPABILITY_ITEM).map(e -> e).orElse(null); if (energyHandler == null){ IEnergyStorage storage = stack.getCapability(CapabilityEnergy.ENERGY).map(e -> e).orElse(null); @@ -54,28 +57,33 @@ public static Optional getWrappedEnergyHandlerItem(ItemStack return Optional.ofNullable(energyHandler); } - public static Optional getEnergyHandler(BlockEntity entity, Direction side){ + @Override + public Optional getEnergyHandler(BlockEntity entity, Direction side){ LazyOptional energyHandler = entity.getCapability(TesseractCaps.ENERGY_HANDLER_CAPABILITY, side); if (energyHandler.isPresent()) return energyHandler.map(e -> e); energyHandler = getWrappedHandler(entity, side); return energyHandler.map(e -> e); } - public static LazyOptional getWrappedHandler(BlockEntity be, @Nullable Direction side){ + public LazyOptional getWrappedHandler(BlockEntity be, @Nullable Direction side){ IEnergyStorage storage = be.getCapability(CapabilityEnergy.ENERGY, side).map(i -> i).orElse(null); if (storage == null) return LazyOptional.empty(); if (storage instanceof IEnergyHandlerStorage handlerStorage) return LazyOptional.of(handlerStorage::getEnergyHandler); return LazyOptional.of(() -> new EnergyTileWrapper(be, storage)); } - public static Optional getHeatHandler(BlockEntity entity, Direction side){ + @Override + public Optional getHeatHandler(BlockEntity entity, Direction side){ return entity.getCapability(TesseractCaps.HEAT_CAPABILITY, side).map(e -> e); } - public static Optional getItemHandler(BlockEntity entity, Direction side){ + @Override + public Optional getItemHandler(BlockEntity entity, Direction side){ return entity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side).map(ForgePlatformItemHandler::new); } - public static Optional getFluidHandler(Level level, BlockPos pos, Direction side){ + + @Override + public Optional getFluidHandler(Level level, BlockPos pos, Direction side){ BlockEntity entity = level.getBlockEntity(pos); if (entity == null){ BlockState state = level.getBlockState(pos); @@ -87,7 +95,8 @@ public static Optional getFluidHandler(Level level, BlockP return FluidHooks.safeGetBlockFluidManager(entity, side); } - public static IFluidNode getFluidNode(Level level, long pos, Direction capSide, Runnable capCallback){ + @Override + public IFluidNode getFluidNode(Level level, long pos, Direction capSide, Runnable capCallback){ BlockEntity tile = level.getBlockEntity(BlockPos.of(pos)); LazyOptional capability; if (tile == null){ @@ -113,7 +122,8 @@ public static IFluidNode getFluidNode(Level level, long pos, Direction capSide, } } - public static IItemNode getItemNode(Level level, long pos, Direction capSide, Runnable capCallback){ + @Override + public IItemNode getItemNode(Level level, long pos, Direction capSide, Runnable capCallback){ BlockEntity tile = level.getBlockEntity(BlockPos.of(pos)); if (tile == null) { return null; diff --git a/forge/src/main/java/tesseract/forge/TesseractPlatformUtilsImpl.java b/forge/src/main/java/tesseract/forge/TesseractPlatformUtilsImpl.java index de2cbe7..453ebf4 100644 --- a/forge/src/main/java/tesseract/forge/TesseractPlatformUtilsImpl.java +++ b/forge/src/main/java/tesseract/forge/TesseractPlatformUtilsImpl.java @@ -25,9 +25,10 @@ import java.util.Optional; public class TesseractPlatformUtilsImpl implements TesseractPlatformUtils { + @Override public IGTNode getGTNode(Level level, long pos, Direction direction, Runnable invalidate){ BlockEntity tile = level.getBlockEntity(BlockPos.of(pos)); - LazyOptional capability = TesseractCapUtilsImpl.getEnergyHandler(tile, direction).map(e -> LazyOptional.of(() -> e)).orElse(LazyOptional.empty()); + LazyOptional capability = TesseractCapUtils.INSTANCE.getEnergyHandler(tile, direction).map(e -> LazyOptional.of(() -> e)).orElse(LazyOptional.empty()); if (capability.isPresent()) { if (invalidate != null )capability.addListener(o -> invalidate.run()); return capability.resolve().get(); @@ -35,6 +36,7 @@ public IGTNode getGTNode(Level level, long pos, Direction direction, Runnable in return null; } + @Override public IRFNode getRFNode(Level level, long pos, Direction capSide, Runnable capCallback){ BlockEntity tile = level.getBlockEntity(BlockPos.of(pos)); if (tile == null) { @@ -54,6 +56,7 @@ public IRFNode getRFNode(Level level, long pos, Direction capSide, Runnable capC } } + @Override public IHeatNode getHeatNode(Level level, long pos, Direction direction, Runnable invalidate){ BlockEntity tile = level.getBlockEntity(BlockPos.of(pos)); if (tile == null) return null; @@ -65,14 +68,17 @@ public IHeatNode getHeatNode(Level level, long pos, Direction direction, Runnabl return null; } + @Override public boolean isFeCap(Class cap){ return cap == IEnergyStorage.class; } + @Override public boolean isForge(){ return true; } + @Override public ConfigHandler createConfig(Config config){ return CarbonConfig.CONFIGS.createConfig(config); } diff --git a/forge/src/main/resources/META-INF/services/tesseract.TesseractCapUtils b/forge/src/main/resources/META-INF/services/tesseract.TesseractCapUtils new file mode 100644 index 0000000..12d8df2 --- /dev/null +++ b/forge/src/main/resources/META-INF/services/tesseract.TesseractCapUtils @@ -0,0 +1 @@ +tesseract.forge.TesseractCapUtilsImpl \ No newline at end of file