A Javascript library for translating Minecraft Color Codes!
Recently, I was making my own Minecraft Server website, and I wanted to display the MOTD and the amount of players online. Then, came the problem. When loading the MOTD, I stumbled across this weird character. § After searching, I realised this was a piece of Minecraft Color Codes...
I decided to translate this into HTML using JavaScript, and I wanted to share this with all of you around the world with the same problem.
Download the latest version of Minecraft Color Codes and unzip it. Then, copy MinecraftColorCodes.VERSION.js into your initial directory of your website. You can choose to use the minified compressed version (Faster, for production) or the uncompressed version (Slightly slower, for personal use). Open your HTML file for your website, and copy this into the
section.<head>
<script src="MinecraftColorCodes.VERSION.js"></script>
</head>
You are now ready to launch!
I'll just give an example.
<script>
var myMOTD = "§d§lMy Example MOTD: §eDoes it look nice?"
var newMOTD = myMOTD.replaceColorCodes(); //The brackets here are essential.
console.log(newMOTD); //Just displaying it to the console.
</script>
Now, if you don't understand a piece of that code, you probably don't know JavaScript. There are amazing website on the internet that provide you a full tutorial and a lesson on how to use JavaScript. People who do know Javascript, can probably understand all the lines.
Easy example, huh? Best part, there is no complex example. This is all there is to it. Easy to use, and easy to understand.
If you find one, please submit an issue ticket or a pull request.
You might want to get your server's MOTD, but you don't know how to access the server from your website? Use this code! (jQuery is required!)
$(document).ready(function(){
$.getJSON('https://mcapi.us/server/status?ip=c.nerd.nu', function(obj){
if(obj.online === true){
motdHTML = obj.motd.replaceColorCodes();
console.log(motdHTML);
} else {
console.log("Server is offline...");
}
});
})
https://mcapi.us provides JSON responses from the server provided in the URL. By using this, you can parse the JSON, and get your desired information! Visit http://mcapi.us for more information on the JSON format.
Basically, it gets the JSON from the website once your website has finished loading, then converts the Color Codes if the server is online, and if the server is offline... Well yeah.
v1.0 - Created this file, added replacements for color codes
v1.1 - Added formatting replacements (e.g. §l)
v1.2 - Added support for multiple occurence of one color code in a string
v1.3 - Added support for New Line, and Spaces because spaces were buggy. The \n
character translates into <br>
, and the spaces translate into Unicode spaces ( 
).
v2.0 - A complete recode. Now fixed Bug #2, 'Having multiple formatting codes does not work'. New line and spaces was removed.
v3.0 - Added §k, §r, and \n support. Now also requires the output element ID.
v3.1 - Finally! Support for class names too!
v3.5 - Fixed Bug #3, 'Spaces dissappear', by adding a CSS property to the output element.'
v3.7 - Fixed Bug #4, put everything in a huge String.prototype
function, so now you can't input your output element ID/Class. I mean come on, what if you just wanted to output to the console? I also changed a few tweaks here and there.
You can modify this file in any way, but if you want, create a pull request so I can have a look. Also, try not sell this file/work for a price. I mean come on, if you really want money, go get a proper job. Thirdly and lastly, you can not give away this file/work without giving credit to me, and possibly giving the URL to this Github page. Due to the informality of this piece of text, you could ignore this if you want to.