2013-01-18 15:36:42 +01:00
|
|
|
package com.comphenix.protocol;
|
|
|
|
|
2015-04-06 23:30:01 +02:00
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.when;
|
2015-05-22 20:35:58 +02:00
|
|
|
import net.minecraft.server.v1_8_R3.DispenserRegistry;
|
2015-04-06 23:30:01 +02:00
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Server;
|
|
|
|
import org.bukkit.inventory.ItemFactory;
|
|
|
|
import org.bukkit.inventory.meta.ItemMeta;
|
|
|
|
|
|
|
|
import com.comphenix.protocol.reflect.FieldUtils;
|
2014-11-29 04:08:46 +01:00
|
|
|
import com.comphenix.protocol.utility.Constants;
|
2013-01-18 15:36:42 +01:00
|
|
|
import com.comphenix.protocol.utility.MinecraftReflection;
|
2015-04-06 01:23:06 +02:00
|
|
|
import com.comphenix.protocol.utility.MinecraftVersion;
|
2015-04-06 23:30:01 +02:00
|
|
|
import com.comphenix.protocol.wrappers.ItemFactoryDelegate;
|
2013-01-18 15:36:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to ensure that ProtocolLib and Bukkit is prepared to be tested.
|
2014-11-15 19:02:03 +01:00
|
|
|
*
|
2013-01-18 15:36:42 +01:00
|
|
|
* @author Kristian
|
|
|
|
*/
|
|
|
|
public class BukkitInitialization {
|
2015-04-06 23:30:01 +02:00
|
|
|
private static boolean initialized;
|
2014-11-15 19:02:03 +01:00
|
|
|
|
2013-01-18 15:36:42 +01:00
|
|
|
/**
|
|
|
|
* Initialize Bukkit and ProtocolLib such that we can perfrom unit testing.
|
|
|
|
* @throws IllegalAccessException If we are unable to initialize Bukkit.
|
|
|
|
*/
|
|
|
|
public static void initializeItemMeta() throws IllegalAccessException {
|
|
|
|
if (!initialized) {
|
|
|
|
// Denote that we're done
|
|
|
|
initialized = true;
|
2014-11-15 19:02:03 +01:00
|
|
|
|
2013-01-18 15:36:42 +01:00
|
|
|
initializePackage();
|
2014-11-15 19:02:03 +01:00
|
|
|
|
2015-04-06 23:30:01 +02:00
|
|
|
DispenserRegistry.c(); // Basically registers everything
|
2015-04-06 01:23:06 +02:00
|
|
|
|
2013-01-18 15:36:42 +01:00
|
|
|
// Mock the server object
|
|
|
|
Server mockedServer = mock(Server.class);
|
|
|
|
ItemMeta mockedMeta = mock(ItemMeta.class);
|
2015-04-06 23:30:01 +02:00
|
|
|
ItemFactory mockedFactory = new ItemFactoryDelegate(mockedMeta);
|
2014-11-15 19:02:03 +01:00
|
|
|
|
2013-01-18 15:36:42 +01:00
|
|
|
when(mockedServer.getItemFactory()).thenReturn(mockedFactory);
|
2013-12-04 04:17:02 +01:00
|
|
|
when(mockedServer.isPrimaryThread()).thenReturn(true);
|
2015-04-06 23:30:01 +02:00
|
|
|
// when(mockedFactory.getItemMeta(any(Material.class))).thenReturn(mockedMeta);
|
2014-11-15 19:02:03 +01:00
|
|
|
|
2013-01-18 15:36:42 +01:00
|
|
|
// Inject this fake server
|
|
|
|
FieldUtils.writeStaticField(Bukkit.class, "server", mockedServer, true);
|
2014-11-15 19:02:03 +01:00
|
|
|
|
2015-04-06 23:30:01 +02:00
|
|
|
// TODO Figure this out
|
|
|
|
/* try {
|
|
|
|
FieldUtils.writeStaticFinalField(CraftItemFactory.class, "instance", mockedFactory, true);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
System.err.println("Failed to inject fake item factory: ");
|
|
|
|
ex.printStackTrace();
|
|
|
|
} */
|
|
|
|
}
|
2013-01-18 15:36:42 +01:00
|
|
|
}
|
2014-11-15 19:02:03 +01:00
|
|
|
|
2013-01-18 15:36:42 +01:00
|
|
|
/**
|
|
|
|
* Ensure that package names are correctly set up.
|
|
|
|
*/
|
|
|
|
public static void initializePackage() {
|
|
|
|
// Initialize reflection
|
2014-11-29 04:08:46 +01:00
|
|
|
MinecraftReflection.setMinecraftPackage(Constants.NMS, Constants.OBC);
|
2015-04-06 01:23:06 +02:00
|
|
|
MinecraftVersion.setCurrentVersion(MinecraftVersion.BOUNTIFUL_UPDATE);
|
2013-01-18 15:36:42 +01:00
|
|
|
}
|
|
|
|
}
|