Skip to content

Commit 0b5caec

Browse files
committed
wip porting
1 parent bb7baa7 commit 0b5caec

File tree

3 files changed

+68
-97
lines changed

3 files changed

+68
-97
lines changed

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

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
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.network.EnergyNetworkPacket;
1110
import com.mrh0.createaddition.network.IObserveTileEntity;
12-
import com.mrh0.createaddition.network.ObservePacketLegacy;
11+
import com.mrh0.createaddition.network.ObservePacketPayload;
1312
import com.mrh0.createaddition.sound.CASoundScapes;
1413
import com.mrh0.createaddition.util.Util;
1514
import com.simibubi.create.Create;
@@ -22,6 +21,7 @@
2221
import net.minecraft.ChatFormatting;
2322
import net.minecraft.core.BlockPos;
2423
import net.minecraft.core.Direction;
24+
import net.minecraft.core.HolderLookup;
2525
import net.minecraft.nbt.CompoundTag;
2626
import net.minecraft.nbt.NbtUtils;
2727
import net.minecraft.network.chat.Component;
@@ -33,15 +33,8 @@
3333
import net.minecraft.world.level.block.state.BlockState;
3434
import net.minecraft.world.phys.AABB;
3535
import net.minecraft.world.phys.shapes.VoxelShape;
36-
import net.minecraftforge.api.distmarker.Dist;
37-
import net.minecraftforge.common.capabilities.Capability;
38-
import net.minecraftforge.common.capabilities.ForgeCapabilities;
39-
import net.minecraftforge.common.util.LazyOptional;
40-
import net.minecraftforge.energy.IEnergyStorage;
41-
import net.minecraftforge.fml.DistExecutor;
42-
43-
import javax.annotation.Nonnull;
44-
import javax.annotation.Nullable;
36+
import net.neoforged.neoforge.energy.IEnergyStorage;
37+
4538
import java.util.EnumMap;
4639
import java.util.EnumSet;
4740
import java.util.List;
@@ -72,8 +65,7 @@ public ModularAccumulatorBlockEntity(BlockEntityType<?> type, BlockPos pos, Bloc
7265
width = 1;
7366
refreshCapability();
7467

75-
if (CreateAddition.CC_ACTIVE)
76-
this.peripheral = LazyOptional.of(() -> Peripherals.createModularAccumulatorPeripheral(this));
68+
// if (CreateAddition.CC_ACTIVE) this.peripheral = LazyOptional.of(() -> Peripherals.createModularAccumulatorPeripheral(this));
7769
}
7870

7971
@Override
@@ -100,7 +92,7 @@ private void invalidCache(Direction side) {
10092
}
10193

10294
public void updateCache() {
103-
if(level.isClientSide()) return;
95+
if (level.isClientSide()) return;
10496
for(Direction side : Direction.values()) {
10597
updateCache(side);
10698
}
@@ -153,12 +145,10 @@ public void tick() {
153145

154146
if (syncCooldown > 0) {
155147
syncCooldown--;
156-
if (syncCooldown == 0 && queuedSync)
157-
sendData();
148+
if (syncCooldown == 0 && queuedSync) sendData();
158149
}
159150

160-
if (lastKnownPos == null)
161-
lastKnownPos = getBlockPos();
151+
if (lastKnownPos == null) lastKnownPos = getBlockPos();
162152
else if (!lastKnownPos.equals(worldPosition) && worldPosition != null) {
163153
onPositionChanged();
164154
return;
@@ -364,76 +354,64 @@ protected AABB createRenderBoundingBox() {
364354
}
365355

366356
@Override
367-
protected void read(CompoundTag compound, boolean clientPacket) {
368-
super.read(compound, clientPacket);
357+
protected void read(CompoundTag tag, HolderLookup.Provider registries, boolean clientPacket) {
358+
super.read(tag, registries, clientPacket);
369359

370360
BlockPos controllerBefore = controller;
371361
int prevSize = width;
372362
int prevHeight = height;
373363

374-
updateConnectivity = compound.contains("Uninitialized");
364+
updateConnectivity = tag.contains("Uninitialized");
375365
controller = null;
376366
lastKnownPos = null;
377367

378-
if (compound.contains("LastKnownPos"))
379-
lastKnownPos = NbtUtils.readBlockPos(compound.getCompound("LastKnownPos"));
380-
if (compound.contains("Controller"))
381-
controller = NbtUtils.readBlockPos(compound.getCompound("Controller"));
368+
if (tag.contains("LastKnownPos")) lastKnownPos = NbtUtils.readBlockPos(tag, "LastKnownPos").orElse(BlockPos.ZERO);
369+
if (tag.contains("Controller")) controller = NbtUtils.readBlockPos(tag, "Controller").orElse(BlockPos.ZERO);
382370

383371
if (isController()) {
384-
width = compound.getInt("Size");
385-
height = compound.getInt("Height");
372+
width = tag.getInt("Size");
373+
height = tag.getInt("Height");
386374
energyStorage.setCapacity(getTotalAccumulatorSize() * getCapacityMultiplier());
387-
energyStorage.read(compound.getCompound("EnergyContent"));
375+
energyStorage.read(tag.getCompound("EnergyContent"));
388376
if (energyStorage.getSpace() < 0)
389377
energyStorage.extractEnergy(-energyStorage.getSpace(), true);
390378
}
391379

380+
if (!clientPacket) return;
392381

393-
if (!clientPacket)
394-
return;
395-
396-
boolean changeOfController =
397-
controllerBefore == null ? controller != null : !controllerBefore.equals(controller);
382+
boolean changeOfController = controllerBefore == null ? controller != null : !controllerBefore.equals(controller);
398383
if (changeOfController || prevSize != width || prevHeight != height) {
399-
if (hasLevel())
400-
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 16);
401-
if (isController())
402-
energyStorage.setCapacity(getCapacityMultiplier() * getTotalAccumulatorSize());
384+
if (hasLevel()) level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 16);
385+
if (isController()) energyStorage.setCapacity(getCapacityMultiplier() * getTotalAccumulatorSize());
403386
invalidateRenderBoundingBox();
404387
}
405388

406-
if (isController())
407-
gauge.chase(getFillState(), 0.125f, LerpedFloat.Chaser.EXP);
389+
if (isController()) gauge.chase(getFillState(), 0.125f, LerpedFloat.Chaser.EXP);
408390
}
409391

410392
public float getFillState() {
411393
return (float) energyStorage.getEnergyStored() / energyStorage.getMaxEnergyStored();
412394
}
413395

414396
@Override
415-
public void write(CompoundTag compound, boolean clientPacket) {
416-
if (updateConnectivity)
417-
compound.putBoolean("Uninitialized", true);
418-
if (lastKnownPos != null)
419-
compound.put("LastKnownPos", NbtUtils.writeBlockPos(lastKnownPos));
420-
if (!isController())
421-
compound.put("Controller", NbtUtils.writeBlockPos(controller));
397+
public void writeSafe(CompoundTag tag, HolderLookup.Provider registries) {
398+
if (updateConnectivity) tag.putBoolean("Uninitialized", true);
399+
if (lastKnownPos != null) tag.put("LastKnownPos", NbtUtils.writeBlockPos(lastKnownPos));
400+
if (!isController()) tag.put("Controller", NbtUtils.writeBlockPos(controller));
422401
if (isController()) {
423-
compound.put("EnergyContent", energyStorage.write(new CompoundTag()));
402+
tag.put("EnergyContent", energyStorage.write(new CompoundTag()));
424403
// Used by contraptions.
425-
compound.putInt("EnergyCapacity", getTotalAccumulatorSize() * getCapacityMultiplier());
426-
compound.putInt("Size", width);
427-
compound.putInt("Height", height);
404+
tag.putInt("EnergyCapacity", getTotalAccumulatorSize() * getCapacityMultiplier());
405+
tag.putInt("Size", width);
406+
tag.putInt("Height", height);
428407
}
429-
super.write(compound, clientPacket);
408+
super.writeSafe(tag, registries);
430409

431-
if (!clientPacket)
432-
return;
433-
if (queuedSync)
434-
compound.putBoolean("LazySync", true);
410+
// if (!clientPacket) return;
411+
if (queuedSync) tag.putBoolean("LazySync", true);
435412
}
436413

414+
/*
437415
@Nonnull
438416
@Override
439417
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
@@ -442,10 +420,11 @@ public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable D
442420
if (CreateAddition.CC_ACTIVE && Peripherals.isPeripheral(cap)) return this.peripheral.cast();
443421
return super.getCapability(cap, side);
444422
}
423+
*/
445424

446425
@Override
447426
public void invalidate() {
448-
energyCap.invalidate();
427+
// energyCap.invalidate();
449428
super.invalidate();
450429
}
451430

@@ -524,7 +503,7 @@ public boolean addToGoggleTooltip(List<Component> tooltip, boolean isPlayerSneak
524503
ModularAccumulatorBlockEntity controllerTE = getControllerBE();
525504
if (controllerTE == null) return false;
526505

527-
ObservePacketLegacy.send(worldPosition, 0);
506+
ObservePacketPayload.send(worldPosition, 0);
528507

529508
String spacing = " ";
530509
tooltip.add(Component.literal(spacing)
@@ -544,7 +523,7 @@ public boolean addToGoggleTooltip(List<Component> tooltip, boolean isPlayerSneak
544523
public void observe() {}
545524

546525
@Override
547-
public void onObserved(ServerPlayer player, ObservePacketLegacy pack) {
526+
public void onObserved(ServerPlayer player, ObservePacketPayload pack) {
548527
ModularAccumulatorBlockEntity controllerTE = getControllerBE();
549528
if (controllerTE == null) return;
550529

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

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import net.minecraft.world.level.Level;
1616
import net.minecraft.world.level.block.Block;
1717
import net.minecraft.world.level.block.state.BlockState;
18+
import org.jetbrains.annotations.NotNull;
1819

1920
public class ModularAccumulatorBlockItem extends BlockItem {
2021

@@ -23,20 +24,19 @@ public ModularAccumulatorBlockItem(Block block, Properties props) {
2324
}
2425

2526
@Override
26-
public InteractionResult place(BlockPlaceContext ctx) {
27+
public @NotNull InteractionResult place(@NotNull BlockPlaceContext ctx) {
2728
InteractionResult initialResult = super.place(ctx);
28-
if (!initialResult.consumesAction())
29-
return initialResult;
29+
if (!initialResult.consumesAction()) return initialResult;
3030
tryMultiPlace(ctx);
3131
return initialResult;
3232
}
3333

34+
/* Removed from 1.21, not sure if this ever worked anyway
3435
@Override
35-
protected boolean updateCustomBlockEntityTag(BlockPos pos, Level level, Player player,
36-
ItemStack stack, BlockState state) {
36+
protected boolean updateCustomBlockEntityTag(@NotNull BlockPos pos, Level level, Player player,
37+
@NotNull ItemStack stack, @NotNull BlockState state) {
3738
MinecraftServer minecraftserver = level.getServer();
38-
if (minecraftserver == null)
39-
return false;
39+
if (minecraftserver == null) return false;
4040
CompoundTag nbt = stack.getTagElement("BlockEntityTag");
4141
if (nbt != null) {
4242
nbt.remove("Size");
@@ -55,76 +55,63 @@ protected boolean updateCustomBlockEntityTag(BlockPos pos, Level level, Player p
5555
}
5656
return super.updateCustomBlockEntityTag(pos, level, player, stack, state);
5757
}
58+
*/
5859

5960
private void tryMultiPlace(BlockPlaceContext ctx) {
6061
Player player = ctx.getPlayer();
61-
if (player == null)
62-
return;
63-
if (player.isShiftKeyDown())
64-
return;
62+
if (player == null) return;
63+
if (player.isShiftKeyDown()) return;
6564
Direction face = ctx.getClickedFace();
66-
if (!face.getAxis()
67-
.isVertical())
68-
return;
65+
if (!face.getAxis().isVertical()) return;
6966
ItemStack stack = ctx.getItemInHand();
7067
Level world = ctx.getLevel();
7168
BlockPos pos = ctx.getClickedPos();
7269
BlockPos placedOnPos = pos.relative(face.getOpposite());
7370
BlockState placedOnState = world.getBlockState(placedOnPos);
7471

75-
if (!ModularAccumulatorBlock.isAccumulator(placedOnState))
76-
return;
72+
if (!ModularAccumulatorBlock.isAccumulator(placedOnState)) return;
7773
ModularAccumulatorBlockEntity accumulatorAt = CAConnectivityHandler.partAt(CABlockEntities.MODULAR_ACCUMULATOR.get(), world, placedOnPos);
78-
if (accumulatorAt == null)
79-
return;
74+
if (accumulatorAt == null) return;
8075
ModularAccumulatorBlockEntity controllerTE = accumulatorAt.getControllerBE();
81-
if (controllerTE == null)
82-
return;
76+
if (controllerTE == null) return;
8377

8478
int width = controllerTE.width;
85-
if (width == 1)
86-
return;
79+
if (width == 1) return;
8780

8881
int blocksToPlace = 0;
8982
BlockPos startPos = face == Direction.DOWN ? controllerTE.getBlockPos()
9083
.below()
9184
: controllerTE.getBlockPos()
9285
.above(controllerTE.height);
9386

94-
if (startPos.getY() != pos.getY())
95-
return;
87+
if (startPos.getY() != pos.getY()) return;
9688

9789
for (int xOffset = 0; xOffset < width; xOffset++) {
9890
for (int zOffset = 0; zOffset < width; zOffset++) {
9991
BlockPos offsetPos = startPos.offset(xOffset, 0, zOffset);
10092
BlockState blockState = world.getBlockState(offsetPos);
101-
if (ModularAccumulatorBlock.isAccumulator(blockState))
102-
continue;
103-
if (!blockState.canBeReplaced())
104-
return;
93+
if (ModularAccumulatorBlock.isAccumulator(blockState)) continue;
94+
if (!blockState.canBeReplaced()) return;
10595
blocksToPlace++;
10696
}
10797
}
10898

109-
if (!player.isCreative() && stack.getCount() < blocksToPlace)
110-
return;
99+
if (!player.isCreative() && stack.getCount() < blocksToPlace) return;
111100

112101
for (int xOffset = 0; xOffset < width; xOffset++) {
113102
for (int zOffset = 0; zOffset < width; zOffset++) {
114103
BlockPos offsetPos = startPos.offset(xOffset, 0, zOffset);
115104
BlockState blockState = world.getBlockState(offsetPos);
116-
if (ModularAccumulatorBlock.isAccumulator(blockState))
117-
continue;
105+
if (ModularAccumulatorBlock.isAccumulator(blockState)) continue;
118106
BlockPlaceContext context = BlockPlaceContext.at(ctx, offsetPos, face);
119-
player.getPersistentData()
120-
.putBoolean("SilenceTankSound", true);
107+
player.getPersistentData().putBoolean("SilenceTankSound", true);
121108
super.place(context);
122-
player.getPersistentData()
123-
.remove("SilenceTankSound");
109+
player.getPersistentData().remove("SilenceTankSound");
124110
}
125111
}
126112
}
127113

114+
/*
128115
private static int getOrCreateEnergy(ItemStack stack) {
129116
var tag = stack.getTag();
130117
if(tag == null)
@@ -133,4 +120,5 @@ private static int getOrCreateEnergy(ItemStack stack) {
133120
tag.putInt("energy", 0);
134121
return tag.getInt("energy");
135122
}
123+
*/
136124
}

src/main/java/com/mrh0/createaddition/util/Util.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
import com.mrh0.createaddition.item.WireSpool;
55
import net.minecraft.ChatFormatting;
66
import net.minecraft.core.BlockPos;
7+
import net.minecraft.core.component.DataComponents;
8+
import net.minecraft.nbt.CompoundTag;
9+
import net.minecraft.nbt.Tag;
710
import net.minecraft.network.chat.Component;
811
import net.minecraft.network.chat.MutableComponent;
912
import net.minecraft.world.entity.player.Inventory;
1013
import net.minecraft.world.item.Item;
1114
import net.minecraft.world.item.ItemStack;
15+
import net.minecraft.world.item.component.CustomData;
1216
import net.minecraft.world.level.Level;
1317
import net.minecraft.world.level.LightLayer;
1418
import net.neoforged.neoforge.energy.IEnergyStorage;
@@ -105,9 +109,9 @@ public static <A, B, C> Triple<A, B, C> of(A a, B b, C c) {
105109
public static Util.Triple<BlockPos, Integer, WireType> getWireNodeOfSpools(ItemStack...stacks) {
106110
for(ItemStack stack : stacks) {
107111
if(stack.isEmpty()) continue;
108-
if(stack.getTag() == null) continue;
109-
if(WireSpool.hasPos(stack.getTag())) {
110-
return Util.Triple.of(WireSpool.getPos(stack.getTag()), WireSpool.getNode(stack.getTag()), WireType.of(stack.getItem()));
112+
CompoundTag tag = stack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.of(new CompoundTag())).copyTag();
113+
if(WireSpool.hasPos(tag)) {
114+
return Util.Triple.of(WireSpool.getPos(tag), WireSpool.getNode(tag), WireType.of(stack.getItem()));
111115
}
112116
}
113117
return null;

0 commit comments

Comments
 (0)