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-07-29 18:29:59 +02:00
|
|
|
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2017-05-14 20:28:56 +02:00
|
|
|
import net.minecraft.server.v1_12_R1.DispenserRegistry;
|
2016-11-17 03:56:46 +01:00
|
|
|
|
2015-04-06 23:30:01 +02:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Server;
|
2017-05-14 20:28:56 +02:00
|
|
|
import org.bukkit.craftbukkit.v1_12_R1.CraftServer;
|
|
|
|
import org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemFactory;
|
|
|
|
import org.bukkit.craftbukkit.v1_12_R1.util.Versioning;
|
2015-04-06 23:30:01 +02:00
|
|
|
|
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;
|
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;
|
2016-08-27 19:07:49 +02:00
|
|
|
private static boolean packaged;
|
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
|
|
|
|
2016-08-27 19:07:49 +02:00
|
|
|
initializePackage();
|
|
|
|
|
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);
|
2014-11-15 19:02:03 +01:00
|
|
|
|
2015-07-29 18:29:59 +02:00
|
|
|
when(mockedServer.getLogger()).thenReturn(Logger.getLogger("Minecraft"));
|
|
|
|
when(mockedServer.getName()).thenReturn("Mock Server");
|
2015-06-25 23:34:34 +02:00
|
|
|
when(mockedServer.getVersion()).thenReturn(CraftServer.class.getPackage().getImplementationVersion());
|
2015-07-29 18:29:59 +02:00
|
|
|
when(mockedServer.getBukkitVersion()).thenReturn(Versioning.getBukkitVersion());
|
|
|
|
|
|
|
|
when(mockedServer.getItemFactory()).thenReturn(CraftItemFactory.instance());
|
2013-12-04 04:17:02 +01:00
|
|
|
when(mockedServer.isPrimaryThread()).thenReturn(true);
|
2014-11-15 19:02:03 +01:00
|
|
|
|
2013-01-18 15:36:42 +01:00
|
|
|
// Inject this fake server
|
2015-07-29 18:29:59 +02:00
|
|
|
Bukkit.setServer(mockedServer);
|
2015-04-06 23:30:01 +02:00
|
|
|
}
|
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() {
|
2016-08-27 19:07:49 +02:00
|
|
|
if (!packaged) {
|
|
|
|
packaged = true;
|
|
|
|
|
|
|
|
MinecraftReflection.setMinecraftPackage(Constants.NMS, Constants.OBC);
|
2017-05-18 18:02:47 +02:00
|
|
|
MinecraftVersion.setCurrentVersion(MinecraftVersion.COLOR_UPDATE);
|
2016-08-27 19:07:49 +02:00
|
|
|
}
|
2013-01-18 15:36:42 +01:00
|
|
|
}
|
|
|
|
}
|