Game Rules and Modes

Master control over your Minecraft server's rules and modes to fine-tune the gameplay experience. The "Enchanted" API's "Game Rules and Modes" category provides server-side methods to configure essential game settings through your plugins. This versatility allows for custom game modes and rule enforcement.

Game Modes

Default Game Mode

Set the server's default game mode for new players.

setDefaultGameMode(mode: 'survival' | 'creative' | 'adventure' | 'spectator'): Promise<void>

Player Game Mode

Change the game mode for a specific player or set of players.

setGameMode(gamemode: string, target: string = '@s'): Promise<void>

Game Rules

Set Difficulty

Adjust the server's difficulty level.

setDifficulty(difficulty: string): Promise<boolean>

Modify Experience

Modify experience points or levels for players.

experience(
    action: 'add' | 'set' | 'query',
    target: string,
    amount?: number,
    type: 'levels' | 'points' = 'points'
): Promise<string>

Usage and Code Examples

Set Default Game Mode

// Set default game mode to creative
await api.setDefaultGameMode('creative');

Change Player Game Mode

// Change game mode for all players to survival
await api.setGameMode('survival', '@a');

Set Difficulty Level

// Set server difficulty to hard
const success = await api.setDifficulty('hard');
if (success) {
  console.log('Difficulty set to hard successfully.');
}

Adjust Player Experience

// Add experience points to a player
await api.experience('add', 'PlayerName', 500, 'points');

// Set a player's experience level
await api.experience('set', 'PlayerName', 20, 'levels');

These methods enable your plugin to interact with core gameplay mechanics, offering flexibility for custom scenarios and server management. For actual use, ensure that the permissions and conditions are appropriately checked before altering game settings.

Was this page helpful?