blockly 12 for blocklycraft @ spigot 1.9.4

This commit is contained in:
Anton Zykov
2025-07-14 13:23:38 +03:00
commit e48083d7c8
42 changed files with 8214 additions and 0 deletions
+22
View File
@@ -0,0 +1,22 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Dependency directories
node_modules/
# Generated files
dist/
build/
.DS_Store
# Optional npm cache directory
.npm
# IDEs and editors
.idea
*.sublime-workspace
.vscode/*
+4828
View File
File diff suppressed because it is too large Load Diff
+35
View File
@@ -0,0 +1,35 @@
{
"name": "hello-world",
"version": "1.0.0",
"description": "A sample app using Blockly",
"main": "index.js",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --mode production",
"start": "webpack serve --mode development"
},
"keywords": [
"blockly"
],
"author": "",
"license": "Apache-2.0",
"devDependencies": {
"@types/js-cookie": "^3.0.6",
"css-loader": "^6.7.1",
"html-webpack-plugin": "^5.5.0",
"source-map-loader": "^4.0.1",
"style-loader": "^3.3.1",
"ts-loader": "^9.4.2",
"typescript": "^5.0.2",
"webpack": "^5.76.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.11.1"
},
"dependencies": {
"@blockly/continuous-toolbox": "^7.0.1",
"blockly": "^12.0.0",
"js-cookie": "^3.0.5",
"nano": "^10.1.4"
}
}
+1
View File
@@ -0,0 +1 @@
export * from './text';
+45
View File
@@ -0,0 +1,45 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly/core';
export const addText = {
block: Blockly.common.createBlockDefinitionsFromJsonArray([{
// category: 'text',
type: 'add_text',
message0: 'Add text %1',
args0: [
{
type: 'input_value',
name: 'TEXT',
check: 'String',
},
],
previousStatement: null,
nextStatement: null,
colour: 160,
tooltip: '',
helpUrl: '',
}]),
toJS: (block: Blockly.Block, generator: Blockly.CodeGenerator) => {
const text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
const addText = generator.provideFunction_(
'addText',
`function ${generator.FUNCTION_NAME_PLACEHOLDER_}(text) {
// Add text to the output area.
const outputDiv = document.getElementById('output');
const textEl = document.createElement('p');
textEl.innerText = text;
outputDiv.appendChild(textEl);
}`,
);
// Generate the function call for this block.
const code = `${addText}(${text});\n`;
return code;
},
};
+2
View File
@@ -0,0 +1,2 @@
export * from './example';
export * from './minecraft';
@@ -0,0 +1,34 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly/core';
export const minecraftBlockGet = {
block: Blockly.common.createBlockDefinitionsFromJsonArray([{
// category: 'text',
type: 'minecraft_block_get',
message0: 'Get block at %1',
inputsInline: true,
args0: [
{
type: 'input_value',
name: 'LOCATION',
check: 'Location',
},
],
output: 'Material',
colour: '#e08080',
tooltip: '',
helpUrl: '',
}]),
toJS: (block: Blockly.Block, generator: Blockly.CodeGenerator) => {
// const location = block.getFieldValue('LOCATION');
const location = generator.valueToCode(block, 'LOCATION', Order.NONE) || "''";
const code = `${location}.getBlock().getType()\n`;
return [code, Order.ATOMIC];
},
};
@@ -0,0 +1,50 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly/core';
export const minecraftBlockPower = {
block: Blockly.common.createBlockDefinitionsFromJsonArray([{
// category: 'text',
type: 'minecraft_block_power',
message0: 'Power block at %1',
inputsInline: true,
args0: [
{
type: 'input_value',
name: 'LOCATION',
check: 'Location',
},
],
previousStatement: null,
nextStatement: null,
colour: '#e08080',
tooltip: '',
helpUrl: '',
}]),
toJS: (block: Blockly.Block, generator: Blockly.CodeGenerator) => {
const changeBlockData = generator.provideFunction_(
'changeBlockData',
`function ${generator.FUNCTION_NAME_PLACEHOLDER_}(location, callback, player) {
var block = location.getBlock();
echo(player, block.getPropertyForName('power'));
// block.setBlockData(callback(block.getBlockData()));
}`,
);
// const location = block.getFieldValue('LOCATION');
const location = generator.valueToCode(block, 'LOCATION', Order.NONE) || "''";
const code = `
${changeBlockData}(${location}, function (data) {
// data.setPowered(true);
echo(player, data);
return data;
}, player);\n`;
return code;
},
};
@@ -0,0 +1,41 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly/core';
export const minecraftBlockSet = {
block: Blockly.common.createBlockDefinitionsFromJsonArray([{
// category: 'text',
type: 'minecraft_block_set',
message0: 'Set block at %1 to %2',
inputsInline: true,
args0: [
{
type: 'input_value',
name: 'LOCATION',
check: 'Location',
},
{
type: 'input_value',
name: 'MATERIAL',
check: 'Material',
},
],
previousStatement: null,
nextStatement: null,
colour: '#e08080',
tooltip: '',
helpUrl: '',
}]),
toJS: (block: Blockly.Block, generator: Blockly.CodeGenerator) => {
// const location = block.getFieldValue('LOCATION');
const location = generator.valueToCode(block, 'LOCATION', Order.NONE) || "''";
const material = generator.valueToCode(block, 'MATERIAL', Order.NONE) || "''";
const code = `${location}.getBlock().setType(${material});\n`;
return code;
},
};
@@ -0,0 +1,4 @@
export * from './blockSet';
export * from './blockGet';
export * from './blockPower';
export * from './shape';
@@ -0,0 +1,101 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly/core';
export const minecraftBlockSetCube = {
block: Blockly.common.createBlockDefinitionsFromJsonArray([{
// category: 'text',
type: 'minecraft_block_set_cube',
message0: 'Cube centered at %1 size x:%2 y:%3 z:%4 set to %5',
inputsInline: true,
args0: [
{
type: 'input_value',
name: 'LOCATION',
check: 'Location',
},
{
type: 'input_value',
name: 'X',
check: 'Number',
},
{
type: 'input_value',
name: 'Y',
check: 'Number',
},
{
type: 'input_value',
name: 'Z',
check: 'Number',
},
// {
// type: 'field_number',
// name: 'X',
// value: 1,
// min: 1,
// max: 200,
// precision: 0,
// },
// {
// type: 'field_number',
// name: 'Y',
// value: 1,
// min: 1,
// max: 200,
// precision: 0,
// },
// {
// type: 'field_number',
// name: 'Z',
// value: 1,
// min: 1,
// max: 200,
// precision: 0,
// },
{
type: 'input_value',
name: 'MATERIAL',
check: 'Material',
},
],
previousStatement: null,
nextStatement: null,
colour: '#e08080',
tooltip: '',
helpUrl: '',
}]),
toJS: (block: Blockly.Block, generator: Blockly.CodeGenerator) => {
const blockSetShape = generator.provideFunction_(
'blockSetShape',
`function ${generator.FUNCTION_NAME_PLACEHOLDER_}(location, material, positions) {
var block = location.getBlock();
for (var i = 0; i < positions.length; i++) {
var pos = positions[i];
block.getRelative(pos[0], pos[1], pos[2]).setType(material);
}
}`,
);
// const location = block.getFieldValue('LOCATION');
const location = generator.valueToCode(block, 'LOCATION', Order.NONE) || "''";
const material = generator.valueToCode(block, 'MATERIAL', Order.NONE) || "''";
const x = Number(generator.valueToCode(block, 'X', Order.NONE) ?? 1);
const y = Number(generator.valueToCode(block, 'Y', Order.NONE) ?? 1);
const z = Number(generator.valueToCode(block, 'Z', Order.NONE) ?? 1);
const code = `var positions = [];
for (var xi = 0; xi < ${x}; xi++)
for (var yi = 0; yi < ${y}; yi++)
for (var zi = 0; zi < ${z}; zi++)
positions.push([xi - ${Math.floor(x / 2)}, yi - ${Math.floor(y / 2)}, zi - ${Math.floor(z / 2)}]);
${blockSetShape}(${location}, ${material}, positions);\n`;
return code;
},
};
@@ -0,0 +1,101 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly/core';
export const minecraftBlockSetCylinder = {
block: Blockly.common.createBlockDefinitionsFromJsonArray([{
// category: 'text',
type: 'minecraft_block_set_cylinder',
message0: 'Cylinder centered at %1 diameter:%2 height:%3 set to %4',
inputsInline: true,
args0: [
{
type: 'input_value',
name: 'LOCATION',
check: 'Location',
},
{
type: 'input_value',
name: 'D',
check: 'Number',
},
{
type: 'input_value',
name: 'H',
check: 'Number',
},
// {
// type: 'field_number',
// name: 'X',
// value: 1,
// min: 1,
// max: 200,
// precision: 0,
// },
// {
// type: 'field_number',
// name: 'Y',
// value: 1,
// min: 1,
// max: 200,
// precision: 0,
// },
// {
// type: 'field_number',
// name: 'Z',
// value: 1,
// min: 1,
// max: 200,
// precision: 0,
// },
{
type: 'input_value',
name: 'MATERIAL',
check: 'Material',
},
],
previousStatement: null,
nextStatement: null,
colour: '#e08080',
tooltip: '',
helpUrl: '',
}]),
toJS: (block: Blockly.Block, generator: Blockly.CodeGenerator) => {
const blockSetShape = generator.provideFunction_(
'blockSetShape',
`function ${generator.FUNCTION_NAME_PLACEHOLDER_}(location, material, positions) {
var block = location.getBlock();
for (var i = 0; i < positions.length; i++) {
var pos = positions[i];
block.getRelative(pos[0], pos[1], pos[2]).setType(material);
}
}`,
);
// const location = block.getFieldValue('LOCATION');
const location = generator.valueToCode(block, 'LOCATION', Order.NONE) || "''";
const material = generator.valueToCode(block, 'MATERIAL', Order.NONE) || "''";
const d = Number(generator.valueToCode(block, 'D', Order.NONE) ?? 1);
const h = Number(generator.valueToCode(block, 'H', Order.NONE) ?? 1);
const code = `var positions = [];
for (var yi = 0; yi < ${h}; yi++)
for (var xi = 0; xi < ${d/2}; xi++) {
var sz = Math.sqrt(${(d / 2) * (d / 2)} - xi * xi);
for (var zi = 0; zi < sz; zi++) {
positions.push([Math.round(xi), yi, Math.round(zi)]);
positions.push([Math.round(xi), yi, Math.round(-zi)]);
positions.push([Math.round(-xi), yi, Math.round(zi)]);
positions.push([Math.round(-xi), yi, Math.round(-zi)]);
}
}
${blockSetShape}(${location}, ${material}, positions);\n`;
return code;
},
};
@@ -0,0 +1,2 @@
export * from './cube';
export * from './cylinder';
+44
View File
@@ -0,0 +1,44 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly/core';
export const minecraftCommand = {
block: Blockly.common.createBlockDefinitionsFromJsonArray([{
// category: 'text',
type: 'minecraft_command',
message0: 'Command %1 %2',
args0: [
{
type: 'field_input',
name: 'NAME',
text: 'name',
},
{
"type": "input_statement",
"name": "STATEMENT",
"check": "Local"
},
],
previousStatement: null,
nextStatement: null,
colour: '#e08080',
tooltip: '',
helpUrl: '',
}]),
toJS: (block: Blockly.Block, generator: Blockly.CodeGenerator) => {
const name = block.getFieldValue('NAME');
const statement = generator.statementToCode(block, 'STATEMENT') || '';
const code = `command(
'${name}',
function ( parameters, player ) {
${statement}
}
);`;
return code;
},
};
+32
View File
@@ -0,0 +1,32 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly/core';
export const minecraftEcho = {
block: Blockly.common.createBlockDefinitionsFromJsonArray([{
type: 'minecraft_echo',
message0: 'Print %1',
args0: [
{
type: 'input_value',
name: 'VALUE',
// check: 'Number',
},
],
previousStatement: null,
nextStatement: null,
colour: '#e08080',
tooltip: '',
helpUrl: '',
}]),
toJS: (block: Blockly.Block, generator: Blockly.CodeGenerator) => {
const value = generator.valueToCode(block, 'VALUE', Order.ATOMIC) || "''";
const code = `echo(player, ${value});\n`;
return code;
},
};
+6
View File
@@ -0,0 +1,6 @@
export * from './command';
export * from './echo';
export * from './location';
export * from './block';
export * from './material';
export * from './scheduler';
@@ -0,0 +1,7 @@
export * from './playerLocation';
export * from './playerLookAtLocation';
export * from './xyzLocation';
export * from './locationMoveForward';
export * from './locationMove';
export * from './locationLook';
export * from './locationTurn';
@@ -0,0 +1,91 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly/core';
enum Direction {
'Up' = 'UP',
'Down' = 'DOWN',
'North' = 'NORTH',
'South' = 'SOUTH',
'East' = 'EAST',
'West' = 'WEST',
};
function yawByDirection(direction: Direction): number {
switch (direction) {
case Direction.Up:
return -90;
case Direction.Down:
return 90;
default:
return 0;
}
}
function pitchByDirection(direction: Direction): number {
switch (direction) {
case Direction.East:
return -90;
case Direction.South:
return 0;
case Direction.West:
return 90;
case Direction.North:
return 180;
default:
return 0;
}
}
export const minecraftLookLocation = {
block: Blockly.common.createBlockDefinitionsFromJsonArray([{
type: 'minecraft_location_look',
message0: 'Look %1 to %2',
args0: [
{
type: 'input_value',
name: 'LOCATION',
check: 'Location',
},
{
type: 'field_dropdown',
name: 'DIRECTION',
options: [
['Up', 'UP'],
['Down', 'DOWN'],
['East', 'EAST'],
['West', 'WEST'],
['North', 'NORTH'],
['South', 'SOUTH'],
],
},
],
previousStatement: null,
nextStatement: null,
colour: '#e08080',
tooltip: '',
helpUrl: '',
}]),
toJS: (block: Blockly.Block, generator: Blockly.CodeGenerator) => {
const locationLook = generator.provideFunction_(
'locationLook',
`function ${generator.FUNCTION_NAME_PLACEHOLDER_}(location, yaw, pitch) {
location.setYaw(yaw);
location.setPitch(pitch);
}`,
);
const location = generator.valueToCode(block, 'LOCATION', Order.NONE) || "''";
const direction = block.getFieldValue('DIRECTION');
const yaw = yawByDirection(direction);
const pitch = pitchByDirection(direction);
const code = `${locationLook}(${location}, ${yaw}, ${pitch})\n`;
return code;
},
};
@@ -0,0 +1,51 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly/core';
export const minecraftMoveLocation = {
block: Blockly.common.createBlockDefinitionsFromJsonArray([{
type: 'minecraft_location_move',
message0: 'Move %1 by %2 %3 %4',
// output: 'Location',
args0: [
{
type: 'input_value',
name: 'LOCATION',
check: 'Location',
},
{
type: 'field_number',
name: 'X',
precision: 0,
},
{
type: 'field_number',
name: 'Y',
precision: 0,
},
{
type: 'field_number',
name: 'Z',
precision: 0,
},
],
previousStatement: null,
nextStatement: null,
colour: '#e08080',
tooltip: '',
helpUrl: '',
}]),
toJS: (block: Blockly.Block, generator: Blockly.CodeGenerator) => {
const location = generator.valueToCode(block, 'LOCATION', Order.NONE) || "''";
const x = block.getFieldValue('X');
const y = block.getFieldValue('Y');
const z = block.getFieldValue('Z');
const code = `${location}.add(${x}, ${y}, ${z});\n`;
return code;
},
};
@@ -0,0 +1,46 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly/core';
export const minecraftMoveForwardLocation = {
block: Blockly.common.createBlockDefinitionsFromJsonArray([{
type: 'minecraft_location_move_forward',
message0: 'Move %1 forward by %2',
// output: 'Location',
args0: [
{
type: 'input_value',
name: 'LOCATION',
check: 'Location',
},
{
type: 'field_number',
name: 'DELTA',
precision: 0,
},
],
previousStatement: null,
nextStatement: null,
colour: '#e08080',
tooltip: '',
helpUrl: '',
}]),
toJS: (block: Blockly.Block, generator: Blockly.CodeGenerator) => {
const locationMove = generator.provideFunction_(
'locationMove',
`function ${generator.FUNCTION_NAME_PLACEHOLDER_}(location, delta) {
location.add(location.getDirection().multiply(delta));
}`,
);
const location = generator.valueToCode(block, 'LOCATION', Order.NONE) || "''";
const delta = block.getFieldValue('DELTA');
const code = `${locationMove}(${location}, ${delta});\n`;
return code;
},
};
@@ -0,0 +1,45 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly/core';
export const minecraftTurnLocation = {
block: Blockly.common.createBlockDefinitionsFromJsonArray([{
type: 'minecraft_location_turn',
message0: 'Turn %1 by %2 %3',
// output: 'Location',
args0: [
{
type: 'input_value',
name: 'LOCATION',
check: 'Location',
},
{
type: 'field_number',
name: 'YAW',
precision: 2,
},
{
type: 'field_number',
name: 'PITCH',
precision: 2,
},
],
previousStatement: null,
nextStatement: null,
colour: '#e08080',
tooltip: '',
helpUrl: '',
}]),
toJS: (block: Blockly.Block, generator: Blockly.CodeGenerator) => {
const location = generator.valueToCode(block, 'LOCATION', Order.NONE) || "''";
const yaw = block.getFieldValue('YAW');
const pitch = block.getFieldValue('PITCH');
const code = `${location}.setYaw((${yaw}, ${pitch});\n`;
return code;
},
};
@@ -0,0 +1,24 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly/core';
export const minecraftPlayerLocation = {
block: Blockly.common.createBlockDefinitionsFromJsonArray([{
type: 'minecraft_player_location',
message0: 'Player location',
output: 'Location',
args0: [],
colour: '#e08080',
tooltip: '',
helpUrl: '',
}]),
toJS: (block: Blockly.Block, generator: Blockly.CodeGenerator) => {
const code = `player.getLocation()`;
return [code, Order.ATOMIC];
},
};
@@ -0,0 +1,24 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly/core';
export const minecraftPlayerLookAtLocation = {
block: Blockly.common.createBlockDefinitionsFromJsonArray([{
type: 'minecraft_player_look_at_location',
message0: 'Player look at location',
output: 'Location',
args0: [],
colour: '#e08080',
tooltip: '',
helpUrl: '',
}]),
toJS: (block: Blockly.Block, generator: Blockly.CodeGenerator) => {
const code = `player.getTargetBlock(null, 1000).getLocation()`;
return [code, Order.ATOMIC];
},
};
@@ -0,0 +1,43 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly/core';
export const minecraftXyzLocation = {
block: Blockly.common.createBlockDefinitionsFromJsonArray([{
type: 'minecraft_xyz_location',
message0: 'Location x:%1 y:%2 z:%3',
output: 'Location',
args0: [
{
type: 'field_number',
name: 'X',
precision: 0,
},
{
type: 'field_number',
name: 'Y',
precision: 0,
},
{
type: 'field_number',
name: 'Z',
precision: 0,
},
],
colour: '#e08080',
tooltip: '',
helpUrl: '',
}]),
toJS: (block: Blockly.Block, generator: Blockly.CodeGenerator) => {
const x = block.getFieldValue('X');
const y = block.getFieldValue('Y');
const z = block.getFieldValue('Z');
const code = `new org.bukkit.Location(player.getWorld(), ${x}, ${y}, ${z})`;
return [code, Order.ATOMIC];
},
};
@@ -0,0 +1 @@
export * from './namedMaterial';
@@ -0,0 +1,348 @@
export const materials = [
['AIR', 'AIR'],
['STONE', 'STONE'],
['GRASS', 'GRASS'],
['DIRT', 'DIRT'],
['COBBLESTONE', 'COBBLESTONE'],
['WOOD', 'WOOD'],
['SAPLING', 'SAPLING'],
['BEDROCK', 'BEDROCK'],
['WATER', 'WATER'],
['STATIONARY_WATER', 'STATIONARY_WATER'],
['LAVA', 'LAVA'],
['STATIONARY_LAVA', 'STATIONARY_LAVA'],
['SAND', 'SAND'],
['GRAVEL', 'GRAVEL'],
['GOLD_ORE', 'GOLD_ORE'],
['IRON_ORE', 'IRON_ORE'],
['COAL_ORE', 'COAL_ORE'],
['LOG', 'LOG'],
['LEAVES', 'LEAVES'],
['SPONGE', 'SPONGE'],
['GLASS', 'GLASS'],
['LAPIS_ORE', 'LAPIS_ORE'],
['LAPIS_BLOCK', 'LAPIS_BLOCK'],
['DISPENSER', 'DISPENSER'],
['SANDSTONE', 'SANDSTONE'],
['NOTE_BLOCK', 'NOTE_BLOCK'],
['BED_BLOCK', 'BED_BLOCK'],
['POWERED_RAIL', 'POWERED_RAIL'],
['DETECTOR_RAIL', 'DETECTOR_RAIL'],
['PISTON_STICKY_BASE', 'PISTON_STICKY_BASE'],
['WEB', 'WEB'],
['LONG_GRASS', 'LONG_GRASS'],
['DEAD_BUSH', 'DEAD_BUSH'],
['PISTON_BASE', 'PISTON_BASE'],
['PISTON_EXTENSION', 'PISTON_EXTENSION'],
['WOOL', 'WOOL'],
['PISTON_MOVING_PIECE', 'PISTON_MOVING_PIECE'],
['YELLOW_FLOWER', 'YELLOW_FLOWER'],
['RED_ROSE', 'RED_ROSE'],
['BROWN_MUSHROOM', 'BROWN_MUSHROOM'],
['RED_MUSHROOM', 'RED_MUSHROOM'],
['GOLD_BLOCK', 'GOLD_BLOCK'],
['IRON_BLOCK', 'IRON_BLOCK'],
['DOUBLE_STEP', 'DOUBLE_STEP'],
['STEP', 'STEP'],
['BRICK', 'BRICK'],
['TNT', 'TNT'],
['BOOKSHELF', 'BOOKSHELF'],
['MOSSY_COBBLESTONE', 'MOSSY_COBBLESTONE'],
['OBSIDIAN', 'OBSIDIAN'],
['TORCH', 'TORCH'],
['FIRE', 'FIRE'],
['MOB_SPAWNER', 'MOB_SPAWNER'],
['WOOD_STAIRS', 'WOOD_STAIRS'],
['CHEST', 'CHEST'],
['REDSTONE_WIRE', 'REDSTONE_WIRE'],
['DIAMOND_ORE', 'DIAMOND_ORE'],
['DIAMOND_BLOCK', 'DIAMOND_BLOCK'],
['WORKBENCH', 'WORKBENCH'],
['CROPS', 'CROPS'],
['SOIL', 'SOIL'],
['FURNACE', 'FURNACE'],
['BURNING_FURNACE', 'BURNING_FURNACE'],
['SIGN_POST', 'SIGN_POST'],
['WOODEN_DOOR', 'WOODEN_DOOR'],
['LADDER', 'LADDER'],
['RAILS', 'RAILS'],
['COBBLESTONE_STAIRS', 'COBBLESTONE_STAIRS'],
['WALL_SIGN', 'WALL_SIGN'],
['LEVER', 'LEVER'],
['STONE_PLATE', 'STONE_PLATE'],
['IRON_DOOR_BLOCK', 'IRON_DOOR_BLOCK'],
['WOOD_PLATE', 'WOOD_PLATE'],
['REDSTONE_ORE', 'REDSTONE_ORE'],
['GLOWING_REDSTONE_ORE', 'GLOWING_REDSTONE_ORE'],
['REDSTONE_TORCH_OFF', 'REDSTONE_TORCH_OFF'],
['REDSTONE_TORCH_ON', 'REDSTONE_TORCH_ON'],
['STONE_BUTTON', 'STONE_BUTTON'],
['SNOW', 'SNOW'],
['ICE', 'ICE'],
['SNOW_BLOCK', 'SNOW_BLOCK'],
['CACTUS', 'CACTUS'],
['CLAY', 'CLAY'],
['SUGAR_CANE_BLOCK', 'SUGAR_CANE_BLOCK'],
['JUKEBOX', 'JUKEBOX'],
['FENCE', 'FENCE'],
['PUMPKIN', 'PUMPKIN'],
['NETHERRACK', 'NETHERRACK'],
['SOUL_SAND', 'SOUL_SAND'],
['GLOWSTONE', 'GLOWSTONE'],
['PORTAL', 'PORTAL'],
['JACK_O_LANTERN', 'JACK_O_LANTERN'],
['CAKE_BLOCK', 'CAKE_BLOCK'],
['DIODE_BLOCK_OFF', 'DIODE_BLOCK_OFF'],
['DIODE_BLOCK_ON', 'DIODE_BLOCK_ON'],
// deprecated
['LOCKED_CHEST', 'LOCKED_CHEST'],
['STAINED_GLASS', 'STAINED_GLASS'],
['TRAP_DOOR', 'TRAP_DOOR'],
['MONSTER_EGGS', 'MONSTER_EGGS'],
['SMOOTH_BRICK', 'SMOOTH_BRICK'],
['HUGE_MUSHROOM_1', 'HUGE_MUSHROOM_1'],
['HUGE_MUSHROOM_2', 'HUGE_MUSHROOM_2'],
['IRON_FENCE', 'IRON_FENCE'],
['THIN_GLASS', 'THIN_GLASS'],
['MELON_BLOCK', 'MELON_BLOCK'],
['PUMPKIN_STEM', 'PUMPKIN_STEM'],
['MELON_STEM', 'MELON_STEM'],
['VINE', 'VINE'],
['FENCE_GATE', 'FENCE_GATE'],
['BRICK_STAIRS', 'BRICK_STAIRS'],
['SMOOTH_STAIRS', 'SMOOTH_STAIRS'],
['MYCEL', 'MYCEL'],
['WATER_LILY', 'WATER_LILY'],
['NETHER_BRICK', 'NETHER_BRICK'],
['NETHER_FENCE', 'NETHER_FENCE'],
['NETHER_BRICK_STAIRS', 'NETHER_BRICK_STAIRS'],
['NETHER_WARTS', 'NETHER_WARTS'],
['ENCHANTMENT_TABLE', 'ENCHANTMENT_TABLE'],
['BREWING_STAND', 'BREWING_STAND'],
['CAULDRON', 'CAULDRON'],
['ENDER_PORTAL', 'ENDER_PORTAL'],
['ENDER_PORTAL_FRAME', 'ENDER_PORTAL_FRAME'],
['ENDER_STONE', 'ENDER_STONE'],
['DRAGON_EGG', 'DRAGON_EGG'],
['REDSTONE_LAMP_OFF', 'REDSTONE_LAMP_OFF'],
['REDSTONE_LAMP_ON', 'REDSTONE_LAMP_ON'],
['WOOD_DOUBLE_STEP', 'WOOD_DOUBLE_STEP'],
['WOOD_STEP', 'WOOD_STEP'],
['COCOA', 'COCOA'],
['SANDSTONE_STAIRS', 'SANDSTONE_STAIRS'],
['EMERALD_ORE', 'EMERALD_ORE'],
['ENDER_CHEST', 'ENDER_CHEST'],
['TRIPWIRE_HOOK', 'TRIPWIRE_HOOK'],
['TRIPWIRE', 'TRIPWIRE'],
['EMERALD_BLOCK', 'EMERALD_BLOCK'],
['SPRUCE_WOOD_STAIRS', 'SPRUCE_WOOD_STAIRS'],
['BIRCH_WOOD_STAIRS', 'BIRCH_WOOD_STAIRS'],
['JUNGLE_WOOD_STAIRS', 'JUNGLE_WOOD_STAIRS'],
['COMMAND', 'COMMAND'],
['BEACON', 'BEACON'],
['COBBLE_WALL', 'COBBLE_WALL'],
['FLOWER_POT', 'FLOWER_POT'],
['CARROT', 'CARROT'],
['POTATO', 'POTATO'],
['WOOD_BUTTON', 'WOOD_BUTTON'],
['SKULL', 'SKULL'],
['ANVIL', 'ANVIL'],
['TRAPPED_CHEST', 'TRAPPED_CHEST'],
['GOLD_PLATE', 'GOLD_PLATE'],
['IRON_PLATE', 'IRON_PLATE'],
['REDSTONE_COMPARATOR_OFF', 'REDSTONE_COMPARATOR_OFF'],
['REDSTONE_COMPARATOR_ON', 'REDSTONE_COMPARATOR_ON'],
['DAYLIGHT_DETECTOR', 'DAYLIGHT_DETECTOR'],
['REDSTONE_BLOCK', 'REDSTONE_BLOCK'],
['QUARTZ_ORE', 'QUARTZ_ORE'],
['HOPPER', 'HOPPER'],
['QUARTZ_BLOCK', 'QUARTZ_BLOCK'],
['QUARTZ_STAIRS', 'QUARTZ_STAIRS'],
['ACTIVATOR_RAIL', 'ACTIVATOR_RAIL'],
['DROPPER', 'DROPPER'],
['STAINED_CLAY', 'STAINED_CLAY'],
['STAINED_GLASS_PANE', 'STAINED_GLASS_PANE'],
['LEAVES_2', 'LEAVES_2'],
['LOG_2', 'LOG_2'],
['ACACIA_STAIRS', 'ACACIA_STAIRS'],
['DARK_OAK_STAIRS', 'DARK_OAK_STAIRS'],
['HAY_BLOCK', 'HAY_BLOCK'],
['CARPET', 'CARPET'],
['HARD_CLAY', 'HARD_CLAY'],
['COAL_BLOCK', 'COAL_BLOCK'],
['PACKED_ICE', 'PACKED_ICE'],
['DOUBLE_PLANT', 'DOUBLE_PLANT'],
['IRON_SPADE', 'IRON_SPADE'],
['IRON_PICKAXE', 'IRON_PICKAXE'],
['IRON_AXE', 'IRON_AXE'],
['FLINT_AND_STEEL', 'FLINT_AND_STEEL'],
['APPLE', 'APPLE'],
['BOW', 'BOW'],
['ARROW', 'ARROW'],
['COAL', 'COAL'],
['DIAMOND', 'DIAMOND'],
['IRON_INGOT', 'IRON_INGOT'],
['GOLD_INGOT', 'GOLD_INGOT'],
['IRON_SWORD', 'IRON_SWORD'],
['WOOD_SWORD', 'WOOD_SWORD'],
['WOOD_SPADE', 'WOOD_SPADE'],
['WOOD_PICKAXE', 'WOOD_PICKAXE'],
['WOOD_AXE', 'WOOD_AXE'],
['STONE_SWORD', 'STONE_SWORD'],
['STONE_SPADE', 'STONE_SPADE'],
['STONE_PICKAXE', 'STONE_PICKAXE'],
['STONE_AXE', 'STONE_AXE'],
['DIAMOND_SWORD', 'DIAMOND_SWORD'],
['DIAMOND_SPADE', 'DIAMOND_SPADE'],
['DIAMOND_PICKAXE', 'DIAMOND_PICKAXE'],
['DIAMOND_AXE', 'DIAMOND_AXE'],
['STICK', 'STICK'],
['BOWL', 'BOWL'],
['MUSHROOM_SOUP', 'MUSHROOM_SOUP'],
['GOLD_SWORD', 'GOLD_SWORD'],
['GOLD_SPADE', 'GOLD_SPADE'],
['GOLD_PICKAXE', 'GOLD_PICKAXE'],
['GOLD_AXE', 'GOLD_AXE'],
['STRING', 'STRING'],
['FEATHER', 'FEATHER'],
['SULPHUR', 'SULPHUR'],
['WOOD_HOE', 'WOOD_HOE'],
['STONE_HOE', 'STONE_HOE'],
['IRON_HOE', 'IRON_HOE'],
['DIAMOND_HOE', 'DIAMOND_HOE'],
['GOLD_HOE', 'GOLD_HOE'],
['SEEDS', 'SEEDS'],
['WHEAT', 'WHEAT'],
['BREAD', 'BREAD'],
['LEATHER_HELMET', 'LEATHER_HELMET'],
['LEATHER_CHESTPLATE', 'LEATHER_CHESTPLATE'],
['LEATHER_LEGGINGS', 'LEATHER_LEGGINGS'],
['LEATHER_BOOTS', 'LEATHER_BOOTS'],
['CHAINMAIL_HELMET', 'CHAINMAIL_HELMET'],
['CHAINMAIL_CHESTPLATE', 'CHAINMAIL_CHESTPLATE'],
['CHAINMAIL_LEGGINGS', 'CHAINMAIL_LEGGINGS'],
['CHAINMAIL_BOOTS', 'CHAINMAIL_BOOTS'],
['IRON_HELMET', 'IRON_HELMET'],
['IRON_CHESTPLATE', 'IRON_CHESTPLATE'],
['IRON_LEGGINGS', 'IRON_LEGGINGS'],
['IRON_BOOTS', 'IRON_BOOTS'],
['DIAMOND_HELMET', 'DIAMOND_HELMET'],
['DIAMOND_CHESTPLATE', 'DIAMOND_CHESTPLATE'],
['DIAMOND_LEGGINGS', 'DIAMOND_LEGGINGS'],
['DIAMOND_BOOTS', 'DIAMOND_BOOTS'],
['GOLD_HELMET', 'GOLD_HELMET'],
['GOLD_CHESTPLATE', 'GOLD_CHESTPLATE'],
['GOLD_LEGGINGS', 'GOLD_LEGGINGS'],
['GOLD_BOOTS', 'GOLD_BOOTS'],
['FLINT', 'FLINT'],
['PORK', 'PORK'],
['GRILLED_PORK', 'GRILLED_PORK'],
['PAINTING', 'PAINTING'],
['GOLDEN_APPLE', 'GOLDEN_APPLE'],
['SIGN', 'SIGN'],
['WOOD_DOOR', 'WOOD_DOOR'],
['BUCKET', 'BUCKET'],
['WATER_BUCKET', 'WATER_BUCKET'],
['LAVA_BUCKET', 'LAVA_BUCKET'],
['MINECART', 'MINECART'],
['SADDLE', 'SADDLE'],
['IRON_DOOR', 'IRON_DOOR'],
['REDSTONE', 'REDSTONE'],
['SNOW_BALL', 'SNOW_BALL'],
['BOAT', 'BOAT'],
['LEATHER', 'LEATHER'],
['MILK_BUCKET', 'MILK_BUCKET'],
['CLAY_BRICK', 'CLAY_BRICK'],
['CLAY_BALL', 'CLAY_BALL'],
['SUGAR_CANE', 'SUGAR_CANE'],
['PAPER', 'PAPER'],
['BOOK', 'BOOK'],
['SLIME_BALL', 'SLIME_BALL'],
['STORAGE_MINECART', 'STORAGE_MINECART'],
['POWERED_MINECART', 'POWERED_MINECART'],
['EGG', 'EGG'],
['COMPASS', 'COMPASS'],
['FISHING_ROD', 'FISHING_ROD'],
['WATCH', 'WATCH'],
['GLOWSTONE_DUST', 'GLOWSTONE_DUST'],
['RAW_FISH', 'RAW_FISH'],
['COOKED_FISH', 'COOKED_FISH'],
['INK_SACK', 'INK_SACK'],
['BONE', 'BONE'],
['SUGAR', 'SUGAR'],
['CAKE', 'CAKE'],
['BED', 'BED'],
['DIODE', 'DIODE'],
['COOKIE', 'COOKIE'],
// MapView
['MAP', 'MAP'],
['SHEARS', 'SHEARS'],
['MELON', 'MELON'],
['PUMPKIN_SEEDS', 'PUMPKIN_SEEDS'],
['MELON_SEEDS', 'MELON_SEEDS'],
['RAW_BEEF', 'RAW_BEEF'],
['COOKED_BEEF', 'COOKED_BEEF'],
['RAW_CHICKEN', 'RAW_CHICKEN'],
['COOKED_CHICKEN', 'COOKED_CHICKEN'],
['ROTTEN_FLESH', 'ROTTEN_FLESH'],
['ENDER_PEARL', 'ENDER_PEARL'],
['BLAZE_ROD', 'BLAZE_ROD'],
['GHAST_TEAR', 'GHAST_TEAR'],
['GOLD_NUGGET', 'GOLD_NUGGET'],
['NETHER_STALK', 'NETHER_STALK'],
// Potion
['POTION', 'POTION'],
['GLASS_BOTTLE', 'GLASS_BOTTLE'],
['SPIDER_EYE', 'SPIDER_EYE'],
['FERMENTED_SPIDER_EYE', 'FERMENTED_SPIDER_EYE'],
['BLAZE_POWDER', 'BLAZE_POWDER'],
['MAGMA_CREAM', 'MAGMA_CREAM'],
['BREWING_STAND_ITEM', 'BREWING_STAND_ITEM'],
['CAULDRON_ITEM', 'CAULDRON_ITEM'],
['EYE_OF_ENDER', 'EYE_OF_ENDER'],
['SPECKLED_MELON', 'SPECKLED_MELON'],
['MONSTER_EGG', 'MONSTER_EGG'],
['EXP_BOTTLE', 'EXP_BOTTLE'],
['FIREBALL', 'FIREBALL'],
['BOOK_AND_QUILL', 'BOOK_AND_QUILL'],
['WRITTEN_BOOK', 'WRITTEN_BOOK'],
['EMERALD', 'EMERALD'],
['ITEM_FRAME', 'ITEM_FRAME'],
['FLOWER_POT_ITEM', 'FLOWER_POT_ITEM'],
['CARROT_ITEM', 'CARROT_ITEM'],
['POTATO_ITEM', 'POTATO_ITEM'],
['BAKED_POTATO', 'BAKED_POTATO'],
['POISONOUS_POTATO', 'POISONOUS_POTATO'],
['EMPTY_MAP', 'EMPTY_MAP'],
['GOLDEN_CARROT', 'GOLDEN_CARROT'],
['SKULL_ITEM', 'SKULL_ITEM'],
['CARROT_STICK', 'CARROT_STICK'],
['NETHER_STAR', 'NETHER_STAR'],
['PUMPKIN_PIE', 'PUMPKIN_PIE'],
['FIREWORK', 'FIREWORK'],
['FIREWORK_CHARGE', 'FIREWORK_CHARGE'],
['ENCHANTED_BOOK', 'ENCHANTED_BOOK'],
['REDSTONE_COMPARATOR', 'REDSTONE_COMPARATOR'],
['NETHER_BRICK_ITEM', 'NETHER_BRICK_ITEM'],
['QUARTZ', 'QUARTZ'],
['EXPLOSIVE_MINECART', 'EXPLOSIVE_MINECART'],
['HOPPER_MINECART', 'HOPPER_MINECART'],
['IRON_BARDING', 'IRON_BARDING'],
['GOLD_BARDING', 'GOLD_BARDING'],
['DIAMOND_BARDING', 'DIAMOND_BARDING'],
['LEASH', 'LEASH'],
['NAME_TAG', 'NAME_TAG'],
['COMMAND_MINECART', 'COMMAND_MINECART'],
['GOLD_RECORD', 'GOLD_RECORD'],
['GREEN_RECORD', 'GREEN_RECORD'],
['RECORD_3', 'RECORD_3'],
['RECORD_4', 'RECORD_4'],
['RECORD_5', 'RECORD_5'],
['RECORD_6', 'RECORD_6'],
['RECORD_7', 'RECORD_7'],
['RECORD_8', 'RECORD_8'],
['RECORD_9', 'RECORD_9'],
['RECORD_10', 'RECORD_10'],
['RECORD_11', 'RECORD_11'],
['RECORD_12', 'RECORD_12'],
];
@@ -0,0 +1,32 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly/core';
import { materials as options } from './materials';
export const minecraftNamedMaterial = {
block: Blockly.common.createBlockDefinitionsFromJsonArray([{
type: 'minecraft_named_material',
message0: 'Material %1',
output: 'Material',
args0: [
{
type: 'field_dropdown',
name: 'NAME',
options: options.sort((a, b) => a[0].localeCompare(b[0])),
},
],
colour: '#e08080',
tooltip: '',
helpUrl: '',
}]),
toJS: (block: Blockly.Block, generator: Blockly.CodeGenerator) => {
const name = block.getFieldValue('NAME');
const code = `org.bukkit.Material.${String(name).toUpperCase()}`;
return [code, Order.ATOMIC];
},
};
@@ -0,0 +1 @@
export * from './runLater';
@@ -0,0 +1,43 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import * as Blockly from 'blockly/core';
export const minecraftSchedulerRunLater = {
block: Blockly.common.createBlockDefinitionsFromJsonArray([{
// category: 'text',
type: 'minecraft_scheduler_run_later',
message0: 'Wait %1 ticks, then %2',
args0: [
{
type: 'field_number',
name: 'DELAY',
precision: 0,
},
{
"type": "input_statement",
"name": "STATEMENT",
"check": "Local"
},
],
previousStatement: null,
nextStatement: null,
colour: '#e08080',
tooltip: '',
helpUrl: '',
}]),
toJS: (block: Blockly.Block, generator: Blockly.CodeGenerator) => {
const delay = block.getFieldValue('DELAY');
const statement = generator.statementToCode(block, 'STATEMENT') || '';
const code = `server.scheduler.runTaskLater(global.plugin,
() => {
${statement}
},
${delay},
);`;
return code;
},
};
+61
View File
@@ -0,0 +1,61 @@
body {
margin: 0;
max-width: 100vw;
}
pre,
code {
overflow: auto;
}
#pageContainer {
display: flex;
width: 100%;
max-width: 100vw;
height: 100vh;
}
#overlay {
position: relative;
flex-basis: 100%;
height: 100%;
min-width: 600px;
}
#outputDiv {
position: absolute;
top: 0;
left: 40%;
width: 60%;
height: 100%;
opacity: 0.5;
pointer-events: none;
z-index: 2;
}
#blocklyDiv {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
}
#outputPane {
display: flex;
flex-direction: column;
width: 400px;
flex: 0 0 400px;
overflow: auto;
/* margin: 1rem; */
}
#generatedCode {
background-color: rgb(247, 240, 228);
margin: 0px;
}
#output {
height: 50%;
}
+20
View File
@@ -0,0 +1,20 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Blockly Sample App</title>
</head>
<body>
<div id="pageContainer">
<div id="outputPane" style="display:none">
<pre id="generatedCode"><code></code></pre>
<div id="output"></div>
</div>
<div id="overlay">
<pre id="outputDiv"><code></code></pre>
<div id="blocklyDiv"></div>
</div>
<!-- <div><button onClick="ws.clear(); save_ws()">aaa</button></div> -->
</div>
</body>
</html>
+125
View File
@@ -0,0 +1,125 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import * as Blockly from 'blockly';
import * as newBlocks from './blocks';
import { javascriptGenerator } from 'blockly/javascript';
import { save, load } from './serialization';
import { toolbox } from './toolbox';
import { registerContinuousToolbox } from '@blockly/continuous-toolbox';
import Cookies from 'js-cookie';
import * as nano from 'nano';
import './index.css';
function randomValueHex(length: number) {
let hex = '';
for (let i = 0; i < length; i++)
hex += Math.floor(Math.random() * 256).toString(16);
return hex;
}
let browserId = Cookies.get('id');
if (!browserId) {
browserId = randomValueHex(24);
if (!Cookies.get('id'))
Cookies.set('id', browserId);
}
const blocks = Object.values(newBlocks).reduce((acc, val) => {
acc = { ...acc, ...val.block };
return acc;
}, {});
const generators = Object.values(newBlocks).reduce((acc, val) => {
acc = { ...acc, [Object.keys(val.block)[0]]: val.toJS };
return acc;
}, {});
// Register the blocks and generator with Blockly
Blockly.common.defineBlocks(blocks);
Object.assign(javascriptGenerator.forBlock, generators);
registerContinuousToolbox();
// Set up UI elements and inject Blockly
const codeDiv = document.getElementById('outputDiv')?.firstChild;
const outputDiv = document.getElementById('output');
const blocklyDiv = document.getElementById('blocklyDiv');
if (!blocklyDiv) {
throw new Error(`div with id 'blocklyDiv' not found`);
}
const ws = Blockly.inject(
blocklyDiv,
{
toolbox,
plugins: {
flyoutsVerticalToolbox: 'ContinuousFlyout',
metricsManager: 'ContinuousMetrics',
toolbox: 'ContinuousToolbox',
},
},
);
// This function resets the code and output divs, shows the
// generated code from the workspace, and evals the code.
// In a real application, you probably shouldn't use `eval`.
const runCode = () => {
const code = javascriptGenerator.workspaceToCode(ws as Blockly.Workspace);
// console.dir(code);
try {
fetch(
'http://10.0.64.121:8081/set/' + browserId,
{
method: 'POST',
body: code,
}
);
}
catch (err) {
window.alert('Error');
}
if (codeDiv) codeDiv.textContent = code;
// if (outputDiv) outputDiv.innerHTML = '';
// eval(code);
};
if (ws) {
(window as any)['ws'] = ws;
(window as any)['save_ws'] = () => save(ws);
// Load the initial state from storage and run the code.
load(ws);
runCode();
// Every time the workspace changes state, save the changes to storage.
ws.addChangeListener((e: Blockly.Events.Abstract) => {
// UI events are things like scrolling, zooming, etc.
// No need to save after one of these.
if (e.isUiEvent) return;
save(ws);
});
// Whenever the workspace changes meaningfully, run the code again.
ws.addChangeListener((e: Blockly.Events.Abstract) => {
// Don't run the code when the workspace finishes loading; we're
// already running it once when the application starts.
// Don't run the code during drags; we might have invalid state.
if (
e.isUiEvent ||
e.type == Blockly.Events.FINISHED_LOADING ||
ws.isDragging()
) {
return;
}
runCode();
});
}
+260
View File
@@ -0,0 +1,260 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Plugin overview.
*/
import * as Blockly from 'blockly/core';
import {getLocaleDateString} from './utils';
/**
* Class for a date input field.
*/
export class FieldDate extends Blockly.FieldTextInput {
/**
* Serializable fields are saved by the XML renderer, non-serializable fields
* are not. Editable fields should also be serializable.
*/
SERIALIZABLE = true;
/**
* Class for a date input field. Derived from the Closure library date
* picker.
*
* @param value The initial value of the field. Should be in
* 'YYYY-MM-DD' format. Defaults to the current date.
* @param validator A function that is called to validate
* changes to the field's value. Takes in a date string & returns a
* validated date string ('YYYY-MM-DD' format), or null to abort the
* change.
* @param config A map of options used to configure the field.
*/
constructor(
value?: string,
validator?: FieldDateValidator,
config?: FieldDateConfig,
) {
super(value, validator, config);
}
/**
* Constructs a FieldDate from a JSON arg object.
*
* @param options A JSON object with options (date).
* @returns The new field instance.
* @package
* @nocollapse
*/
static fromJson(options: FieldDateFromJsonConfig): FieldDate {
const {date, ...fieldDateConfig} = options;
// `this` might be a subclass of FieldDate if that class doesn't
// override the static fromJson method.
return new this(date, undefined, fieldDateConfig);
}
/* eslint-disable @typescript-eslint/naming-convention */
/**
* Ensures that the input value is a valid date. Additionally, if the date
* string provided includes a time, the time will be removed and the date for
* relative to the user's timezone will be used.
*
* @param newValue The input value. Ex: '2023-04-28'
* @returns A valid date string, or null if invalid.
* @override
*/
protected doClassValidation_(newValue?: string): string | null {
if (!newValue) return null;
const newDate = typeof newValue === 'string' ? new Date(newValue) : null;
if (!newDate || isNaN(newDate.getTime())) return null;
// NOTE: 'newValue' should be a valid date format here.
if (isISOFormat(newValue)) return newValue;
// Assume the time needs to be corrected.
return toLocalISOString(newDate);
}
/**
* Get the text to display on the block when the input hasn't spawned in.
*
* @returns The text to display on the block.
* @override
*/
protected getText_(): string | null {
const value = this.getValue();
if (!value) return null;
// NOTE: There may be discrepancies between the text and the input based on
// browser. For example, 'en-US' would display the text '2/14/2020', then
// clicking in Safari on iOS would display 'Feb 14, 2020'.
return getLocaleDateString(value);
}
/**
* Renders the field. If the picker is shown make sure it has the current
* date selected.
*/
protected render_() {
super.render_();
}
/**
* Shows the inline free-text editor on top of the text along with the date
* editor.
*
* @param e Optional mouse event that triggered the field to
* open, or undefined if triggered programmatically.
* @override
*/
protected showEditor_(e?: Event) {
// Pass in `true` for `quietInput` to disable modal inputs for the date
// block without setting `this.sourceBlock_.workspace.options.modalInputs`,
// which would impact the entire workspace.
super.showEditor_(e, true);
// Even though `quietInput` was set true, focus on the element.
this.htmlInput_?.focus({
preventScroll: true,
});
this.htmlInput_?.select();
this.showDropdown();
}
/**
* Updates the size of the field based on the text.
*
* @param margin margin to use when positioning the text element.
* @override
*/
protected updateSize_(margin?: number) {
// Add margin so that the date input's datepicker icon doesn't clip with
// the text when sized for the date.
super.updateSize_((margin ?? 0) + 20);
}
/**
* Shows the datepicker.
*/
private showDropdown(): void {
if (!this.htmlInput_) return;
Blockly.utils.dom.addClass(this.htmlInput_, 'blocklyDateInput');
// Delay showing the picker until the editor has a chance to position
window.requestAnimationFrame(() => {
// NOTE: HTMLInputElement.showPicker() is not available in earlier
// TypeScript versions (like 4.7.4), so casting to `any` to be compatible
// with dev scripts. Additionally, it's not available for date inputs for
// Safari. For browser compatibility of showPicker, see:
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/showPicker
/* eslint-disable @typescript-eslint/no-explicit-any */
(this.htmlInput_ as any).showPicker();
/* eslint-enable @typescript-eslint/no-explicit-any */
});
}
/**
* Create the html input and set it to type date.
*
* @returns The newly created date input editor.
*/
protected widgetCreate_(): HTMLInputElement {
// NOTE: field_input should return HTMLInputElement for this.
const htmlInput = super.widgetCreate_() as HTMLInputElement;
htmlInput.type = 'date';
return htmlInput;
}
/* eslint-enable @typescript-eslint/naming-convention */
}
/**
* NOTE: There are a few minor ways to tweak the datepicker CSS, though they're
* not consistent across browsers.
*
* @see{@link https://developer.mozilla.org/en-US/docs/Learn/Forms/Property_compatibility_table_for_form_controls#date_pickers}
*
* Below are a few ways this can be tweaked on *some* browsers:
* Blockly.Css.register(`
* ::-webkit-datetime-edit { }
* ::-webkit-datetime-edit-fields-wrapper { }
* ::-webkit-datetime-edit-text { }
* ::-webkit-datetime-edit-month-field { }
* ::-webkit-datetime-edit-day-field { }
* ::-webkit-datetime-edit-year-field { }
* ::-webkit-inner-spin-button { }
* ::-webkit-calendar-picker-indicator { }
* `);
*/
if (Blockly.utils.userAgent.MAC) {
// NOTE: By default, 4 px padding total are added within the User Agent
// Shadow Content on Safari on MAC. Remove the padding so the inner input
// matches the outer input's height and, by extension, the height of the text
// node.
Blockly.Css.register(`
input.blocklyDateInput::-webkit-datetime-edit,
input.blocklyDateInput::-webkit-datetime-edit-month-field,
input.blocklyDateInput::-webkit-datetime-edit-day-field,
input.blocklyDateInput::-webkit-datetime-edit-year-field {
padding: 0;
}
`);
}
Blockly.fieldRegistry.register('field_date', FieldDate);
/**
* A config object for defining a field date.
*/
export interface FieldDateConfig extends Blockly.FieldTextInputConfig {
// NOTE: spellcheck is defined for FieldInput though irrelevant for FieldDate.
spellcheck?: never;
}
/**
* Options used to define a field date from JSON.
*/
export interface FieldDateFromJsonConfig extends FieldDateConfig {
date?: string;
}
export type FieldDateValidator = Blockly.FieldTextInputValidator;
/**
* Validate a string value to see if it matches the format.
*
* @param value The value to validate the format for.
* @returns true if the value is in 'yyyy-mm-dd' format.
* @example
* isISOFormat('2000-02-20T00:00:00Z') === false
* isISOFormat('2000-02-20') === true
*/
export function isISOFormat(value: string): boolean {
const valueMatch = value.match(/\d\d\d\d-\d\d-\d\d/);
// If it matches ####-##-## and is the same as its input string,
// then assume this is the right format
return valueMatch !== null && valueMatch[0] === valueMatch.input;
}
/**
* Convert the date to ISO format for the current timezone.
*
* @param date The date to convert to an ISO string.
* @returns The string in 'yyyy-mm-dd' format, though for the current timezone.
* Ex: new Date('2000-02-20')
*/
export function toLocalISOString(date: Date) {
// NOTE: If the date is Feb 20, 2000 at 23:00 for GMT-6, it would be
// '2000-02-21' at GMT+0, which is what `date.toISOString()` would return.
// For a user whose timezone is GMT-6, this should return '2000-02-20'.
// For a user whose timezone is GMT-5, that date should return '2000-02-21'.
return date
.toLocaleDateString('en-US')
.replace(/(\d+)\/(\d+)\/(\d+)/, '$3-$1-$2')
.replace(/-(\d)(?!\d)/g, '-0$1');
}
// NOTE: Set default here instead of in class so it's available at Field.
FieldDate.prototype.DEFAULT_VALUE = toLocalISOString(new Date());
+32
View File
@@ -0,0 +1,32 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import * as Blockly from 'blockly/core';
const storageKey = 'mainWorkspace';
/**
* Saves the state of the workspace to browser's local storage.
* @param workspace Blockly workspace to save.
*/
export const save = function (workspace: Blockly.Workspace) {
const data = Blockly.serialization.workspaces.save(workspace);
window.localStorage?.setItem(storageKey, JSON.stringify(data));
};
/**
* Loads saved state from local storage into the given workspace.
* @param workspace Blockly workspace to load into.
*/
export const load = function (workspace: Blockly.Workspace) {
const data = window.localStorage?.getItem(storageKey);
if (!data) return;
// Don't emit events during loading.
Blockly.Events.disable();
Blockly.serialization.workspaces.load(JSON.parse(data), workspace, undefined);
Blockly.Events.enable();
};
+632
View File
@@ -0,0 +1,632 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/*
This toolbox contains nearly every single built-in block that Blockly offers,
in addition to the custom block 'add_text' this sample app adds.
You probably don't need every single block, and should consider either rewriting
your toolbox from scratch, or carefully choosing whether you need each block
listed here.
*/
import { category as minecraft } from './minecraft';
export const toolbox = {
kind: 'categoryToolbox',
contents: [
minecraft,
{
kind: 'category',
name: 'Logic',
categorystyle: 'logic_category',
contents: [
{
kind: 'block',
type: 'controls_if',
},
{
kind: 'block',
type: 'logic_compare',
},
{
kind: 'block',
type: 'logic_operation',
},
{
kind: 'block',
type: 'logic_negate',
},
{
kind: 'block',
type: 'logic_boolean',
},
{
kind: 'block',
type: 'logic_null',
},
{
kind: 'block',
type: 'logic_ternary',
},
],
},
{
kind: 'category',
name: 'Loops',
categorystyle: 'loop_category',
contents: [
{
kind: 'block',
type: 'controls_repeat_ext',
inputs: {
TIMES: {
shadow: {
type: 'math_number',
fields: {
NUM: 10,
},
},
},
},
},
{
kind: 'block',
type: 'controls_whileUntil',
},
{
kind: 'block',
type: 'controls_for',
inputs: {
FROM: {
shadow: {
type: 'math_number',
fields: {
NUM: 1,
},
},
},
TO: {
shadow: {
type: 'math_number',
fields: {
NUM: 10,
},
},
},
BY: {
shadow: {
type: 'math_number',
fields: {
NUM: 1,
},
},
},
},
},
{
kind: 'block',
type: 'controls_forEach',
},
{
kind: 'block',
type: 'controls_flow_statements',
},
],
},
{
kind: 'category',
name: 'Math',
categorystyle: 'math_category',
contents: [
{
kind: 'block',
type: 'math_number',
fields: {
NUM: 123,
},
},
{
kind: 'block',
type: 'math_arithmetic',
inputs: {
A: {
shadow: {
type: 'math_number',
fields: {
NUM: 1,
},
},
},
B: {
shadow: {
type: 'math_number',
fields: {
NUM: 1,
},
},
},
},
},
{
kind: 'block',
type: 'math_single',
inputs: {
NUM: {
shadow: {
type: 'math_number',
fields: {
NUM: 9,
},
},
},
},
},
{
kind: 'block',
type: 'math_trig',
inputs: {
NUM: {
shadow: {
type: 'math_number',
fields: {
NUM: 45,
},
},
},
},
},
{
kind: 'block',
type: 'math_constant',
},
{
kind: 'block',
type: 'math_number_property',
inputs: {
NUMBER_TO_CHECK: {
shadow: {
type: 'math_number',
fields: {
NUM: 0,
},
},
},
},
},
{
kind: 'block',
type: 'math_round',
fields: {
OP: 'ROUND',
},
inputs: {
NUM: {
shadow: {
type: 'math_number',
fields: {
NUM: 3.1,
},
},
},
},
},
{
kind: 'block',
type: 'math_on_list',
fields: {
OP: 'SUM',
},
},
{
kind: 'block',
type: 'math_modulo',
inputs: {
DIVIDEND: {
shadow: {
type: 'math_number',
fields: {
NUM: 64,
},
},
},
DIVISOR: {
shadow: {
type: 'math_number',
fields: {
NUM: 10,
},
},
},
},
},
{
kind: 'block',
type: 'math_constrain',
inputs: {
VALUE: {
shadow: {
type: 'math_number',
fields: {
NUM: 50,
},
},
},
LOW: {
shadow: {
type: 'math_number',
fields: {
NUM: 1,
},
},
},
HIGH: {
shadow: {
type: 'math_number',
fields: {
NUM: 100,
},
},
},
},
},
{
kind: 'block',
type: 'math_random_int',
inputs: {
FROM: {
shadow: {
type: 'math_number',
fields: {
NUM: 1,
},
},
},
TO: {
shadow: {
type: 'math_number',
fields: {
NUM: 100,
},
},
},
},
},
{
kind: 'block',
type: 'math_random_float',
},
{
kind: 'block',
type: 'math_atan2',
inputs: {
X: {
shadow: {
type: 'math_number',
fields: {
NUM: 1,
},
},
},
Y: {
shadow: {
type: 'math_number',
fields: {
NUM: 1,
},
},
},
},
},
],
},
{
kind: 'category',
name: 'Text',
categorystyle: 'text_category',
contents: [
{
kind: 'block',
type: 'text',
},
{
kind: 'block',
type: 'text_join',
},
{
kind: 'block',
type: 'text_append',
inputs: {
TEXT: {
shadow: {
type: 'text',
fields: {
TEXT: '',
},
},
},
},
},
{
kind: 'block',
type: 'text_length',
inputs: {
VALUE: {
shadow: {
type: 'text',
fields: {
TEXT: 'abc',
},
},
},
},
},
{
kind: 'block',
type: 'text_isEmpty',
inputs: {
VALUE: {
shadow: {
type: 'text',
fields: {
TEXT: '',
},
},
},
},
},
{
kind: 'block',
type: 'text_indexOf',
inputs: {
VALUE: {
block: {
type: 'variables_get',
},
},
FIND: {
shadow: {
type: 'text',
fields: {
TEXT: 'abc',
},
},
},
},
},
{
kind: 'block',
type: 'text_charAt',
inputs: {
VALUE: {
block: {
type: 'variables_get',
},
},
},
},
{
kind: 'block',
type: 'text_getSubstring',
inputs: {
STRING: {
block: {
type: 'variables_get',
},
},
},
},
{
kind: 'block',
type: 'text_changeCase',
inputs: {
TEXT: {
shadow: {
type: 'text',
fields: {
TEXT: 'abc',
},
},
},
},
},
{
kind: 'block',
type: 'text_trim',
inputs: {
TEXT: {
shadow: {
type: 'text',
fields: {
TEXT: 'abc',
},
},
},
},
},
{
kind: 'block',
type: 'text_count',
inputs: {
SUB: {
shadow: {
type: 'text',
},
},
TEXT: {
shadow: {
type: 'text',
},
},
},
},
{
kind: 'block',
type: 'text_replace',
inputs: {
FROM: {
shadow: {
type: 'text',
},
},
TO: {
shadow: {
type: 'text',
},
},
TEXT: {
shadow: {
type: 'text',
},
},
},
},
{
kind: 'block',
type: 'text_reverse',
inputs: {
TEXT: {
shadow: {
type: 'text',
},
},
},
},
{
kind: 'block',
type: 'add_text',
inputs: {
TEXT: {
shadow: {
type: 'text',
fields: {
TEXT: 'abc',
},
},
},
},
},
],
},
{
kind: 'category',
name: 'Lists',
categorystyle: 'list_category',
contents: [
{
kind: 'block',
type: 'lists_create_with',
},
{
kind: 'block',
type: 'lists_create_with',
},
{
kind: 'block',
type: 'lists_repeat',
inputs: {
NUM: {
shadow: {
type: 'math_number',
fields: {
NUM: 5,
},
},
},
},
},
{
kind: 'block',
type: 'lists_length',
},
{
kind: 'block',
type: 'lists_isEmpty',
},
{
kind: 'block',
type: 'lists_indexOf',
inputs: {
VALUE: {
block: {
type: 'variables_get',
},
},
},
},
{
kind: 'block',
type: 'lists_getIndex',
inputs: {
VALUE: {
block: {
type: 'variables_get',
},
},
},
},
{
kind: 'block',
type: 'lists_setIndex',
inputs: {
LIST: {
block: {
type: 'variables_get',
},
},
},
},
{
kind: 'block',
type: 'lists_getSublist',
inputs: {
LIST: {
block: {
type: 'variables_get',
},
},
},
},
{
kind: 'block',
type: 'lists_split',
inputs: {
DELIM: {
shadow: {
type: 'text',
fields: {
TEXT: ',',
},
},
},
},
},
{
kind: 'block',
type: 'lists_sort',
},
{
kind: 'block',
type: 'lists_reverse',
},
],
},
{
kind: 'sep',
},
{
kind: 'category',
name: 'Variables',
categorystyle: 'variable_category',
custom: 'VARIABLE',
},
{
kind: 'category',
name: 'Functions',
categorystyle: 'procedure_category',
custom: 'PROCEDURE',
},
],
};
+72
View File
@@ -0,0 +1,72 @@
export const category = {
kind: 'category',
name: 'Minecraft',
// categorystyle: 'minecraft_category',
colour: "#e08080",
contents: [
{
kind: 'block',
type: 'minecraft_command',
},
{
kind: 'block',
type: 'minecraft_echo',
},
{
kind: 'block',
type: 'minecraft_scheduler_run_later',
},
{
kind: 'block',
type: 'minecraft_block_set_cube',
},
{
kind: 'block',
type: 'minecraft_block_set_cylinder',
},
{
kind: 'block',
type: 'minecraft_block_set',
},
{
kind: 'block',
type: 'minecraft_block_get',
},
{
kind: 'block',
type: 'minecraft_block_power',
},
{
kind: 'block',
type: 'minecraft_xyz_location',
},
{
kind: 'block',
type: 'minecraft_player_location',
},
{
kind: 'block',
type: 'minecraft_player_look_at_location',
},
{
kind: 'block',
type: 'minecraft_named_material',
},
{
kind: 'block',
type: 'minecraft_location_move',
},
{
kind: 'block',
type: 'minecraft_location_move_forward',
},
{
kind: 'block',
type: 'minecraft_location_look',
},
{
kind: 'block',
type: 'minecraft_location_turn',
},
],
};
+13
View File
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"baseUrl": "./",
"outDir": "dist",
"declaration": true,
"declarationMap": true,
"module": "ES2015",
"moduleResolution": "node",
"target": "ES2015",
"strict": true,
"allowSyntheticDefaultImports": true
}
}
+73
View File
@@ -0,0 +1,73 @@
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
// Base config that applies to either development or production mode.
const config = {
entry: './src/index.ts',
watch: true,
watchOptions: {
aggregateTimeout: 200,
poll: 1000,
ignored: '**/node_modules',
},
output: {
// Compile the source files into a bundle.
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
clean: true,
},
// Enable webpack-dev-server to get hot refresh of the app.
devServer: {
static: './build',
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
// Load CSS files. They can be imported into JS files.
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
plugins: [
// Generate the HTML index page based on our template.
// This will output the same index page with the bundle we
// created above added in a script tag.
new HtmlWebpackPlugin({
template: 'src/index.html',
}),
],
};
module.exports = (env, argv) => {
if (argv.mode === 'development') {
// Set the output path to the `build` directory
// so we don't clobber production builds.
config.output.path = path.resolve(__dirname, 'build');
// Generate source maps for our code for easier debugging.
// Not suitable for production builds. If you want source maps in
// production, choose a different one from https://webpack.js.org/configuration/devtool
config.devtool = 'eval-cheap-module-source-map';
// Include the source maps for Blockly for easier debugging Blockly code.
config.module.rules.push({
test: /(blockly\/.*\.js)$/,
use: [require.resolve('source-map-loader')],
enforce: 'pre',
});
// Ignore spurious warnings from source-map-loader
// It can't find source maps for some Closure modules and that is expected
config.ignoreWarnings = [/Failed to parse source map/];
}
return config;
};