Skip to content

Commit 0c61eff

Browse files
committed
Fix cardboard boxes not persisting data when broken or pick blocked
1 parent 53a6954 commit 0c61eff

File tree

6 files changed

+84
-13
lines changed

6 files changed

+84
-13
lines changed

src/datagen/generated/mekanism/.cache/59eb3dbb5f86130e09b3c62d89b9525ee01cf52d

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datagen/generated/mekanism/.cache/c2007283a832f570d2167eeffdb50ecb2f811654

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datagen/generated/mekanism/assets/emi/aliases/mekanism.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datagen/generated/mekanism/data/mekanism/loot_tables/blocks/cardboard_box.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/mekanism/common/item/block/ItemBlockCardboardBox.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,10 @@ public InteractionResult onItemUseFirst(ItemStack stack, UseOnContext context) {
9797
CommonWorldTickHandler.monitoringCardboardBox = false;
9898
TileEntityCardboardBox box = WorldUtils.getTileEntity(TileEntityCardboardBox.class, world, pos);
9999
if (box != null) {
100-
DataComponentMap blockData = DataComponentMap.builder()
100+
box.setComponents(DataComponentMap.builder()
101+
.addAll(box.components())
101102
.set(MekanismDataComponents.BLOCK_DATA, data)
102-
.build();
103-
if (box.components().isEmpty()) {
104-
box.setComponents(blockData);
105-
} else {
106-
box.setComponents(DataComponentMap.composite(blockData, box.components()));
107-
}
103+
.build());
108104
}
109105
}
110106
return InteractionResult.SUCCESS;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,79 @@
11
package mekanism.common.tile;
22

3+
import com.mojang.datafixers.util.Pair;
4+
import com.mojang.serialization.DataResult;
5+
import com.mojang.serialization.DataResult.Error;
6+
import java.util.List;
7+
import java.util.Optional;
8+
import mekanism.common.Mekanism;
9+
import mekanism.common.attachments.BlockData;
10+
import mekanism.common.registries.MekanismDataComponents;
311
import mekanism.common.registries.MekanismTileEntityTypes;
412
import mekanism.common.tile.base.TileEntityUpdateable;
513
import net.minecraft.core.BlockPos;
14+
import net.minecraft.core.HolderLookup;
15+
import net.minecraft.core.component.DataComponentMap;
16+
import net.minecraft.core.component.DataComponentType;
17+
import net.minecraft.nbt.CompoundTag;
18+
import net.minecraft.nbt.NbtOps;
19+
import net.minecraft.nbt.Tag;
620
import net.minecraft.world.level.block.state.BlockState;
21+
import org.jetbrains.annotations.NotNull;
722

23+
//TODO: If we end up with more blocks where we care about the actual backing component, we should abstract some of this up into TileEntityUpdateable
824
public class TileEntityCardboardBox extends TileEntityUpdateable {
925

1026
public TileEntityCardboardBox(BlockPos pos, BlockState state) {
1127
super(MekanismTileEntityTypes.CARDBOARD_BOX, pos, state);
1228
}
29+
30+
@Override
31+
public List<DataComponentType<?>> getRemapEntries() {
32+
List<DataComponentType<?>> remapEntries = super.getRemapEntries();
33+
if (!remapEntries.contains(MekanismDataComponents.BLOCK_DATA.get())) {
34+
remapEntries.add(MekanismDataComponents.BLOCK_DATA.get());
35+
}
36+
return remapEntries;
37+
}
38+
39+
@NotNull
40+
@Override
41+
public CompoundTag getReducedUpdateTag(@NotNull HolderLookup.Provider provider) {
42+
CompoundTag updateTag = super.getReducedUpdateTag(provider);
43+
DataComponentType<BlockData> componentType = MekanismDataComponents.BLOCK_DATA.get();
44+
if (components().has(componentType)) {
45+
DataResult<Tag> encoded = componentType.codecOrThrow().encodeStart(
46+
provider.createSerializationContext(NbtOps.INSTANCE), components().get(componentType)
47+
);
48+
Optional<Error<Tag>> error = encoded.error();
49+
if (error.isPresent()) {
50+
Mekanism.logger.error("Failed to encode cardboard box block data: {}", error.get().message());
51+
} else {
52+
updateTag.put(MekanismDataComponents.BLOCK_DATA.getId().toString(), encoded.getOrThrow());
53+
}
54+
}
55+
return updateTag;
56+
}
57+
58+
@Override
59+
public void handleUpdateTag(@NotNull CompoundTag tag, @NotNull HolderLookup.Provider provider) {
60+
super.handleUpdateTag(tag, provider);
61+
String key = MekanismDataComponents.BLOCK_DATA.getId().toString();
62+
if (tag.contains(key)) {
63+
DataComponentType<BlockData> componentType = MekanismDataComponents.BLOCK_DATA.get();
64+
DataResult<Pair<BlockData, Tag>> decoded = componentType.codecOrThrow().decode(
65+
provider.createSerializationContext(NbtOps.INSTANCE), tag.get(key)
66+
);
67+
Optional<Error<Pair<BlockData, Tag>>> error = decoded.error();
68+
if (error.isPresent()) {
69+
Mekanism.logger.error("Failed to decode cardboard box block data: {}", error.get().message());
70+
} else {
71+
BlockData data = decoded.getOrThrow().getFirst();
72+
setComponents(DataComponentMap.builder()
73+
.addAll(components())
74+
.set(MekanismDataComponents.BLOCK_DATA, data)
75+
.build());
76+
}
77+
}
78+
}
1379
}

0 commit comments

Comments
 (0)