github.com/df-mc/dragonfly@v0.9.13/server/world/game_mode.go (about)

     1  package world
     2  
     3  // GameMode represents a game mode that may be assigned to a player. Upon joining the world, players will be
     4  // given the default game mode that the world holds.
     5  // Game modes specify the way that a player interacts with and plays in the world.
     6  type GameMode interface {
     7  	// AllowsEditing specifies if a player with this GameMode can edit the World it's in.
     8  	AllowsEditing() bool
     9  	// AllowsTakingDamage specifies if a player with this GameMode can take damage from other entities.
    10  	AllowsTakingDamage() bool
    11  	// CreativeInventory specifies if a player with this GameMode has access to the creative inventory.
    12  	CreativeInventory() bool
    13  	// HasCollision specifies if a player with this GameMode can collide with blocks or entities in the world.
    14  	HasCollision() bool
    15  	// AllowsFlying specifies if a player with this GameMode can fly freely.
    16  	AllowsFlying() bool
    17  	// AllowsInteraction specifies if a player with this GameMode can interact with the world through entities or if it
    18  	// can use items in the world.
    19  	AllowsInteraction() bool
    20  	// Visible specifies if a player with this GameMode can be visible to other players. If false, the player will be
    21  	// invisible under any circumstance.
    22  	Visible() bool
    23  }
    24  
    25  var (
    26  	// GameModeSurvival is the survival game mode: Players with this game mode have limited supplies and can break blocks
    27  	// after taking some time.
    28  	GameModeSurvival survival
    29  	// GameModeCreative represents the creative game mode: Players with this game mode have infinite blocks and
    30  	// items and can break blocks instantly. Players with creative mode can also fly.
    31  	GameModeCreative creative
    32  	// GameModeAdventure represents the adventure game mode: Players with this game mode cannot edit the world
    33  	// (placing or breaking blocks).
    34  	GameModeAdventure adventure
    35  	// GameModeSpectator represents the spectator game mode: Players with this game mode cannot interact with the
    36  	// world and cannot be seen by other players. spectator players can fly, like creative mode, and can
    37  	// move through blocks.
    38  	GameModeSpectator spectator
    39  )
    40  
    41  var gameModeReg = newGameModeRegistry(map[int]GameMode{
    42  	0: GameModeSurvival,
    43  	1: GameModeCreative,
    44  	2: GameModeAdventure,
    45  	3: GameModeSpectator,
    46  })
    47  
    48  // GameModeByID looks up a GameMode for the ID passed, returning
    49  // GameModeSurvival for 0, GameModeCreative for 1, GameModeAdventure for 2 and
    50  // GameModeSpectator for 3. If the ID is unknown, the bool returned is false. In
    51  // this case the GameMode returned is GameModeSurvival.
    52  func GameModeByID(id int) (GameMode, bool) {
    53  	return gameModeReg.Lookup(id)
    54  }
    55  
    56  // GameModeID looks up the ID that a GameMode was registered with. If not
    57  // found, false is returned.
    58  func GameModeID(mode GameMode) (int, bool) {
    59  	return gameModeReg.LookupID(mode)
    60  }
    61  
    62  type gameModeRegistry struct {
    63  	gameModes map[int]GameMode
    64  	ids       map[GameMode]int
    65  }
    66  
    67  // newGameModeRegistry returns an initialised gameModeRegistry.
    68  func newGameModeRegistry(mode map[int]GameMode) *gameModeRegistry {
    69  	ids := make(map[GameMode]int, len(mode))
    70  	for k, v := range mode {
    71  		ids[v] = k
    72  	}
    73  	return &gameModeRegistry{gameModes: mode, ids: ids}
    74  }
    75  
    76  // Lookup looks up a GameMode for the ID passed, returning GameModeSurvival for
    77  // 0, GameModeCreative for 1, GameModeAdventure for 2 and GameModeSpectator for
    78  // 3. If the ID is unknown, the bool returned is false. In this case the
    79  // GameMode returned is GameModeSurvival.
    80  func (reg *gameModeRegistry) Lookup(id int) (GameMode, bool) {
    81  	mode, ok := reg.gameModes[id]
    82  	if !ok {
    83  		mode = GameModeSurvival
    84  	}
    85  	return mode, ok
    86  }
    87  
    88  // LookupID looks up the ID that a GameMode was registered with. If not found,
    89  // false is returned.
    90  func (reg *gameModeRegistry) LookupID(mode GameMode) (int, bool) {
    91  	id, ok := reg.ids[mode]
    92  	return id, ok
    93  }
    94  
    95  // survival is the survival game mode: Players with this game mode have limited supplies and can break blocks after
    96  // taking some time.
    97  type survival struct{}
    98  
    99  func (survival) AllowsEditing() bool      { return true }
   100  func (survival) AllowsTakingDamage() bool { return true }
   101  func (survival) CreativeInventory() bool  { return false }
   102  func (survival) HasCollision() bool       { return true }
   103  func (survival) AllowsFlying() bool       { return false }
   104  func (survival) AllowsInteraction() bool  { return true }
   105  func (survival) Visible() bool            { return true }
   106  
   107  // creative represents the creative game mode: Players with this game mode have infinite blocks and
   108  // items and can break blocks instantly. Players with creative mode can also fly.
   109  type creative struct{}
   110  
   111  func (creative) AllowsEditing() bool      { return true }
   112  func (creative) AllowsTakingDamage() bool { return false }
   113  func (creative) CreativeInventory() bool  { return true }
   114  func (creative) HasCollision() bool       { return true }
   115  func (creative) AllowsFlying() bool       { return true }
   116  func (creative) AllowsInteraction() bool  { return true }
   117  func (creative) Visible() bool            { return true }
   118  
   119  // adventure represents the adventure game mode: Players with this game mode cannot edit the world
   120  // (placing or breaking blocks).
   121  type adventure struct{}
   122  
   123  func (adventure) AllowsEditing() bool      { return false }
   124  func (adventure) AllowsTakingDamage() bool { return true }
   125  func (adventure) CreativeInventory() bool  { return false }
   126  func (adventure) HasCollision() bool       { return true }
   127  func (adventure) AllowsFlying() bool       { return false }
   128  func (adventure) AllowsInteraction() bool  { return true }
   129  func (adventure) Visible() bool            { return true }
   130  
   131  // spectator represents the spectator game mode: Players with this game mode cannot interact with the
   132  // world and cannot be seen by other players. spectator players can fly, like creative mode, and can
   133  // move through blocks.
   134  type spectator struct{}
   135  
   136  func (spectator) AllowsEditing() bool      { return false }
   137  func (spectator) AllowsTakingDamage() bool { return false }
   138  func (spectator) CreativeInventory() bool  { return false }
   139  func (spectator) HasCollision() bool       { return false }
   140  func (spectator) AllowsFlying() bool       { return true }
   141  func (spectator) AllowsInteraction() bool  { return false }
   142  func (spectator) Visible() bool            { return false }