Skip to content

Commit

Permalink
Add BlockPos, Vec3, and more world operators
Browse files Browse the repository at this point in the history
  • Loading branch information
ZXMushroom63 committed Dec 17, 2024
1 parent b2663dd commit 7b723f3
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 3 deletions.
65 changes: 65 additions & 0 deletions blocks/BlockPos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const blockpos_getxyz = {
init: function () {
this.appendDummyInput('TAB')
.setAlign(Blockly.inputs.Align.RIGHT)
.appendField(new Blockly.FieldDropdown([
['x', '$x'],
['y', '$y'],
['z', '$z']
]), 'TAB');
this.appendValueInput('VALUE')
.setAlign(Blockly.inputs.Align.RIGHT)
.appendField('component of BlockPos');
this.setInputsInline(true)
this.setOutput(true, "Number");
this.setTooltip('Get a component of a block pos');
this.setHelpUrl('');
this.setColour(270);
}
};
Blockly.common.defineBlocks({ blockpos_getxyz: blockpos_getxyz });



javascript.javascriptGenerator.forBlock['blockpos_getxyz'] = function () {
const dropdown_tab = block.getFieldValue('TAB');
const value_value = javascript.javascriptGenerator.valueToCode(this, 'VALUE', javascript.Order.ATOMIC);

const code = `(${value_value})["${dropdown_tab}"]`;
return [code, javascript.Order.NONE];
}



const blockpos_fromxyz = {
init: function () {
this.appendValueInput('X')
.setAlign(Blockly.inputs.Align.RIGHT)
.setCheck('Number')
.appendField('Make BlockPos from x:');
this.appendValueInput('Y')
.setAlign(Blockly.inputs.Align.RIGHT)
.setCheck('Number')
.appendField('y:');
this.appendValueInput('Z')
.setAlign(Blockly.inputs.Align.RIGHT)
.setCheck('Number')
.appendField('z:');
this.setInputsInline(true)
this.setOutput(true, null);
this.setTooltip('Create a BlockPos from components');
this.setHelpUrl('');
this.setColour(270);
},
libs: ["construct_blockpos"]
};
Blockly.common.defineBlocks({ blockpos_fromxyz: blockpos_fromxyz });

javascript.javascriptGenerator.forBlock['blockpos_fromxyz'] = function () {
const value_x = javascript.javascriptGenerator.valueToCode(this, 'X', javascript.Order.ATOMIC);
const value_y = javascript.javascriptGenerator.valueToCode(this, 'Y', javascript.Order.ATOMIC);
const value_z = javascript.javascriptGenerator.valueToCode(this, 'Z', javascript.Order.ATOMIC);

const code = `efb2__makeBlockPos(${value_x},${value_y},${value_z})`;
return [code, javascript.Order.NONE];
}
65 changes: 65 additions & 0 deletions blocks/Vec3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const vec3_getxyz = {
init: function () {
this.appendDummyInput('TAB')
.setAlign(Blockly.inputs.Align.RIGHT)
.appendField(new Blockly.FieldDropdown([
['x', '$xCoord'],
['y', '$yCoord'],
['z', '$zCoord']
]), 'TAB');
this.appendValueInput('VALUE')
.setAlign(Blockly.inputs.Align.RIGHT)
.appendField('component of Vec3');
this.setInputsInline(true)
this.setOutput(true, "Number");
this.setTooltip('Get a component of a Vec3');
this.setHelpUrl('');
this.setColour(270);
}
};
Blockly.common.defineBlocks({ vec3_getxyz: vec3_getxyz });



javascript.javascriptGenerator.forBlock['vec3_getxyz'] = function () {
const dropdown_tab = block.getFieldValue('TAB');
const value_value = javascript.javascriptGenerator.valueToCode(this, 'VALUE', javascript.Order.ATOMIC);

const code = `(${value_value})["${dropdown_tab}"]`;
return [code, javascript.Order.NONE];
}



const vec3_fromxyz = {
init: function () {
this.appendValueInput('X')
.setAlign(Blockly.inputs.Align.RIGHT)
.setCheck('Number')
.appendField('Make Vec3 from x:');
this.appendValueInput('Y')
.setAlign(Blockly.inputs.Align.RIGHT)
.setCheck('Number')
.appendField('y:');
this.appendValueInput('Z')
.setAlign(Blockly.inputs.Align.RIGHT)
.setCheck('Number')
.appendField('z:');
this.setInputsInline(true)
this.setOutput(true, null);
this.setTooltip('Create a Vec3 from components');
this.setHelpUrl('');
this.setColour(270);
},
libs: ["construct_vec3"]
};
Blockly.common.defineBlocks({ vec3_fromxyz: vec3_fromxyz });

javascript.javascriptGenerator.forBlock['vec3_fromxyz'] = function () {
const value_x = javascript.javascriptGenerator.valueToCode(this, 'X', javascript.Order.ATOMIC);
const value_y = javascript.javascriptGenerator.valueToCode(this, 'Y', javascript.Order.ATOMIC);
const value_z = javascript.javascriptGenerator.valueToCode(this, 'Z', javascript.Order.ATOMIC);

const code = `efb2__makeVec3(${value_x},${value_y},${value_z})`;
return [code, javascript.Order.NONE];
}
65 changes: 65 additions & 0 deletions blocks/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,68 @@ javascript.javascriptGenerator.forBlock['world_command'] = function () {
const code = `efb2__executeCommand(${value_world}, ${value_pos}, ${value_cmd});`;
return code;
}



const world_is_not_remote = {
init: function () {
this.appendValueInput('WORLD')
.setAlign(Blockly.inputs.Align.RIGHT)
.appendField('world is on the server');
this.setInputsInline(true)
this.setOutput(true, 'Boolean');
this.setTooltip('Check if the world is running on the server.');
this.setHelpUrl('');
this.setColour(195);
}
};
Blockly.common.defineBlocks({ world_is_not_remote: world_is_not_remote });

javascript.javascriptGenerator.forBlock['world_is_not_remote'] = function () {
const value_world = javascript.javascriptGenerator.valueToCode(this, 'WORLD', javascript.Order.ATOMIC);
const code = `!(${value_world}).$isRemote`;
return [code, javascript.Order.NONE];
}



const world_get_loaded_entities = {
init: function () {
this.appendValueInput('WORLD')
.setAlign(Blockly.inputs.Align.RIGHT)
.appendField('get list of entities in world');
this.setInputsInline(true)
this.setOutput(true, 'Array');
this.setTooltip('Returns an array of entities in the world');
this.setHelpUrl('');
this.setColour(195);
}
};
Blockly.common.defineBlocks({ world_get_loaded_entities: world_get_loaded_entities });

javascript.javascriptGenerator.forBlock['world_get_loaded_entities'] = function () {
const value_world = generator.valueToCode(this, 'WORLD', javascript.Order.ATOMIC);
const code = `(${value_world}).loadedEntityList.toArray1().getRef().data`;
return [code, javascript.Order.NONE];
}



const world_get_player_entities = {
init: function () {
this.appendValueInput('WORLD')
.setAlign(Blockly.inputs.Align.RIGHT)
.appendField('get list of players in world');
this.setInputsInline(true)
this.setOutput(true, 'Array');
this.setTooltip('Returns an array of player entities in the world');
this.setHelpUrl('');
this.setColour(195);
}
};
Blockly.common.defineBlocks({ world_get_player_entities: world_get_player_entities });
javascript.javascriptGenerator.forBlock['world_get_player_entities'] = function () {
const value_world = javascript.javascriptGenerator.valueToCode(thiss, 'WORLD', javascript.Order.ATOMIC);
const code = `(${value_world}).playerEntities.toArray1().getRef().data`;
return [code, javascript.Order.NONE];
}
30 changes: 30 additions & 0 deletions functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,36 @@ FUNCTIONS["execute_command"] = {
},
};

FUNCTIONS["construct_vec3"] = {
identifier: "construct_vec3",
//Very important that there is no name and a whitespace before and after the parantheses
code: function () {
function EFB2__defineMakeVec3() {
var mkVec3 = ModAPI.reflect.getClassById("net.minecraft.util.Vec3").constructors.find(x=>x.length===3);
globalThis.efb2__makeVec3 = function efb2__makeVec3(x, y, z) {
return mkVec3(x, y, z);
}
}
ModAPI.dedicatedServer.appendCode(EFB2__defineMakeVec3);
EFB2__defineMakeVec3();
},
};

FUNCTIONS["construct_blockpos"] = {
identifier: "construct_blockpos",
//Very important that there is no name and a whitespace before and after the parantheses
code: function () {
function EFB2__defineMakeBlockPos() {
var mkBlockPos = ModAPI.reflect.getClassById("net.minecraft.util.BlockPos").constructors.find(x=>x.length===3);
globalThis.efb2__makeBlockPos = function efb2__makeBlockPos(x, y, z) {
return mkBlockPos(x, y, z);
}
}
ModAPI.dedicatedServer.appendCode(EFB2__defineMakeBlockPos);
EFB2__defineMakeBlockPos();
},
};

function getFunctionCode(fn) {
return fn.code.toString().match(codeGrabberRegex)?.[0]
|| (()=>{console.error("Malformed function: ", fn); return "";})();
Expand Down
31 changes: 28 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -366,23 +366,46 @@
<category name="Variables" colour="#a55b80" custom="VARIABLE"></category>
<!-- <category name="Functions" colour="#995ba5" custom="PROCEDURE"></category> -->
<sep></sep>
<category name="Blocks (H)" colour="#ff00ff">
<category name="Blocks (H)" colour="#dd11dd">
<block type="handle_BlockConstructor"> </block>
<block type="handle_BlockBreak"> </block>
<block type="handle_BlockAdded"> </block>
<block type="handle_BlockNeighbourChange"> </block>
<block type="handle_BlockBrokenByPlayer"> </block>
<block type="handle_BlockUpdateTick"> </block>
</category>
<category name="Blocks (C)" colour="#ff00ff">
<category name="Blocks (C)" colour="#dd11dd">
<block type="blocks_blockproperty"> </block>
<block type="blocks_blockswitch"> </block>
<block type="blocks_boundingbox"> </block>
<block type="blocks_creativetab"> </block>
</category>
<category name="Items (H)" colour="#0033dd">

</category>
<category name="Items (C)" colour="#0033dd">

</category>
<category name="ItemStack (C)" colour="#44eebb">

</category>
<category name="BlockPos (C)" colour="#6633cc">
<block type="blockpos_fromxyz"> </block>
<block type="blockpos_getxyz"> </block>
</category>
<category name="Vec3 (C)" colour="#6633cc">
<block type="vec3_fromxyz"> </block>
<block type="vec3_getxyz"> </block>
</category>
<category name="World (C)" colour="#22aabb">
<block type="world_is_not_remote"> </block>
<block type="world_explosion"> </block>
<block type="world_command"> </block>
<block type="world_get_loaded_entities"> </block>
<block type="world_get_player_entities"> </block>
</category>
<category name="Entity (C)" colour="#ff8811">

</category>
</xml>
</head>
Expand Down Expand Up @@ -442,7 +465,9 @@ <h4>Dirt Block</h4>
<!-- Additional blocks -->
<script src="blocks/World.js"></script>
<script src="blocks/Blocks.js"></script>
<script src="blocks/Events.js"></script>
<script src="blocks/Vec3.js"></script>
<script src="blocks/BlockPos.js"></script>
<script src="blocks/Events.js"></script> <!-- Currently unused -->
<script src="blocks/ReturnBlock.js"></script>

<!-- The datablock types -->
Expand Down

0 comments on commit 7b723f3

Please sign in to comment.