77import com .mrh0 .createaddition .debug .IDebugDrawer ;
88import com .mrh0 .createaddition .energy .IMultiTileEnergyContainer ;
99import com .mrh0 .createaddition .energy .InternalEnergyStorage ;
10+ import com .mrh0 .createaddition .index .CABlockEntities ;
1011import com .mrh0 .createaddition .network .IObserveTileEntity ;
1112import com .mrh0 .createaddition .network .ObservePacketPayload ;
1213import com .mrh0 .createaddition .sound .CASoundScapes ;
1819import com .simibubi .create .foundation .blockEntity .behaviour .BlockEntityBehaviour ;
1920import net .createmod .catnip .animation .LerpedFloat ;
2021import net .createmod .catnip .outliner .Outliner ;
22+ import net .createmod .catnip .platform .CatnipServices ;
2123import net .minecraft .ChatFormatting ;
2224import net .minecraft .core .BlockPos ;
2325import net .minecraft .core .Direction ;
2628import net .minecraft .nbt .NbtUtils ;
2729import net .minecraft .network .chat .Component ;
2830import net .minecraft .network .chat .MutableComponent ;
31+ import net .minecraft .server .level .ServerLevel ;
2932import net .minecraft .server .level .ServerPlayer ;
3033import net .minecraft .world .level .block .Block ;
3134import net .minecraft .world .level .block .entity .BlockEntity ;
3235import net .minecraft .world .level .block .entity .BlockEntityType ;
3336import net .minecraft .world .level .block .state .BlockState ;
3437import net .minecraft .world .phys .AABB ;
3538import net .minecraft .world .phys .shapes .VoxelShape ;
39+ import net .neoforged .api .distmarker .Dist ;
40+ import net .neoforged .neoforge .capabilities .BlockCapabilityCache ;
41+ import net .neoforged .neoforge .capabilities .Capabilities ;
42+ import net .neoforged .neoforge .capabilities .RegisterCapabilitiesEvent ;
3643import net .neoforged .neoforge .energy .IEnergyStorage ;
3744
3845import java .util .EnumMap ;
3946import java .util .EnumSet ;
4047import java .util .List ;
4148
4249public class ModularAccumulatorBlockEntity extends SmartBlockEntity implements IHaveGoggleInformation , IMultiTileEnergyContainer , IObserveTileEntity , IDebugDrawer , ThresholdSwitchObservable {
43- protected LazyOptional < IEnergyStorage > energyCap ;
44- protected InternalEnergyStorage energyStorage ;
50+ protected final InternalEnergyStorage energyStorage ;
51+ private final IEnergyStorage capability ;
4552 protected BlockPos controller ;
4653 protected BlockPos lastKnownPos ;
4754 protected boolean updateConnectivity ;
@@ -52,74 +59,52 @@ public class ModularAccumulatorBlockEntity extends SmartBlockEntity implements I
5259 protected int syncCooldown ;
5360 protected boolean queuedSync ;
5461
55- private EnumSet <Direction > invalidSides = EnumSet .of (Direction .DOWN , Direction . UP );
56- private EnumMap <Direction , LazyOptional <IEnergyStorage >> escacheMap = new EnumMap <>(Direction .class );
57- protected LazyOptional <ModularAccumulatorPeripheral > peripheral ;
62+ private final EnumSet <Direction > invalidSides = EnumSet .allOf (Direction .class );
63+ private final EnumMap <Direction , BlockCapabilityCache <IEnergyStorage , Direction >> cache = new EnumMap <>(Direction .class );
64+ // protected LazyOptional<ModularAccumulatorPeripheral> peripheral;
5865
5966 public ModularAccumulatorBlockEntity (BlockEntityType <?> type , BlockPos pos , BlockState state ) {
6067 super (type , pos , state );
6168 energyStorage = createEnergyStorage ();
62- energyCap = LazyOptional . of (() -> energyStorage ) ;
69+ capability = energyStorage ;
6370 updateConnectivity = false ;
6471 height = 1 ;
6572 width = 1 ;
66- refreshCapability ();
73+ // refreshCapability();
6774
6875 // if (CreateAddition.CC_ACTIVE) this.peripheral = LazyOptional.of(() -> Peripherals.createModularAccumulatorPeripheral(this));
6976 }
7077
78+ public static void registerCapabilities (RegisterCapabilitiesEvent event ) {
79+ event .registerBlockEntity (
80+ Capabilities .EnergyStorage .BLOCK ,
81+ CABlockEntities .MODULAR_ACCUMULATOR .get (),
82+ (be , context ) -> be .capability
83+ );
84+ }
85+
7186 @ Override
7287 public void onChunkUnloaded () {}
7388
7489 protected InternalEnergyStorage createEnergyStorage () {
7590 return new InternalEnergyStorage (getCapacityMultiplier (), CommonConfig .ACCUMULATOR_MAX_INPUT .get (), CommonConfig .ACCUMULATOR_MAX_OUTPUT .get ());
7691 }
7792
78- public void setCache (Direction side , LazyOptional <IEnergyStorage > storage ) {
79- switch (side ) {
80- case DOWN , UP -> escacheMap .put (side , storage );
81- }
82- }
83-
84- public LazyOptional <IEnergyStorage > getCachedEnergy (Direction side ) {
85- return escacheMap .getOrDefault (side , LazyOptional .empty ());
86- }
87-
88- private void invalidCache (Direction side ) {
89- switch (side ) {
90- case DOWN , UP -> invalidSides .add (side );
91- }
92- }
93-
9493 public void updateCache () {
94+ if (level == null ) return ;
9595 if (level .isClientSide ()) return ;
96- for (Direction side : Direction .values ()) {
97- updateCache (side );
96+ for (Direction side : Direction .values ()) {
97+ cache .put (side , BlockCapabilityCache .create (
98+ Capabilities .EnergyStorage .BLOCK ,
99+ (ServerLevel ) level ,
100+ getBlockPos ().relative (side ),
101+ side .getOpposite (),
102+ () -> !this .isRemoved (),
103+ () -> { invalidSides .add (side ); }
104+ ));
98105 }
99106 }
100107
101- public void updateCache (Direction side ) {
102- // No need to update the cache if we're removed.
103- if (isRemoved ()) return ;
104- // Make sure the side we're checking is loaded.
105- if (!level .isLoaded (worldPosition .relative (side ))) {
106- setCache (side , LazyOptional .empty ());
107- return ;
108- }
109- BlockEntity te = level .getBlockEntity (worldPosition .relative (side ));
110- if (te == null ) {
111- setCache (side , LazyOptional .empty ());
112- return ;
113- }
114- LazyOptional <IEnergyStorage > le = te .getCapability (ForgeCapabilities .ENERGY , side .getOpposite ());
115- // Make sure that the side we're caching can actually be cached.
116- if (side != Direction .UP && side != Direction .DOWN ) return ;
117- // Make sure the side isn't already cached.
118- if (le .equals (getCachedEnergy (side ))) return ;
119- setCache (side , le );
120- le .addListener ((es ) -> invalidCache (side ));
121- }
122-
123108 protected void updateConnectivity () {
124109 updateConnectivity = false ;
125110 if (level == null ) return ;
@@ -168,7 +153,7 @@ else if (!lastKnownPos.equals(worldPosition) && worldPosition != null) {
168153
169154 if (level == null ) return ;
170155 if (level .isClientSide ()) {
171- DistExecutor . unsafeRunWhenOn ( Dist . CLIENT , () -> this ::tickAudio );
156+ CatnipServices . PLATFORM . executeOnClientOnly ( () -> this ::tickAudio );
172157 gauge .tickChaser ();
173158 float current = gauge .getValue (1 );
174159 if (current > 1 && Create .RANDOM .nextFloat () < 1 / 2f )
@@ -191,7 +176,7 @@ public void tickOutputSide(Direction side) {
191176 if (level == null ) return ;
192177 if (!level .isLoaded (getBlockPos ())) return ;
193178 if (!level .isLoaded (getBlockPos ().relative (side ))) return ;
194- IEnergyStorage ies = getCachedEnergy (side ).orElse ( null );
179+ IEnergyStorage ies = cache . get (side ).getCapability ( );
195180 if (ies == null ) return ;
196181 int ext = getControllerBE ().energyStorage .extractEnergy (ies .receiveEnergy (CommonConfig .ACCUMULATOR_MAX_OUTPUT .get (), true ), false );
197182 int rec = ies .receiveEnergy (ext , false );
@@ -295,7 +280,7 @@ public void removeController(boolean keepEnergy) {
295280 getLevel ().setBlock (worldPosition , state , 22 );
296281 }
297282
298- refreshCapability ();
283+ // refreshCapability();
299284 setChanged ();
300285 sendData ();
301286 }
@@ -323,17 +308,10 @@ public void setController(BlockPos controller) {
323308 if (level .isClientSide && !isVirtual ()) return ;
324309 if (controller .equals (this .controller )) return ;
325310 this .controller = controller ;
326- refreshCapability ();
327311 setChanged ();
328312 sendData ();
329313 }
330314
331- private void refreshCapability () {
332- LazyOptional <IEnergyStorage > oldCap = energyCap ;
333- energyCap = LazyOptional .of (this ::handlerForCapability );
334- oldCap .invalidate ();
335- }
336-
337315 private InternalEnergyStorage handlerForCapability () {
338316 return isController () ? energyStorage
339317 : (getControllerBE () != null ? getControllerBE ().handlerForCapability () : new InternalEnergyStorage (0 , CommonConfig .ACCUMULATOR_MAX_INPUT .get (), CommonConfig .ACCUMULATOR_MAX_OUTPUT .get ()));
0 commit comments