Skip to content

Instantly share code, notes, and snippets.

@serverhiccups
Last active October 20, 2023 08:09
Show Gist options
  • Save serverhiccups/f6a42e4903278f951d97 to your computer and use it in GitHub Desktop.
Save serverhiccups/f6a42e4903278f951d97 to your computer and use it in GitHub Desktop.
A script to update a minecraft server status on a scratch project. Requires scratch-api, yargs and boing.
#!/usr/bin/env node
// Minecraft server > Scratch Status
// by hiccup01
// hiccup01.com
// Copyright (c) 2016 hiccup01 <hiccup@hiccup01.com>
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Load the scratch-api, argv and minecraft-ping
var Scratch = require('scratch-api');
var argv = require('yargs').argv;
var boing = require('boing');
// Minecraft credentials (For boing)
var username = "";
var password = "";
//Scratch credentials
var susername = "";
var spassword = "";
// Project ID
var projectid = "";
// Packet - Info to send
var packet = "The ping may have failed... ";
//The server to use
var server = "";
//Encoding function
function encode(text) {
// Encoding chart
var characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789.,%/\"'!@#$&()[]_-¶";
var chars = [];
for (var i = characters.length - 1; i >= 0; i--) {
chars.splice(0, 0, characters[i]); // Prepend the character
};
// Variable inting for encoding
var out = "";
var encodelength = (text.length);
var padder= "";
var length =padder.length;
for (var i = 0; i < encodelength; i++) {
padder = chars.indexOf(text.charAt(i)); // Find index for char
padder++ // Normanilize for scratch because scratch counts from one
padder = padder.toString(); // Convert padder to string to avoid errors
length = padder.length;
if (length == 1) { // If the index is a single digit number then,
padder = "0" + padder; // Add a zero on the front
};
out = out.concat(padder); // Add the index to the string to be sent
}
return out // Return the number for the scratch-api to use
}
// A function to make players readable into a list by scratch
function playersencode (inplayers) {
var niceplayers = ""; // Int the string for scratch to read
for (x in inplayers) {
niceplayers += inplayers[x]; //Add player "x" from "inplayers" to string plus a paragraph sign to let scratch know it's the end of the player name
niceplayers += "¶";
};
return niceplayers // Return the "Nicerfied" string
}
boing({ ip: server, username: username, password: password }, function(err, response) { // Ping the server
if (err) { // If there are errors log them
console.log(err);
} else {
var players = []; // Create array for players
for (x in response.players.sample) {
players.push(response.players.sample[x].name); // Add the players from the response to the array
};
console.log("Successfully pinged server \"" + server + "\"");
packet = "Quatcraft, running version " + "1.9" + " with " + (response.players.online) + "/" + (response.players.max) + " " + "players online."; // String for scratch to display
Scratch.UserSession.create(susername, spassword, function(err, user) { // Load the UserSession
if (err == "null") { // If there is an error log it, if there is not don't
console.log(err);
} else {
console.log("UserSession loaded with no errors.");
}
user.cloudSession(projectid, function(err, cloudSession) { // Load the cloudSession
if (err == "null") { // If there is an error log it, if there is not don't
console.log(err);
} else {
console.log("cloudSession loaded with no errors.");
}
// Check if the command specifies a different packet
if (argv.packet != null || undefined) {
packet = argv.packet;
};
var encodedpacket = encode(packet); // Encode packet into a sendable state
var semicodedplayers = playersencode(players); // Encode players into a readable state
encodedplayers = encode(semicodedplayers); // Encode players into a sendable state
cloudSession.set("☁ status", encodedpacket); //Set the vars
cloudSession.set("☁ statusplayers", encodedplayers);
console.log("Set cloud variable \"☁ statusplayers\" to " + players); // Say about it
console.log("Set cloud variable \"☁ status\" to " + packet);
setTimeout(function(){ cloudSession.end(); }, 700); // Wait 700 milliseconds then finish so forever can run it again
});
});
};
});
#!/usr/bin/env node
function encode(e){for(var r="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789.,%/\"'!@#$&()[]_-¶",s=[],o=r.length-1;o>=0;o--)s.splice(0,0,r[o]);for(var n="",a=e.length,l="",t=l.length,o=0;a>o;o++)l=s.indexOf(e.charAt(o)),l++,l=l.toString(),t=l.length,1==t&&(l="0"+l),n=n.concat(l);return n}function playersencode(e){var r="";for(x in e)r+=e[x],r+="¶";return r}var Scratch=require("scratch-api"),argv=require("yargs").argv,boing=require("boing"),username="",password="",susername="",spassword="",projectid="",packet="The ping may have failed... ",server="";boing({ip:server,username:username,password:password},function(e,r){if(e)console.log(e);else{var s=[];for(x in r.players.sample)s.push(r.players.sample[x].name);console.log('Successfully pinged server "'+server+'"'),packet="Quatcraft, running version 1.9 with "+r.players.online+"/"+r.players.max+" players online.",Scratch.UserSession.create(susername,spassword,function(e,r){"null"==e?console.log(e):console.log("UserSession loaded with no errors."),r.cloudSession(projectid,function(e,r){"null"==e?console.log(e):console.log("cloudSession loaded with no errors."),null!=argv.packet&&(packet=argv.packet);var o=encode(packet),n=playersencode(s);encodedplayers=encode(n),r.set("☁ status",o),r.set("☁ statusplayers",encodedplayers),console.log('Set cloud variable "☁ statusplayers" to '+s),console.log('Set cloud variable "☁ status" to '+packet),setTimeout(function(){r.end()},700)})})}});
{
"name": "mcscratchjs",
"version": "1.6.0",
"description": "Minecraft server > Scratch Status",
"main": "index.js",
"dependencies": {
"scratch-api": "^1.1.2",
"boing": "^1.0.7",
"ursa": "^0.9.1",
"yargs": "^3.32.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://gist.github.com/f6a42e4903278f951d97.git"
},
"keywords": [
"scratch",
"minecraft",
"bot"
],
"author": "hiccup01",
"license": "MIT",
"bugs": {
"url": "https://gist.github.com/f6a42e4903278f951d97"
},
"homepage": "https://gist.github.com/f6a42e4903278f951d97"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment