2727import net .minecraft .common .block .tileentity .TileEntity ;
2828import net .minecraft .common .util .Direction ;
2929import org .jetbrains .annotations .NotNull ;
30+ import org .jetbrains .annotations .Nullable ;
3031
3132import java .util .Arrays ;
33+ import java .util .Objects ;
3234
3335public 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