Merge branch 'tool-items'

This commit is contained in:
ForestOfLight
2025-06-07 23:45:18 -07:00
Unverified
3 changed files with 35 additions and 24 deletions
+5 -2
View File
@@ -19,8 +19,11 @@ function giveActionItem(playerId) {
const player = world.getEntity(playerId); const player = world.getEntity(playerId);
const container = player.getComponent(EntityComponentTypes.Inventory)?.container; const container = player.getComponent(EntityComponentTypes.Inventory)?.container;
const itemStack = new ItemStack('construct:easy_place'); const itemStack = new ItemStack('construct:easy_place');
if (!container.contains(itemStack)) if (!container.contains(itemStack)) {
container.addItem(itemStack); const remainingItemStack = container.addItem(itemStack);
if (remainingItemStack)
player.dimension.spawnItem(remainingItemStack, player.location);
}
} }
function removeActionItem(playerId) { function removeActionItem(playerId) {
+24 -19
View File
@@ -7,7 +7,6 @@ import { Builders } from '../classes/Builder/Builders';
const PROCESS_INTERVAL = 2; const PROCESS_INTERVAL = 2;
let runner = void 0;
const builderOption = new BuilderOption({ const builderOption = new BuilderOption({
identifier: 'fastEasyPlace', identifier: 'fastEasyPlace',
displayName: 'Fast Easy Place', displayName: 'Fast Easy Place',
@@ -21,8 +20,11 @@ function giveActionItem(playerId) {
const player = world.getEntity(playerId); const player = world.getEntity(playerId);
const container = player.getComponent(EntityComponentTypes.Inventory)?.container; const container = player.getComponent(EntityComponentTypes.Inventory)?.container;
const itemStack = new ItemStack('construct:easy_place'); const itemStack = new ItemStack('construct:easy_place');
if (!container.contains(itemStack)) if (!container.contains(itemStack)) {
container.addItem(itemStack); const remainingItemStack = container.addItem(itemStack);
if (remainingItemStack)
player.dimension.spawnItem(remainingItemStack, player.location);
}
} }
function removeActionItem(playerId) { function removeActionItem(playerId) {
@@ -44,22 +46,20 @@ function removeActionItem(playerId) {
} }
system.runInterval(onTick, PROCESS_INTERVAL); system.runInterval(onTick, PROCESS_INTERVAL);
world.beforeEvents.playerInteractWithBlock.subscribe(onPlayerInteractWithBlock);
function onTick() { function onTick() {
for (const player of world.getAllPlayers()) { 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); processEasyPlace(player);
} }
} }
world.beforeEvents.itemUse.subscribe(event => { function onPlayerInteractWithBlock(event) {
const player = event.source; const { player, block, isFirstEvent } = event;
if (player && builderOption.isEnabled(event.source?.id) && player.inputInfo.lastInputModeUsed !== InputMode.Touch) { if (!player || !isFirstEvent || !block || !builderOption.isEnabled(player.id) || !isHoldingActionItem(player)) return;
system.run(() => { preventAction(event, player);
processEasyPlace(event.source);
});
} }
});
function processEasyPlace(player) { function processEasyPlace(player) {
if (!player || !isHoldingActionItem(player)) return; if (!player || !isHoldingActionItem(player)) return;
@@ -70,6 +70,13 @@ function processEasyPlace(player) {
tryPlaceBlock(player, worldBlock, structureBlock.permutation); 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) { function isHoldingActionItem(player) {
const mainhandItemStack = player.getComponent(EntityComponentTypes.Equippable).getEquipment(EquipmentSlot.Mainhand); const mainhandItemStack = player.getComponent(EntityComponentTypes.Equippable).getEquipment(EquipmentSlot.Mainhand);
if (!mainhandItemStack) if (!mainhandItemStack)
@@ -78,17 +85,17 @@ function isHoldingActionItem(player) {
} }
function tryPlaceBlock(player, worldBlock, structureBlock) { function tryPlaceBlock(player, worldBlock, structureBlock) {
if (isBannedBlock(player, structureBlock) || !locationIsPlaceable(player, worldBlock)) return; if (isBannedBlock(player, structureBlock) || !locationIsPlaceable(worldBlock)) return;
structureBlock = tryConvertBannedToValidBlock(structureBlock); structureBlock = tryConvertBannedToValidBlock(structureBlock);
if (player.getGameMode() === GameMode.creative) { if (player.getGameMode() === GameMode.creative) {
placeBlock(worldBlock, structureBlock); placeBlock(player, worldBlock, structureBlock);
} else if (player.getGameMode() === GameMode.survival) { } else if (player.getGameMode() === GameMode.survival) {
structureBlock = tryConvertToDefaultState(structureBlock); structureBlock = tryConvertToDefaultState(structureBlock);
tryPlaceBlockSurvival(player, worldBlock, structureBlock); tryPlaceBlockSurvival(player, worldBlock, structureBlock);
} }
} }
function locationIsPlaceable(player, worldBlock) { function locationIsPlaceable(worldBlock) {
return worldBlock.isAir; return worldBlock.isAir;
} }
@@ -130,10 +137,9 @@ function tryConvertToDefaultState(structureBlock) {
function tryPlaceBlockSurvival(player, block, structureBlock) { function tryPlaceBlockSurvival(player, block, structureBlock) {
const placeableItemStack = getPlaceableItemStack(structureBlock); const placeableItemStack = getPlaceableItemStack(structureBlock);
// console.warn(`Looking for item to place ${structureBlock?.type.id} (${placeableItemStack?.typeId})...`);
const itemSlotToUse = fetchMatchingItemSlot(player, placeableItemStack?.typeId); const itemSlotToUse = fetchMatchingItemSlot(player, placeableItemStack?.typeId);
if (itemSlotToUse) { if (itemSlotToUse) {
placeBlock(block, structureBlock, itemSlotToUse); placeBlock(player, block, structureBlock, itemSlotToUse);
} }
} }
@@ -156,11 +162,10 @@ function fetchMatchingItemSlot(player, itemToMatchId) {
} }
} }
function placeBlock(block, structureBlock, itemSlot) { function placeBlock(player, block, structureBlock, itemSlot) {
system.run(() => { system.run(() => {
if (itemSlot) { if (itemSlot)
consumeItem(itemSlot); consumeItem(itemSlot);
}
block.setPermutation(structureBlock); block.setPermutation(structureBlock);
}); });
} }
@@ -17,8 +17,11 @@ function giveActionItem(playerId) {
const player = world.getEntity(playerId); const player = world.getEntity(playerId);
const container = player.getComponent(EntityComponentTypes.Inventory)?.container; const container = player.getComponent(EntityComponentTypes.Inventory)?.container;
const itemStack = new ItemStack('construct:material_grabber'); const itemStack = new ItemStack('construct:material_grabber');
if (!container.contains(itemStack)) if (!container.contains(itemStack)) {
container.addItem(itemStack); const remainingItemStack = container.addItem(itemStack);
if (remainingItemStack)
player.dimension.spawnItem(remainingItemStack, player.location);
}
} }
function removeActionItem(playerId) { function removeActionItem(playerId) {