11package com .mrh0 .createaddition .blocks .alternator ;
22
3+ import java .util .EnumMap ;
4+ import java .util .EnumSet ;
35import java .util .List ;
46
57import com .mrh0 .createaddition .CreateAddition ;
68import com .mrh0 .createaddition .config .CommonConfig ;
79import com .mrh0 .createaddition .energy .IEnergyProvider ;
810import com .mrh0 .createaddition .energy .InternalEnergyStorage ;
11+ import com .mrh0 .createaddition .index .CABlockEntities ;
912import com .mrh0 .createaddition .index .CABlocks ;
1013import com .mrh0 .createaddition .sound .CASoundScapes ;
1114import com .mrh0 .createaddition .sound .CASoundScapes .AmbienceGroup ;
1215import com .mrh0 .createaddition .util .Util ;
16+ import com .simibubi .create .AllBlockEntityTypes ;
1317import com .simibubi .create .content .kinetics .base .KineticBlockEntity ;
1418import com .simibubi .create .foundation .utility .CreateLang ;
1519
1620import net .minecraft .ChatFormatting ;
1721import net .minecraft .core .BlockPos ;
1822import net .minecraft .core .Direction ;
23+ import net .minecraft .core .HolderLookup ;
1924import net .minecraft .nbt .CompoundTag ;
2025import net .minecraft .network .chat .Component ;
26+ import net .minecraft .server .level .ServerLevel ;
2127import net .minecraft .util .Mth ;
2228import net .minecraft .world .level .block .Block ;
2329import net .minecraft .world .level .block .entity .BlockEntity ;
2834//import net.neoforged.neoforge.capabilities.BlockCapability;
2935//import net.neoforged.neoforge.capabilities.ForgeCapabilities;
3036//import net.neoforged.fml.util.LazyOptional;
37+ import net .neoforged .neoforge .capabilities .BlockCapabilityCache ;
38+ import net .neoforged .neoforge .capabilities .Capabilities ;
39+ import net .neoforged .neoforge .capabilities .RegisterCapabilitiesEvent ;
3140import net .neoforged .neoforge .energy .IEnergyStorage ;
3241
3342import javax .annotation .Nullable ;
3443
3544public class AlternatorBlockEntity extends KineticBlockEntity implements IEnergyProvider {
3645
3746 protected final InternalEnergyStorage energy ;
38- //private LazyOptional<IEnergyStorage> lazyEnergy;
47+ private final IEnergyStorage capability ;
48+
49+ private final EnumSet <Direction > invalidSides = EnumSet .allOf (Direction .class );
50+ private final EnumMap <Direction , BlockCapabilityCache <IEnergyStorage , Direction >> cache = new EnumMap <>(Direction .class );
3951
4052 public AlternatorBlockEntity (BlockEntityType <?> typeIn , BlockPos pos , BlockState state ) {
4153 super (typeIn , pos , state );
4254 energy = new InternalEnergyStorage (CommonConfig .ALTERNATOR_CAPACITY .get (), 0 , CommonConfig .ALTERNATOR_MAX_OUTPUT .get ());
43- lazyEnergy = LazyOptional .of (() -> energy );
55+ capability = energy ;
56+ }
57+
58+ public static void registerCapabilities (RegisterCapabilitiesEvent event ) {
59+ event .registerBlockEntity (
60+ Capabilities .EnergyStorage .BLOCK ,
61+ CABlockEntities .ALTERNATOR .get (),
62+ (be , context ) -> be .capability
63+ );
4464 }
4565
4666 @ Override
@@ -59,12 +79,6 @@ public float calculateStressApplied() {
5979 return impact ;
6080 }
6181
62- @ Override
63- public <T > LazyOptional <T > getCapability (Capability <T > cap , Direction side ) {
64- if (cap == ForgeCapabilities .ENERGY ) return lazyEnergy .cast ();
65- return super .getCapability (cap , side );
66- }
67-
6882 public boolean isEnergyInput (Direction side ) {
6983 return false ;
7084 }
@@ -74,32 +88,33 @@ public boolean isEnergyOutput(Direction side) {
7488 }
7589
7690 @ Override
77- public void read (CompoundTag compound , boolean clientPacket ) {
78- super .read (compound , clientPacket );
79- energy .read (compound );
91+ protected void read (CompoundTag tag , HolderLookup . Provider registries , boolean clientPacket ) {
92+ super .read (tag , registries , clientPacket );
93+ energy .read (tag );
8094 }
8195
8296 @ Override
83- public void write (CompoundTag compound , boolean clientPacket ) {
84- super .write ( compound , clientPacket );
85- energy .write (compound );
97+ public void writeSafe (CompoundTag tag , HolderLookup . Provider registries ) {
98+ super .writeSafe ( tag , registries );
99+ energy .write (tag );
86100 }
87101
88102 private boolean firstTickState = true ;
89103
90104 @ Override
91105 public void tick () {
92106 super .tick ();
93- if (level .isClientSide ()) return ;
94- if (firstTickState ) firstTick ();
107+ if (level == null ) return ;
108+ if (level .isClientSide ()) return ;
109+ if (firstTickState ) firstTick ();
95110 firstTickState = false ;
96111
97- if (Math .abs (getSpeed ()) > 0 && isSpeedRequirementFulfilled ())
112+ if (Math .abs (getSpeed ()) > 0 && isSpeedRequirementFulfilled ())
98113 energy .internalProduceEnergy (getEnergyProductionRate ((int )getSpeed ()));
99114
100- for (Direction d : Direction .values ()) {
115+ for (Direction d : Direction .values ()) {
101116 if (!isEnergyOutput (d )) continue ;
102- IEnergyStorage ies = getCachedEnergy ( d );
117+ IEnergyStorage ies = cache . get ( d ). getCapability ( );
103118 if (ies == null ) continue ;
104119 int ext = energy .extractEnergy (ies .receiveEnergy (CommonConfig .ALTERNATOR_MAX_OUTPUT .get (), true ), false );
105120 ies .receiveEnergy (ext , false );
@@ -137,45 +152,17 @@ public void updateCache() {
137152 if (level == null ) return ;
138153 if (level .isClientSide ()) return ;
139154 for (Direction side : Direction .values ()) {
140- BlockEntity te = level .getBlockEntity (worldPosition .relative (side ));
141- if (te == null ) {
142- setCache (side , LazyOptional .empty ());
143- continue ;
144- }
145- LazyOptional <IEnergyStorage > le = te .getCapability (ForgeCapabilities .ENERGY , side .getOpposite ());
146- setCache (side , le );
147- }
148- }
149-
150- private LazyOptional <IEnergyStorage > escacheUp = LazyOptional .empty ();
151- private LazyOptional <IEnergyStorage > escacheDown = LazyOptional .empty ();
152- private LazyOptional <IEnergyStorage > escacheNorth = LazyOptional .empty ();
153- private LazyOptional <IEnergyStorage > escacheEast = LazyOptional .empty ();
154- private LazyOptional <IEnergyStorage > escacheSouth = LazyOptional .empty ();
155- private LazyOptional <IEnergyStorage > escacheWest = LazyOptional .empty ();
156-
157- public void setCache (Direction side , LazyOptional <IEnergyStorage > storage ) {
158- switch (side ) {
159- case DOWN -> escacheDown = storage ;
160- case EAST -> escacheEast = storage ;
161- case NORTH -> escacheNorth = storage ;
162- case SOUTH -> escacheSouth = storage ;
163- case UP -> escacheUp = storage ;
164- case WEST -> escacheWest = storage ;
155+ cache .put (side , BlockCapabilityCache .create (
156+ Capabilities .EnergyStorage .BLOCK ,
157+ (ServerLevel ) level ,
158+ getBlockPos ().relative (side ),
159+ side .getOpposite (),
160+ () -> !this .isRemoved (),
161+ () -> { invalidSides .add (side ); }
162+ ));
165163 }
166164 }
167165
168- public IEnergyStorage getCachedEnergy (Direction side ) {
169- return switch (side ) {
170- case DOWN -> escacheDown .orElse (null );
171- case EAST -> escacheEast .orElse (null );
172- case NORTH -> escacheNorth .orElse (null );
173- case SOUTH -> escacheSouth .orElse (null );
174- case UP -> escacheUp .orElse (null );
175- case WEST -> escacheWest .orElse (null );
176- };
177- }
178-
179166 @ Override
180167 public IEnergyStorage getEnergyStorage (@ Nullable Direction direction ) {
181168 return energy ;
0 commit comments