From c5cc2710c09cf58d516702ff4b963d94db0d49ee Mon Sep 17 00:00:00 2001 From: ZXMushroom63 Date: Wed, 4 Dec 2024 10:07:21 +0800 Subject: [PATCH] slippery mod --- docs/tutorials/index.md | 2 +- docs/tutorials/slippery.md | 108 +++++++++++++++++++++++++++++++++++++ examplemods/slippery.js | 2 - 3 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 docs/tutorials/slippery.md diff --git a/docs/tutorials/index.md b/docs/tutorials/index.md index b505779..c0a36df 100644 --- a/docs/tutorials/index.md +++ b/docs/tutorials/index.md @@ -23,7 +23,7 @@ Tutorials: - [Disable All Particles](disable_all_particles.md) - [/hat mod](comingsoon) - [/spawnxp command](comingsoon) -- [Slippery Mod](comingsoon) +- [Slippery Mod](slippery.md) ### Advanced Prerequisites: diff --git a/docs/tutorials/slippery.md b/docs/tutorials/slippery.md new file mode 100644 index 0000000..7a13370 --- /dev/null +++ b/docs/tutorials/slippery.md @@ -0,0 +1,108 @@ +## Slippery Mod with ModAPI +In this tutorial you will learn how to modify properties of blocks, and run the code that does this on both the dedicated server and the client. + +We'll begin with the basic boilerplate mod code: +```javascript +(function SlipperyMod() { + ModAPI.meta.title("Slippery Mod"); + ModAPI.meta.description("Makes everything turn into ice."); + ModAPI.meta.credits("By "); + + //New code will go here! +})(); +``` + +Let's write the client side part of the code first. +- We'll get the keys for the ModAPI.blocks object (ids of each block) using [`Object.keys()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) +- Then, we'll loop over those keys, and modify their respective block to be as slippery as ice. +```javascript +var blockKeys = Object.keys(ModAPI.blocks); + +blockKeys.forEach(key => { //for each key (block id) + //make sure the block has a slipperiness property + //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining + if(ModAPI.blocks[key]?.slipperiness) { + ModAPI.blocks[key].slipperiness = 0.98; //Set the slipperiness value of the block at that key to 0.98 + } +}); +``` + +Your code should now look like this: +```javascript +(function SlipperyMod() { + ModAPI.meta.title("Slippery Mod"); + ModAPI.meta.description("Makes everything turn into ice."); + ModAPI.meta.credits("By "); + + var blockKeys = Object.keys(ModAPI.blocks); + blockKeys.forEach(key => { + if(ModAPI.blocks[key]?.slipperiness) { + ModAPI.blocks[key].slipperiness = 0.98; + } + }); +})(); +``` + +Your code should now look like this: +```javascript +(function SlipperyMod() { + ModAPI.meta.title("Slippery Mod"); + ModAPI.meta.description("Makes everything turn into ice."); + ModAPI.meta.credits("By "); + + var blockKeys = Object.keys(ModAPI.blocks); + blockKeys.forEach(key => { + if(ModAPI.blocks[key]?.slipperiness) { + ModAPI.blocks[key].slipperiness = 0.98; + } + }); + + //dedicated server code will be added here +})(); +``` + +Currently this only runs on the client meaning in singleplayer, when you throw an item or punch a sheep, it still won't slide. Only you will. + +We'll have to duplicate the code to run on the server using [`ModAPI.dedicatedServer`](../apidoc/dedicatedserver.md). +We also have to run the code after the `serverstart` event, as on the server, `ModAPI.blocks` is only created when it is needed. +```javascript +ModAPI.dedicatedServer.appendCode(function () { + //Code in here cannot reference outside variables. + ModAPI.addEventListener("serverstart", function () { + var blockKeys = Object.keys(ModAPI.blocks); + blockKeys.forEach(key => { + if(ModAPI.blocks[key]?.slipperiness) { + ModAPI.blocks[key].slipperiness = 0.98; + } + }); + }); +}); +``` + +let's add this to the final mod, and we'll be finished! +```javascript +(function SlipperyMod() { + ModAPI.meta.title("Slippery Mod"); + ModAPI.meta.description("Makes everything turn into ice."); + ModAPI.meta.credits("By "); + + var blockKeys = Object.keys(ModAPI.blocks); + blockKeys.forEach(key => { + if(ModAPI.blocks[key]?.slipperiness) { + ModAPI.blocks[key].slipperiness = 0.98; + } + }); + + ModAPI.dedicatedServer.appendCode(function () { + //Code in here cannot reference outside variables. + ModAPI.addEventListener("serverstart", function () { + var blockKeys = Object.keys(ModAPI.blocks); + blockKeys.forEach(key => { + if(ModAPI.blocks[key]?.slipperiness) { + ModAPI.blocks[key].slipperiness = 0.98; + } + }); + }); + }); +})(); +``` \ No newline at end of file diff --git a/examplemods/slippery.js b/examplemods/slippery.js index ac1a902..7cb81a0 100644 --- a/examplemods/slippery.js +++ b/examplemods/slippery.js @@ -9,7 +9,6 @@ ModAPI.dedicatedServer.appendCode(()=>{ //Add version of the code to the dedicat blockKeys.forEach(key=>{ //Loop through all the identifiers if(ModAPI?.blocks?.[key]?.slipperiness) {// TeaVM likes to add metadata properties which are `null` or `undefined` ModAPI.blocks[key].slipperiness = 0.98; //Ice slipperiness value. - ModAPI.blocks[key].reload();// load the value } }); }); @@ -18,6 +17,5 @@ var blockKeys = Object.keys(ModAPI.blocks); //Get keys (identifiers) of all the blockKeys.forEach(key=>{ //Loop through all the identifiers if(ModAPI?.blocks?.[key]?.slipperiness) {// TeaVM likes to add metadata properties which are `null` or `undefined` ModAPI.blocks[key].slipperiness = 0.98; //Ice slipperiness value. - ModAPI.blocks[key].reload();// load the value } });