Skip to content

Commit f7785b9

Browse files
committed
Refine power cable implementation flexibility.
1 parent 5938a08 commit f7785b9

File tree

2 files changed

+42
-13
lines changed

2 files changed

+42
-13
lines changed

loader/src/main/java/com/fox2code/foxloader/energy/FoxPowerCableBlock.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public FoxPowerCableTileEntity getBlockEntity() {
5151
((FoxPowerCableTileEntity) tileEntity).powerInterface : null;
5252
}
5353

54+
@Override
55+
public @Nullable FoxPowerInterface getPowerInterfaceForFace(BlockAccess blockAccess, int x, int y, int z, int blockFace) {
56+
TileEntity tileEntity = blockAccess.getBlockTileEntity(x, y, z);
57+
return tileEntity instanceof FoxPowerCableTileEntity ?
58+
((FoxPowerCableTileEntity) tileEntity).getPowerInterfaceForFace(
59+
Direction.EnumDirection.VALID_DIRECTIONS[blockFace]) : null;
60+
}
61+
5462
@Override
5563
public void onBlockAdded(World world, int x, int y, int z) {
5664
FoxPowerCableTileEntity foxPowerCableTileEntity = this.getBlockEntity();

loader/src/main/java/com/fox2code/foxloader/energy/FoxPowerCableTileEntity.java

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
import net.minecraft.common.block.tileentity.TileEntity;
2828
import net.minecraft.common.util.Direction;
2929
import org.jetbrains.annotations.NotNull;
30+
import org.jetbrains.annotations.Nullable;
3031

3132
import java.util.Arrays;
33+
import java.util.Objects;
3234

3335
public class FoxPowerCableTileEntity extends TileEntity {
3436
private final FoxPowerCableBlock cableBlock;
@@ -38,19 +40,27 @@ public class FoxPowerCableTileEntity extends TileEntity {
3840
private int sinkPriority;
3941
private int sinkSource;
4042

41-
public FoxPowerCableTileEntity(FoxPowerCableBlock cableBlock) {
43+
public FoxPowerCableTileEntity(@NotNull FoxPowerCableBlock cableBlock) {
4244
this.cableBlock = cableBlock;
43-
this.powerInterface = this.makeFoxPowerInterface();
45+
this.powerInterface = Objects.requireNonNull(this.makeFoxPowerInterface(), "makeFoxPowerInterface() -> null");
4446
this.sinkPriorities = new int[6];
4547
this.storedFoxPower = 0;
4648
this.sinkPriority = 1;
4749
this.sinkSource = -1;
4850
}
4951

50-
protected FoxPowerInterface makeFoxPowerInterface() {
52+
protected @NotNull FoxPowerInterface makeFoxPowerInterface() {
5153
return new FoxPowerInterfaceCable(this);
5254
}
5355

56+
protected @Nullable FoxPowerInterface getPowerInterfaceForFace(@NotNull Direction.EnumDirection direction) {
57+
return this.powerInterface;
58+
}
59+
60+
protected boolean allowDispatchPower(@NotNull Direction.EnumDirection direction) {
61+
return true;
62+
}
63+
5464
@Override
5565
public void readFromNBT(CompoundTag tag) {
5666
super.readFromNBT(tag);
@@ -88,19 +98,27 @@ public void writeToNBT(CompoundTag tag) {
8898
tag.setIntArray("sinkPriorities", this.sinkPriorities);
8999
}
90100

101+
public int getMinSinkPriority() {
102+
return 1;
103+
}
104+
91105
public void updateSinkPriorities() {
92-
int cableSinkPriority = 1;
106+
final int minSinkPriority = this.worldObj.isRemote ? 1 : this.getMinSinkPriority();
107+
int cableSinkPriority = minSinkPriority;
93108
int cableSinkFaceSource = -1;
94109
final int oldCableSinkPriority = this.sinkPriority;
95110
final int oldCableSinkFaceSource = this.sinkSource;
96111
final int oldCableSinkFaceSourcePriority = // For fast un-propagation.
97112
oldCableSinkFaceSource == -1 ? 0 : this.sinkPriorities[oldCableSinkFaceSource];
98113
for (Direction.EnumDirection blockFace : Direction.EnumDirection.VALID_DIRECTIONS) {
99-
FoxPowerInterface powerInterface = FoxPowerUtils.getPowerInterface(this.worldObj,
114+
FoxPowerInterface powerInterface = this.allowDispatchPower(blockFace) ?
115+
FoxPowerUtils.getPowerInterface(this.worldObj,
100116
this.xCoord + blockFace.offsX, this.yCoord + blockFace.offsY,
101-
this.zCoord + blockFace.offsZ, blockFace.ordinal() ^ 1);
117+
this.zCoord + blockFace.offsZ, blockFace.ordinal() ^ 1) : null;
102118
int faceSinkPriority = powerInterface == null ? 0 :
103-
Math.min(FoxPowerUtils.maxSinkPriorityValue, powerInterface.getCableSinkPriority());
119+
Math.min(FoxPowerUtils.maxSinkPriorityValue,
120+
powerInterface.getFoxPowerStorageMaxInput() > 0 ?
121+
powerInterface.getCableSinkPriority() : 0);
104122
this.sinkPriorities[blockFace.ordinal()] = faceSinkPriority;
105123
faceSinkPriority -= 1;
106124
if (faceSinkPriority >= cableSinkPriority) {
@@ -112,7 +130,7 @@ public void updateSinkPriorities() {
112130
if (!this.worldObj.isRemote) {
113131
if (this.updateSinkPriorityValues(oldCableSinkFaceSource,
114132
oldCableSinkPriority, oldCableSinkFaceSourcePriority,
115-
cableSinkPriority, cableSinkFaceSource)) {
133+
cableSinkPriority, cableSinkFaceSource, minSinkPriority)) {
116134
FoxPowerUtils.notifyNeighboringPowerBlocks(
117135
this.worldObj, this.xCoord, this.yCoord, this.zCoord);
118136
}
@@ -124,12 +142,15 @@ public boolean updateSinkPriorityForFace(Direction.EnumDirection blockFace) {
124142
this.xCoord + blockFace.offsX, this.yCoord + blockFace.offsY,
125143
this.zCoord + blockFace.offsZ, blockFace.ordinal() ^ 1);
126144
this.sinkPriorities[blockFace.ordinal()] = powerInterface == null ? 0 :
127-
Math.min(FoxPowerUtils.maxSinkPriorityValue, powerInterface.getCableSinkPriority());
145+
Math.min(FoxPowerUtils.maxSinkPriorityValue,
146+
powerInterface.getFoxPowerStorageMaxInput() > 0 ?
147+
powerInterface.getCableSinkPriority() : 0);
128148
if (worldObj.isRemote) {
129149
// Skip any expensive calculations on client only worlds
130150
return false;
131151
}
132-
int cableSinkPriority = 1;
152+
final int minSinkPriority = this.getMinSinkPriority();
153+
int cableSinkPriority = minSinkPriority;
133154
int cableSinkFaceSource = -1;
134155
final int oldCableSinkPriority = this.sinkPriority;
135156
final int oldCableSinkFaceSource = this.sinkSource;
@@ -144,16 +165,16 @@ public boolean updateSinkPriorityForFace(Direction.EnumDirection blockFace) {
144165
}
145166
return this.updateSinkPriorityValues(oldCableSinkFaceSource,
146167
oldCableSinkPriority, oldCableSinkFaceSourcePriority,
147-
cableSinkPriority, cableSinkFaceSource);
168+
cableSinkPriority, cableSinkFaceSource, minSinkPriority);
148169
}
149170

150171
protected final boolean updateSinkPriorityValues(
151172
int oldCableSinkFaceSource, int oldCableSinkPriority, int oldCableSinkFaceSourcePriority,
152-
int cableSinkPriority, int cableSinkFaceSource) {
173+
int cableSinkPriority, int cableSinkFaceSource, int minSinkPriority) {
153174
if (oldCableSinkFaceSource != -1 && cableSinkPriority < oldCableSinkPriority &&
154175
this.sinkPriorities[oldCableSinkFaceSource] < oldCableSinkFaceSourcePriority) {
155176
// Allow power sink to un-propagate faster, this is an attempt to get "O(n)" un-propagation.
156-
cableSinkPriority = Math.max(this.sinkPriorities[oldCableSinkFaceSource] - 1, 1);
177+
cableSinkPriority = Math.max(this.sinkPriorities[oldCableSinkFaceSource] - 1, minSinkPriority);
157178
}
158179
this.sinkSource = cableSinkFaceSource;
159180
this.sinkPriority = cableSinkPriority;

0 commit comments

Comments
 (0)