Skip to content

Commit 1c2bc27

Browse files
committed
Fix differently mapped fastutil classes in Paper
Fixes #1228
1 parent 9b54794 commit 1c2bc27

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/main/java/com/comphenix/protocol/reflect/instances/DefaultInstances.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import java.lang.reflect.Constructor;
2121
import java.util.Collection;
2222
import java.util.List;
23+
import java.util.Map;
2324
import java.util.UUID;
25+
import java.util.concurrent.ConcurrentHashMap;
2426
import java.util.logging.Level;
2527

2628
import javax.annotation.Nullable;
@@ -33,8 +35,6 @@
3335
import net.minecraft.network.PacketDataSerializer;
3436
import net.minecraft.world.entity.EntityTypes;
3537
import net.minecraft.world.item.ItemStack;
36-
import org.bukkit.craftbukkit.libs.it.unimi.dsi.fastutil.ints.Int2ObjectMap;
37-
import org.bukkit.craftbukkit.libs.it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
3838

3939
/**
4040
* Used to construct default instances of any type.
@@ -44,6 +44,8 @@
4444
public class DefaultInstances implements InstanceProvider {
4545
private static final UUID SYS_UUID = new UUID(0L, 0L);
4646

47+
private static final Map<Class<?>, Constructor<?>> FAST_MAP_CONSTRUCTORS = new ConcurrentHashMap<>();
48+
4749
public static final InstanceProvider MINECRAFT_GENERATOR = type -> {
4850
if (type != null) {
4951
if (type == UUID.class) {
@@ -54,8 +56,21 @@ public class DefaultInstances implements InstanceProvider {
5456
return ItemStack.b;
5557
} else if (type == EntityTypes.class) {
5658
return EntityTypes.b;
57-
} else if (type == Int2ObjectMap.class) {
58-
return new Int2ObjectOpenHashMap<>();
59+
} else if (type.isAssignableFrom(Map.class)) {
60+
Constructor<?> ctor = FAST_MAP_CONSTRUCTORS.computeIfAbsent(type, __ -> {
61+
try {
62+
String name = type.getCanonicalName();
63+
if (name != null && name.contains("it.unimi.fastutils")) {
64+
return Class.forName(name.substring(name.length() - 3) + "OpenHashMap").getConstructor();
65+
}
66+
} catch (Exception ignored) {}
67+
return null;
68+
});
69+
if (ctor != null) {
70+
try {
71+
return ctor.newInstance();
72+
} catch (ReflectiveOperationException ignored) {}
73+
}
5974
} else if (type == NonNullList.class) {
6075
return NonNullList.a();
6176
}

0 commit comments

Comments
 (0)