Skip to content

Commit bee45ef

Browse files
committed
Use Cache get instead of Map compute
This may or may not fix concurrency issues from... somewhere (bad plugins/old guava impl?)
1 parent 1946a65 commit bee45ef

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

common/src/main/java/com/viaversion/viaversion/protocols/v1_21_4to1_21_5/storage/ItemHashStorage1_21_5.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package com.viaversion.viaversion.protocols.v1_21_4to1_21_5.storage;
1919

20+
import com.google.common.cache.Cache;
2021
import com.google.common.cache.CacheBuilder;
2122
import com.viaversion.viaversion.api.minecraft.codec.CodecContext;
2223
import com.viaversion.viaversion.api.minecraft.codec.CodecContext.RegistryAccess;
@@ -31,11 +32,11 @@
3132
import com.viaversion.viaversion.util.SerializerVersion;
3233
import java.util.ArrayList;
3334
import java.util.List;
34-
import java.util.Map;
35+
import java.util.concurrent.ExecutionException;
3536

3637
public class ItemHashStorage1_21_5 implements ItemHasher {
3738

38-
private final Map<Long, StructuredData<?>> hashToStructuredData = CacheBuilder.newBuilder().concurrencyLevel(1).maximumSize(512).<Long, StructuredData<?>>build().asMap();
39+
private final Cache<Long, StructuredData<?>> hashToStructuredData = CacheBuilder.newBuilder().concurrencyLevel(1).maximumSize(512).build();
3940
private final List<String> enchantmentRegistry = new ArrayList<>();
4041
private boolean processingClientboundInventoryPacket;
4142
private final CodecContext context;
@@ -63,12 +64,16 @@ public void trackStructuredData(final StructuredData<?> structuredData) {
6364
}
6465

6566
final long key = (long) structuredData.id() << 32 | hash;
66-
this.hashToStructuredData.computeIfAbsent(key, $ -> structuredData.copy());
67+
try {
68+
this.hashToStructuredData.get(key, structuredData::copy);
69+
} catch (final ExecutionException e) {
70+
throw new RuntimeException(e);
71+
}
6772
}
6873

6974
public StructuredData<?> dataFromHash(final int dataComponentId, final int hash) {
7075
final long key = (long) dataComponentId << 32 | hash;
71-
final StructuredData<?> data = this.hashToStructuredData.get(key);
76+
final StructuredData<?> data = this.hashToStructuredData.getIfPresent(key);
7277
return data != null ? data.copy() : null;
7378
}
7479

0 commit comments

Comments
 (0)