mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2025-03-30 16:37:15 +02:00
Try fixing some of the unit tests
This commit is contained in:
parent
90a38cc15c
commit
c54a99945d
src
main/java/com/comphenix/protocol/reflect/instances
test/java/com/comphenix/protocol
@ -18,83 +18,21 @@
|
||||
package com.comphenix.protocol.reflect.instances;
|
||||
|
||||
import com.comphenix.protocol.ProtocolLogger;
|
||||
import com.comphenix.protocol.reflect.accessors.MethodAccessor;
|
||||
import com.comphenix.protocol.utility.MinecraftReflection;
|
||||
import com.comphenix.protocol.wrappers.BukkitConverters;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import javax.annotation.Nullable;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* Used to construct default instances of any type.
|
||||
* @author Kristian
|
||||
*/
|
||||
public class DefaultInstances implements InstanceProvider {
|
||||
// system unique id representation
|
||||
private static final UUID SYS_UUID = new UUID(0L, 0L);
|
||||
// minecraft default types
|
||||
private static final Object AIR_ITEM_STACK = BukkitConverters.getItemStackConverter().getGeneric(
|
||||
new ItemStack(Material.AIR));
|
||||
private static Object DEFAULT_ENTITY_TYPES; // modern servers only (older servers will use an entity type id)
|
||||
// minecraft method accessors
|
||||
private static final MethodAccessor NON_NULL_LIST_CREATE = MinecraftReflection.getNonNullListCreateAccessor();
|
||||
// fast util mappings for paper relocation
|
||||
private static final Map<Class<?>, Constructor<?>> FAST_MAP_CONSTRUCTORS = new ConcurrentHashMap<>();
|
||||
|
||||
public static final InstanceProvider MINECRAFT_GENERATOR = type -> {
|
||||
if (type != null) {
|
||||
if (type == UUID.class) {
|
||||
return SYS_UUID;
|
||||
} else if (type.isEnum()) {
|
||||
return type.getEnumConstants()[0];
|
||||
} else if (type == MinecraftReflection.getItemStackClass()) {
|
||||
return AIR_ITEM_STACK;
|
||||
} else if (type == MinecraftReflection.getEntityTypes()) {
|
||||
if (DEFAULT_ENTITY_TYPES == null) {
|
||||
// try to initialize now
|
||||
try {
|
||||
DEFAULT_ENTITY_TYPES = BukkitConverters.getEntityTypeConverter().getGeneric(EntityType.AREA_EFFECT_CLOUD);
|
||||
} catch (Exception ignored) {
|
||||
// not available in this version of minecraft
|
||||
}
|
||||
}
|
||||
return DEFAULT_ENTITY_TYPES;
|
||||
} else if (type.isAssignableFrom(Map.class)) {
|
||||
Constructor<?> ctor = FAST_MAP_CONSTRUCTORS.computeIfAbsent(type, __ -> {
|
||||
try {
|
||||
String name = type.getCanonicalName();
|
||||
if (name != null && name.contains("it.unimi.fastutils")) {
|
||||
return Class.forName(name.substring(name.length() - 3) + "OpenHashMap").getConstructor();
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
return null;
|
||||
});
|
||||
if (ctor != null) {
|
||||
try {
|
||||
return ctor.newInstance();
|
||||
} catch (ReflectiveOperationException ignored) {}
|
||||
}
|
||||
} else if (NON_NULL_LIST_CREATE != null && type == MinecraftReflection.getNonNullListClass()) {
|
||||
return NON_NULL_LIST_CREATE.invoke(null);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Standard default instance provider.
|
||||
@ -102,7 +40,7 @@ public class DefaultInstances implements InstanceProvider {
|
||||
public static final DefaultInstances DEFAULT = DefaultInstances.fromArray(
|
||||
PrimitiveGenerator.INSTANCE,
|
||||
CollectionGenerator.INSTANCE,
|
||||
MINECRAFT_GENERATOR
|
||||
MinecraftGenerator.INSTANCE
|
||||
);
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,77 @@
|
||||
package com.comphenix.protocol.reflect.instances;
|
||||
|
||||
import com.comphenix.protocol.reflect.accessors.MethodAccessor;
|
||||
import com.comphenix.protocol.utility.MinecraftReflection;
|
||||
import com.comphenix.protocol.wrappers.BukkitConverters;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class MinecraftGenerator {
|
||||
// system unique id representation
|
||||
private static final UUID SYS_UUID;
|
||||
// minecraft default types
|
||||
private static final Object AIR_ITEM_STACK;
|
||||
private static Object DEFAULT_ENTITY_TYPES; // modern servers only (older servers will use an entity type id)
|
||||
// minecraft method accessors
|
||||
private static final MethodAccessor NON_NULL_LIST_CREATE;
|
||||
// fast util mappings for paper relocation
|
||||
private static final Map<Class<?>, Constructor<?>> FAST_MAP_CONSTRUCTORS;
|
||||
|
||||
static {
|
||||
try {
|
||||
SYS_UUID = new UUID(0L, 0L);
|
||||
AIR_ITEM_STACK = BukkitConverters.getItemStackConverter().getGeneric(new ItemStack(Material.AIR));
|
||||
FAST_MAP_CONSTRUCTORS = new ConcurrentHashMap<>();
|
||||
NON_NULL_LIST_CREATE = MinecraftReflection.getNonNullListCreateAccessor();
|
||||
} catch (Throwable ex) {
|
||||
throw new RuntimeException("Failed to create static fields in MinecraftGenerator", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static final InstanceProvider INSTANCE = type -> {
|
||||
if (type != null) {
|
||||
if (type == UUID.class) {
|
||||
return SYS_UUID;
|
||||
} else if (type.isEnum()) {
|
||||
return type.getEnumConstants()[0];
|
||||
} else if (type == MinecraftReflection.getItemStackClass()) {
|
||||
return AIR_ITEM_STACK;
|
||||
} else if (type == MinecraftReflection.getEntityTypes()) {
|
||||
if (DEFAULT_ENTITY_TYPES == null) {
|
||||
// try to initialize now
|
||||
try {
|
||||
DEFAULT_ENTITY_TYPES = BukkitConverters.getEntityTypeConverter().getGeneric(EntityType.AREA_EFFECT_CLOUD);
|
||||
} catch (Exception ignored) {
|
||||
// not available in this version of minecraft
|
||||
}
|
||||
}
|
||||
return DEFAULT_ENTITY_TYPES;
|
||||
} else if (type.isAssignableFrom(Map.class)) {
|
||||
Constructor<?> ctor = FAST_MAP_CONSTRUCTORS.computeIfAbsent(type, __ -> {
|
||||
try {
|
||||
String name = type.getCanonicalName();
|
||||
if (name != null && name.contains("it.unimi.fastutils")) {
|
||||
return Class.forName(name.substring(name.length() - 3) + "OpenHashMap").getConstructor();
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
return null;
|
||||
});
|
||||
if (ctor != null) {
|
||||
try {
|
||||
return ctor.newInstance();
|
||||
} catch (ReflectiveOperationException ignored) {}
|
||||
}
|
||||
} else if (NON_NULL_LIST_CREATE != null && type == MinecraftReflection.getNonNullListClass()) {
|
||||
return NON_NULL_LIST_CREATE.invoke(null);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
}
|
@ -8,6 +8,7 @@ import com.comphenix.protocol.utility.Constants;
|
||||
import com.mojang.bridge.game.GameVersion;
|
||||
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraft.core.IRegistry;
|
||||
import net.minecraft.server.DispenserRegistry;
|
||||
import net.minecraft.server.level.WorldServer;
|
||||
|
||||
@ -55,8 +56,6 @@ public class BukkitInitialization {
|
||||
// Denote that we're done
|
||||
initialized = true;
|
||||
|
||||
initializePackage();
|
||||
|
||||
try {
|
||||
LogManager.getLogger();
|
||||
} catch (Throwable ex) {
|
||||
@ -64,9 +63,17 @@ public class BukkitInitialization {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
initializePackage();
|
||||
|
||||
SharedConstants.a();
|
||||
DispenserRegistry.init();
|
||||
|
||||
try {
|
||||
IRegistry.class.getName();
|
||||
} catch (Throwable ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
// Mock the server object
|
||||
Server mockedServer = mock(Server.class);
|
||||
|
||||
|
@ -12,7 +12,7 @@ public class CloningTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void initializeBukkit() {
|
||||
BukkitInitialization.initializePackage();
|
||||
BukkitInitialization.initializeItemMeta();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -34,7 +34,7 @@ public class PlayerInfoDataTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void initializeBukkit() {
|
||||
BukkitInitialization.initializePackage();
|
||||
BukkitInitialization.initializeItemMeta();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -28,7 +28,7 @@ public class NbtCompoundTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void initializeBukkit() {
|
||||
BukkitInitialization.initializePackage();
|
||||
BukkitInitialization.initializeItemMeta();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -14,7 +14,7 @@ public class NbtConfigurationSerializerTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void initializeBukkit() {
|
||||
BukkitInitialization.initializePackage();
|
||||
BukkitInitialization.initializeItemMeta();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user