mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2024-06-26 23:04:44 +02:00
Note that things may not initially work as expected. There are known issues (see #880) relating to dimensions, chat packets, and tile entities. There were also internal changes to attributes, though hopefully those are fixed. As always, report issues on GitHub.
94 lines
2.6 KiB
Java
94 lines
2.6 KiB
Java
package com.comphenix.protocol;
|
|
|
|
import com.comphenix.protocol.utility.Constants;
|
|
import com.comphenix.protocol.utility.MinecraftReflection;
|
|
import com.comphenix.protocol.utility.MinecraftVersion;
|
|
|
|
import net.minecraft.server.v1_16_R1.DispenserRegistry;
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Server;
|
|
import org.bukkit.craftbukkit.v1_16_R1.CraftServer;
|
|
import org.bukkit.craftbukkit.v1_16_R1.inventory.CraftItemFactory;
|
|
import org.bukkit.craftbukkit.v1_16_R1.util.Versioning;
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
/**
|
|
* Used to ensure that ProtocolLib and Bukkit is prepared to be tested.
|
|
*
|
|
* @author Kristian
|
|
*/
|
|
public class BukkitInitialization {
|
|
private static final BukkitInitialization instance = new BukkitInitialization();
|
|
|
|
private BukkitInitialization() {
|
|
System.out.println("Created new BukkitInitialization on " + Thread.currentThread().getName());
|
|
}
|
|
|
|
private boolean initialized;
|
|
private boolean packaged;
|
|
|
|
public static synchronized void initializePackage() {
|
|
instance.setPackage();
|
|
}
|
|
|
|
public static synchronized void initializeItemMeta() {
|
|
instance.initialize();
|
|
}
|
|
|
|
/**
|
|
* Initialize Bukkit and ProtocolLib such that we can perfrom unit testing
|
|
*/
|
|
private void initialize() {
|
|
if (!initialized) {
|
|
// Denote that we're done
|
|
initialized = true;
|
|
|
|
initializePackage();
|
|
|
|
try {
|
|
LogManager.getLogger();
|
|
} catch (Throwable ex) {
|
|
// Happens only on my Jenkins, but if it errors here it works when it matters
|
|
ex.printStackTrace();
|
|
}
|
|
|
|
DispenserRegistry.init();
|
|
|
|
// Mock the server object
|
|
Server mockedServer = mock(Server.class);
|
|
|
|
when(mockedServer.getLogger()).thenReturn(java.util.logging.Logger.getLogger("Minecraft"));
|
|
when(mockedServer.getName()).thenReturn("Mock Server");
|
|
when(mockedServer.getVersion()).thenReturn(CraftServer.class.getPackage().getImplementationVersion());
|
|
when(mockedServer.getBukkitVersion()).thenReturn(Versioning.getBukkitVersion());
|
|
|
|
when(mockedServer.getItemFactory()).thenReturn(CraftItemFactory.instance());
|
|
when(mockedServer.isPrimaryThread()).thenReturn(true);
|
|
|
|
// Inject this fake server
|
|
Bukkit.setServer(mockedServer);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ensure that package names are correctly set up.
|
|
*/
|
|
private void setPackage() {
|
|
if (!packaged) {
|
|
packaged = true;
|
|
|
|
try {
|
|
LogManager.getLogger();
|
|
} catch (Throwable ex) {
|
|
// Happens only on my Jenkins, but if it errors here it works when it matters
|
|
ex.printStackTrace();
|
|
}
|
|
|
|
Constants.init();
|
|
}
|
|
}
|
|
} |