Skip to content

Commit

Permalink
repair capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
khjxiaogu committed Dec 31, 2024
1 parent 688dc9e commit c49c7af
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,17 @@ public LazyOptional<C> getCapability(Object cap) {
return ((ICapabilityProvider)cap).getCapability(capability);
return LazyOptional.empty();
}
public LazyOptional<C> getCapability(Object cap,Direction dir) {
if(cap instanceof ICapabilityProvider)
return ((ICapabilityProvider)cap).getCapability(capability,dir);
return LazyOptional.empty();
}
public Capability<C> capability() {
return capability;
}
public boolean isCapability(Capability<?> cap) {
return capability==cap;
}
public Class<C> getCapClass() {
return capClass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@
import java.util.function.Function;

public class T2GeneratorLogic extends GeneratorLogic<T2GeneratorLogic, T2GeneratorState> {
private static final MultiblockFace REDSTONE_OFFSET = new MultiblockFace(1, 1, 2, RelativeBlockFace.BACK);
private static final MultiblockFace REDSTONE_OFFSET = new MultiblockFace(1, 1, 0, RelativeBlockFace.FRONT);
// TODO: Check if FRONT is correct
private static final MultiblockFace NETWORK_OFFSET = new MultiblockFace(1, 0, 0, RelativeBlockFace.FRONT);
private static final CapabilityPosition NETWORK_CAP = CapabilityPosition.opposing(NETWORK_OFFSET);
private static final MultiblockFace FLUID_INPUT_OFFSET = new MultiblockFace(1, 0, 2, RelativeBlockFace.BACK);
private static final CapabilityPosition FLUID_INPUT_CAP = CapabilityPosition.opposing(FLUID_INPUT_OFFSET);
private static final CapabilityPosition NETWORK_CAP = new CapabilityPosition(1, 0, 2, RelativeBlockFace.BACK);
private static final CapabilityPosition FLUID_INPUT_CAP = new CapabilityPosition(1, 0, 0, RelativeBlockFace.FRONT);

public T2GeneratorLogic() {
super();
Expand Down Expand Up @@ -85,7 +83,9 @@ public void tickEffects(IMultiblockContext<T2GeneratorState> ctx, BlockPos pos,

@Override
public <C> LazyOptional<C> getCapability(IMultiblockContext<T2GeneratorState> ctx, CapabilityPosition position, Capability<C> capability) {
if (capability == FHCapabilities.HEAT_EP.capability() && NETWORK_CAP.equals(position)) {
//System.out.println(position);
//System.out.println(capability);
if (FHCapabilities.HEAT_EP.isCapability(capability) && NETWORK_CAP.equalsOrNullFace(position)) {
return getData(ctx).map(t -> t.epcap).orElseGet(LazyOptional::empty).cast();
} else if (capability == ForgeCapabilities.FLUID_HANDLER && FLUID_INPUT_CAP.equals(position)) {
LazyOptional<IFluidHandler> tankCap = ctx.getState().tankCap;
Expand All @@ -96,13 +96,12 @@ public <C> LazyOptional<C> getCapability(IMultiblockContext<T2GeneratorState> ct

@Override
protected boolean tickFuel(IMultiblockContext<T2GeneratorState> ctx) {
if (ctx.getState().manager == null) {
ctx.getState().manager = new HeatEnergyNetwork(ctx.getLevel().getBlockEntity(BlockPos.ZERO), c -> {
BlockPos pos = FHMultiblockHelper.getAbsoluteMaster(ctx.getLevel());
BlockPos networkPos = pos.offset(NETWORK_OFFSET.posInMultiblock());
Direction dir = ctx.getLevel().getOrientation().front();
c.accept(networkPos.relative(dir.getOpposite()), dir.getOpposite());

if (!ctx.getState().manager.hasBounded()) {
ctx.getState().manager.bind(ctx.getLevel().getBlockEntity(BlockPos.ZERO), c -> {
BlockPos networkPos = ctx.getLevel().toAbsolute(NETWORK_CAP.posInMultiblock());
Direction dir = NETWORK_CAP.side().forFront(ctx.getLevel().getOrientation());
// System.out.println(networkPos.relative(dir)+"-"+dir);
c.accept(networkPos.relative(dir), dir);
});
}
if ((!getData(ctx).map(t -> t.ep.hasValidNetwork()).orElse(true) || ctx.getState().manager.data.size() <= 1) && !ctx.getState().manager.isUpdateRequested()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ public void disassemble(Level world, BlockPos origin, boolean mirrored, Directio
ChunkHeatData.removeTempAdjust(world, origin.offset(master));
FHMultiblockHelper.getBEHelper(world, origin.offset(master)).ifPresent(te -> {
T2GeneratorState state = (T2GeneratorState) te.getState();
if (state != null)
state.manager.invalidate();
else
if (state != null) {
if(state.manager!=null)
state.manager.invalidate();
}else
FHMain.LOGGER.error("T2GeneratorState is null when disassembling T2GeneratorMultiblock.");
});
super.disassemble(world, origin, mirrored, clickDirectionAtCreation);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package com.teammoeg.frostedheart.content.climate.heatdevice.generator.t2;

import blusunrize.immersiveengineering.api.multiblocks.blocks.env.IInitialMultiblockContext;
import blusunrize.immersiveengineering.api.multiblocks.blocks.util.StoredCapability;
import blusunrize.immersiveengineering.common.fluids.ArrayFluidHandler;
import com.teammoeg.frostedheart.content.climate.heatdevice.generator.GeneratorSteamRecipe;
import com.teammoeg.frostedheart.content.climate.heatdevice.generator.GeneratorState;
import com.teammoeg.frostedheart.content.steamenergy.HeatEnergyNetwork;
import com.teammoeg.frostedheart.content.steamenergy.capabilities.HeatProviderEndPoint;

import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fluids.IFluidTank;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.templates.FluidTank;

Expand All @@ -20,7 +13,7 @@ public class T2GeneratorState extends GeneratorState {
public FluidTank tank = new FluidTank(TANK_CAPACITY,
f -> GeneratorSteamRecipe.findRecipe(f) != null);
public LazyOptional<IFluidHandler> tankCap = LazyOptional.of(() -> tank);
HeatEnergyNetwork manager;
HeatEnergyNetwork manager=new HeatEnergyNetwork();

int liquidtick = 0;
int noliquidtick = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected void tickShutdown(IMultiblockContext<RadiatorState> ctx) {

@Override
public <T> LazyOptional<T> getCapability(IMultiblockContext<RadiatorState> ctx, CapabilityPosition position, Capability<T> cap) {
if (cap == FHCapabilities.HEAT_EP.capability() && position.posInMultiblock().getY() == 0) {
if (FHCapabilities.HEAT_EP.isCapability(cap) && position.posInMultiblock().getY() == 0) {
return ctx.getState().heatcap.cast();
}
return super.getCapability(ctx, position, cap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public HeatIncubatorTileEntity(BlockPos bp,BlockState bs) {
LazyOptional<HeatConsumerEndpoint> heatcap=LazyOptional.of(()->network);
@Nonnull
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, Direction facing) {
if(capability==FHCapabilities.HEAT_EP.capability()&&facing == this.getBlockState().getValue(IncubatorBlock.HORIZONTAL_FACING)) {
if(FHCapabilities.HEAT_EP.isCapability(capability)&&facing == this.getBlockState().getValue(IncubatorBlock.HORIZONTAL_FACING)) {
return heatcap.cast();
}
return super.getCapability(capability, facing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class HeatEnergyNetwork implements MenuProvider,NBTSerializable{
private transient Consumer<BiConsumer<BlockPos, Direction>> onConnect;
transient Level world;
transient BlockEntity cur;

transient boolean isBound;
transient PriorityQueue<HeatEndpoint> endpoints=new PriorityQueue<>(Comparator.comparingInt(HeatEndpoint::getPriority).reversed().thenComparing(HeatEndpoint::getDistance));
public Map<HeatEndpoint,EndPointData> data=new HashMap<>();
public Set<EndPointData> epdataset=new HashSet<>();
Expand Down Expand Up @@ -94,15 +94,16 @@ public void load(CompoundTag nbt, boolean isPacket) {
}
private BiConsumer<BlockPos, Direction> connect = (pos, d) -> {
if(getWorld()!=null) {
System.out.println("running");
BlockEntity te = Utils.getExistingTileEntity(getWorld(), pos);
if (te instanceof INetworkConsumer)
((INetworkConsumer) te).tryConnectAt(this,d, 1);
else if(te!=null)
te.getCapability(FHCapabilities.HEAT_EP.capability(),d).ifPresent(t->t.reciveConnection(getWorld(),pos,this,d,1));
FHCapabilities.HEAT_EP.getCapability(te,d).ifPresent(t->t.reciveConnection(getWorld(),pos,this,d,1));
if(cur instanceof INetworkConsumer) {
((INetworkConsumer) cur).tryConnectAt(this, d.getOpposite(), 0);
}else if(cur!=null) {
cur.getCapability(FHCapabilities.HEAT_EP.capability(),d.getOpposite()).ifPresent(t->t.reciveConnection(getWorld(),cur.getBlockPos(),this,d.getOpposite(),0));
FHCapabilities.HEAT_EP.getCapability(cur,d.getOpposite()).ifPresent(t->t.reciveConnection(getWorld(),cur.getBlockPos(),this,d.getOpposite(),0));
}
}
};
Expand Down Expand Up @@ -153,13 +154,12 @@ public boolean removeEndpoint(BlockPos pos,HeatEndpoint heatEndpoint) {
public AbstractContainerMenu createMenu(int p1, Inventory p2, Player p3) {
return new HeatStatContainer(p1,p3,this);
}
/**
* Instantiates a new HeatProviderManager.<br>
*
* @param cur the current tile entity<br>
* @param con the function that called when refresh is required. Should provide connect direction and location when called.<br>
*/
public HeatEnergyNetwork(BlockEntity cur, Consumer<BiConsumer<BlockPos, Direction>> con) {

public boolean hasBounded() {
return isBound;
}
public void bind(BlockEntity cur, Consumer<BiConsumer<BlockPos, Direction>> con) {
isBound=true;
this.cur=cur;
this.onConnect = con;
}
Expand All @@ -184,6 +184,7 @@ public void tick() {
if (interval > 0) {
interval--;
}else if(interval==0){
//System.out.println("run network");
for(BlockPos bp:propagated.keySet()) {
HeatPipeTileEntity hpte=FHUtils.getExistingTileEntity(getWorld(), bp, HeatPipeTileEntity.class);
if(hpte!=null) {
Expand All @@ -195,7 +196,8 @@ public void tick() {
bp.clearConnection();
}
data.clear();
onConnect.accept(connect);
if(onConnect!=null)
onConnect.accept(connect);
dataModified=true;
interval = -1;
}
Expand Down

0 comments on commit c49c7af

Please sign in to comment.