11package github .kasuminova .novaeng .common .tile .ecotech .efabricator ;
22
3+ import appeng .api .config .Actionable ;
4+ import appeng .api .networking .energy .IEnergyGrid ;
35import appeng .api .storage .data .IAEItemStack ;
46import appeng .api .storage .data .IItemList ;
7+ import appeng .me .GridAccessException ;
58import appeng .util .item .AEItemStack ;
69import github .kasuminova .novaeng .NovaEngineeringCore ;
710import github .kasuminova .novaeng .common .block .ecotech .efabricator .prop .WorkerStatus ;
@@ -43,7 +46,7 @@ public class EFabricatorWorker extends EFabricatorPart {
4346 public EFabricatorWorker () {
4447 }
4548
46- public int getRemainingSpace (){
49+ public int getRemainingSpace () {
4750 return this .getQueueDepth () - this .queue .size ();
4851 }
4952
@@ -56,7 +59,18 @@ public synchronized int doWork() {
5659 } else {
5760 energyUsage = ENERGY_USAGE ;
5861 }
59- int parallelism = Math .min (Math .max (controller .getAvailableParallelism (), 1 ), energyCache / energyUsage );
62+ double energy = this .energyCache ;
63+ var c = controller .getChannel ();
64+ IEnergyGrid grid = null ;
65+ if (c != null ) {
66+ try {
67+ grid = c .proxy .getGrid ().getCache (IEnergyGrid .class );
68+ energy += grid .getStoredPower ();
69+ } catch (GridAccessException ignored ) {
70+
71+ }
72+ }
73+ int parallelism = (int ) Math .min (Math .max (controller .getAvailableParallelism (), 1 ), energy / energyUsage );
6074 if (controller .isActiveCooling ()) {
6175 parallelism = Math .min (parallelism , coolantCache / COOLANT_USAGE );
6276 }
@@ -69,7 +83,7 @@ public synchronized int doWork() {
6983 var workSize = parallelism - completed ;
7084 final var size = craftWork .size ;
7185 final CraftWork out ;
72- if (size > workSize ){
86+ if (size > workSize ) {
7387 out = craftWork .split (workSize );
7488 queue .add (craftWork );
7589 } else {
@@ -87,7 +101,18 @@ public synchronized int doWork() {
87101 }
88102
89103 if (completed > 0 ) {
90- energyCache -= energyUsage * completed ;
104+ int in = energyUsage * completed ;
105+ if (in > energyCache ) {
106+ energyCache = 0 ;
107+ in -= energyCache ;
108+ if (grid != null ) {
109+ synchronized (grid ) {
110+ grid .injectPower (in , Actionable .MODULATE );
111+ }
112+ }
113+ } else {
114+ energyCache -= in ;
115+ }
91116 if (controller .isActiveCooling ()) {
92117 controller .consumeCoolant (COOLANT_USAGE * completed );
93118 }
@@ -317,10 +342,10 @@ public static class CraftWork {
317342 private final ItemStack output ;
318343 private int size ;
319344
320- public CraftWork (final ItemStack [] remaining , final ItemStack output ,final int size ) {
345+ public CraftWork (final ItemStack [] remaining , final ItemStack output , final int size ) {
321346 this .remaining = remaining ;
322347 this .output = output ;
323- this .size = Math .max (1 ,size );
348+ this .size = Math .max (1 , size );
324349 }
325350
326351 public CraftWork (final NBTTagCompound nbt , final List <ItemStack > stackSet ) {
@@ -330,11 +355,11 @@ public CraftWork(final NBTTagCompound nbt, final List<ItemStack> stackSet) {
330355 remaining [remainIdx ] = setIdx == -1 ? ItemStack .EMPTY : stackSet .get (setIdx );
331356 }
332357 output = stackSet .get (nbt .getInteger (OUTPUT_TAG ));
333- size = Math .max (1 ,nbt .getInteger (SIZE ));
358+ size = Math .max (1 , nbt .getInteger (SIZE ));
334359 }
335360
336- public CraftWork split (int amount ){
337- final var i = Math .min (amount ,this .size );
361+ public CraftWork split (int amount ) {
362+ final var i = Math .min (amount , this .size );
338363 if (i > 0 ) {
339364 final var inputs = new ItemStack [this .remaining .length ];
340365 for (int ii = 0 ; ii < remaining .length ; ii ++) {
@@ -387,13 +412,13 @@ public NBTTagCompound writeToNBT(final List<ItemStack> stackSet) {
387412
388413 stackSet .add (output );
389414 nbt .setShort (OUTPUT_TAG , (short ) (stackSet .size () - 1 ));
390- nbt .setInteger (SIZE ,size );
415+ nbt .setInteger (SIZE , size );
391416 return nbt ;
392417 }
393418
394419 public CraftWork copy () {
395420 ItemStack [] remaining = Arrays .stream (this .remaining ).map (ItemStack ::copy ).toArray (ItemStack []::new );
396- return new CraftWork (remaining , output .copy (),this .size );
421+ return new CraftWork (remaining , output .copy (), this .size );
397422 }
398423
399424 @ Override
@@ -414,4 +439,4 @@ private static boolean matchStacksStrict(final ItemStack stack1, final ItemStack
414439 }
415440
416441 }
417- }
442+ }
0 commit comments