Skip to content

Commit ddf0c91

Browse files
committed
wip porting
1 parent 0b5caec commit ddf0c91

File tree

3 files changed

+90
-57
lines changed

3 files changed

+90
-57
lines changed

src/main/java/com/mrh0/createaddition/CreateAddition.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.mrh0.createaddition.config.CommonConfig;
44
import com.mrh0.createaddition.index.*;
55
import com.mrh0.createaddition.index.CASounds;
6+
import com.mrh0.createaddition.network.EnergyNetworkPacketPayload;
67
import com.mrh0.createaddition.network.ObservePacketPayload;
8+
import com.mrh0.createaddition.network.ServerPayloadHandler;
79
import com.mrh0.createaddition.ponder.CAPonderPlugin;
810
import com.mrh0.createaddition.trains.schedule.CASchedule;
911
import com.simibubi.create.content.processing.burner.BlazeBurnerBlock;
@@ -165,6 +167,15 @@ public static void register(final RegisterPayloadHandlersEvent event) {
165167
registrar.playBidirectional(
166168
ObservePacketPayload.TYPE,
167169
ObservePacketPayload.STREAM_CODEC,
170+
new DirectionalPayloadHandler<>(
171+
ClientPayloadHandler::handleDataOnMain,
172+
ServerPayloadHandler::handleObservePayload
173+
)
174+
);
175+
176+
registrar.playBidirectional(
177+
EnergyNetworkPacketPayload.TYPE,
178+
EnergyNetworkPacketPayload.STREAM_CODEC,
168179
new DirectionalPayloadHandler<>(
169180
ClientPayloadHandler::handleDataOnMain,
170181
ServerPayloadHandler::handleDataOnMain

src/main/java/com/mrh0/createaddition/blocks/modular_accumulator/ModularAccumulatorBlockEntity.java

Lines changed: 35 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.mrh0.createaddition.debug.IDebugDrawer;
88
import com.mrh0.createaddition.energy.IMultiTileEnergyContainer;
99
import com.mrh0.createaddition.energy.InternalEnergyStorage;
10+
import com.mrh0.createaddition.index.CABlockEntities;
1011
import com.mrh0.createaddition.network.IObserveTileEntity;
1112
import com.mrh0.createaddition.network.ObservePacketPayload;
1213
import com.mrh0.createaddition.sound.CASoundScapes;
@@ -18,6 +19,7 @@
1819
import com.simibubi.create.foundation.blockEntity.behaviour.BlockEntityBehaviour;
1920
import net.createmod.catnip.animation.LerpedFloat;
2021
import net.createmod.catnip.outliner.Outliner;
22+
import net.createmod.catnip.platform.CatnipServices;
2123
import net.minecraft.ChatFormatting;
2224
import net.minecraft.core.BlockPos;
2325
import net.minecraft.core.Direction;
@@ -26,22 +28,27 @@
2628
import net.minecraft.nbt.NbtUtils;
2729
import net.minecraft.network.chat.Component;
2830
import net.minecraft.network.chat.MutableComponent;
31+
import net.minecraft.server.level.ServerLevel;
2932
import net.minecraft.server.level.ServerPlayer;
3033
import net.minecraft.world.level.block.Block;
3134
import net.minecraft.world.level.block.entity.BlockEntity;
3235
import net.minecraft.world.level.block.entity.BlockEntityType;
3336
import net.minecraft.world.level.block.state.BlockState;
3437
import net.minecraft.world.phys.AABB;
3538
import 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;
3643
import net.neoforged.neoforge.energy.IEnergyStorage;
3744

3845
import java.util.EnumMap;
3946
import java.util.EnumSet;
4047
import java.util.List;
4148

4249
public 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()));
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.mrh0.createaddition.network;
2+
3+
import com.mrh0.createaddition.CreateAddition;
4+
import io.netty.buffer.ByteBuf;
5+
import net.minecraft.core.BlockPos;
6+
import net.minecraft.network.codec.ByteBufCodecs;
7+
import net.minecraft.network.codec.StreamCodec;
8+
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
9+
import net.minecraft.server.level.ServerPlayer;
10+
import net.neoforged.neoforge.network.PacketDistributor;
11+
12+
public record EnergyNetworkPacketPayload(BlockPos pos, int demand, int buff) implements CustomPacketPayload {
13+
public static double clientSaturation = 0;
14+
public static int clientDemand = 0;
15+
public static int clientBuff = 0;
16+
17+
public static final CustomPacketPayload.Type<EnergyNetworkPacketPayload> TYPE = new CustomPacketPayload.Type<>(CreateAddition.asResource("energy_network_packet"));
18+
19+
public static final StreamCodec<ByteBuf, EnergyNetworkPacketPayload> STREAM_CODEC = StreamCodec.composite(
20+
BlockPos.STREAM_CODEC,
21+
EnergyNetworkPacketPayload::pos,
22+
ByteBufCodecs.VAR_INT,
23+
EnergyNetworkPacketPayload::demand,
24+
ByteBufCodecs.VAR_INT,
25+
EnergyNetworkPacketPayload::buff,
26+
EnergyNetworkPacketPayload::new
27+
);
28+
29+
@Override
30+
public CustomPacketPayload.Type<? extends CustomPacketPayload> type() {
31+
return TYPE;
32+
}
33+
34+
private static void updateClientCache(BlockPos pos, int demand, int buff) {
35+
clientDemand = demand;
36+
clientBuff = buff;
37+
clientSaturation = buff - demand;
38+
}
39+
40+
public static boolean send(BlockPos pos, int demand, int buff, ServerPlayer player) {
41+
PacketDistributor.sendToPlayer(player, new EnergyNetworkPacketPayload(pos, demand, buff));
42+
return true;
43+
}
44+
}

0 commit comments

Comments
 (0)