Skip to content

Commit

Permalink
changed TesseractCapUtils to a service
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinsdar committed Mar 6, 2024
1 parent 0eec28a commit 69f9d86
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 69 deletions.
65 changes: 25 additions & 40 deletions common/src/main/java/tesseract/TesseractCapUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,30 @@
import tesseract.api.item.PlatformItemHandler;

import java.util.Optional;
import java.util.ServiceLoader;

public class TesseractCapUtils {
@ExpectPlatform
public static Optional<IEnergyHandlerItem> getEnergyHandlerItem(ItemStack stack){
throw new AssertionError();
}

@ExpectPlatform
public static Optional<IEnergyHandlerItem> getWrappedEnergyHandlerItem(ItemStack stack){
throw new AssertionError();
}

@ExpectPlatform
public static Optional<IEnergyHandler> getEnergyHandler(BlockEntity entity, Direction side){
throw new AssertionError();
}

@ExpectPlatform
public static Optional<IHeatHandler> getHeatHandler(BlockEntity entity, Direction side){
throw new AssertionError();
}

@ExpectPlatform
public static Optional<PlatformItemHandler> getItemHandler(BlockEntity entity, Direction side){
throw new AssertionError();
}

@ExpectPlatform
public static Optional<PlatformFluidHandler> 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<IEnergyHandlerItem> getEnergyHandlerItem(ItemStack stack);

Optional<IEnergyHandlerItem> getWrappedEnergyHandlerItem(ItemStack stack);


Optional<IEnergyHandler> getEnergyHandler(BlockEntity entity, Direction side);


Optional<IHeatHandler> getHeatHandler(BlockEntity entity, Direction side);


Optional<PlatformItemHandler> getItemHandler(BlockEntity entity, Direction side);


Optional<PlatformFluidHandler> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/tesseract/api/fluid/IFluidNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ default FluidHolder getFluidInTank(int tank){
return getFluids().get(tank);
}

GraphWrapper.ICapabilityGetter<IFluidNode> GETTER = (TesseractCapUtils::getFluidNode);
GraphWrapper.ICapabilityGetter<IFluidNode> GETTER = (TesseractCapUtils.INSTANCE::getFluidNode);
}
2 changes: 1 addition & 1 deletion common/src/main/java/tesseract/api/item/IItemNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ default boolean canInput(ItemStack item, Direction direction) {
return true;
}

GraphWrapper.ICapabilityGetter<IItemNode> GETTER = (TesseractCapUtils::getItemNode);
GraphWrapper.ICapabilityGetter<IItemNode> GETTER = (TesseractCapUtils.INSTANCE::getItemNode);
}
31 changes: 20 additions & 11 deletions fabric/src/main/java/tesseract/fabric/TesseractCapUtilsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,13 +38,15 @@

import java.util.Optional;

public class TesseractCapUtilsImpl {
public static Optional<IEnergyHandlerItem> getEnergyHandlerItem(ItemStack stack){
public class TesseractCapUtilsImpl implements TesseractCapUtils {
@Override
public Optional<IEnergyHandlerItem> getEnergyHandlerItem(ItemStack stack){
IEnergyHandlerItem energyHandler = ContainerItemContext.withInitial(stack).find(TesseractLookups.ENERGY_HANDLER_ITEM);
return Optional.ofNullable(energyHandler);
}

public static Optional<IEnergyHandlerItem> getWrappedEnergyHandlerItem(ItemStack stack){
@Override
public Optional<IEnergyHandlerItem> getWrappedEnergyHandlerItem(ItemStack stack){
IEnergyHandlerItem energyHandler = ContainerItemContext.withInitial(stack).find(TesseractLookups.ENERGY_HANDLER_ITEM);
if (energyHandler == null){
EnergyStorage storage = ContainerItemContext.withInitial(stack).find(EnergyStorage.ITEM);
Expand All @@ -57,7 +60,8 @@ public static Optional<IEnergyHandlerItem> getWrappedEnergyHandlerItem(ItemStack
return Optional.ofNullable(energyHandler);
}

public static Optional<IEnergyHandler> getEnergyHandler(@NotNull BlockEntity entity, Direction side){
@Override
public Optional<IEnergyHandler> 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()){
Expand All @@ -69,26 +73,30 @@ public static Optional<IEnergyHandler> getEnergyHandler(@NotNull BlockEntity ent
return Optional.ofNullable(energyHandler);
}

public static Optional<IHeatHandler> getHeatHandler(BlockEntity entity, Direction side){
@Override
public Optional<IHeatHandler> 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<PlatformItemHandler> getItemHandler(BlockEntity tile, Direction side){
@Override
public Optional<PlatformItemHandler> getItemHandler(BlockEntity tile, Direction side){
Storage<ItemVariant> 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<PlatformFluidHandler> getFluidHandler(Level level, BlockPos pos, Direction side){
@Override
public Optional<PlatformFluidHandler> getFluidHandler(Level level, BlockPos pos, Direction side){
BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity != null) return FluidHooks.safeGetBlockFluidManager(blockEntity, side);
Storage<FluidVariant> 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;
Expand All @@ -105,23 +113,24 @@ 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;
if (storage instanceof IEnergyHandlerStorage handlerStorage) return handlerStorage.getEnergyHandler();
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;
if (moveable instanceof IEnergyHandlerMoveable moveable1) return moveable1.getEnergyHandler();
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@

@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<IEnergyHandler> capability = TesseractCapUtilsImpl.getEnergyHandler(tile, direction);
Optional<IEnergyHandler> capability = TesseractCapUtils.INSTANCE.getEnergyHandler(tile, direction);
if (capability.isPresent()) {
if (invalidate != null) ((TileListeners)tile).addListener(() -> invalidate.run());
return capability.get();
}
return null;
}

@Override
public IRFNode getRFNode(Level level, long pos, Direction capSide, Runnable capCallback){
BlockEntity tile = level.getBlockEntity(BlockPos.of(pos));
if (tile == null) {
Expand All @@ -60,25 +62,29 @@ 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<IHeatHandler> capability = TesseractCapUtils.getHeatHandler(tile, direction);
Optional<IHeatHandler> capability = TesseractCapUtils.INSTANCE.getHeatHandler(tile, direction);
if (capability.isPresent()) {
if (invalidate != null) ((TileListeners)tile).addListener(invalidate);
return capability.get();
}
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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tesseract.fabric.TesseractCapUtilsImpl
30 changes: 20 additions & 10 deletions forge/src/main/java/tesseract/forge/TesseractCapUtilsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -36,12 +37,14 @@
import javax.annotation.Nullable;
import java.util.Optional;

public class TesseractCapUtilsImpl {
public static Optional<IEnergyHandlerItem> getEnergyHandlerItem(ItemStack stack){
public class TesseractCapUtilsImpl implements TesseractCapUtils {
@Override
public Optional<IEnergyHandlerItem> getEnergyHandlerItem(ItemStack stack){
return stack.getCapability(TesseractCaps.ENERGY_HANDLER_CAPABILITY_ITEM).map(e -> e);
}

public static Optional<IEnergyHandlerItem> getWrappedEnergyHandlerItem(ItemStack stack){
@Override
public Optional<IEnergyHandlerItem> 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);
Expand All @@ -54,28 +57,33 @@ public static Optional<IEnergyHandlerItem> getWrappedEnergyHandlerItem(ItemStack
return Optional.ofNullable(energyHandler);
}

public static Optional<IEnergyHandler> getEnergyHandler(BlockEntity entity, Direction side){
@Override
public Optional<IEnergyHandler> getEnergyHandler(BlockEntity entity, Direction side){
LazyOptional<IEnergyHandler> 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<IEnergyHandler> getWrappedHandler(BlockEntity be, @Nullable Direction side){
public LazyOptional<IEnergyHandler> 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<IHeatHandler> getHeatHandler(BlockEntity entity, Direction side){
@Override
public Optional<IHeatHandler> getHeatHandler(BlockEntity entity, Direction side){
return entity.getCapability(TesseractCaps.HEAT_CAPABILITY, side).map(e -> e);
}

public static Optional<PlatformItemHandler> getItemHandler(BlockEntity entity, Direction side){
@Override
public Optional<PlatformItemHandler> getItemHandler(BlockEntity entity, Direction side){
return entity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side).map(ForgePlatformItemHandler::new);
}
public static Optional<PlatformFluidHandler> getFluidHandler(Level level, BlockPos pos, Direction side){

@Override
public Optional<PlatformFluidHandler> getFluidHandler(Level level, BlockPos pos, Direction side){
BlockEntity entity = level.getBlockEntity(pos);
if (entity == null){
BlockState state = level.getBlockState(pos);
Expand All @@ -87,7 +95,8 @@ public static Optional<PlatformFluidHandler> 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<IFluidHandler> capability;
if (tile == null){
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@
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<IEnergyHandler> capability = TesseractCapUtilsImpl.getEnergyHandler(tile, direction).map(e -> LazyOptional.of(() -> e)).orElse(LazyOptional.empty());
LazyOptional<IEnergyHandler> 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();
}
return null;
}

@Override
public IRFNode getRFNode(Level level, long pos, Direction capSide, Runnable capCallback){
BlockEntity tile = level.getBlockEntity(BlockPos.of(pos));
if (tile == null) {
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tesseract.forge.TesseractCapUtilsImpl

0 comments on commit 69f9d86

Please sign in to comment.