33
33
import net .minecraft .core .BlockPos ;
34
34
import net .minecraft .core .Direction ;
35
35
import net .minecraft .core .GlobalPos ;
36
+ import net .minecraft .core .registries .BuiltInRegistries ;
36
37
import net .minecraft .core .registries .Registries ;
37
38
import net .minecraft .nbt .CompoundTag ;
38
39
import net .minecraft .nbt .NbtUtils ;
59
60
import net .minecraft .world .level .block .state .BlockState ;
60
61
import net .minecraft .world .phys .AABB ;
61
62
import net .neoforged .neoforge .client .model .data .ModelData ;
63
+ import net .neoforged .neoforge .common .NeoForge ;
62
64
import net .neoforged .neoforge .energy .EnergyStorage ;
63
65
import net .neoforged .neoforge .energy .IEnergyStorage ;
64
66
import net .neoforged .neoforge .fluids .capability .IFluidHandlerItem ;
@@ -114,7 +116,7 @@ public class ModularRouterBlockEntity extends BlockEntity implements ICamouflage
114
116
private byte recompileNeeded = COMPILE_MODULES | COMPILE_UPGRADES ;
115
117
private int tickRate = ConfigHolder .common .router .baseTickRate .get ();
116
118
private int itemsPerTick = 1 ;
117
- private final Map <Item , Integer > upgradeCount = new HashMap <>();
119
+ private final Map <UpgradeItem , Integer > upgradeCount = new HashMap <>();
118
120
119
121
private int fluidTransferRate ; // mB/t
120
122
private int fluidTransferRemainingIn = 0 ;
@@ -186,6 +188,13 @@ public CompoundTag getUpdateTag() {
186
188
if (nEnergy > 0 ) {
187
189
tag .putInt (NBT_ENERGY_UPGRADES , nEnergy );
188
190
}
191
+
192
+ getAllUpgrades ().keySet ().forEach (item -> {
193
+ final var updateTag = item .createUpdateTag (this );
194
+ if (updateTag != null ) {
195
+ tag .put (BuiltInRegistries .ITEM .getKey (item ).toString (), updateTag );
196
+ }
197
+ });
189
198
});
190
199
}
191
200
@@ -214,6 +223,11 @@ private void processClientSync(CompoundTag compound) {
214
223
}
215
224
216
225
energyStorage .updateForEnergyUpgrades (compound .getInt (NBT_ENERGY_UPGRADES ));
226
+
227
+ getAllUpgrades ().keySet ().forEach (item -> {
228
+ final var updateTag = compound .get (BuiltInRegistries .ITEM .getKey (item ).toString ());
229
+ item .processClientSync (this , (CompoundTag ) updateTag );
230
+ });
217
231
}
218
232
219
233
@ Override
@@ -397,7 +411,24 @@ private boolean runAllModules(boolean powered, boolean pulsed) {
397
411
398
412
for (CompiledIndexedModule cim : compiledModules ) {
399
413
CompiledModule cm = cim .compiledModule ;
400
- if (cm != null && cm .hasTarget () && cm .getEnergyCost () <= getEnergyStorage ().getEnergyStored () && cm .shouldRun (powered , pulsed ))
414
+ if (cm != null && cm .hasTarget () && cm .getEnergyCost () <= getEnergyStorage ().getEnergyStored () && cm .shouldRun (powered , pulsed )) {
415
+ var event = cm .getEvent ();
416
+ if (event != null ) {
417
+ event .setExecuted (false );
418
+ event .setCanceled (false );
419
+ NeoForge .EVENT_BUS .post (event );
420
+ if (event .isExecuted ()) {
421
+ newActive = true ;
422
+ }
423
+
424
+ if (event .isCanceled ()) {
425
+ if ((newActive && cm .termination () == ModuleItem .Termination .RAN ) || cm .termination () == ModuleItem .Termination .NOT_RAN ) {
426
+ break ;
427
+ }
428
+ continue ;
429
+ }
430
+ }
431
+
401
432
if (cm .execute (this )) {
402
433
cm .getFilter ().cycleRoundRobin ().ifPresent (counter -> {
403
434
ItemStack moduleStack = modulesHandler .getStackInSlot (cim .index );
@@ -411,6 +442,7 @@ private boolean runAllModules(boolean powered, boolean pulsed) {
411
442
} else if (cm .termination () == ModuleItem .Termination .NOT_RAN ) {
412
443
break ;
413
444
}
445
+ }
414
446
}
415
447
return newActive ;
416
448
}
@@ -550,7 +582,7 @@ private void compileUpgrades() {
550
582
for (int i = 0 ; i < N_UPGRADE_SLOTS ; i ++) {
551
583
ItemStack stack = upgradesHandler .getStackInSlot (i );
552
584
if (stack .getItem () instanceof UpgradeItem upgradeItem ) {
553
- upgradeCount .put (stack . getItem () , getUpgradeCount (stack .getItem ()) + stack .getCount ());
585
+ upgradeCount .put (upgradeItem , getUpgradeCount (stack .getItem ()) + stack .getCount ());
554
586
upgradeItem .onCompiled (stack , this );
555
587
}
556
588
}
@@ -605,7 +637,7 @@ public int getUpgradeCount(Item type) {
605
637
return upgradeCount .getOrDefault (type , 0 );
606
638
}
607
639
608
- public Map <Item , Integer > getAllUpgrades () {
640
+ public Map <UpgradeItem , Integer > getAllUpgrades () {
609
641
return Collections .unmodifiableMap (upgradeCount );
610
642
}
611
643
@@ -984,11 +1016,18 @@ class UpgradeHandler extends RouterItemHandler {
984
1016
985
1017
@ Override
986
1018
public boolean isItemValid (int slot , @ Nonnull ItemStack stack ) {
987
- // can't have the same upgrade in more than one slot
1019
+ if (!super .isItemValid (slot , stack )) return false ;
1020
+ UpgradeItem item = (UpgradeItem ) stack .getItem ();
988
1021
for (int i = 0 ; i < getSlots (); i ++) {
989
- if (slot != i && stack .getItem () == getStackInSlot (i ).getItem ()) return false ;
1022
+ ItemStack inSlot = getStackInSlot (i );
1023
+ if (inSlot .isEmpty () || slot == i ) continue ;
1024
+ // can't have the same upgrade in more than one slot
1025
+ // incompatible upgrades can't coexist
1026
+ if (stack .getItem () == inSlot .getItem () || !((UpgradeItem ) inSlot .getItem ()).isCompatibleWith (item )) {
1027
+ return false ;
1028
+ }
990
1029
}
991
- return super . isItemValid ( slot , stack ) ;
1030
+ return true ;
992
1031
}
993
1032
994
1033
@ Override
0 commit comments