Skip to content

Control the environment

Build a panel for supported Minecraft environment settings: time, Moon phase, gamma, and distance.

Let’s build a small window for configuring the Minecraft environment: time of day, Moon phase, gamma, and distance. All changes use editorAPI.sky.

Download the complete environment.js example. After installation, click the sun icon in the toolbar. Opening the window does not change the scene; settings are applied when you click a button.

Panel settings

Field Method Values in the example
Time of day setDayTime(ticks) 0-23999 ticks within the day
Moon phase setMoonPhase(index) An index from getMoonPhases()
Gamma setGamma(value) 0-1
Distance setRenderDistance(chunks) 2-250 chunks

Time changes only in response to the user. No timer advances the day automatically. tick(delta) is available for advancing time in steps, but starting and stopping that loop would be the plugin’s responsibility.

These are settings for the editor’s Minecraft environment. The panel does not configure a Minecraft server, write game commands, or control gamma in RT image post-processing.

Confirm the methods

const sky = window.editorAPI.sky;
const state = sky.getState();
if (!state) throw new Error('The environment is unavailable.');
console.log(state.dayTime, state.moonPhaseName, state.gamma);
console.log(sky.getMoonPhases());

getState() returns the current state, including active, owners, totalTicks, dayTime, moonPhase, moonPhaseName, gamma, and renderDistanceChunks. If the subsystem is unavailable, it returns null; the phase list is empty in that case.

setTime(ticks) sets total time, including days. setDayTime(ticks) preserves the current day and changes the time within it, normalizing the value to a 24000-tick cycle. To restore a state snapshot, use totalTicks rather than just dayTime: the day number determines the Moon phase.

setMoonPhase accepts an index or an exact name from getMoonPhases(). Internally, it changes the day within the current lunar cycle while preserving the time of day. setGamma clamps its value to 0-1; distance is clamped to 2-250. In the current environment, the fog boundary is calculated as min(chunks * 16, 512). This is not the distance at which game chunks are loaded.

State fields and other operations: environment API reference.

The panel and its values

The example creates a standard window with createWindow, fills its fields from getState(), and checks them using built-in HTML form validation. It has three actions:

  • “Apply and enable” sends the settings and enables the environment on behalf of the API.
  • “Release API activation” removes only the activation owned by editor-api.
  • “Restore values from opening” restores the snapshot captured when this window opened.
const initial = window.editorAPI.sky.getState();
const panel = window.editorAPI.createWindow(
'Sample environment', 'my-plugin.environment', 'icon-sun',
520, 470, 320, 380
);
// Create input/select elements and append them to panel.container.

For window structure and avoiding duplicate instances, see add a button and a custom window.

Apply settings and manage state

{render: false} lets you set several values without rendering after each call. The final enable() updates the image:

const sky = window.editorAPI.sky;
sky.setDayTime(18000, {render: false});
sky.setMoonPhase(0, {render: false});
sky.setGamma(0.5, {render: false});
sky.setRenderDistance(16, {render: false});
sky.enable();

If the environment is already active and you only need to update time and lighting, use refresh(). Values can also be changed while the environment is disabled; changing them does not enable it.

The environment can have multiple owners. For example, Immersive mode, Animator, or Live Link may need it. sky.disable() removes the editor-api owner, but the environment may remain active because of another owner. All plugins calling the public sky.enable() also use the same editor-api name rather than a separate name per plugin. Do not promise independent activation for each extension.

The complete example restores time, gamma, distance, and the previous participation of editor-api. This restores a snapshot; it is not an Undo operation. If the user or another plugin changed the environment in the meantime, restoring the snapshot also replaces those newer values. For that reason, restoration is an explicit button, and closing the window leaves the environment unchanged.

Check the boundaries

  1. Open the window and check that its fields reflect the current state.
  2. Apply noon and night, then change the Moon phase. The time within the day should stay the same when the phase changes.
  3. Test gamma at 0 and 1, and distance at 2 and 32 chunks. Increasing distance further does not necessarily move the fog beyond 512 blocks.
  4. Restore the snapshot and compare the fields with their original values. For a controlled test, pause other mechanisms that update time.
  5. Release API activation and check active and owners. A remaining owner explains why the environment is still visible.

These API calls do not create Undo entries or serialize settings into the project. If a plugin needs to restore its values after a restart, design separate storage and an explicit point at which to apply them. Do not automatically associate these settings with exports, RT, or the state of a connected game world.

Methods and ranges were checked against publicFunctions.js and MinecraftSky.js. The example does not access the internal editor or start timers that keep changing the scene after its window closes.