Skip to content

Commit 4011395

Browse files
committed
fix!: Fix DataAttachmentLookup#get initializing the attachment on NeoForge, but not on Fabric. Introduce getOrCreate as an explicit alternative. get now only returns existing data on NeoForge, so this is a breaking change. However, since this never worked right in multi-loader I doubt anyone is affected by this.
1 parent 9f9c1fb commit 4011395

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

common/src/main/java/net/blay09/mods/balm/platform/attachment/DataAttachmentLookup.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public interface DataAttachmentLookup<T> {
1111
@Nullable
1212
T get(Player player);
1313

14+
T getOrCreate(Player player);
15+
1416
boolean has(Player player);
1517

1618
@Nullable
@@ -22,6 +24,8 @@ public interface DataAttachmentLookup<T> {
2224
@Nullable
2325
T get(Level level);
2426

27+
T getOrCreate(Level level);
28+
2529
boolean has(Level level);
2630

2731
@Nullable
@@ -33,6 +37,8 @@ public interface DataAttachmentLookup<T> {
3337
@Nullable
3438
T get(Entity entity);
3539

40+
T getOrCreate(Entity entity);
41+
3642
boolean has(Entity entity);
3743

3844
@Nullable
@@ -44,6 +50,8 @@ public interface DataAttachmentLookup<T> {
4450
@Nullable
4551
T get(BlockEntity blockEntity);
4652

53+
T getOrCreate(BlockEntity blockEntity);
54+
4755
boolean has(BlockEntity blockEntity);
4856

4957
@Nullable
@@ -55,6 +63,8 @@ public interface DataAttachmentLookup<T> {
5563
@Nullable
5664
T get(ChunkAccess chunk);
5765

66+
T getOrCreate(ChunkAccess chunk);
67+
5868
boolean has(ChunkAccess chunk);
5969

6070
@Nullable

fabric/src/main/java/net/blay09/mods/balm/fabric/platform/attachment/internal/FabricDataAttachmentLookup.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public FabricDataAttachmentLookup(AttachmentType<T> type) {
2121
return player.getAttached(type);
2222
}
2323

24+
@Override
25+
public @Nullable T getOrCreate(Player player) {
26+
return player.getAttachedOrCreate(type);
27+
}
28+
2429
@Override
2530
public boolean has(Player player) {
2631
return player.hasAttached(type);
@@ -41,6 +46,11 @@ public boolean has(Player player) {
4146
return level.getAttached(type);
4247
}
4348

49+
@Override
50+
public @Nullable T getOrCreate(Level level) {
51+
return level.getAttachedOrCreate(type);
52+
}
53+
4454
@Override
4555
public boolean has(Level level) {
4656
return level.hasAttached(type);
@@ -61,6 +71,11 @@ public boolean has(Level level) {
6171
return entity.getAttached(type);
6272
}
6373

74+
@Override
75+
public @Nullable T getOrCreate(Entity entity) {
76+
return entity.getAttachedOrCreate(type);
77+
}
78+
6479
@Override
6580
public boolean has(Entity entity) {
6681
return entity.hasAttached(type);
@@ -81,6 +96,11 @@ public boolean has(Entity entity) {
8196
return blockEntity.getAttached(type);
8297
}
8398

99+
@Override
100+
public @Nullable T getOrCreate(BlockEntity blockEntity) {
101+
return blockEntity.getAttachedOrCreate(type);
102+
}
103+
84104
@Override
85105
public boolean has(BlockEntity blockEntity) {
86106
return blockEntity.hasAttached(type);
@@ -101,6 +121,11 @@ public boolean has(BlockEntity blockEntity) {
101121
return chunk.getAttached(type);
102122
}
103123

124+
@Override
125+
public @Nullable T getOrCreate(ChunkAccess chunk) {
126+
return chunk.getAttachedOrCreate(type);
127+
}
128+
104129
@Override
105130
public boolean has(ChunkAccess chunk) {
106131
return chunk.hasAttached(type);

neoforge/src/main/java/net/blay09/mods/balm/neoforge/platform/attachment/internal/NeoForgeDataAttachmentLookup.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public NeoForgeDataAttachmentLookup(Holder<?> type) {
2020

2121
@Override
2222
public @Nullable T get(Player player) {
23+
return player.getExistingDataOrNull(type::value);
24+
}
25+
26+
@Override
27+
public T getOrCreate(Player player) {
2328
return player.getData(type::value);
2429
}
2530

@@ -40,6 +45,11 @@ public boolean has(Player player) {
4045

4146
@Override
4247
public @Nullable T get(Level level) {
48+
return level.getExistingDataOrNull(type::value);
49+
}
50+
51+
@Override
52+
public T getOrCreate(Level level) {
4353
return level.getData(type::value);
4454
}
4555

@@ -60,6 +70,11 @@ public boolean has(Level level) {
6070

6171
@Override
6272
public @Nullable T get(Entity entity) {
73+
return entity.getExistingDataOrNull(type::value);
74+
}
75+
76+
@Override
77+
public T getOrCreate(Entity entity) {
6378
return entity.getData(type::value);
6479
}
6580

@@ -80,6 +95,11 @@ public boolean has(Entity entity) {
8095

8196
@Override
8297
public @Nullable T get(BlockEntity blockEntity) {
98+
return blockEntity.getExistingDataOrNull(type::value);
99+
}
100+
101+
@Override
102+
public T getOrCreate(BlockEntity blockEntity) {
83103
return blockEntity.getData(type::value);
84104
}
85105

@@ -100,6 +120,11 @@ public boolean has(BlockEntity blockEntity) {
100120

101121
@Override
102122
public @Nullable T get(ChunkAccess chunk) {
123+
return chunk.getExistingDataOrNull(type::value);
124+
}
125+
126+
@Override
127+
public T getOrCreate(ChunkAccess chunk) {
103128
return chunk.getData(type::value);
104129
}
105130

0 commit comments

Comments
 (0)