### Initialize and Start Serenity Server Source: https://github.com/serenityjs/serenity/blob/main/packages/core/README.md Demonstrates how to create a new Serenity instance with configuration, register providers like LevelDB, and start the server. It also shows how to listen for player initialization events. ```typescript import { Serenity, LevelDBProvider, WorldEvent } from "@serenityjs/core"; // Create a new Serenity instance const serenity = new Serenity({ path: "./properties.json", serenity: { permissions: "./permissions.json", resourcePacks: "./resource_packs", } }); // Welcome all players that join serenity.on(WorldEvent.PlayerInitialized, ({ player }) => { player.sendMessage("Welcome to my server!"); }); // Register the LevelDBProvider serenity.registerProvider(LevelDBProvider, { path: "./worlds" }); // Start the server serenity.start(); ``` -------------------------------- ### Start Serenity Server Source: https://github.com/serenityjs/serenity/blob/main/README.md Starts the Serenity development server. This command should be run after navigating to the devapp directory. ```shell yarn dev ``` -------------------------------- ### Initialize Project with Yarn Source: https://github.com/serenityjs/serenity/blob/main/README.md Initializes the project folder using Yarn. This command installs all necessary dependencies for the project. ```shell yarn install ``` -------------------------------- ### Install SerenityJS CLI Source: https://github.com/serenityjs/serenity/blob/main/README.md Installs SerenityJS using package managers like npm, yarn, or pnpm. This command scaffolds a new traditional NodeJS project with SerenityJS packages pre-installed, allowing for deeper integration into new or existing NodeJS projects. ```bash npm create serenity ``` ```bash yarn create serenity ``` ```bash pnpm create serenity ``` -------------------------------- ### Serenity Event Listener: 'on' (Example 1) Source: https://github.com/serenityjs/serenity/blob/main/packages/core/README.md Demonstrates the `serenity.on()` method for event listening, which is called on the same process tick but after `before` listeners. This listener cannot cancel the event loop. This example checks block identifiers during player block placement. ```typescript serenity.on(WorldEvent.PlayerPlaceBlock, ({ block, permutationBeingPlaced }) => { block.identifier; // Expected value: "minecraft:air" permutationBeingPlaced.identifier; // Expected value to be the block type being placed assert(block.identifier !== permutationBeingPlaced.identifier) // Expected to be true }); ``` -------------------------------- ### Serenity Event Listener: 'on' (Example 2) Source: https://github.com/serenityjs/serenity/blob/main/packages/core/README.md Provides another example of the `serenity.on()` method for event listening. This listener executes after `before` listeners and cannot cancel the event. This specific example asserts that the block identifier matches the permutation being placed. ```typescript serenity.on(WorldEvent.PlayerPlaceBlock, ({ block, permutationBeingPlaced }) => { block.identifier; // Expected value to be the block type being placed permutationBeingPlaced.identifier; // Expected value to be the block type being placed assert(block.identifier === permutationBeingPlaced.identifier) // Expected to be true }); ``` -------------------------------- ### Navigate to Dev App Directory Source: https://github.com/serenityjs/serenity/blob/main/README.md Changes the current directory to the 'devapp' folder, which is required before starting the server. ```shell cd devapp ``` -------------------------------- ### Override Components for Block Permutations in SerenityJS Source: https://github.com/serenityjs/serenity/blob/main/docs/custom-block/README.md This example illustrates how to override base block components for specific permutations. It creates two permutations, one with light emission disabled (level 0) and another with light emission enabled (level 15), based on a 'powered' state. ```typescript // Create a permutation with a different light emission level const permutation1 = exampleBlockType.createPermutation({ powered: false }); permutation1.components.setLightEmission(0); // Level 0 will not emit light // Create another permutation with a different light emission level const permutation2 = exampleBlockType.createPermutation({ powered: true }); permutation2.components.setLightEmission(15); // Level 15 will emit light ``` -------------------------------- ### Build TypeScript Project Source: https://github.com/serenityjs/serenity/blob/main/README.md Compiles the TypeScript project into JavaScript. This step is necessary before running the application. ```shell yarn build ``` -------------------------------- ### Configure Block Components in SerenityJS Source: https://github.com/serenityjs/serenity/blob/main/docs/custom-block/README.md This code configures the components of a custom block type, including setting light emission levels, defining geometry models, and creating default material instances with specific textures and render methods. ```typescript import { MaterialRenderMethod } from '@serenityjs/protocol'; // The base type will emit light with a level of 15 exampleBlockType.components.setLightEmission(15); // Get the base geometry component and set the model identifier const geometry = exampleBlockType.components.getGeometry(); geometry.setModelIdentifier("geometry.example_block"); // Get the base material component and create a default material instance const materials = exampleBlockType.components.getMaterialInstances(); materials.createMaterialInstance("*", { texture: "example_block", render_method: MaterialRenderMethod.AlphaTest }); ``` -------------------------------- ### Serenity Event Listener: 'before' Source: https://github.com/serenityjs/serenity/blob/main/packages/core/README.md Illustrates the usage of the `serenity.before()` method for event listening. Listeners registered with `before` are executed before any other listeners and can cancel the event loop by returning `false`. ```typescript serenity.before(WorldEvent.PlayerChat, ({ player, message }) => { // If false is retured in the function, // the loop will be canceled and the message wont be displayed. // Meaning 'on' & 'after' listeners will never receive the signal. if (message === "cancel") return false; // If true is retured in the function, // The event loop will continue as usual. return true; }); ``` -------------------------------- ### Define Custom Block Type with SerenityJS Source: https://github.com/serenityjs/serenity/blob/main/docs/custom-block/README.md This snippet demonstrates how to create a new custom block type using the `CustomBlockType` class from `@serenityjs/core`. It initializes the block with a unique ID and basic properties like `solid`. ```typescript import { CustomBlockType } from '@serenityjs/core'; // Define a custom block type // The first argument is the block ID, and the second argument is an object with additional options const exampleBlockType = new CustomBlockType("serenity:example_block", { solid: true }); ``` -------------------------------- ### Register Block Type and Trait with World Source: https://github.com/serenityjs/serenity/blob/main/docs/custom-block/README.md Registers the custom block type and its associated trait with the game's world instance. This makes the block and its behaviors available in the game environment. ```TypeScript world.blockPalette.registerType(exampleBlockType); // Optionally, you can register the block trait as well world.blockPalette.registerTrait(ExampleBlockTrait); ``` -------------------------------- ### Windows Network Loopback Exemption Source: https://github.com/serenityjs/serenity/blob/main/README.md A Windows-specific command to exempt the Minecraft client from UWP loopback restrictions. This is necessary if the server is running but cannot be seen or joined from the Minecraft server list. Execute this command in a new terminal while running as administrator. ```bash CheckNetIsolation.exe LoopbackExempt -a -p=S-1-15-2-1958404141-86561845-1752920682-3514627264-368642714-62675701-733520436 ``` -------------------------------- ### Create Block Permutations with States in SerenityJS Source: https://github.com/serenityjs/serenity/blob/main/docs/custom-block/README.md This snippet shows how to create block permutations, which represent variations of a block. It demonstrates defining permutations with different state values (boolean, number, string) and assigning them to the custom block type. ```typescript const permutation = exampleBlockType.createPermutation({ boolean_state: true, number_state: 42, string_state: "example", }); ``` -------------------------------- ### Make Block Interactable Source: https://github.com/serenityjs/serenity/blob/main/docs/custom-block/README.md Sets a block type to be interactable by calling `setIsInteractable` on its components. This is a prerequisite for applying interaction-based traits. ```TypeScript exampleBlockType.components.setIsInteractable(true); ``` -------------------------------- ### Define Custom Block Trait Source: https://github.com/serenityjs/serenity/blob/main/docs/custom-block/README.md Creates a reusable block trait by extending the `BlockTrait` class. It defines a unique identifier and implements custom behavior, such as toggling a 'powered' state on interaction. ```TypeScript import { BlockTrait } from '@serenityjs/core'; class ExampleBlockTrait extends BlockTrait { // Every trait must have a unique identifier public static readonly identifier = "serenity:example_block_trait"; public onInteract(): void { // Get the powered state of the block, which we defined in the permutation const state = this.block.getState("powered"); // Toggle the powered state of the block this.block.setState("powered", !state); } } ``` -------------------------------- ### Register Trait with Block Type Source: https://github.com/serenityjs/serenity/blob/main/docs/custom-block/README.md Applies a previously defined `BlockTrait` to a specific block type instance. This ensures the trait's behavior is available when the block is used. ```TypeScript exampleBlockType.registerTrait(ExampleBlockTrait); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.