20
20
import java .lang .reflect .Constructor ;
21
21
import java .util .Collection ;
22
22
import java .util .List ;
23
+ import java .util .Map ;
23
24
import java .util .UUID ;
25
+ import java .util .concurrent .ConcurrentHashMap ;
24
26
import java .util .logging .Level ;
25
27
26
28
import javax .annotation .Nullable ;
33
35
import net .minecraft .network .PacketDataSerializer ;
34
36
import net .minecraft .world .entity .EntityTypes ;
35
37
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 ;
38
38
39
39
/**
40
40
* Used to construct default instances of any type.
44
44
public class DefaultInstances implements InstanceProvider {
45
45
private static final UUID SYS_UUID = new UUID (0L , 0L );
46
46
47
+ private static final Map <Class <?>, Constructor <?>> FAST_MAP_CONSTRUCTORS = new ConcurrentHashMap <>();
48
+
47
49
public static final InstanceProvider MINECRAFT_GENERATOR = type -> {
48
50
if (type != null ) {
49
51
if (type == UUID .class ) {
@@ -54,8 +56,21 @@ public class DefaultInstances implements InstanceProvider {
54
56
return ItemStack .b ;
55
57
} else if (type == EntityTypes .class ) {
56
58
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
+ }
59
74
} else if (type == NonNullList .class ) {
60
75
return NonNullList .a ();
61
76
}
0 commit comments