From 3bba78fc9d52352c19ad7736414ed1d62bfc4715 Mon Sep 17 00:00:00 2001 From: dzx-dzx Date: Fri, 31 Jan 2020 21:29:33 +0800 Subject: [PATCH] Adding functionality --- packs/behaviors/scripts/client/client.js | 43 +++++++++-- packs/behaviors/scripts/server/server.js | 94 ++++++++++-------------- 2 files changed, 74 insertions(+), 63 deletions(-) diff --git a/packs/behaviors/scripts/client/client.js b/packs/behaviors/scripts/client/client.js index 19736cc..681f9ed 100644 --- a/packs/behaviors/scripts/client/client.js +++ b/packs/behaviors/scripts/client/client.js @@ -96,7 +96,7 @@ clientSystem.initialize = function () { if (playerID == eventData.data.playerID) { displayObject(eventData.data.position) if (generatorArray[generatorIndex].positionArray.length >= generatorArray[generatorIndex].positionArrayLengthRequired) { - displayChat("Too many positions.New one is ignored") + displayChat("Too many positions!New one is ignored") } else { generatorArray[generatorIndex].positionArray.push(eventData.data.position) @@ -108,25 +108,56 @@ clientSystem.initialize = function () { if (playerID == eventData.data.playerID) { displayObject(eventData.data.blockType) if (generatorArray[generatorIndex].blockTypeArray.length >= generatorArray[generatorIndex].blockTypeArrayLengthRequired) { - displayChat("Too many blocktypes.New one is ignored") + displayChat("Too many blockTypes!New one is ignored") } else { generatorArray[generatorIndex].blockTypeArray.push(eventData.data.blockType) } } }) + clientSystem.listenForEvent("NormaConstructor:command", (eventData) => { + if (playerID == eventData.data.playerID) { + switch (eventData.data.command) { + case "removeLastPosition": { + displayChat("Removing the last position...") + generatorArray[generatorIndex].positionArray.pop() + displayChat("Current positionArray:") + displayObject(generatorArray[generatorIndex].positionArray) + break; + } + case "removeLastblockType": { + displayChat("Removing the last blockType...") + generatorArray[generatorIndex].blockTypeArray.pop() + displayChat("Current blockTypeArray:") + displayObject(generatorArray[generatorIndex].blockTypeArray) + break; + } + case "chooseNextGenerator": { + displayChat("Choosing next generator...") + generatorIndex = (generatorIndex + 1) % generatorArray.length + displayChat("Current generator:") + displayObject(generatorArray[generatorIndex]) + break; + } + case "showSavedData": { + displayChat("Current positionArray:") + displayObject(generatorArray[generatorIndex].positionArray) + displayChat("Current blockTypeArray:") + displayObject(generatorArray[generatorIndex].blockTypeArray) + break; + } + } + } + }) clientSystem.listenForEvent("NormaConstructor:ExecutionRequest", (eventData) => { if (playerID == eventData.data.playerID) { if (generatorArray[generatorIndex].blockTypeArrayLengthRequired > generatorArray[generatorIndex].blockTypeArray.length) - displayChat("Too few blocktypes!Refusing to execute.") + displayChat("Too few blockTypes!Refusing to execute.") else if (generatorArray[generatorIndex].positionArrayLengthRequired > generatorArray[generatorIndex].positionArray.length) displayChat("Too few positions!Refusing to execute.") else { displayChat("Execution started.") - displayObject(generatorArray[generatorIndex].blockTypeArray) - displayObject(generatorArray[generatorIndex].positionArray) let blockArray = generatorArray[0].generator() - //displayObject(blockArray) let executionResponseEventData = clientSystem.createEventData("NormaConstructor:ExecutionResponse") executionResponseEventData.data.blockArray = blockArray clientSystem.broadcastEvent("NormaConstructor:ExecutionResponse", executionResponseEventData) diff --git a/packs/behaviors/scripts/server/server.js b/packs/behaviors/scripts/server/server.js index 8d3af18..adf1688 100644 --- a/packs/behaviors/scripts/server/server.js +++ b/packs/behaviors/scripts/server/server.js @@ -39,6 +39,7 @@ serverSystem.initialize = function () { playerID: undefined } ) + serverSystem.registerEventData("NormaConstructor:command", { command: undefined, playerID: undefined }) serverSystem.registerEventData("NormaConstructor:ExecutionRequest", { playerID: undefined }) serverSystem.listenForEvent("minecraft:player_placed_block", (eventData) => { @@ -64,62 +65,9 @@ serverSystem.initialize = function () { serverSystem.broadcastEvent("NormaConstructor:getBlockType", getblockTypeEventData) }) - serverSystem.listenForEvent("minecraft:entity_created", (eventData) => { - //displayChat(JSON.stringify(eventData,null,' ')) - var entity = eventData.data.entity; - if (entity.__identifier__ === "NormaConstructor:select") { - var position = serverSystem.getComponent(entity, "minecraft:position").data; - - displayChat(`\nSelecting position:\nx:${position.x}\ny:${position.y}\nz:${position.z}`); - - positionArray.push(position); - if (positionArray.length >= 3) { - displayChat("\nWarning:Positions exceeded.The first position is ignored."); - positionArray.shift(); - } - //serverSystem.destroyEntity(entity); - } - else if (entity.__identifier__ === "NormaConstructor:execute") { - displayChat("§b NZ is JULAO!") - - //displayChat(`/fill ${positionArray[0].x} ${positionArray[0].y} ${positionArray[0].z} ${positionArray[1].x} ${positionArray[1].y} ${positionArray[1].z} ${block.__identifier__.slice("minecraft:".length)}`); - //serverSystem.executeCommand(`/fill ${positionArray[0].x} ${positionArray[0].y} ${positionArray[0].z} ${positionArray[1].x} ${positionArray[1].y} ${positionArray[1].z} ${block.__identifier__.slice("minecraft:".length)}`, (commandResultData) => { ; }); - var minPosition = { - x: Math.min(positionArray[0].x, positionArray[1].x), - y: Math.min(positionArray[0].y, positionArray[1].y), - z: Math.min(positionArray[0].z, positionArray[1].z), - } - var maxPosition = { - x: Math.max(positionArray[0].x, positionArray[1].x), - y: Math.max(positionArray[0].y, positionArray[1].y), - z: Math.max(positionArray[0].z, positionArray[1].z) - } - - displayChat(minPosition.x) - displayChat(minPosition.y) - displayChat(minPosition.z) - displayChat(maxPosition.x) - displayChat(maxPosition.y) - displayChat(maxPosition.z) - - for (var x = minPosition.x; x <= maxPosition.x; x++) { - for (var y = minPosition.y; y <= maxPosition.y; y++) { - for (var z = minPosition.z; z <= maxPosition.z; z++) { - displayChat("Position:") - displayChat(x) - displayChat(y) - displayChat(z) - generate(x, y, z) - } - } - } - - - //serverSystem.destroyEntity(entity); - } - }) serverSystem.listenForEvent("minecraft:block_interacted_with", (eventData) => { + let playerID = eventData.data.player.id //TODO:Verify whether the player is permitted to use this addon. let handContainer = serverSystem.getComponent(eventData.data.player, "minecraft:hand_container").data let mainHandItem = handContainer[0].__identifier__ @@ -136,15 +84,47 @@ serverSystem.initialize = function () { let getPositionEventData = serverSystem.createEventData("NormaConstructor:getPosition") getPositionEventData.data.position = position - getPositionEventData.data.playerID = eventData.data.player.id + getPositionEventData.data.playerID = playerID serverSystem.broadcastEvent("NormaConstructor:getPosition", getPositionEventData) break; } - case "minecraft:diamond_axe": { + case "minecraft:stone_axe": { + //Remove the last position. + let commandEventData = serverSystem.createEventData("NormaConstructor:command") + commandEventData.data.command = "removeLastPosition" + commandEventData.data.playerID = playerID + serverSystem.broadcastEvent("NormaConstructor:command", commandEventData) + break; + } + case "minecraft:iron_axe": { + //Remove the last blockType. + let commandEventData = serverSystem.createEventData("NormaConstructor:command") + commandEventData.data.command = "removeLastblockType" + commandEventData.data.playerID = playerID + serverSystem.broadcastEvent("NormaConstructor:command", commandEventData) + break; + } + case "minecraft:golden_axe": { + //Choose the next generator. + let commandEventData = serverSystem.createEventData("NormaConstructor:command") + commandEventData.data.command = "chooseNextGenerator" + commandEventData.data.playerID = playerID + serverSystem.broadcastEvent("NormaConstructor:command", commandEventData) + break; + } + case "minecraft:diamond_axe":{ + //Show currently saved data. + let commandEventData = serverSystem.createEventData("NormaConstructor:command") + commandEventData.data.command = "showSavedData" + commandEventData.data.playerID = playerID + serverSystem.broadcastEvent("NormaConstructor:command", commandEventData) + break; + } + case "minecraft:stick": { //Execute. let executeRequestEventData = serverSystem.createEventData("NormaConstructor:ExecutionRequest") - executeRequestEventData.data.playerID = eventData.data.player.id + executeRequestEventData.data.playerID = playerID serverSystem.broadcastEvent("NormaConstructor:ExecutionRequest", executeRequestEventData) break; }