title: Kotlin can't use 'this' on init
body: <p>I want <code>game</code> to be set to a new instance of <code>Game</code> and I want to pass <code>this</code> into game. SO this is what I have.</p>
  
  <pre><code>var game: Game
  
  init {
      game = Game(this)
  }
  </code></pre>
  
  <p>I have also tried</p>
  
  <pre><code>var game: Game = Game(this)
  </code></pre>
  
  <p>both threw a <code>NullPointer</code> at run time but it seems fine in Intellij and has no problem compiling. What am I doing wrong?</p>
  
  <p><em>Stack Trace</em></p>
  
  <pre><code>org.bukkit.plugin.InvalidPluginException: java.lang.NullPointerException
  at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[server.jar:git-Spigot-21fe707-e1ebe52]
  at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:329) ~[server.jar:git-Spigot-21fe707-e1ebe52]
  at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:251) [server.jar:git-Spigot-21fe707-e1ebe52]
  at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugins(CraftServer.java:292) [server.jar:git-Spigot-21fe707-e1ebe52]
  at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:198) [server.jar:git-Spigot-21fe707-e1ebe52]
  at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [server.jar:git-Spigot-21fe707-e1ebe52]
  at java.lang.Thread.run(Thread.java:745) [?:1.8.0_121]
  Caused by: java.lang.NullPointerException
  at org.bukkit.plugin.SimplePluginManager.registerEvents(SimplePluginManager.java:523) ~[server.jar:git-Spigot-21fe707-e1ebe52]
  at me.darkpaladin.uhc.game.gameSettings.GameSettingsManager.addGameSettings(GameSettingsManager.kt:43) ~[?:?]
  at me.darkpaladin.uhc.game.gameSettings.GameSettingsManager.&lt;init&gt;(GameSettingsManager.kt:20) ~[?:?]
  at me.darkpaladin.uhc.game.Game.&lt;init&gt;(Game.kt:49) ~[?:?]
  at me.darkpaladin.uhc.UHC.&lt;init&gt;(UHC.kt:20) ~[?:?]
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_121]
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_121]
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_121]
  at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[?:1.8.0_121]
  at java.lang.Class.newInstance(Class.java:442) ~[?:1.8.0_121]
  at org.bukkit.plugin.java.PluginClassLoader.&lt;init&gt;(PluginClassLoader.java:76) ~[server.jar:git-Spigot-21fe707-e1ebe52]
  at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131) ~[server.jar:git-Spigot-21fe707-e1ebe52]
  ... 6 more
  </code></pre>
  
  <p><em>UHC Class</em></p>
  
  <pre><code>var game: Game
  
  init {
      game = Game(this)
  }
  
  override fun onReady() {
      //        new Lobby(Bukkit.getWorld("world"), this, false);
      TeamManager()
      registerCommands()
  }
  
  fun registerCommands() {
      val c = commandHandler
      c.addCommand(
              TeamCommand(),
              SetGameTypeCommand(game),
              ClaimHostCommand(game),
              StartGameCommand(game),
              ScenarioManagerCommand(game),
              KillTopCommand(game),
              ConfigCommand(game),
              ScatterCommand(game)
      )
  }
  
  fun registerListeners() {
      val p = Bukkit.getPluginManager()
      p.registerEvents(GamePlayerListener(game), this)
  }
  </code></pre>
  
  <p><em>Game Class</em></p>
  
  <pre><code>package me.darkpaladin.uhc.game
  
  import me.darkpaladin.core.Core
  import me.darkpaladin.core.utils.CoreUtils
  import me.darkpaladin.core.utils.PacketUtils
  import me.darkpaladin.uhc.UHC
  import me.darkpaladin.uhc.events.GameStartEvent
  import me.darkpaladin.uhc.events.GameStopEvent
  import me.darkpaladin.uhc.game.gameEvents.GameEvent
  import me.darkpaladin.uhc.game.gameEvents.GameEventRunnable
  import me.darkpaladin.uhc.game.gamePlayers.GamePlayer
  import me.darkpaladin.uhc.game.gamePlayers.GamePlayerManager
  import me.darkpaladin.uhc.game.gameSettings.GameSettingsManager
  import me.darkpaladin.uhc.scenarios.ScenarioManager
  import org.apache.commons.lang.WordUtils
  import org.bukkit.*
  import org.bukkit.entity.Item
  import org.bukkit.entity.Monster
  import org.bukkit.entity.Player
  import org.bukkit.scheduler.BukkitRunnable
  
  import java.util.*
  
  /**
   * Created by Caleb on 4/28/2017.
   */
  class Game(private val plugin: UHC) {
  
      var gameType = GameType.NORMAL
      var gameState = GameState.SETTING_UP
      var gameStartTicks = (CoreUtils.ticksPerSecond * 10).toLong()
      var finalHealTime = (CoreUtils.ticksPerSecond * 15).toLong()
      var pvpTicks = (CoreUtils.ticksPerSecond * 20).toLong()
      var meetupTicks = (CoreUtils.ticksPerSecond * 25).toLong()
      var isBorderShrink = true
      var episodeLength = (CoreUtils.ticksPerMinute * 20).toLong()
      private var episode = 1
  
      private val worlds = ArrayList&lt;World&gt;()
  
      private val gameEvents = ArrayList&lt;GameEvent&gt;()
  
      var gameTicks: Long = 0L
  
      var hostUuid: UUID? = null
  
      val scenarioManager: ScenarioManager = ScenarioManager(this)
      val gamePlayerManager: GamePlayerManager = GamePlayerManager()
      val gameSettingsManager: GameSettingsManager = GameSettingsManager(this)
  
      private val instance: Game
  
      init {
          worlds.add(Bukkit.getWorld("world")) //TODO: remove
          instance = this
      }
  
      val overworld: World?
          get() = getWorldWithEnvironment(World.Environment.NORMAL)
  
      val nether: World?
          get() = getWorldWithEnvironment(World.Environment.NETHER)
  
      val end: World?
          get() = getWorldWithEnvironment(World.Environment.THE_END)
  
      fun getWorldWithEnvironment(environment: World.Environment): World? {
          return worlds.firstOrNull { it.environment == environment }
      }
  
      fun addGameEvent(vararg events: GameEvent) {
          gameEvents.addAll(Arrays.asList(*events))
      }
  
      val host: Player
          get() = Bukkit.getPlayer(hostUuid)
  
      fun start() {
          gameState = GameState.STARTING
  
          val finalHeal = GameEvent("Final Heal", finalHealTime, object : GameEventRunnable(this) {
              override fun run() {
                  Bukkit.getOnlinePlayers().forEach { player -&gt; player.health = player.maxHealth }
                  CoreUtils.broadcast(Core.PREFIX + "Final Heal has been given. This is the FINAL heal. Do not ask for more.")
              }
          })
  
          val pvp = GameEvent("PvP", pvpTicks, object : GameEventRunnable(this) {
              override fun run() {
                  for (world in worlds) {
                      world.pvp = true
                      world.setGameRuleValue("doMobSpawning", "true")
                  }
              }
          })
  
          val starting = GameEvent("Starting in", gameStartTicks, object : GameEventRunnable(this) {
              override fun run() {
                  gameTicks = 0L
                  addGameEvent(finalHeal, pvp)
                  if (isBorderShrink)
                      addGameEvent(BorderShrinkGameEvent(meetupTicks, instance))
  
                  for (player in Bukkit.getOnlinePlayers()) {
                      player.health = player.maxHealth
                      player.foodLevel = 20
                      player.saturation = 20f
                      player.level = 0
                      player.exp = 0f
                      player.totalExperience = 0
                      player.closeInventory()
                      player.inventory.clear()
                      player.inventory.armorContents = null
                      player.gameMode = GameMode.SURVIVAL
                      player.activePotionEffects.forEach { potionEffect -&gt; player.removePotionEffect(potionEffect.type) }
                  }
  
                  for (world in worlds) {
                      for (entity in world.entities) {
                          if (entity is Item || entity is Monster) {
                              entity.remove()
                          }
                      }
                      world.pvp = false
                      world.difficulty = Difficulty.HARD
                      world.time = 20
                      world.setSpawnFlags(false, false)
                  }
  
                  Bukkit.getOnlinePlayers().forEach { player -&gt; gamePlayerManager.addGamePlayers(GamePlayer(player.uniqueId)) }
  
                  val players = ArrayList&lt;Player&gt;()
                  gamePlayerManager.aliveGamePlayers
                          .filter { gamePlayer -&gt; gamePlayer.player != null }
                          .forEach { gamePlayer -&gt; players.add(gamePlayer.player) }
  
                  scenarioManager.giveStartingItems(players)
  
                  gameState = GameState.RUNNING
                  Bukkit.getPluginManager().callEvent(GameStartEvent(instance))
  
              }
          })
  
          addGameEvent(starting)
  
          GameTimer().runTaskTimer(plugin, 0, 1)
          if (gameType === GameType.RECORDED)
              EpisodeTimerTask()
      }
  
      val nextEvent: GameEvent?
          get() {
              val events = gameEvents
  
              val times = HashMap&lt;GameEvent, Long&gt;()
  
              events.forEach { gameEvent -&gt; times.put(gameEvent, gameEvent.time) }
  
              var nextEvent: GameEvent? = null
              var nextEventTime: Long = 999999999999999999L
              for (event in times.keys) {
                  if (event.time &lt; nextEventTime) {
                      nextEvent = event
                      nextEventTime = event.time
                  }
              }
              return nextEvent
          }
  
      fun getNextBorderRadius(world: World): Int {
          return Math.round(world.worldBorder.size / 2 * .75).toInt()
      }
  
      private inner class GameTimer : BukkitRunnable() {
          override fun run() {
  
              val nextEvent = nextEvent
  
              if (nextEvent != null) {
                  if (gameTicks!! % 20 == 0L) {
                      PacketUtils.sendAction(Core.HIGHLIGHTED_COLOR.toString() + ChatColor.BOLD.toString() + WordUtils.capitalizeFully(nextEvent.name) + Core.EXTRA_COLOR + " » " + Core.HIGHLIGHTED_COLOR + CoreUtils.formatTicks(nextEvent.time - gameTicks!!))
                  }
  
                  if (gameTicks &gt;= nextEvent.time) {
                      nextEvent.runnable!!.run()
                      gameEvents.remove(nextEvent)
                  }
              }
  
              gameTicks++
  
          }
      }
  
      private inner class BorderShrinkGameEvent(time: Long, game: Game) : GameEvent("Border Shrink", time, object : GameEventRunnable(game) {
          override fun run() {
              for (world in worlds) {
                  val wb = world.worldBorder
  
                  val radius = Math.round(wb.size / 2).toInt()
                  val newRadius = Math.round(wb.size / 2 * .75).toInt()
  
                  if (newRadius &lt; 50)
                      wb.size = 100.0
                  else
                      wb.size = (newRadius * 2).toDouble()
  
                  CoreUtils.broadcast(Core.PREFIX + "The border has shrunk from " + radius + " to " + Math.round(wb.size / 2).toInt() + "!")
  
                  if (wb.size &gt; 100) {
                      game.addGameEvent(BorderShrinkGameEvent(CoreUtils.ticksPerSecond * 5 + game.gameTicks!!, game))
                  }
  
              }
          }
      })
  
      private inner class EpisodeTimerTask : BukkitRunnable() {
  
          private var ticks: Long = 0
  
          init {
              ticks = episodeLength
          }
  
          override fun run() {
  
              if (ticks &lt;= 0) {
                  cancel()
  
                  CoreUtils.broadcast(Core.PREFIX + "End of episode " + episode + ". Start episode " + episode + 1)
                  episode++
                  return
              }
  
              ticks--
          }
      }
  
      fun stop() {
          scenarioManager.disableAll()
          Bukkit.getPluginManager().callEvent(GameStopEvent(this))
  
          plugin.game = Game(plugin)
      }
  
  }
  </code></pre>
  
  <p><em>Game Setting Manager</em></p>
  
  <pre><code>package me.darkpaladin.uhc.game.gameSettings
  
  import me.darkpaladin.core.Core
  import me.darkpaladin.uhc.game.Game
  import org.bukkit.Bukkit
  import org.bukkit.event.Listener
  
  import java.util.ArrayList
  import java.util.Arrays
  import java.util.stream.Collectors
  
  /**
   * Created by caleb on 5/6/17.
   */
  class GameSettingsManager(game: Game) {
  
      private val gameSettings = ArrayList&lt;GameSetting&gt;()
  
      init {
          addGameSettings(
                  FriendlyFireGameSetting(game),
                  NotchApplesGameSetting(),
                  DoubleArrowsGameSetting()
          )
      }
  
      fun getGameSettings(): List&lt;GameSetting&gt; {
          return gameSettings
      }
  
      val toggleableGameSettings: List&lt;ToggleableGameSetting&gt;
          get() {
              val settings = ArrayList&lt;ToggleableGameSetting&gt;()
              for (setting in getGameSettings())
                  if (setting is ToggleableGameSetting)
                      settings.add(setting)
              return settings
          }
  
      fun addGameSettings(vararg gameSettings: GameSetting) {
          this.gameSettings.addAll(Arrays.asList(*gameSettings))
          for(setting in gameSettings)
              Bukkit.getPluginManager().registerEvents(setting as Listener, Core.instance)
      }
  
      fun getGameSetting(name: String): GameSetting {
          return gameSettings.stream()
                  .filter { gameSetting -&gt; gameSetting.name.equals(name, ignoreCase = true) }
                  .collect(Collectors.toList&lt;GameSetting&gt;())[0]
      }
  
  }
  </code></pre>