ReadonlyafterReadonlybeforeThis property can be read in early-execution mode.
ReadonlycurrentThis property can be read in early-execution mode.
import { system, world } from "@minecraft/server";
// A function that will be executed periodically
function looper() {
// Send a message to all players in the world with the current game tick count
world.sendMessage(`Current tick: ${system.currentTick}`);
}
// Define constants for time intervals in ticks
const TICKS_IN_SEC = 20; // 20 ticks per second (Minecraft runs at 20 ticks per second)
const TICKS_IN_20_SECS = TICKS_IN_SEC * 20; // 20 seconds in ticks (20 ticks/second * 20 seconds = 400 ticks)
// Set up the looper function to run every 20 seconds in the game
system.runInterval(looper, TICKS_IN_20_SECS);
/*
Usage:
This script sends a message to all players in the game every 20 seconds,
displaying the current tick count in the Minecraft world.
The message will appear as:
"Current tick: [currentTick]"
where [currentTick] is the current game tick at the time the message is sent.
/
ReadonlyisThis property can be read in early-execution mode.
ReadonlyserverThis property can be read in early-execution mode.
This function can be called in early-execution mode.
This function can't be called in read-only mode.
import { system, world } from "@minecraft/server";
/**
Variable to hold the job reference for later management
@type {number}
/
let job;
/**
A generator function to continuously monitor if any players are sneaking
@returns {Generator<void, void, void>}
/
function* generatorFunction() {
// Infinite loop to create a long-running task
while (true) {
// Retrieve the list of all players currently in the world
let worldPlayers = world.getAllPlayers();
// If no players are found, stop the job and exit the function
if (worldPlayers.length === 0) {
system.clearJob(job); // Clear the running job
return; // Exit the generator function
}
// Iterate over each player in the world
for (let worldPlayer of worldPlayers) {
// Check if the player is sneaking
if (worldPlayer.isSneaking) {
// Display an alert message on the player's action bar
worldPlayer.onScreenDisplay.setActionBar("[Alert] You are now sneaking");
}
// Pause the generator function until the next game tick
yield;
}
}
}
// Create an instance of the generator function
const generator = generatorFunction();
// Start the generator function as a periodic job in the game system
job = system.runJob(generator);
This function can be called in early-execution mode.
This function can't be called in read-only mode.
import { system } from "@minecraft/server";
const runId = system.run(() => {
// console.log removed for example clarity
});
// Clear the run, so it will not run again.
system.clearRun(runId);
// by WavePlayz
import { system, world } from "@minecraft/server";
/**
@type {number}
/
let interval;
// A function to check if any player in the world is sneaking
function looper() {
// Retrieve all players currently in the world
let worldPlayers = world.getAllPlayers();
// If no players are found, stop the job and exit the function
if (worldPlayers.length === 0) {
system.clearRun(interval); // Clear the running job
return; // Exit the generator function
}
// Iterate over each player in the world
for (let worldPlayer of worldPlayers) {
// If the player is sneaking, display an alert on their action bar
if (worldPlayer.isSneaking) {
worldPlayer.onScreenDisplay.setActionBar("[Alert] You are now sneaking");
}
}
}
// Run the generator function as a periodic job in the game system
interval = system.runInterval(looper);
This function can be called in early-execution mode.
This function can't be called in read-only mode.
OptionaltickInterval: numberThis function can be called in early-execution mode.
This function can't be called in read-only mode.
// by WavePlayz
import { system, world } from "@minecraft/server";
// A function to check if any player in the world is sneaking
function looper() {
// Retrieve all players currently in the world
let worldPlayers = world.getAllPlayers();
// Iterate over each player in the world
for (let worldPlayer of worldPlayers) {
// If the player is sneaking, display an alert on their action bar
if (worldPlayer.isSneaking) {
worldPlayer.onScreenDisplay.setActionBar("[Alert] You are now sneaking");
}
}
}
// Attach the looper function as interval in the game system
system.runInterval(looper);
This function can be called in early-execution mode.
This function can't be called in read-only mode.
// by WavePlayz
import { system, world } from "@minecraft/server";
/**
A generator function to continuously monitor if any players are sneaking
@returns {Generator<void, void, void>}
/
function* generatorFunction() {
// Long running task
while (true) {
// Retrieve all players currently in the world
let worldPlayers = world.getAllPlayers();
// Iterate over each player in the world
for (let worldPlayer of worldPlayers) {
// If the player is sneaking, display an alert on their action bar
if (worldPlayer.isSneaking) {
worldPlayer.onScreenDisplay.setActionBar("[Alert] You are now sneaking");
}
// Pause the generator function until the next game tick
yield;
}
}
}
// Create an instance of the generator function
const generator = generatorFunction();
// Run the generator function as a periodic job in the game system
system.runJob(generator);
OptionaltickDelay: numberThis function can be called in early-execution mode.
This function can't be called in read-only mode.
This function can't be called in read-only mode.
This function can't be called in early-execution mode.
This function can be called in early-execution mode.
This function can't be called in read-only mode.
import { Player, system, world } from "@minecraft/server";
// Subscribe to the entityHurt event, which triggers whenever an entity takes damage
world.afterEvents.entityHurt.subscribe(async (eventData) => {
// Extract the hurt entity and damage amount from the event data
const { hurtEntity, damage } = eventData;
// Wait for a number of game ticks equal to the damage value before proceeding
await system.waitTicks(damage);
// Attempt to get the health component of the hurt entity
const health = hurtEntity.getComponent("health");
// If the entity has a health component, reset its health to the maximum value
if (health != null) {
health.resetToMaxValue(); // Reset the entity's health to full
// Send a message to the entity (if it supports receiving messages)
if (hurtEntity instanceof Player) {
hurtEntity.sendMessage("Health reset");
}
}
});
Remarks
This property can be read in early-execution mode.
Example: greet.js