Change GameMode with PlayerDeathEvent?

Pl
- in Plugins
3

I'm currently learning to code plugins for Minecraft with Java and would like to know how I can change the GameMode after a user dies? In the case of GameMode Spectator

cl

With player # setGameMode (GameMode)

So like this:

player.setGameMode (GameMode.SPECTATOR);

I'm just not sure whether this already works in the PlayerDeathEvent or whether you should first call it in the PlayerRespawnEvent.

Pl

Ok thanks, but how can I implement that in the event?

cl

First you get your player, let's start with the PlayerDeathEvent:

@EventHandler
public void onDeath (PlayerDeathEvent event) {
Player player = event.getEntity ();
}

Then you just set the game mode for the player:

@EventHandler
public void onDeath (PlayerDeathEvent event) {
Player player = event.getEntity ();

player.setGameMode (GameMode.SPECTATOR);
}

GameMode is an enum, i.e. You call it via GameMode. + the corresponding GameMode, e.g. CREATIVE, SPECTATOR or SURVIVAL.

https://hub.spigotmc.org/...eMode.html