Giter VIP home page Giter VIP logo

gametest-api's Introduction

Gametest-API × BETA 1.18.20.21

guide to begain with the new scripting api, The GameTest Framework !

• DOWNLOAD SAMPLE PACK

Prerequisites

Setup

Accessing Minecraft directory

this is the place where we will spend most of our time and work.

Win10 users can find this at the given path %localappdata%\Packages\Microsoft.MinecraftUWP_8wekyb3d8bbwe\LocalState\games\com.mojang

Android users can find this by visiting the following path /Android/data/com.mojang.minecraftpe/files/games/com.mojang/

but if you are a Android 10+ user you won't be able to access the folder directly as per android restrictions, but dont worry we got a way! no its not root :P, all you need is an external File Manager app which will allow you to locate those internal folders, here are some apps that you can use

Tip: Create shortcuts to those directories for quicker access.

once done, you will see bunch of folder in the directory, mainly we are only intrested in two of them during the development process

  • development_behavior_packs
  • development_resource_packs

Getting Started

Creating folder

to start with, first we need to create the folder in the development_behavior_packs directory, folder structure looks like this

development_behavior_packs/
┗━• sample_pack/
    ┣━• scripts/
    ┇    ┗━• script.js
    ┣━• manifest.json
    ┗━• pack_icon.png

DOWNLOAD SAMPLE PACK

Then make the following changes in manifest.json in order to use the framework

  1. format version needs to be 2
"format_version": 2,
  1. modules section must contain one module object with the type javascript and an entry point to a javascript file, which will be the main file for our gametest
{
	"description": "Example gametest behavior pack",
	"type": "javascript",
	"uuid": "9e896681-01d8-4a21-b1e2-e350e3c9c1ae",
	"version": [0, 0, 1],
	"entry": "scripts/script.js"
}
  1. manifest must contain gametest dependencies to be able to import gametest modules within javascript files
"dependencies": [
	{
		// Minecraft native module - needed to use the "mojang-minecraft" module
		"uuid": "b26a4d4c-afdf-4690-88f8-931846312678",
		"version": [0, 1, 0]
	},
	{
		// GameTest native module - needed to use the "mojang-gametest" module
		"uuid": "6f4b6893-1bb6-42fd-b458-7fa3d0c89616",
		"version": [0, 1, 0]
	},
	{
		// Minecraft Ui native module - needed to use the "mojang-minecraft-ui" module
		"uuid": "2BD50A27-AB5F-4F40-A596-3641627C635E",
		"version": [0, 1, 0]
	}
]
manifest.json

the complete manifest.json will look something like this

{
	"format_version": 2,
	
	"header": {
		"name": "Example gametest behavior pack",
		"description": "Example gametest behavior pack",
		"uuid": "b3bc569d-3144-4473-82ae-c5704a6064e3",
		"version": [0, 0, 1],
		"min_engine_version": [1, 18, 20]
	},
	
	"modules": [
		{
			"description": "Example gametest behavior pack",
			"type": "javascript",
			"uuid": "9e896681-01d8-4a21-b1e2-e350e3c9c1ae",
			"version": [0, 0, 1],
			"entry": "scripts/script.js"
		}
	],
	
	"dependencies": [
		{
			// Minecraft native module - needed to use the "mojang-minecraft" module
			"uuid": "b26a4d4c-afdf-4690-88f8-931846312678",
			"version": [0, 1, 0]
		},
		{
			// GameTest native module - needed to use the "mojang-gametest" module
			"uuid": "6f4b6893-1bb6-42fd-b458-7fa3d0c89616",
			"version": [0, 1, 0]
		},
		{
			// Minecraft Ui native module - needed to use the "mojang-minecraft-ui" module
			"uuid": "2BD50A27-AB5F-4F40-A596-3641627C635E",
			"version": [0, 1, 0]
		}
	]
}

Tip: Add additional properties such as metadata for additional info

"metadata": {
	"authors": ["<your-name>"],
	"url": "<your-github-repo-url>",
	"license": "TBD"
}
Native modules

As of 1.18.10.20 we have two native modules to work with

  • mojang-gametest
  • mojang-minecraft
  • mojang-minecraft-ui (in addition to beta 1.18.20.21)

the mojang-gametest module is not something to very excite about as its intended for game devlopers and is not very useful for us the creators plus it only works within the range of stucture blocks
on the other side mojang-minecraft module is what we can consider as the "new scripting api" that the creators can utilze to create stuff and where all the intresting things exists

Importing pack

once your done with the above steps, you can now load the pack into the world, lets create a new one, in the game option scroll down till you see an option with title Enable Gametest Framework enable it,

gametest-option

then head over Behavior Packs button in the lower-left corner of the "Add-Ons" sub-menu.

addon-button

Click Behavior Packs > My Packs your add-on now should show up in the list of behavior packs!

Clicking on your add-on should bring up a button titled "Activate". Click it.

pack-section

your behavior pack now should have been moved to the "Active" section!

now you can join the world to see everything works, but wait if you join you wont see anything diffrent its obvious since we have nothing added in the script file yet

script.js
creating our first script

the very first thing we need to do in the script is to import the native minecraft modules like that

// importing native minecraft module
import * as Minecraft from "mojang-minecraft"

next we need to listen for some event, in this tutorial we will use the tick event which runs every tick, here is how you do it

Minecraft.world.events.tick.subscribe(eventData => {
	const { currentTick } = eventData
	
	// your code here

})
chat spam, our first script

lets create chat spam as our first script to see if everything is all right

// importing native minecraft module
import * as Minecraft from "mojang-minecraft";

// variable to track world is empty or filled
let worldHasPlayer = false 

// player join event triggered when a player joins
Minecraft.world.events.playerJoin.subscribe(player => {
	
	// as player has joined set worldHasPlayer to yes
	worldHasPlayer = true
})

const TICKS_IN_FIVE_SECONDS = 20 * 5

// world tick event that fires our function every tick - 20 time in a sec
Minecraft.world.events.tick.subscribe(eventData => {

	// current world tick count value returned by the event
	const { currentTick } = eventData
	
	// if world not has player or current tick is not divisable by TICKS_IN_FIVE_SECONDS the break the code
	if (!worldHasPlayer || currentTick % TICKS_IN_FIVE_SECONDS !== 0) return;
	
	// get all players 
	let players = Minecraft.world.getPlayers()
	
	// loop through players 
	for (let player of players) {
		// run command as player 
		player.runCommand("say Hello " + player.nameTag)
	}
})

save the code in your script.js / main file then open the world, you should now see a spam in the chat.. if not then something is wrong :( recheck what went wrong.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.