Package com.comphenix.protocol

Source Code of com.comphenix.protocol.BukkitInitialization

package com.comphenix.protocol;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import net.minecraft.server.v1_7_R3.Block;
import net.minecraft.server.v1_7_R3.Item;
import net.minecraft.server.v1_7_R3.StatisticList;

// Will have to be updated for every version though
import org.bukkit.craftbukkit.v1_7_R3.inventory.CraftItemFactory;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.ItemMeta;
import com.comphenix.protocol.reflect.FieldUtils;
import com.comphenix.protocol.utility.MinecraftReflection;

/**
* Used to ensure that ProtocolLib and Bukkit is prepared to be tested.
*
* @author Kristian
*/
public class BukkitInitialization {
  private static boolean initialized;
 
  /**
   * 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;
           
      initializePackage();
     
      try {
        Block.p();
        Item.l();
        StatisticList.a();
      } catch (Exception e) {
        // Swallow
        e.printStackTrace();
      }
     
      // Mock the server object
      Server mockedServer = mock(Server.class);
      ItemFactory mockedFactory = mock(CraftItemFactory.class);
      ItemMeta mockedMeta = mock(ItemMeta.class);
 
      when(mockedServer.getItemFactory()).thenReturn(mockedFactory);
      when(mockedServer.isPrimaryThread()).thenReturn(true);
      when(mockedFactory.getItemMeta(any(Material.class))).thenReturn(mockedMeta);
 
      // Inject this fake server
      FieldUtils.writeStaticField(Bukkit.class, "server", mockedServer, true);
     
      // And the fake item factory
      FieldUtils.writeStaticField(CraftItemFactory.class, "instance", mockedFactory, true);
    }
  }
 
  /**
   * Ensure that package names are correctly set up.
   */
  public static void initializePackage() {
    // Initialize reflection
    MinecraftReflection.setMinecraftPackage("net.minecraft.server.v1_7_R3", "org.bukkit.craftbukkit.v1_7_R3");
  }
}
TOP

Related Classes of com.comphenix.protocol.BukkitInitialization

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.