Skip to content

Commit

Permalink
Improve/fix exposure of capabilities from transmitters so that they p…
Browse files Browse the repository at this point in the history
…roperly don't expose a capability at all on a given side when the connection type is set to none mekanism#6708
  • Loading branch information
pupnewfster committed Nov 24, 2020
1 parent d68da7f commit 66d4b4f
Show file tree
Hide file tree
Showing 51 changed files with 464 additions and 421 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import mekanism.api.RelativeSide;
import mekanism.api.math.FloatingLong;
import mekanism.common.capabilities.Capabilities;
import mekanism.common.capabilities.resolver.basic.BasicCapabilityResolver;
import mekanism.common.capabilities.resolver.BasicCapabilityResolver;
import mekanism.common.tile.interfaces.IBoundingBlock;
import mekanism.common.util.WorldUtils;
import mekanism.generators.common.config.MekanismGeneratorsConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import mekanism.api.lasers.ILaserReceptor;
import mekanism.api.math.FloatingLong;
import mekanism.common.capabilities.Capabilities;
import mekanism.common.capabilities.resolver.basic.BasicCapabilityResolver;
import mekanism.common.capabilities.resolver.BasicCapabilityResolver;
import mekanism.generators.common.content.fusion.FusionReactorMultiblockData;
import mekanism.generators.common.registries.GeneratorsBlocks;
import net.minecraft.entity.player.PlayerEntity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private VoxelShape getRealShape(IBlockReader world, BlockPos pos) {
return getCenter();
}
Transmitter<?, ?, ?> transmitter = tile.getTransmitter();
ConnectionType[] connectionTypes = new ConnectionType[transmitter.connectionTypes.length];
ConnectionType[] connectionTypes = new ConnectionType[transmitter.getConnectionTypesRaw().length];
for (int i = 0; i < EnumUtils.DIRECTIONS.length; i++) {
//Get the actual connection types
connectionTypes[i] = transmitter.getConnectionType(EnumUtils.DIRECTIONS[i]);
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/mekanism/common/capabilities/DynamicHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package mekanism.common.capabilities;

import java.util.List;
import java.util.function.Function;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import mcp.MethodsReturnNonnullByDefault;
import mekanism.api.IContentsListener;
import mekanism.api.annotations.FieldsAreNonnullByDefault;
import net.minecraft.util.Direction;

@FieldsAreNonnullByDefault
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public abstract class DynamicHandler<TANK> implements IContentsListener {

protected final Function<Direction, List<TANK>> containerSupplier;
protected final InteractPredicate canExtract;
protected final InteractPredicate canInsert;
@Nullable
private final IContentsListener listener;

protected DynamicHandler(Function<Direction, List<TANK>> containerSupplier, InteractPredicate canExtract, InteractPredicate canInsert,
@Nullable IContentsListener listener) {
this.containerSupplier = containerSupplier;
this.canExtract = canExtract;
this.canInsert = canInsert;
this.listener = listener;
}

@Override
public void onContentsChanged() {
if (listener != null) {
listener.onContentsChanged();
}
}

@FunctionalInterface
public interface InteractPredicate {

InteractPredicate ALWAYS_TRUE = (tank, side) -> true;

boolean test(int tank, @Nullable Direction side);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,23 @@
import mekanism.api.chemical.slurry.ISlurryTank;
import mekanism.api.chemical.slurry.Slurry;
import mekanism.api.chemical.slurry.SlurryStack;
import mekanism.common.capabilities.DynamicHandler;
import net.minecraft.util.Direction;

@FieldsAreNonnullByDefault
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public abstract class DynamicChemicalHandler<CHEMICAL extends Chemical<CHEMICAL>, STACK extends ChemicalStack<CHEMICAL>, TANK extends IChemicalTank<CHEMICAL, STACK>>
implements IMekanismChemicalHandler<CHEMICAL, STACK, TANK> {
extends DynamicHandler<TANK> implements IMekanismChemicalHandler<CHEMICAL, STACK, TANK> {

private final Function<Direction, List<TANK>> tankSupplier;
private final InteractPredicate canExtract;
private final InteractPredicate canInsert;
private final IContentsListener listener;

protected DynamicChemicalHandler(Function<Direction, List<TANK>> tankSupplier, InteractPredicate canExtract, InteractPredicate canInsert, IContentsListener listener) {
this.tankSupplier = tankSupplier;
this.canExtract = canExtract;
this.canInsert = canInsert;
this.listener = listener;
protected DynamicChemicalHandler(Function<Direction, List<TANK>> tankSupplier, InteractPredicate canExtract, InteractPredicate canInsert,
@Nullable IContentsListener listener) {
super(tankSupplier, canExtract, canInsert, listener);
}

@Override
public List<TANK> getChemicalTanks(@Nullable Direction side) {
return tankSupplier.apply(side);
}

@Override
public void onContentsChanged() {
listener.onContentsChanged();
return containerSupplier.apply(side);
}

@Override
Expand All @@ -70,38 +59,34 @@ public STACK extractChemical(int tank, long amount, @Nullable Direction side, Ac
return canExtract.test(tank, side) ? IMekanismChemicalHandler.super.extractChemical(tank, amount, side, action) : getEmptyStack();
}

@FunctionalInterface
public interface InteractPredicate {

InteractPredicate ALWAYS_TRUE = (tank, side) -> true;

boolean test(int tank, @Nullable Direction side);
}

public static class DynamicGasHandler extends DynamicChemicalHandler<Gas, GasStack, IGasTank> implements IMekanismGasHandler {

public DynamicGasHandler(Function<Direction, List<IGasTank>> tankSupplier, InteractPredicate canExtract, InteractPredicate canInsert, IContentsListener listener) {
public DynamicGasHandler(Function<Direction, List<IGasTank>> tankSupplier, InteractPredicate canExtract, InteractPredicate canInsert,
@Nullable IContentsListener listener) {
super(tankSupplier, canExtract, canInsert, listener);
}
}

public static class DynamicInfusionHandler extends DynamicChemicalHandler<InfuseType, InfusionStack, IInfusionTank> implements IMekanismInfusionHandler {

public DynamicInfusionHandler(Function<Direction, List<IInfusionTank>> tankSupplier, InteractPredicate canExtract, InteractPredicate canInsert, IContentsListener listener) {
public DynamicInfusionHandler(Function<Direction, List<IInfusionTank>> tankSupplier, InteractPredicate canExtract, InteractPredicate canInsert,
@Nullable IContentsListener listener) {
super(tankSupplier, canExtract, canInsert, listener);
}
}

public static class DynamicPigmentHandler extends DynamicChemicalHandler<Pigment, PigmentStack, IPigmentTank> implements IMekanismPigmentHandler {

public DynamicPigmentHandler(Function<Direction, List<IPigmentTank>> tankSupplier, InteractPredicate canExtract, InteractPredicate canInsert, IContentsListener listener) {
public DynamicPigmentHandler(Function<Direction, List<IPigmentTank>> tankSupplier, InteractPredicate canExtract, InteractPredicate canInsert,
@Nullable IContentsListener listener) {
super(tankSupplier, canExtract, canInsert, listener);
}
}

public static class DynamicSlurryHandler extends DynamicChemicalHandler<Slurry, SlurryStack, ISlurryTank> implements IMekanismSlurryHandler {

public DynamicSlurryHandler(Function<Direction, List<ISlurryTank>> tankSupplier, InteractPredicate canExtract, InteractPredicate canInsert, IContentsListener listener) {
public DynamicSlurryHandler(Function<Direction, List<ISlurryTank>> tankSupplier, InteractPredicate canExtract, InteractPredicate canInsert,
@Nullable IContentsListener listener) {
super(tankSupplier, canExtract, canInsert, listener);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import mcp.MethodsReturnNonnullByDefault;
import mekanism.api.NBTConstants;
import mekanism.api.chemical.merged.MergedChemicalTank;
import mekanism.common.capabilities.DynamicHandler.InteractPredicate;
import mekanism.common.capabilities.chemical.dynamic.DynamicChemicalHandler.DynamicGasHandler;
import mekanism.common.capabilities.chemical.dynamic.DynamicChemicalHandler.DynamicInfusionHandler;
import mekanism.common.capabilities.chemical.dynamic.DynamicChemicalHandler.DynamicPigmentHandler;
import mekanism.common.capabilities.chemical.dynamic.DynamicChemicalHandler.DynamicSlurryHandler;
import mekanism.common.capabilities.chemical.dynamic.DynamicChemicalHandler.InteractPredicate;
import mekanism.common.capabilities.chemical.item.ChemicalTankRateLimitChemicalTank.GasTankRateLimitChemicalTank;
import mekanism.common.capabilities.chemical.item.ChemicalTankRateLimitChemicalTank.InfusionTankRateLimitChemicalTank;
import mekanism.common.capabilities.chemical.item.ChemicalTankRateLimitChemicalTank.PigmentTankRateLimitChemicalTank;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import mekanism.api.chemical.gas.IGasTank;
import mekanism.common.capabilities.Capabilities;
import mekanism.common.capabilities.CapabilityCache;
import mekanism.common.capabilities.resolver.basic.BasicCapabilityResolver;
import mekanism.common.capabilities.resolver.BasicCapabilityResolver;

/**
* Helper class for implementing gas handlers for items
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import mekanism.api.chemical.infuse.InfusionStack;
import mekanism.common.capabilities.Capabilities;
import mekanism.common.capabilities.CapabilityCache;
import mekanism.common.capabilities.resolver.basic.BasicCapabilityResolver;
import mekanism.common.capabilities.resolver.BasicCapabilityResolver;

/**
* Helper class for implementing infusion handlers for items
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import mekanism.api.chemical.pigment.PigmentStack;
import mekanism.common.capabilities.Capabilities;
import mekanism.common.capabilities.CapabilityCache;
import mekanism.common.capabilities.resolver.basic.BasicCapabilityResolver;
import mekanism.common.capabilities.resolver.BasicCapabilityResolver;

/**
* Helper class for implementing pigment handlers for items
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import mekanism.api.chemical.slurry.SlurryStack;
import mekanism.common.capabilities.Capabilities;
import mekanism.common.capabilities.CapabilityCache;
import mekanism.common.capabilities.resolver.basic.BasicCapabilityResolver;
import mekanism.common.capabilities.resolver.BasicCapabilityResolver;

/**
* Helper class for implementing slurry handlers for items
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package mekanism.common.capabilities.energy;

import java.util.List;
import java.util.function.Function;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import mcp.MethodsReturnNonnullByDefault;
import mekanism.api.Action;
import mekanism.api.IContentsListener;
import mekanism.api.annotations.FieldsAreNonnullByDefault;
import mekanism.api.energy.IEnergyContainer;
import mekanism.api.energy.IMekanismStrictEnergyHandler;
import mekanism.api.math.FloatingLong;
import mekanism.common.capabilities.DynamicHandler;
import net.minecraft.util.Direction;

@FieldsAreNonnullByDefault
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public class DynamicStrictEnergyHandler extends DynamicHandler<IEnergyContainer> implements IMekanismStrictEnergyHandler {

public DynamicStrictEnergyHandler(Function<Direction, List<IEnergyContainer>> tankSupplier, InteractPredicate canExtract, InteractPredicate canInsert,
@Nullable IContentsListener listener) {
super(tankSupplier, canExtract, canInsert, listener);
}

@Override
public List<IEnergyContainer> getEnergyContainers(@Nullable Direction side) {
return containerSupplier.apply(side);
}

@Override
public FloatingLong insertEnergy(int container, FloatingLong amount, @Nullable Direction side, Action action) {
//If we can insert into the specific tank from that side, try to. Otherwise exit
return canInsert.test(container, side) ? IMekanismStrictEnergyHandler.super.insertEnergy(container, amount, side, action) : amount;
}

@Override
public FloatingLong extractEnergy(int container, FloatingLong amount, @Nullable Direction side, Action action) {
//If we can extract from a specific tank from a given side, try to. Otherwise exit
return canExtract.test(container, side) ? IMekanismStrictEnergyHandler.super.extractEnergy(container, amount, side, action) : FloatingLong.ZERO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import mekanism.api.energy.IMekanismStrictEnergyHandler;
import mekanism.common.capabilities.CapabilityCache;
import mekanism.common.capabilities.ItemCapabilityWrapper.ItemCapability;
import mekanism.common.capabilities.resolver.basic.EnergyCapabilityResolver;
import mekanism.common.capabilities.resolver.EnergyCapabilityResolver;
import mekanism.common.util.ItemDataUtils;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Direction;
Expand Down
Loading

0 comments on commit 66d4b4f

Please sign in to comment.