diff --git a/Construct[BP]/scripts/options/easyPlace.js b/Construct[BP]/scripts/options/easyPlace.js index ff7a298..1636781 100644 --- a/Construct[BP]/scripts/options/easyPlace.js +++ b/Construct[BP]/scripts/options/easyPlace.js @@ -21,7 +21,8 @@ function giveActionItem(playerId) { const itemStack = new ItemStack('construct:easy_place'); if (!container.contains(itemStack)) { const remainingItemStack = container.addItem(itemStack); - player.dimension.spawnItem(remainingItemStack, player.location); + if (remainingItemStack) + player.dimension.spawnItem(remainingItemStack, player.location); } } diff --git a/Construct[BP]/scripts/options/fastEasyPlace.js b/Construct[BP]/scripts/options/fastEasyPlace.js index 1185ee6..47d1ead 100644 --- a/Construct[BP]/scripts/options/fastEasyPlace.js +++ b/Construct[BP]/scripts/options/fastEasyPlace.js @@ -7,7 +7,6 @@ import { Builders } from '../classes/Builder/Builders'; const PROCESS_INTERVAL = 2; -let runner = void 0; const builderOption = new BuilderOption({ identifier: 'fastEasyPlace', displayName: 'Fast Easy Place', @@ -23,7 +22,8 @@ function giveActionItem(playerId) { const itemStack = new ItemStack('construct:easy_place'); if (!container.contains(itemStack)) { const remainingItemStack = container.addItem(itemStack); - player.dimension.spawnItem(remainingItemStack, player.location); + if (remainingItemStack) + player.dimension.spawnItem(remainingItemStack, player.location); } } @@ -46,22 +46,20 @@ function removeActionItem(playerId) { } system.runInterval(onTick, PROCESS_INTERVAL); +world.beforeEvents.playerInteractWithBlock.subscribe(onPlayerInteractWithBlock); function onTick() { for (const player of world.getAllPlayers()) { - if (player && builderOption.isEnabled(player.id) && player.inputInfo.lastInputModeUsed === InputMode.Touch) + if (player && builderOption.isEnabled(player.id)) processEasyPlace(player); } } -world.beforeEvents.itemUse.subscribe(event => { - const player = event.source; - if (player && builderOption.isEnabled(event.source?.id) && player.inputInfo.lastInputModeUsed !== InputMode.Touch) { - system.run(() => { - processEasyPlace(event.source); - }); - } -}); +function onPlayerInteractWithBlock(event) { + const { player, block, isFirstEvent } = event; + if (!player || !isFirstEvent || !block || !builderOption.isEnabled(player.id) || !isHoldingActionItem(player)) return; + preventAction(event, player); +} function processEasyPlace(player) { if (!player || !isHoldingActionItem(player)) return; @@ -72,6 +70,13 @@ function processEasyPlace(player) { tryPlaceBlock(player, worldBlock, structureBlock.permutation); } +function preventAction(event, player) { + event.cancel = true; + system.run(() => { + player.onScreenDisplay.setActionBar('§cAction prevented by Easy Place.'); + }); +} + function isHoldingActionItem(player) { const mainhandItemStack = player.getComponent(EntityComponentTypes.Equippable).getEquipment(EquipmentSlot.Mainhand); if (!mainhandItemStack) @@ -80,17 +85,17 @@ function isHoldingActionItem(player) { } function tryPlaceBlock(player, worldBlock, structureBlock) { - if (isBannedBlock(player, structureBlock) || !locationIsPlaceable(player, worldBlock)) return; + if (isBannedBlock(player, structureBlock) || !locationIsPlaceable(worldBlock)) return; structureBlock = tryConvertBannedToValidBlock(structureBlock); if (player.getGameMode() === GameMode.creative) { - placeBlock(worldBlock, structureBlock); + placeBlock(player, worldBlock, structureBlock); } else if (player.getGameMode() === GameMode.survival) { structureBlock = tryConvertToDefaultState(structureBlock); tryPlaceBlockSurvival(player, worldBlock, structureBlock); } } -function locationIsPlaceable(player, worldBlock) { +function locationIsPlaceable(worldBlock) { return worldBlock.isAir; } @@ -132,10 +137,9 @@ function tryConvertToDefaultState(structureBlock) { function tryPlaceBlockSurvival(player, block, structureBlock) { const placeableItemStack = getPlaceableItemStack(structureBlock); - // console.warn(`Looking for item to place ${structureBlock?.type.id} (${placeableItemStack?.typeId})...`); const itemSlotToUse = fetchMatchingItemSlot(player, placeableItemStack?.typeId); if (itemSlotToUse) { - placeBlock(block, structureBlock, itemSlotToUse); + placeBlock(player, block, structureBlock, itemSlotToUse); } } @@ -158,11 +162,10 @@ function fetchMatchingItemSlot(player, itemToMatchId) { } } -function placeBlock(block, structureBlock, itemSlot) { +function placeBlock(player, block, structureBlock, itemSlot) { system.run(() => { - if (itemSlot) { + if (itemSlot) consumeItem(itemSlot); - } block.setPermutation(structureBlock); }); } diff --git a/Construct[BP]/scripts/options/materialGrabber.js b/Construct[BP]/scripts/options/materialGrabber.js index 051d739..e0652c2 100644 --- a/Construct[BP]/scripts/options/materialGrabber.js +++ b/Construct[BP]/scripts/options/materialGrabber.js @@ -19,7 +19,8 @@ function giveActionItem(playerId) { const itemStack = new ItemStack('construct:material_grabber'); if (!container.contains(itemStack)) { const remainingItemStack = container.addItem(itemStack); - player.dimension.spawnItem(remainingItemStack, player.location); + if (remainingItemStack) + player.dimension.spawnItem(remainingItemStack, player.location); } }