Skip to content

Instantly share code, notes, and snippets.

@M4GNV5
Last active March 22, 2017 12:37
Show Gist options
  • Save M4GNV5/faebfaf4e975edfaba69 to your computer and use it in GitHub Desktop.
Save M4GNV5/faebfaf4e975edfaba69 to your computer and use it in GitHub Desktop.
Brainfuck interpreter using CommandblocksJS
//why js? cuz i can and vs needs so long to load
//and yolo ;)
//
//setup
//
command("gamerule commandBlockOutput false");
command("gamerule sendCommandFeedback false");
command("fill 1 31 0 1 10 30 stone");
command("/setblock 0 4 -2 minecraft:chest 0 replace {Items:[{id:minecraft:banner,Count:1,Slot:0,tag:{BlockEntityTag:{Base:15,Patterns:[{Pattern:sc,Color:0},{Pattern:bo,Color:15},{Pattern:ts,Color:15},{Pattern:bs,Color:15}]}}},{id:minecraft:banner,Count:1,Slot:1,tag:{BlockEntityTag:{Base:15,Patterns:[{Pattern:ms,Color:0},{Pattern:bo,Color:15}]}}},{id:minecraft:banner,Count:1,Slot:2,tag:{BlockEntityTag:{Base:15,Patterns:[{Pattern:dls,Color:0},{Pattern:drs,Color:0},{Pattern:rud,Color:15},{Pattern:rd,Color:15},{Pattern:bo,Color:15}]}}},{id:minecraft:banner,Count:1,Slot:3,tag:{BlockEntityTag:{Base:15,Patterns:[{Pattern:dls,Color:0},{Pattern:drs,Color:0},{Pattern:bo,Color:15},{Pattern:lud,Color:15},{Pattern:ld,Color:15}]}}},{id:minecraft:banner,Count:1,Slot:4,tag:{BlockEntityTag:{Base:15,Patterns:[{Pattern:ls,Color:0},{Pattern:ts,Color:0},{Pattern:bs,Color:0},{Pattern:bo,Color:15}]}}},{id:minecraft:banner,Count:1,Slot:5,tag:{BlockEntityTag:{Base:15,Patterns:[{Pattern:rs,Color:0},{Pattern:ts,Color:0},{Pattern:bs,Color:0},{Pattern:bo,Color:15}]}}},{id:minecraft:banner,Count:1,Slot:6,tag:{BlockEntityTag:{Base:15,Patterns:[{Pattern:cs,Color:0},{Pattern:bo,Color:15},{Pattern:hh,Color:15},{Pattern:dls,Color:15},{Pattern:drs,Color:15},{Pattern:mr,Color:15},{Pattern:ss,Color:15}]}}},{id:minecraft:banner,Count:1,Slot:7,tag:{BlockEntityTag:{Base:15,Patterns:[{Pattern:cs,Color:0},{Pattern:bo,Color:15},{Pattern:hh,Color:15},{Pattern:dls,Color:15},{Pattern:drs,Color:15},{Pattern:mr,Color:15},{Pattern:tts,Color:15}]}}},]}");
command("kill @e[type=ArmorStand]");
var chars = "abcdefghijklmnopqrstuvwxyz?!., ";
var inputLines = 1;
var inputPos = new Util.Vector3(0, 30, 0);
var cursor = Entities.Selector.parse("@e[name=bfcursor]");
command("summon ArmorStand {0} {CustomName:\"bfcursor\",NoGravity:true}".format(inputPos));
var varPos = new Util.Vector3(0, 8, 0);
var varCursor = Entities.Selector.parse("@e[name=bfVarCursor]");
command("summon ArmorStand {0} {CustomName:\"bfVarCursor\",NoGravity:true}".format(varPos));
var outPos = new Util.Vector3(0, 6, 0);
var outputVisibleCount = 40;
var awaitingOutput = new Runtime.Boolean();
command("scoreboard objectives add isBfVar dummy");
command("scoreboard objectives add var dummy");
command("scoreboard objectives add input trigger");
command("scoreboard objectives add debug dummy");
//
//parse input
//
var parseInput = function()
{
var goOn = function()
{
command("tp {0} ~ ~ ~1".format(cursor));
call(parseInput, true);
};
new MinecraftCommand("execute @e[name=bfcursor] ~ ~ ~ testforblock ~ ~1 ~ minecraft:wall_banner 4 {Base:15,Patterns:[{Pattern:sc,Color:0},{Pattern:bo,Color:15},{Pattern:ts,Color:15},{Pattern:bs,Color:15}]}").validate(function()
{ // +
command("execute {0} ~ ~ ~ scoreboard players add @e[r=0,c=1,score_isBfVar_min=1] var 1".format(varCursor));
updateCurrentDebugVar();
updateOutChat();
goOn();
}, undefined, true);
new MinecraftCommand("execute @e[name=bfcursor] ~ ~ ~ testforblock ~ ~1 ~ minecraft:wall_banner 4 {Base:15,Patterns:[{Pattern:ms,Color:0},{Pattern:bo,Color:15}]}").validate(function()
{ // -
command("execute {0} ~ ~ ~ scoreboard players remove @e[r=0,c=1,score_isBfVar_min=1] var 1".format(varCursor));
updateCurrentDebugVar();
updateOutChat();
goOn();
}, undefined, true);
new MinecraftCommand("execute @e[name=bfcursor] ~ ~ ~ testforblock ~ ~1 ~ minecraft:wall_banner 4 {Base:15,Patterns:[{Pattern:dls,Color:0},{Pattern:drs,Color:0},{Pattern:rud,Color:15},{Pattern:rd,Color:15},{Pattern:bo,Color:15}]}").validate(function()
{ // >
command("tp {0} ~ ~ ~1".format(varCursor));
updateDebugVars();
updateOutChat();
goOn();
}, undefined, true);
new MinecraftCommand("execute @e[name=bfcursor] ~ ~ ~ testforblock ~ ~1 ~ minecraft:wall_banner 4 {Base:15,Patterns:[{Pattern:dls,Color:0},{Pattern:drs,Color:0},{Pattern:bo,Color:15},{Pattern:lud,Color:15},{Pattern:ld,Color:15}]}").validate(function()
{ // <
command("tp {0} ~ ~ ~-1".format(varCursor));
updateDebugVars();
updateOutChat();
goOn();
}, undefined, true);
new MinecraftCommand("execute @e[name=bfcursor] ~ ~ ~ testforblock ~ ~1 ~ minecraft:wall_banner 4 {Base:15,Patterns:[{Pattern:cs,Color:0},{Pattern:bo,Color:15},{Pattern:hh,Color:15},{Pattern:dls,Color:15},{Pattern:drs,Color:15},{Pattern:mr,Color:15},{Pattern:tts,Color:15}]}").validate(function()
{ // .
command('summon ArmorStand {0} {1} {2} {Customname:"bfvar",NoGravity:true}'.format(outPos.x, outPos.y, outPos.z + outputVisibleCount));
command('scoreboard players set @e[x={0},y={1},z={2},r=0] isBfVar 1'.format(outPos.x, outPos.y, outPos.z + outputVisibleCount));
command('entitydata @e[x={0},y={1},z={2},r=0] {CustomName:"□"}'.format(outPos.x, outPos.y, outPos.z + outputVisibleCount));
command("execute {3} ~ ~ ~ scoreboard players operation @e[x={0},y={1},z={2},r=0] var = @e[r=0,c=1,score_isBfVar_min=1] var"
.format(outPos.x, outPos.y, outPos.z + outputVisibleCount, varCursor));
command('tp @e[x={0},y={1},z={2},dx=0,dy=0,dz={3},score_isBfVar_min=1] ~ ~ ~-1'.format(outPos.x, outPos.y, outPos.z, outputVisibleCount + 1));
command('kill @e[x={0},y={1},z={2},r=0]'.format(outPos.x, outPos.y, outPos.z - 1));
awaitingOutput.set(true);
}, undefined, true);
new MinecraftCommand("execute @e[name=bfcursor] ~ ~ ~ testforblock ~ ~1 ~ minecraft:wall_banner 4 {Base:15,Patterns:[{Color:0,Pattern:cs},{Color:15,Pattern:bo},{Color:15,Pattern:hh},{Color:15,Pattern:dls},{Color:15,Pattern:drs},{Color:15,Pattern:mr},{Color:15,Pattern:ss}]}").validate(function()
{ // ,
command("scoreboard players enable @a input");
updateInChat();
call(waitForInput, true);
}, undefined, true);
new MinecraftCommand("execute @e[name=bfcursor] ~ ~ ~ testforblock ~ ~1 ~ minecraft:wall_banner 4 {Base:15,Patterns:[{Pattern:ls,Color:0},{Pattern:ts,Color:0},{Pattern:bs,Color:0},{Pattern:bo,Color:15}]}").validate(function()
{ // [
command("execute {0} ~ ~ ~ summon ArmorStand ~ ~ ~ {CustomName:\"bfloop\",NoGravity:true}".format(cursor));
goOn();
}, undefined, true);
new MinecraftCommand("execute @e[name=bfcursor] ~ ~ ~ testforblock ~ ~1 ~ minecraft:wall_banner 4 {Base:15,Patterns:[{Pattern:rs,Color:0},{Pattern:ts,Color:0},{Pattern:bs,Color:0},{Pattern:bo,Color:15}]}").validate(function()
{ // ]
new MinecraftCommand("execute {0} ~ ~ ~ scoreboard players test @e[r=0,c=1,score_isBfVar_min=1] var 0 0".format(varCursor)).validate(function()
{
command("kill @e[name=bfloop,c=-1]");
goOn();
}, function()
{
command("tp {0} @e[name=bfloop,c=-1]".format(cursor));
goOn();
}, true);
}, undefined, true);
new MinecraftCommand("execute @e[name=bfcursor] ~ ~ ~ testforblock ~1 ~1 ~ minecraft:stone").validate(undefined, function()
{ // line ending
command("tp {0} {1} ~-2 {2}".format(cursor, inputPos.x, inputPos.z));
call(parseInput, true);
}, undefined, true);
}
// setTimeout(parseInput, 100); // see end of file
//
//chat update
//
var outputExtras = [];
for(var i = 0; i < outputVisibleCount; i++)
{
var sel = Entities.Selector.parse("@e[x={0},y={1},z={2},r=0]".format(outPos.x, outPos.y, outPos.z + i));
outputExtras.push(new Chat.TellrawSelectorExtra(sel));
}
var debugExtras = [];
for(var i = -3; i <= 3; i++)
{
var label;
if(i < 0)
label = i;
else if(i == 0)
label = ">>";
else if(i > 0)
label = "+" + i;
debugExtras.push(new Chat.Message("{0} : ".format(label), Chat.Color.yellow));
var scoreExtra = new Chat.Message();
scoreExtra.score = {objective: "debug", name: "debug{0}".format(i)};
delete scoreExtra.text;
debugExtras.push(scoreExtra);
debugExtras.push("\n");
}
var adExtra = new Chat.Message("Made with CommandblocksJS", Chat.Color.aqua);
adExtra.underlined = true;
adExtra.clickEvent = Chat.ClickEvent.openUrl("https://github.com/M4GNV5/CommandBlocksJS");
function updateOutChat()
{
var tellrawExtras = [];
tellrawExtras.push("Brainfuck interpreter - Better than @WebFreak001's one\n");
tellrawExtras.push(adExtra);
tellrawExtras.push("\n\n\n\n\n\n");
tellrawExtras = tellrawExtras.concat(debugExtras);
tellrawExtras.push("\n\n");
tellrawExtras.push(new Chat.Message("Output: ", Chat.Color.red));
tellrawExtras = tellrawExtras.concat(outputExtras);
tellrawExtras.push("\n\n\n");
tellrawExtras.push(new Chat.Message("Input: ", Chat.Color.gray));
for(var i = 0; i < chars.length; i++)
{
var code = chars.charCodeAt(i);
var msg = new Chat.Message(chars[i], Chat.Color.dark_gray); //grey out input buttons
tellrawExtras.push(msg);
tellrawExtras.push(" ");
}
Chat.Tellraw.create(tellrawExtras).tell(Entities.Selector.AllPlayer);
}
function updateInChat()
{
var tellrawExtras = [];
tellrawExtras.push("Brainfuck interpreter - Better than @WebFreak001's one\n");
tellrawExtras.push(adExtra);
tellrawExtras.push("\n\n\n\n\n\n");
tellrawExtras = tellrawExtras.concat(debugExtras);
tellrawExtras.push("\n\n");
tellrawExtras.push(new Chat.Message("Output: ", Chat.Color.gray));
tellrawExtras = tellrawExtras.concat(outputExtras);
tellrawExtras.push("\n\n\n");
tellrawExtras.push(new Chat.Message("Input: ", Chat.Color.yellow));
for(var i = 0; i < chars.length; i++)
{
var code = chars.charCodeAt(i);
var msg = new Chat.Message(chars[i]);
msg.clickEvent = Chat.ClickEvent.runCommand("/trigger input set {0}".format(code));
tellrawExtras.push(msg);
tellrawExtras.push(" ");
}
Chat.Tellraw.create(tellrawExtras).tell(Entities.Selector.AllPlayer);
}
function updateCurrentDebugVar()
{
command("execute {0} ~ ~ ~ scoreboard players operation debug0 debug = @e[r=0,c=1,score_isBfVar_min=1] var".format(varCursor));
}
function updateDebugVars()
{
command("scoreboard players reset @e[type=ArmorStand] debug");
for(var i = -3; i <= 3; i++)
{
command("execute {0} ~ ~ ~{1} scoreboard players operation debug{1} debug = @e[r=0,c=1,score_isBfVar_min=1] var".format(varCursor, i));
}
}
//
//wait for input
//
function waitForInput()
{
new MinecraftCommand("testfor @p[score_input_min=1]").validate(function()
{
command("execute {0} ~ ~ ~ scoreboard players operation @e[r=0,c=1,score_isBfVar_min=1] var = @p[score_input_min=1] input".format(varCursor));
command("scoreboard players set @a input 0");
updateCurrentDebugVar();
updateOutChat();
command("tp {0} ~ ~ ~1".format(cursor));
call(parseInput, true);
}, waitForInput, true);
}
//
//convert ints to chars
//
var convertToChar1 = function()
{
for(var i = 32; i < 79; i++)
{
command('entitydata @e[score_isBfVar_min=1,score_var_min={0},score_var={0}] {CustomName:"{1}"}'.format(i, String.fromCharCode(i)));
}
call(convertToChar1, true);
}
call(convertToChar1, true);
var convertToChar2 = function()
{
for(var i = 79; i <= 126; i++)
{
command('entitydata @e[score_isBfVar_min=1,score_var_min={0},score_var={0}] {CustomName:"{1}"}'.format(i, String.fromCharCode(i)));
}
awaitingOutput.isTrue().validate(function()
{
awaitingOutput.set(false);
updateOutChat();
command("tp {0} ~ ~ ~1".format(cursor));
call(parseInput, true);
}, undefined, true);
call(convertToChar2, true);
}
call(convertToChar2, true);
//
//add missing variable armorstands
//
var setupVars = function()
{
new MinecraftCommand("execute {0} ~ ~ ~ testfor @e[r=0,c=1,score_isBfVar_min=1]".format(varCursor)).validate(setupVars, function()
{
command("execute {0} ~ ~ ~ summon ArmorStand ~ ~ ~ {CustomName:\"bfvar\",NoGravity:true}".format(varCursor));
command("execute {0} ~ ~ ~ scoreboard players set @e[r=0,c=1,name=bfvar] isBfVar 1".format(varCursor));
call(setupVars, true);
}, true);
}
call(setupVars, true);
updateOutChat();
setTimeout(parseInput, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment