2020-06-28 21:59:30 +02:00
|
|
|
package com.comphenix.protocol;
|
|
|
|
|
2022-02-20 12:16:11 +01:00
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
import static org.mockito.Mockito.when;
|
2020-08-06 20:26:27 +02:00
|
|
|
|
|
|
|
import com.comphenix.protocol.reflect.FieldUtils;
|
2020-06-28 21:59:30 +02:00
|
|
|
import com.comphenix.protocol.utility.Constants;
|
2022-02-20 12:16:11 +01:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2021-06-12 21:28:01 +02:00
|
|
|
import net.minecraft.SharedConstants;
|
2021-06-20 04:45:17 +02:00
|
|
|
import net.minecraft.core.IRegistry;
|
2021-06-12 21:28:01 +02:00
|
|
|
import net.minecraft.server.DispenserRegistry;
|
|
|
|
import net.minecraft.server.level.WorldServer;
|
2020-06-28 21:59:30 +02:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Server;
|
2020-08-06 20:26:27 +02:00
|
|
|
import org.bukkit.World;
|
2022-06-08 03:24:31 +02:00
|
|
|
import org.bukkit.craftbukkit.v1_19_R1.CraftServer;
|
|
|
|
import org.bukkit.craftbukkit.v1_19_R1.CraftWorld;
|
|
|
|
import org.bukkit.craftbukkit.v1_19_R1.inventory.CraftItemFactory;
|
|
|
|
import org.bukkit.craftbukkit.v1_19_R1.util.Versioning;
|
2020-08-06 20:26:27 +02:00
|
|
|
import org.spigotmc.SpigotWorldConfig;
|
2020-06-28 21:59:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to ensure that ProtocolLib and Bukkit is prepared to be tested.
|
|
|
|
*
|
|
|
|
* @author Kristian
|
|
|
|
*/
|
|
|
|
public class BukkitInitialization {
|
2022-02-20 12:16:11 +01:00
|
|
|
|
2020-06-28 21:59:30 +02:00
|
|
|
private static final BukkitInitialization instance = new BukkitInitialization();
|
2022-02-20 12:16:11 +01:00
|
|
|
private boolean initialized;
|
|
|
|
private boolean packaged;
|
2020-06-28 21:59:30 +02:00
|
|
|
|
|
|
|
private BukkitInitialization() {
|
|
|
|
System.out.println("Created new BukkitInitialization on " + Thread.currentThread().getName());
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:29:23 +02:00
|
|
|
/**
|
|
|
|
* Statically initializes the mock server for unit testing
|
|
|
|
*/
|
|
|
|
public static synchronized void initializeAll() {
|
|
|
|
instance.initialize();
|
|
|
|
}
|
|
|
|
|
2020-06-28 21:59:30 +02:00
|
|
|
/**
|
|
|
|
* Initialize Bukkit and ProtocolLib such that we can perfrom unit testing
|
|
|
|
*/
|
|
|
|
private void initialize() {
|
2022-02-20 12:16:11 +01:00
|
|
|
if (!this.initialized) {
|
2020-06-28 21:59:30 +02:00
|
|
|
// Denote that we're done
|
2022-02-20 12:16:11 +01:00
|
|
|
this.initialized = true;
|
2020-06-28 21:59:30 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
LogManager.getLogger();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
// Happens only on my Jenkins, but if it errors here it works when it matters
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:29:23 +02:00
|
|
|
instance.setPackage();
|
2021-06-20 04:45:17 +02:00
|
|
|
|
2021-06-12 21:28:01 +02:00
|
|
|
SharedConstants.a();
|
2021-11-30 20:10:03 +01:00
|
|
|
DispenserRegistry.a();
|
2020-06-28 21:59:30 +02:00
|
|
|
|
2021-06-20 04:45:17 +02:00
|
|
|
try {
|
|
|
|
IRegistry.class.getName();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2021-11-30 20:10:03 +01:00
|
|
|
String releaseTarget = SharedConstants.b().getReleaseTarget();
|
|
|
|
String serverVersion = CraftServer.class.getPackage().getImplementationVersion();
|
|
|
|
|
2020-06-28 21:59:30 +02:00
|
|
|
// 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");
|
2021-11-30 20:10:03 +01:00
|
|
|
when(mockedServer.getVersion()).thenReturn(serverVersion + " (MC: " + releaseTarget + ")");
|
2020-06-28 21:59:30 +02:00
|
|
|
when(mockedServer.getBukkitVersion()).thenReturn(Versioning.getBukkitVersion());
|
|
|
|
|
|
|
|
when(mockedServer.getItemFactory()).thenReturn(CraftItemFactory.instance());
|
|
|
|
when(mockedServer.isPrimaryThread()).thenReturn(true);
|
|
|
|
|
2020-08-06 20:26:27 +02:00
|
|
|
WorldServer nmsWorld = mock(WorldServer.class);
|
|
|
|
|
|
|
|
SpigotWorldConfig mockWorldConfig = mock(SpigotWorldConfig.class);
|
|
|
|
|
|
|
|
try {
|
|
|
|
FieldUtils.writeField(nmsWorld.getClass().getField("spigotConfig"), nmsWorld, mockWorldConfig, true);
|
|
|
|
} catch (ReflectiveOperationException ex) {
|
|
|
|
throw new RuntimeException(ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
CraftWorld world = mock(CraftWorld.class);
|
|
|
|
when(world.getHandle()).thenReturn(nmsWorld);
|
|
|
|
|
|
|
|
List<World> worlds = Collections.singletonList(world);
|
|
|
|
when(mockedServer.getWorlds()).thenReturn(worlds);
|
|
|
|
|
2020-06-28 21:59:30 +02:00
|
|
|
// Inject this fake server
|
|
|
|
Bukkit.setServer(mockedServer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that package names are correctly set up.
|
|
|
|
*/
|
|
|
|
private void setPackage() {
|
2022-02-20 12:16:11 +01:00
|
|
|
if (!this.packaged) {
|
|
|
|
this.packaged = true;
|
2020-06-28 21:59:30 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2017-07-06 20:32:35 +02:00
|
|
|
}
|