public void registerServerAliases() {
    Map<String, String[]> values = server.getCommandAliases();

    for (String alias : values.keySet()) {
      if (alias.contains(":") || alias.contains(" ")) {
        server
            .getLogger()
            .warning(
                "Could not register alias " + alias + " because it contains illegal characters");
        continue;
      }

      String[] commandStrings = values.get(alias);
      List<String> targets = new ArrayList<String>();
      StringBuilder bad = new StringBuilder();

      for (String commandString : commandStrings) {
        String[] commandArgs = commandString.split(" ");
        Command command = getCommand(commandArgs[0]);

        if (command == null) {
          if (bad.length() > 0) {
            bad.append(", ");
          }
          bad.append(commandString);
        } else {
          targets.add(commandString);
        }
      }

      if (bad.length() > 0) {
        server
            .getLogger()
            .warning(
                "Could not register alias "
                    + alias
                    + " because it contains commands that do not exist: "
                    + bad);
        continue;
      }

      // We register these as commands so they have absolute priority.
      if (targets.size() > 0) {
        knownCommands.put(
            alias.toLowerCase(),
            new FormattedCommandAlias(
                alias.toLowerCase(), targets.toArray(new String[targets.size()])));
      } else {
        knownCommands.remove(alias.toLowerCase());
      }
    }
  }
  public void enablePlugin(final Plugin plugin) {
    if (!(plugin instanceof JavaPlugin)) {
      throw new IllegalArgumentException("Plugin is not associated with this PluginLoader");
    }

    if (!plugin.isEnabled()) {
      JavaPlugin jPlugin = (JavaPlugin) plugin;

      String pluginName = jPlugin.getDescription().getName();

      if (!loaders.containsKey(pluginName)) {
        loaders.put(pluginName, (PluginClassLoader) jPlugin.getClassLoader());
      }

      try {
        jPlugin.setEnabled(true);
      } catch (Throwable ex) {
        server
            .getLogger()
            .log(
                Level.SEVERE,
                "Error occurred while enabling "
                    + plugin.getDescription().getFullName()
                    + " (Is it up to date?): "
                    + ex.getMessage(),
                ex);
      }

      // Perhaps abort here, rather than continue going, but as it stands,
      // an abort is not possible the way it's currently written
      server.getPluginManager().callEvent(new PluginEnableEvent(plugin));
    }
  }
Example #3
0
  public void onEnable() {
    // TODO: Place any custom enable code here including the registration of any events

    // Setup Plugin Member Variables
    server = getServer();
    log = server.getLogger();

    commandMap =
        new CommandsManager<Player>() {
          @Override
          public boolean hasPermission(Player player, String perm) {
            // TODO: Implement Permissions
            return true;
          }
        };

    dungeons = new HashMap<String, Dungeon>();
    editSessions = new HashMap<Player, EditSession>();

    PluginManager pm = getServer().getPluginManager();

    try {
      // Register our events
      pm.registerEvent(Event.Type.ENTITY_DAMAGE, entityListener, Priority.Normal, this);
      pm.registerEvent(Event.Type.ENTITY_DEATH, entityListener, Priority.Normal, this);
      pm.registerEvent(Event.Type.ENTITY_COMBUST, entityListener, Priority.Normal, this);
      pm.registerEvent(Event.Type.ENTITY_EXPLODE, entityListener, Priority.Normal, this);
      pm.registerEvent(Event.Type.ENTITY_TARGET, entityListener, Priority.Normal, this);

    } catch (Exception e) {

      log.info("Exception while registering events.");
      log.info(e.getMessage());
    }

    try {

      // Setup PlayerListener class to preprocess command events
      pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, playerListener, Priority.Normal, this);
      pm.registerEvent(Event.Type.PLAYER_MOVE, playerListener, Priority.Normal, this);

      // Register Commands to the command map
      commandMap.register(LairDungeonCommand.class);

    } catch (Exception e) {

      log.info("Exception Registering Commands: ");
      log.info(e.getMessage());
    }

    // Say Hello
    log.info("Legends.Lair Hello!");
  }
  private void fireEvent(Event event) {
    HandlerList handlers = event.getHandlers();
    RegisteredListener[] listeners = handlers.getRegisteredListeners();

    for (RegisteredListener registration : listeners) {
      if (!registration.getPlugin().isEnabled()) {
        continue;
      }

      try {
        registration.callEvent(event);
      } catch (AuthorNagException ex) {
        Plugin plugin = registration.getPlugin();

        if (plugin.isNaggable()) {
          plugin.setNaggable(false);

          server
              .getLogger()
              .log(
                  Level.SEVERE,
                  String.format(
                      "Nag author(s): '%s' of '%s' about the following: %s",
                      plugin.getDescription().getAuthors(),
                      plugin.getDescription().getFullName(),
                      ex.getMessage()));
        }
      } catch (Throwable ex) {
        server
            .getLogger()
            .log(
                Level.SEVERE,
                "Could not pass event "
                    + event.getEventName()
                    + " to "
                    + registration.getPlugin().getDescription().getFullName(),
                ex);
      }
    }
  }
Example #5
0
  @Before
  public void beforeTest() {
    pluginManager = Mockito.mock(PluginManager.class);

    server = Mockito.mock(Server.class);
    Mockito.when(server.getPluginManager()).thenReturn(pluginManager);
    Mockito.when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());

    core = new DummyExpCraft(server);

    testModule = new DummyModule();
    testModule.setInfo(new ModuleInfo("Test", "T", null));
    testModule.setTranslator(new FallbackModuleTranslation(core.getTranslator(), testModule));

    core.setTestModule(testModule);

    cmd = new CommandManager(core);
  }
  public void disablePlugin(Plugin plugin) {
    if (!(plugin instanceof JavaPlugin)) {
      throw new IllegalArgumentException("Plugin is not associated with this PluginLoader");
    }

    if (plugin.isEnabled()) {
      JavaPlugin jPlugin = (JavaPlugin) plugin;
      ClassLoader cloader = jPlugin.getClassLoader();

      try {
        jPlugin.setEnabled(false);
      } catch (Throwable ex) {
        server
            .getLogger()
            .log(
                Level.SEVERE,
                "Error occurred while disabling "
                    + plugin.getDescription().getFullName()
                    + " (Is it up to date?): "
                    + ex.getMessage(),
                ex);
      }

      server.getPluginManager().callEvent(new PluginDisableEvent(plugin));

      loaders.remove(jPlugin.getDescription().getName());

      if (cloader instanceof PluginClassLoader) {
        PluginClassLoader loader = (PluginClassLoader) cloader;
        Set<String> names = loader.getClasses();

        for (String name : names) {
          removeClass(name);
        }
      }
    }
  }
  public void enablePlugin(final Plugin plugin) {
    if (!plugin.isEnabled()) {
      List<Command> pluginCommands = PluginCommandYamlParser.parse(plugin);

      if (!pluginCommands.isEmpty()) {
        commandMap.registerAll(plugin.getDescription().getName(), pluginCommands);
      }

      try {
        plugin.getPluginLoader().enablePlugin(plugin);
      } catch (Throwable ex) {
        server
            .getLogger()
            .log(
                Level.SEVERE,
                "Error occurred (in the plugin loader) while enabling "
                    + plugin.getDescription().getFullName()
                    + " (Is it up to date?)",
                ex);
      }

      HandlerList.bakeAll();
    }
  }
  public void disablePlugin(final Plugin plugin) {
    if (plugin.isEnabled()) {
      try {
        plugin.getPluginLoader().disablePlugin(plugin);
      } catch (Throwable ex) {
        server
            .getLogger()
            .log(
                Level.SEVERE,
                "Error occurred (in the plugin loader) while disabling "
                    + plugin.getDescription().getFullName()
                    + " (Is it up to date?)",
                ex);
      }

      try {
        server.getScheduler().cancelTasks(plugin);
      } catch (Throwable ex) {
        server
            .getLogger()
            .log(
                Level.SEVERE,
                "Error occurred (in the plugin loader) while cancelling tasks for "
                    + plugin.getDescription().getFullName()
                    + " (Is it up to date?)",
                ex);
      }

      try {
        server.getServicesManager().unregisterAll(plugin);
      } catch (Throwable ex) {
        server
            .getLogger()
            .log(
                Level.SEVERE,
                "Error occurred (in the plugin loader) while unregistering services for "
                    + plugin.getDescription().getFullName()
                    + " (Is it up to date?)",
                ex);
      }

      try {
        HandlerList.unregisterAll(plugin);
      } catch (Throwable ex) {
        server
            .getLogger()
            .log(
                Level.SEVERE,
                "Error occurred (in the plugin loader) while unregistering events for "
                    + plugin.getDescription().getFullName()
                    + " (Is it up to date?)",
                ex);
      }

      try {
        server.getMessenger().unregisterIncomingPluginChannel(plugin);
        server.getMessenger().unregisterOutgoingPluginChannel(plugin);
      } catch (Throwable ex) {
        server
            .getLogger()
            .log(
                Level.SEVERE,
                "Error occurred (in the plugin loader) while unregistering plugin channels for "
                    + plugin.getDescription().getFullName()
                    + " (Is it up to date?)",
                ex);
      }
    }
  }
  /**
   * Loads the plugins contained within the specified directory
   *
   * @param directory Directory to check for plugins
   * @return A list of all plugins loaded
   */
  public Plugin[] loadPlugins(File directory) {
    Validate.notNull(directory, "Directory cannot be null");
    Validate.isTrue(directory.isDirectory(), "Directory must be a directory");

    List<Plugin> result = new ArrayList<Plugin>();
    Set<Pattern> filters = fileAssociations.keySet();

    if (!(server.getUpdateFolder().equals(""))) {
      updateDirectory = new File(directory, server.getUpdateFolder());
    }

    Map<String, File> plugins = new HashMap<String, File>();
    Set<String> loadedPlugins = new HashSet<String>();
    Map<String, Collection<String>> dependencies = new HashMap<String, Collection<String>>();
    Map<String, Collection<String>> softDependencies = new HashMap<String, Collection<String>>();

    // This is where it figures out all possible plugins
    for (File file : directory.listFiles()) {
      PluginLoader loader = null;
      for (Pattern filter : filters) {
        Matcher match = filter.matcher(file.getName());
        if (match.find()) {
          loader = fileAssociations.get(filter);
        }
      }

      if (loader == null) continue;

      PluginDescriptionFile description = null;
      try {
        description = loader.getPluginDescription(file);
      } catch (InvalidDescriptionException ex) {
        server
            .getLogger()
            .log(
                Level.SEVERE,
                "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'",
                ex);
        continue;
      }

      plugins.put(description.getName(), file);

      Collection<String> softDependencySet = description.getSoftDepend();
      if (softDependencySet != null) {
        if (softDependencies.containsKey(description.getName())) {
          // Duplicates do not matter, they will be removed together if applicable
          softDependencies.get(description.getName()).addAll(softDependencySet);
        } else {
          softDependencies.put(description.getName(), new LinkedList<String>(softDependencySet));
        }
      }

      Collection<String> dependencySet = description.getDepend();
      if (dependencySet != null) {
        dependencies.put(description.getName(), new LinkedList<String>(dependencySet));
      }

      Collection<String> loadBeforeSet = description.getLoadBefore();
      if (loadBeforeSet != null) {
        for (String loadBeforeTarget : loadBeforeSet) {
          if (softDependencies.containsKey(loadBeforeTarget)) {
            softDependencies.get(loadBeforeTarget).add(description.getName());
          } else {
            // softDependencies is never iterated, so 'ghost' plugins aren't an issue
            Collection<String> shortSoftDependency = new LinkedList<String>();
            shortSoftDependency.add(description.getName());
            softDependencies.put(loadBeforeTarget, shortSoftDependency);
          }
        }
      }
    }

    while (!plugins.isEmpty()) {
      boolean missingDependency = true;
      Iterator<String> pluginIterator = plugins.keySet().iterator();

      while (pluginIterator.hasNext()) {
        String plugin = pluginIterator.next();

        if (dependencies.containsKey(plugin)) {
          Iterator<String> dependencyIterator = dependencies.get(plugin).iterator();

          while (dependencyIterator.hasNext()) {
            String dependency = dependencyIterator.next();

            // Dependency loaded
            if (loadedPlugins.contains(dependency)) {
              dependencyIterator.remove();

              // We have a dependency not found
            } else if (!plugins.containsKey(dependency)) {
              missingDependency = false;
              File file = plugins.get(plugin);
              pluginIterator.remove();
              softDependencies.remove(plugin);
              dependencies.remove(plugin);

              server
                  .getLogger()
                  .log(
                      Level.SEVERE,
                      "Could not load '"
                          + file.getPath()
                          + "' in folder '"
                          + directory.getPath()
                          + "'",
                      new UnknownDependencyException(dependency));
              break;
            }
          }

          if (dependencies.containsKey(plugin) && dependencies.get(plugin).isEmpty()) {
            dependencies.remove(plugin);
          }
        }
        if (softDependencies.containsKey(plugin)) {
          Iterator<String> softDependencyIterator = softDependencies.get(plugin).iterator();

          while (softDependencyIterator.hasNext()) {
            String softDependency = softDependencyIterator.next();

            // Soft depend is no longer around
            if (!plugins.containsKey(softDependency)) {
              softDependencyIterator.remove();
            }
          }

          if (softDependencies.get(plugin).isEmpty()) {
            softDependencies.remove(plugin);
          }
        }
        if (!(dependencies.containsKey(plugin) || softDependencies.containsKey(plugin))
            && plugins.containsKey(plugin)) {
          // We're clear to load, no more soft or hard dependencies left
          File file = plugins.get(plugin);
          pluginIterator.remove();
          missingDependency = false;

          try {
            result.add(loadPlugin(file));
            loadedPlugins.add(plugin);
            continue;
          } catch (InvalidPluginException ex) {
            server
                .getLogger()
                .log(
                    Level.SEVERE,
                    "Could not load '"
                        + file.getPath()
                        + "' in folder '"
                        + directory.getPath()
                        + "'",
                    ex);
          }
        }
      }

      if (missingDependency) {
        // We now iterate over plugins until something loads
        // This loop will ignore soft dependencies
        pluginIterator = plugins.keySet().iterator();

        while (pluginIterator.hasNext()) {
          String plugin = pluginIterator.next();

          if (!dependencies.containsKey(plugin)) {
            softDependencies.remove(plugin);
            missingDependency = false;
            File file = plugins.get(plugin);
            pluginIterator.remove();

            try {
              result.add(loadPlugin(file));
              loadedPlugins.add(plugin);
              break;
            } catch (InvalidPluginException ex) {
              server
                  .getLogger()
                  .log(
                      Level.SEVERE,
                      "Could not load '"
                          + file.getPath()
                          + "' in folder '"
                          + directory.getPath()
                          + "'",
                      ex);
            }
          }
        }
        // We have no plugins left without a depend
        if (missingDependency) {
          softDependencies.clear();
          dependencies.clear();
          Iterator<File> failedPluginIterator = plugins.values().iterator();

          while (failedPluginIterator.hasNext()) {
            File file = failedPluginIterator.next();
            failedPluginIterator.remove();
            server
                .getLogger()
                .log(
                    Level.SEVERE,
                    "Could not load '"
                        + file.getPath()
                        + "' in folder '"
                        + directory.getPath()
                        + "': circular dependency detected");
          }
        }
      }
    }

    return result.toArray(new Plugin[result.size()]);
  }
Example #10
0
 @Override
 public Logger getLogger() {
   return server.getLogger();
 }
  public Plugin loadPlugin(File file, boolean ignoreSoftDependencies)
      throws InvalidPluginException, InvalidDescriptionException, UnknownDependencyException {
    JavaPlugin result = null;
    PluginDescriptionFile description = null;

    if (!file.exists()) {
      throw new InvalidPluginException(
          new FileNotFoundException(String.format("%s does not exist", file.getPath())));
    }
    try {
      JarFile jar = new JarFile(file);
      JarEntry entry = jar.getJarEntry("plugin.yml");

      if (entry == null) {
        throw new InvalidPluginException(
            new FileNotFoundException("Jar does not contain plugin.yml"));
      }

      InputStream stream = jar.getInputStream(entry);

      description = new PluginDescriptionFile(stream);

      stream.close();
      jar.close();
    } catch (IOException ex) {
      throw new InvalidPluginException(ex);
    } catch (YAMLException ex) {
      throw new InvalidPluginException(ex);
    }

    File dataFolder = new File(file.getParentFile(), description.getName());
    File oldDataFolder = getDataFolder(file);

    // Found old data folder
    if (dataFolder.equals(oldDataFolder)) {
      // They are equal -- nothing needs to be done!
    } else if (dataFolder.isDirectory() && oldDataFolder.isDirectory()) {
      server
          .getLogger()
          .log(
              Level.INFO,
              String.format(
                  "While loading %s (%s) found old-data folder: %s next to the new one: %s",
                  description.getName(), file, oldDataFolder, dataFolder));
    } else if (oldDataFolder.isDirectory() && !dataFolder.exists()) {
      if (!oldDataFolder.renameTo(dataFolder)) {
        throw new InvalidPluginException(
            new Exception(
                "Unable to rename old data folder: '"
                    + oldDataFolder
                    + "' to: '"
                    + dataFolder
                    + "'"));
      }
      server
          .getLogger()
          .log(
              Level.INFO,
              String.format(
                  "While loading %s (%s) renamed data folder: '%s' to '%s'",
                  description.getName(), file, oldDataFolder, dataFolder));
    }

    if (dataFolder.exists() && !dataFolder.isDirectory()) {
      throw new InvalidPluginException(
          new Exception(
              String.format(
                  "Projected datafolder: '%s' for %s (%s) exists and is not a directory",
                  dataFolder, description.getName(), file)));
    }

    ArrayList<String> depend;

    try {
      depend = (ArrayList) description.getDepend();
      if (depend == null) {
        depend = new ArrayList<String>();
      }
    } catch (ClassCastException ex) {
      throw new InvalidPluginException(ex);
    }

    for (String pluginName : depend) {
      if (loaders == null) {
        throw new UnknownDependencyException(pluginName);
      }
      PluginClassLoader current = loaders.get(pluginName);

      if (current == null) {
        throw new UnknownDependencyException(pluginName);
      }
    }

    if (!ignoreSoftDependencies) {
      ArrayList<String> softDepend;

      try {
        softDepend = (ArrayList) description.getSoftDepend();
        if (softDepend == null) {
          softDepend = new ArrayList<String>();
        }
      } catch (ClassCastException ex) {
        throw new InvalidPluginException(ex);
      }

      for (String pluginName : softDepend) {
        if (loaders == null) {
          throw new UnknownSoftDependencyException(pluginName);
        }
        PluginClassLoader current = loaders.get(pluginName);

        if (current == null) {
          throw new UnknownSoftDependencyException(pluginName);
        }
      }
    }

    PluginClassLoader loader = null;

    try {
      URL[] urls = new URL[1];

      urls[0] = file.toURI().toURL();
      loader = new PluginClassLoader(this, urls, getClass().getClassLoader());
      Class<?> jarClass = Class.forName(description.getMain(), true, loader);
      Class<? extends JavaPlugin> plugin = jarClass.asSubclass(JavaPlugin.class);

      Constructor<? extends JavaPlugin> constructor = plugin.getConstructor();

      result = constructor.newInstance();

      result.initialize(this, server, description, dataFolder, file, loader);
    } catch (Throwable ex) {
      throw new InvalidPluginException(ex);
    }

    loaders.put(description.getName(), loader);

    return result;
  }
  public boolean setUp() {
    try {
      FileUtils.deleteFolder(invDirectory);
      FileUtils.deleteFolder(serverDirectory);
      invDirectory.mkdirs();
      Assert.assertTrue(invDirectory.exists());

      MockGateway.MOCK_STANDARD_METHODS = false;

      plugin = PowerMockito.spy(new MultiverseInventories());
      core = PowerMockito.spy(new MultiverseCore());

      // Let's let all MV files go to bin/test
      doReturn(invDirectory).when(plugin).getDataFolder();
      // Let's let all MV files go to bin/test
      doReturn(coreDirectory).when(core).getDataFolder();

      // Return a fake PDF file.
      PluginDescriptionFile pdf =
          PowerMockito.spy(
              new PluginDescriptionFile(
                  "Multiverse-Inventories",
                  "2.4-test",
                  "com.onarandombox.multiverseinventories.MultiverseInventories"));
      when(pdf.getAuthors()).thenReturn(new ArrayList<String>());
      doReturn(pdf).when(plugin).getDescription();
      doReturn(core).when(plugin).getCore();
      doReturn(true).when(plugin).isEnabled();
      PluginDescriptionFile pdfCore =
          PowerMockito.spy(
              new PluginDescriptionFile(
                  "Multiverse-Core", "2.2-Test", "com.onarandombox.MultiverseCore.MultiverseCore"));
      when(pdfCore.getAuthors()).thenReturn(new ArrayList<String>());
      doReturn(pdfCore).when(core).getDescription();
      doReturn(true).when(core).isEnabled();
      plugin.setServerFolder(serverDirectory);

      // Add Core to the list of loaded plugins
      JavaPlugin[] plugins = new JavaPlugin[] {plugin, core};

      // Mock the Plugin Manager
      PluginManager mockPluginManager = PowerMockito.mock(PluginManager.class);
      when(mockPluginManager.getPlugins()).thenReturn(plugins);
      when(mockPluginManager.getPlugin("Multiverse-Inventories")).thenReturn(plugin);
      when(mockPluginManager.getPlugin("Multiverse-Core")).thenReturn(core);
      when(mockPluginManager.getPermission(anyString())).thenReturn(null);

      // Make some fake folders to fool the fake MV into thinking these worlds exist
      File worldNormalFile = new File(plugin.getServerFolder(), "world");
      Util.log("Creating world-folder: " + worldNormalFile.getAbsolutePath());
      worldNormalFile.mkdirs();
      MockWorldFactory.makeNewMockWorld("world", Environment.NORMAL, WorldType.NORMAL);
      File worldNetherFile = new File(plugin.getServerFolder(), "world_nether");
      Util.log("Creating world-folder: " + worldNetherFile.getAbsolutePath());
      worldNetherFile.mkdirs();
      MockWorldFactory.makeNewMockWorld("world_nether", Environment.NETHER, WorldType.NORMAL);
      File worldSkylandsFile = new File(plugin.getServerFolder(), "world_the_end");
      Util.log("Creating world-folder: " + worldSkylandsFile.getAbsolutePath());
      worldSkylandsFile.mkdirs();
      MockWorldFactory.makeNewMockWorld("world_the_end", Environment.THE_END, WorldType.NORMAL);
      File world2File = new File(plugin.getServerFolder(), "world2");
      Util.log("Creating world-folder: " + world2File.getAbsolutePath());
      world2File.mkdirs();
      MockWorldFactory.makeNewMockWorld("world2", Environment.NORMAL, WorldType.NORMAL);

      // Initialize the Mock server.
      mockServer = mock(Server.class);
      when(mockServer.getName()).thenReturn("TestBukkit");
      Logger.getLogger("Minecraft").setParent(Util.logger);
      when(mockServer.getLogger()).thenReturn(Util.logger);
      when(mockServer.getWorldContainer()).thenReturn(worldsDirectory);
      when(plugin.getServer()).thenReturn(mockServer);
      when(core.getServer()).thenReturn(mockServer);
      when(mockServer.getPluginManager()).thenReturn(mockPluginManager);
      Answer<Player> playerAnswer =
          new Answer<Player>() {
            public Player answer(InvocationOnMock invocation) throws Throwable {
              String arg;
              try {
                arg = (String) invocation.getArguments()[0];
              } catch (Exception e) {
                return null;
              }
              Player player = players.get(arg);
              if (player == null) {
                player = new MockPlayer(arg, mockServer);
                players.put(arg, player);
              }
              return player;
            }
          };
      when(mockServer.getPlayer(anyString())).thenAnswer(playerAnswer);
      when(mockServer.getOfflinePlayer(anyString())).thenAnswer(playerAnswer);
      when(mockServer.getOfflinePlayers())
          .thenAnswer(
              new Answer<OfflinePlayer[]>() {
                public OfflinePlayer[] answer(InvocationOnMock invocation) throws Throwable {
                  return players.values().toArray(new Player[players.values().size()]);
                }
              });
      when(mockServer.getOnlinePlayers())
          .thenAnswer(
              new Answer<Player[]>() {
                public Player[] answer(InvocationOnMock invocation) throws Throwable {
                  return players.values().toArray(new Player[players.values().size()]);
                }
              });

      // Give the server some worlds
      when(mockServer.getWorld(anyString()))
          .thenAnswer(
              new Answer<World>() {
                public World answer(InvocationOnMock invocation) throws Throwable {
                  String arg;
                  try {
                    arg = (String) invocation.getArguments()[0];
                  } catch (Exception e) {
                    return null;
                  }
                  return MockWorldFactory.getWorld(arg);
                }
              });

      when(mockServer.getWorld(any(UUID.class)))
          .thenAnswer(
              new Answer<World>() {
                @Override
                public World answer(InvocationOnMock invocation) throws Throwable {
                  UUID arg;
                  try {
                    arg = (UUID) invocation.getArguments()[0];
                  } catch (Exception e) {
                    return null;
                  }
                  return MockWorldFactory.getWorld(arg);
                }
              });

      when(mockServer.getWorlds())
          .thenAnswer(
              new Answer<List<World>>() {
                public List<World> answer(InvocationOnMock invocation) throws Throwable {
                  return MockWorldFactory.getWorlds();
                }
              });

      when(mockServer.createWorld(Matchers.isA(WorldCreator.class)))
          .thenAnswer(
              new Answer<World>() {
                public World answer(InvocationOnMock invocation) throws Throwable {
                  WorldCreator arg;
                  try {
                    arg = (WorldCreator) invocation.getArguments()[0];
                  } catch (Exception e) {
                    return null;
                  }
                  // Add special case for creating null worlds.
                  // Not sure I like doing it this way, but this is a special case
                  if (arg.name().equalsIgnoreCase("nullworld")) {
                    return MockWorldFactory.makeNewNullMockWorld(
                        arg.name(), arg.environment(), arg.type());
                  }
                  return MockWorldFactory.makeNewMockWorld(
                      arg.name(), arg.environment(), arg.type());
                }
              });

      when(mockServer.unloadWorld(anyString(), anyBoolean())).thenReturn(true);

      // add mock scheduler
      BukkitScheduler mockScheduler = mock(BukkitScheduler.class);
      when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class), anyLong()))
          .thenAnswer(
              new Answer<Integer>() {
                public Integer answer(InvocationOnMock invocation) throws Throwable {
                  Runnable arg;
                  try {
                    arg = (Runnable) invocation.getArguments()[1];
                  } catch (Exception e) {
                    return null;
                  }
                  arg.run();
                  return null;
                }
              });
      when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class)))
          .thenAnswer(
              new Answer<Integer>() {
                public Integer answer(InvocationOnMock invocation) throws Throwable {
                  Runnable arg;
                  try {
                    arg = (Runnable) invocation.getArguments()[1];
                  } catch (Exception e) {
                    return null;
                  }
                  arg.run();
                  return null;
                }
              });
      when(mockServer.getScheduler()).thenReturn(mockScheduler);

      // Set InventoriesListener
      InventoriesListener il = PowerMockito.spy(new InventoriesListener(plugin));
      Field inventoriesListenerField =
          MultiverseInventories.class.getDeclaredField("inventoriesListener");
      inventoriesListenerField.setAccessible(true);
      inventoriesListenerField.set(plugin, il);

      // Set Core
      Field coreField = MultiverseInventories.class.getDeclaredField("core");
      coreField.setAccessible(true);
      coreField.set(plugin, core);

      // Set server
      Field serverfield = JavaPlugin.class.getDeclaredField("server");
      serverfield.setAccessible(true);
      serverfield.set(plugin, mockServer);

      // Set worldManager
      WorldManager wm = PowerMockito.spy(new WorldManager(core));
      Field worldmanagerfield = MultiverseCore.class.getDeclaredField("worldManager");
      worldmanagerfield.setAccessible(true);
      worldmanagerfield.set(core, wm);

      // Set playerListener
      MVPlayerListener pl = PowerMockito.spy(new MVPlayerListener(core));
      Field playerlistenerfield = MultiverseCore.class.getDeclaredField("playerListener");
      playerlistenerfield.setAccessible(true);
      playerlistenerfield.set(core, pl);

      // Set entityListener
      MVEntityListener el = PowerMockito.spy(new MVEntityListener(core));
      Field entitylistenerfield = MultiverseCore.class.getDeclaredField("entityListener");
      entitylistenerfield.setAccessible(true);
      entitylistenerfield.set(core, el);

      // Set weatherListener
      MVWeatherListener wl = PowerMockito.spy(new MVWeatherListener(core));
      Field weatherlistenerfield = MultiverseCore.class.getDeclaredField("weatherListener");
      weatherlistenerfield.setAccessible(true);
      weatherlistenerfield.set(core, wl);

      // Init our command sender
      final Logger commandSenderLogger = Logger.getLogger("CommandSender");
      commandSenderLogger.setParent(Util.logger);
      commandSender = mock(CommandSender.class);
      doAnswer(
              new Answer<Void>() {
                public Void answer(InvocationOnMock invocation) throws Throwable {
                  commandSenderLogger.info(
                      ChatColor.stripColor((String) invocation.getArguments()[0]));
                  return null;
                }
              })
          .when(commandSender)
          .sendMessage(anyString());
      when(commandSender.getServer()).thenReturn(mockServer);
      when(commandSender.getName()).thenReturn("MockCommandSender");
      when(commandSender.isPermissionSet(anyString())).thenReturn(true);
      when(commandSender.isPermissionSet(Matchers.isA(Permission.class))).thenReturn(true);
      when(commandSender.hasPermission(anyString())).thenReturn(true);
      when(commandSender.hasPermission(Matchers.isA(Permission.class))).thenReturn(true);
      when(commandSender.addAttachment(plugin)).thenReturn(null);
      when(commandSender.isOp()).thenReturn(true);

      Bukkit.setServer(mockServer);

      // Load Multiverse Core
      core.onLoad();
      plugin.onLoad();

      // Enable it.
      core.onEnable();
      plugin.onEnable();

      return true;
    } catch (Exception e) {
      e.printStackTrace();
    }

    return false;
  }