Skip to content

Commit 77c6197

Browse files
committed
fix: cad data shared across all cads
Fixes #858
1 parent 3356ee9 commit 77c6197

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

src/main/java/vazkii/psi/common/core/handler/capability/CADData.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import vazkii.psi.common.item.base.ModDataComponents;
4040
import vazkii.psi.common.item.component.ItemCADSocket;
4141

42+
import java.util.ArrayList;
4243
import java.util.List;
4344
import java.util.Objects;
4445

@@ -51,7 +52,14 @@ public class CADData implements ICapabilityProvider<ItemCapability<?, Void>, Voi
5152
public CADData(ItemStack cad) {
5253
this.cad = cad;
5354
this.cadHandler = (ComponentItemHandler) cad.getCapability(Capabilities.ItemHandler.ITEM);
54-
this.data = cad.get(ModDataComponents.CAD_DATA.get());
55+
Data cadData = cad.get(ModDataComponents.CAD_DATA);
56+
57+
if(cadData == null) {
58+
cadData = new Data(0, 0, new ArrayList<>());
59+
cad.set(ModDataComponents.CAD_DATA, cadData);
60+
}
61+
62+
this.data = cadData;
5563
}
5664

5765
@Nullable
@@ -230,9 +238,9 @@ public boolean shouldShow(IPlayerData data) {
230238
public static class Data {
231239
public static final Codec<Data> CODEC = RecordCodecBuilder.create(
232240
builder -> builder.group(
233-
Codec.INT.fieldOf("time").forGetter(data -> data.time),
234-
Codec.INT.fieldOf("battery").forGetter(data -> data.battery),
235-
Codec.list(Vector3.CODEC).fieldOf("vectors").forGetter(data -> data.vectors)
241+
Codec.INT.fieldOf("Time").forGetter(data -> data.time),
242+
Codec.INT.fieldOf("Battery").forGetter(data -> data.battery),
243+
Codec.list(Vector3.CODEC).fieldOf("Memory").forGetter(data -> data.vectors)
236244
).apply(builder, Data::new));
237245
public static final StreamCodec<RegistryFriendlyByteBuf, Data> STREAM_CODEC = StreamCodec.composite(
238246
ByteBufCodecs.INT, data -> data.time,

src/main/java/vazkii/psi/common/item/ItemCAD.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
*/
99
package vazkii.psi.common.item;
1010

11-
import com.google.common.collect.Lists;
12-
1311
import net.minecraft.ChatFormatting;
1412
import net.minecraft.client.Minecraft;
1513
import net.minecraft.core.BlockPos;
@@ -95,7 +93,7 @@ public class ItemCAD extends Item implements ICAD {
9593

9694
public ItemCAD(Item.Properties properties) {
9795
super(properties
98-
.stacksTo(1).rarity(Rarity.RARE).component(ModDataComponents.BULLETS.get(), ItemContainerContents.EMPTY).component(ModDataComponents.CAD_DATA, new CADData.Data(0, 0, Lists.newArrayList()))
96+
.stacksTo(1).rarity(Rarity.RARE).component(ModDataComponents.BULLETS.get(), ItemContainerContents.EMPTY)
9997
);
10098
}
10199

src/main/java/vazkii/psi/common/item/base/ModDataComponents.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class ModDataComponents {
2828
public static final DeferredHolder<DataComponentType<?>, DataComponentType<Integer>> TIMES_CAST = DATA_COMPONENT_TYPES.registerComponentType("times_cast", builder -> builder.persistent(Codec.INT).networkSynchronized(ByteBufCodecs.INT));
2929
public static final DeferredHolder<DataComponentType<?>, DataComponentType<Integer>> SELECTED_CONTROL_SLOT = DATA_COMPONENT_TYPES.registerComponentType("selected_control_slot", builder -> builder.persistent(Codec.INT).networkSynchronized(ByteBufCodecs.INT));
3030
public static final DeferredHolder<DataComponentType<?>, DataComponentType<String>> CONTRIBUTOR = DATA_COMPONENT_TYPES.registerComponentType("psi_contributor_name", builder -> builder.persistent(Codec.STRING).networkSynchronized(ByteBufCodecs.STRING_UTF8));
31-
public static final DeferredHolder<DataComponentType<?>, DataComponentType<CADData.Data>> CAD_DATA = DATA_COMPONENT_TYPES.registerComponentType("cad_data", builder -> builder.persistent(CADData.Data.CODEC).networkSynchronized(CADData.Data.STREAM_CODEC));
31+
public static final DeferredHolder<DataComponentType<?>, DataComponentType<CADData.Data>> CAD_DATA = DATA_COMPONENT_TYPES.registerComponentType("cad_data", builder -> builder.persistent(CADData.Data.CODEC).networkSynchronized(CADData.Data.STREAM_CODEC).cacheEncoding());
3232
public static final DeferredHolder<DataComponentType<?>, DataComponentType<ItemContainerContents>> BULLETS = DATA_COMPONENT_TYPES.registerComponentType("bullets", builder -> builder.persistent(ItemContainerContents.CODEC).networkSynchronized(ItemContainerContents.STREAM_CODEC).cacheEncoding());
3333
public static final DeferredHolder<DataComponentType<?>, DataComponentType<Integer>> SELECTED_SLOT = DATA_COMPONENT_TYPES.registerComponentType("selected_slot", builder -> builder.persistent(Codec.INT).networkSynchronized(ByteBufCodecs.INT));
3434
public static final DeferredHolder<DataComponentType<?>, DataComponentType<Integer>> REGEN_TIME = DATA_COMPONENT_TYPES.registerComponentType("regen_time", builder -> builder.persistent(Codec.INT).networkSynchronized(ByteBufCodecs.INT));

0 commit comments

Comments
 (0)