Skip to content

Commit

Permalink
Cleaned up redstone signal handling (Note: Default mode for conduits …
Browse files Browse the repository at this point in the history
…has changed to NEVER)
  • Loading branch information
HenryLoenwind committed Jan 16, 2017
1 parent 6ccbb71 commit e1d19be
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 121 deletions.
2 changes: 1 addition & 1 deletion src/main/java/crazypants/enderio/conduit/ConduitUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public static boolean isRedstoneControlModeMet(IConduitBundle bundle, RedstoneCo
}

int signalStrength = getInternalSignalForColor(bundle, col);
if (signalStrength < 15 && DyeColor.RED == col && bundle != null && bundle.getEntity() != null) {
if (signalStrength < RedstoneControlMode.MIN_ON_LEVEL && DyeColor.RED == col && bundle != null && bundle.getEntity() != null) {
TileEntity te = bundle.getEntity();
signalStrength = Math.max(signalStrength, isBlockIndirectlyGettingPoweredIfLoaded(te.getWorld(), te.getPos()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public void setExtractionRedstoneMode(RedstoneControlMode mode, EnumFacing dir)
public RedstoneControlMode getExtractionRedstoneMode(EnumFacing dir) {
RedstoneControlMode res = extractionModes.get(dir);
if(res == null) {
res = RedstoneControlMode.ON;
res = RedstoneControlMode.NEVER;
}
return res;
}
Expand All @@ -365,9 +365,6 @@ public DyeColor getExtractionSignalColor(EnumFacing dir) {
@Override
public boolean isExtractionRedstoneConditionMet(EnumFacing dir) {
RedstoneControlMode mode = getExtractionRedstoneMode(dir);
if(mode == null) {
return true;
}
return ConduitUtil.isRedstoneControlModeMet(getBundle(), mode, getExtractionSignalColor(dir));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package crazypants.enderio.conduit.liquid;

import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Nonnull;

import com.enderio.core.common.fluid.FluidWrapper;
import com.enderio.core.common.fluid.IFluidWrapper;
import com.enderio.core.common.util.DyeColor;
Expand All @@ -15,9 +15,7 @@
import crazypants.enderio.conduit.IConduit;
import crazypants.enderio.conduit.IConduitBundle;
import crazypants.enderio.machine.RedstoneControlMode;
import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
Expand All @@ -27,9 +25,6 @@ public abstract class AbstractLiquidConduit extends AbstractConduit implements I
protected final EnumMap<EnumFacing, RedstoneControlMode> extractionModes = new EnumMap<EnumFacing, RedstoneControlMode>(EnumFacing.class);
protected final EnumMap<EnumFacing, DyeColor> extractionColors = new EnumMap<EnumFacing, DyeColor>(EnumFacing.class);

protected final Map<EnumFacing, Integer> externalRedstoneSignals = new HashMap<EnumFacing, Integer>();
protected boolean redstoneStateDirty = true;

public static IFluidWrapper getExternalFluidHandler(IBlockAccess world, BlockPos pos, EnumFacing side) {
if (world.getTileEntity(pos) instanceof IConduitBundle) {
return null;
Expand All @@ -55,23 +50,16 @@ public Class<? extends IConduit> getBaseConduitType() {
return ILiquidConduit.class;
}

@Override
public boolean onNeighborBlockChange(Block blockId) {
redstoneStateDirty = true;
return super.onNeighborBlockChange(blockId);
}

@Override
public void setExtractionRedstoneMode(RedstoneControlMode mode, EnumFacing dir) {
extractionModes.put(dir, mode);
redstoneStateDirty = true;
}

@Override
public RedstoneControlMode getExtractionRedstoneMode(EnumFacing dir) {
public @Nonnull RedstoneControlMode getExtractionRedstoneMode(EnumFacing dir) {
RedstoneControlMode res = extractionModes.get(dir);
if(res == null) {
res = RedstoneControlMode.ON;
res = RedstoneControlMode.NEVER;
}
return res;
}
Expand Down Expand Up @@ -101,12 +89,6 @@ public boolean canOutputToDir(EnumFacing dir) {
if(!externalConnections.contains(dir)) {
return false;
}
IFluidWrapper ext = getExternalHandler(dir);
// if(ext instanceof TileReservoir) { // dont push to an auto ejecting
// // resevoir or we loop
// TileReservoir tr = (TileReservoir) ext;
// return !tr.isAutoEject();
// }
return true;
}

Expand All @@ -115,45 +97,7 @@ protected boolean autoExtractForDir(EnumFacing dir) {
return false;
}
RedstoneControlMode mode = getExtractionRedstoneMode(dir);
if(mode == RedstoneControlMode.IGNORE) {
return true;
}
if(mode == RedstoneControlMode.NEVER) {
return false;
}
if(redstoneStateDirty) {
externalRedstoneSignals.clear();
redstoneStateDirty = false;
}

DyeColor col = getExtractionSignalColor(dir);
int signal = ConduitUtil.getInternalSignalForColor(getBundle(), col);

boolean res;
if(mode == RedstoneControlMode.OFF) {
//if checking for no signal, must be no signal from both
res = RedstoneControlMode.isConditionMet(mode, signal) && (col != DyeColor.RED || isConditionMetByExternalSignal(dir, mode, col));
} else {
//if checking for a signal, either is fine
res = RedstoneControlMode.isConditionMet(mode, signal) || (col == DyeColor.RED && isConditionMetByExternalSignal(dir, mode, col));
}
return res;
}

private boolean isConditionMetByExternalSignal(EnumFacing dir, RedstoneControlMode mode, DyeColor col) {
int externalSignal = 0;
if(col == DyeColor.RED) {
Integer val = externalRedstoneSignals.get(dir);
if(val == null) {
TileEntity te = getBundle().getEntity();
externalSignal = ConduitUtil.isBlockIndirectlyGettingPoweredIfLoaded(te.getWorld(), te.getPos());
externalRedstoneSignals.put(dir, externalSignal);
} else {
externalSignal = val;
}
}

return RedstoneControlMode.isConditionMet(mode, externalSignal);
return ConduitUtil.isRedstoneControlModeMet(getBundle(), mode, getExtractionSignalColor(dir));
}

@Override
Expand Down
43 changes: 1 addition & 42 deletions src/main/java/crazypants/enderio/conduit/power/PowerConduit.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ public void registerIcons(TextureMap register) {

protected EnumMap<EnumFacing, Long> recievedTicks;

private final Map<EnumFacing, Integer> externalRedstoneSignals = new EnumMap<EnumFacing, Integer>(EnumFacing.class);

private boolean redstoneStateDirty = true;

public PowerConduit() {
}

Expand Down Expand Up @@ -262,44 +258,8 @@ public void setEnergyStored(int energyStored) {


private boolean isRedstoneEnabled(EnumFacing dir) {

RedstoneControlMode mode = getExtractionRedstoneMode(dir);
if(mode == RedstoneControlMode.NEVER) {
return false;
}
if(mode == RedstoneControlMode.IGNORE) {
return true;
}

DyeColor col = getExtractionSignalColor(dir);
int signal = ConduitUtil.getInternalSignalForColor(getBundle(), col);
int exSig = getExternalRedstoneSignalForDir(dir);
boolean res;
if(mode == RedstoneControlMode.OFF) {
//if checking for no signal, must be no signal from both
res = RedstoneControlMode.isConditionMet(mode, signal) && (col != DyeColor.RED || RedstoneControlMode.isConditionMet(mode, exSig));
} else {
//if checking for a signal, either is fine
res = RedstoneControlMode.isConditionMet(mode, signal) || (col == DyeColor.RED && RedstoneControlMode.isConditionMet(mode, exSig));
}
return res;
}

private int getExternalRedstoneSignalForDir(EnumFacing dir) {
if(redstoneStateDirty) {
externalRedstoneSignals.clear();
redstoneStateDirty = false;
}
Integer cached = externalRedstoneSignals.get(dir);
int result;
if(cached == null) {
TileEntity te = getBundle().getEntity();
result = ConduitUtil.isBlockIndirectlyGettingPoweredIfLoaded(te.getWorld(), te.getPos());
externalRedstoneSignals.put(dir, result);
} else {
result = cached;
}
return result;
return ConduitUtil.isRedstoneControlModeMet(getBundle(), mode, getExtractionSignalColor(dir));
}

@Override
Expand Down Expand Up @@ -338,7 +298,6 @@ private boolean recievedRfThisTick(EnumFacing dir) {

@Override
public boolean onNeighborBlockChange(Block blockId) {
redstoneStateDirty = true;
if(network != null && network.powerManager != null) {
network.powerManager.receptorsChanged();
}
Expand Down
38 changes: 25 additions & 13 deletions src/main/java/crazypants/enderio/machine/RedstoneControlMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public enum RedstoneControlMode {
OFF,
NEVER;

/**
* This is the highest level that will make a difference. Meaning, that if it is known that the input level is at least MIN_ON_LEVEL, then there is no need to
* run any checks for a higher level.
*/
public static final int MIN_ON_LEVEL = 1;

// @SideOnly(Side.CLIENT)
@SuppressWarnings("hiding")
public static enum IconHolder implements ICycleEnum {
Expand Down Expand Up @@ -65,23 +71,29 @@ public static IconHolder getFromMode(RedstoneControlMode mode) {
}

public static boolean isConditionMet(RedstoneControlMode redstoneControlMode, int powerLevel) {
boolean redstoneCheckPassed = true;
if(redstoneControlMode == RedstoneControlMode.NEVER) {
redstoneCheckPassed = false;
} else if(redstoneControlMode == RedstoneControlMode.ON) {
if(powerLevel < 1) {
redstoneCheckPassed = false;
}
} else if(redstoneControlMode == RedstoneControlMode.OFF) {
if(powerLevel > 0) {
redstoneCheckPassed = false;
}
switch (redstoneControlMode) {
case IGNORE:
return true;
case NEVER:
return false;
case OFF:
return powerLevel == 0;
case ON:
return powerLevel > 0;
default:
return false;
}
return redstoneCheckPassed;
}

public static boolean isConditionMet(RedstoneControlMode redstoneControlMode, TileEntity te) {
return isConditionMet(redstoneControlMode, ConduitUtil.isBlockIndirectlyGettingPoweredIfLoaded(te.getWorld(), te.getPos()));
switch (redstoneControlMode) {
case IGNORE:
return true;
case NEVER:
return false;
default:
return isConditionMet(redstoneControlMode, ConduitUtil.isBlockIndirectlyGettingPoweredIfLoaded(te.getWorld(), te.getPos()));
}
}

public RedstoneControlMode next() {
Expand Down

0 comments on commit e1d19be

Please sign in to comment.