Package org.bukkit.event.player

# Class PlayerGameModeChangeEvent

java.lang.Object
org.bukkit.event.Event
org.bukkit.event.player.PlayerEvent
org.bukkit.event.player.PlayerGameModeChangeEvent

All Implemented Interfaces:
  `Cancellable`

public class PlayerGameModeChangeEvent extends PlayerEvent implements Cancellable
Called when the GameMode of the player is changed.

NOTE: When `getCause()` is `PlayerGameModeChangeEvent.Cause.DEFAULT_GAMEMODE`, the Player from `PlayerEvent.getPlayer()` might not be fully online at the time this event is fired. Plugins should use `OfflinePlayer.isOnline()` to check before changing player state.

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static enum`
`PlayerGameModeChangeEvent.Cause`

## Nested classes/interfaces inherited from class org.bukkit.event.Event

`Event.Result`

## Field Summary

### Fields inherited from class org.bukkit.event.player.PlayerEvent

`player`

## Constructor Summary

Constructors
Constructor
Description
`PlayerGameModeChangeEvent(@NotNull Player player, @NotNull GameMode newGameMode)`
Deprecated.
`PlayerGameModeChangeEvent(@NotNull Player player, @NotNull GameMode newGameMode, @NotNull PlayerGameModeChangeEvent.Cause cause, Component cancelMessage)`

## Method Summary

Modifier and Type
Method
Description
`Component`
`cancelMessage()`
Only valid if the cause of the gamemode change was directly due to a command..
`void`
`cancelMessage(Component message)`
Sets the message shown to the command user if the event was cancelled.
`@NotNull PlayerGameModeChangeEvent.Cause`
`getCause()`
Gets the cause of this gamemode change.
`static @NotNull HandlerList`
`getHandlerList()`
`@NotNull HandlerList`
`getHandlers()`
`@NotNull GameMode`
`getNewGameMode()`
Gets the GameMode the player is switched to.
`boolean`
`isCancelled()`
Gets the cancellation state of this event.
`void`
`setCancelled(boolean cancel)`
Sets the cancellation state of this event.

### Methods inherited from class org.bukkit.event.player.PlayerEvent

`getPlayer`

### Methods inherited from class org.bukkit.event.Event

`callEvent, getEventName, isAsynchronous`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### PlayerGameModeChangeEvent

@Deprecated public PlayerGameModeChangeEvent(@NotNull @NotNull Player player, @NotNull @NotNull GameMode newGameMode)
Deprecated.

### PlayerGameModeChangeEvent

public PlayerGameModeChangeEvent(@NotNull @NotNull Player player, @NotNull @NotNull GameMode newGameMode, @NotNull @NotNull PlayerGameModeChangeEvent.Cause cause, @Nullable Component cancelMessage)

## Method Details

### isCancelled

public boolean isCancelled()
Description copied from interface: `Cancellable`
Gets the cancellation state of this event. A cancelled event will not be executed in the server, but will still pass to other plugins

Specified by:
  `isCancelled` in interface `Cancellable`
Returns:
  true if this event is cancelled

### setCancelled

public void setCancelled(boolean cancel)
Description copied from interface: `Cancellable`
Sets the cancellation state of this event. A cancelled event will not be executed in the server, but will still pass to other plugins.

Specified by:
  `setCancelled` in interface `Cancellable`
Parameters:
  `cancel` - true if you wish to cancel this event

### getNewGameMode

@NotNull public @NotNull GameMode getNewGameMode()
Gets the GameMode the player is switched to.

Returns:
  player's new GameMode

### getHandlers

@NotNull public @NotNull HandlerList getHandlers()

Specified by:
  `getHandlers` in class `Event`

### getHandlerList

@NotNull public static @NotNull HandlerList getHandlerList()

### getCause

@NotNull public @NotNull PlayerGameModeChangeEvent.Cause getCause()
Gets the cause of this gamemode change.

Returns:
  the cause

### cancelMessage

@Nullable public Component cancelMessage()
Only valid if the cause of the gamemode change was directly due to a command.. Gets the message shown to the command user if the event is cancelled as a notification that a player's gamemode was not changed.

This returns `null` if the gamemode change was due to a plugin, or a player joining the game with a gamemode not equal to the server default gamemode and `force-gamemode` is set to true.

Returns:
  the error message shown to the command user, null if not directly caused by a command

### cancelMessage

public void cancelMessage(@Nullable Component message)
Sets the message shown to the command user if the event was cancelled. The message is only shown to cancelled events that are directly called by a command not by a plugin or a player joining with the wrong gamemode.

Parameters:
  `message` - the error message shown to the command user, null to show no message.