refactor BlockInfo & change action item to paper

This commit is contained in:
ForestOfLight
2025-04-12 14:31:30 -07:00
Unverified
parent fcc47a78a4
commit 0c90e13e2c
3 changed files with 31 additions and 27 deletions
+10 -6
View File
@@ -1,24 +1,28 @@
import { system, world } from '@minecraft/server'; import { system, world } from '@minecraft/server';
import { Raycaster } from '../classes/Raycaster'; import { Raycaster } from '../classes/Raycaster';
system.runInterval(() => { class BlockInfo {
static onTick() {
for (const player of world.getAllPlayers()) { for (const player of world.getAllPlayers()) {
if (!player) if (!player)
continue; continue;
showStructureBlockInfo(player); this.showStructureBlockInfo(player);
}
} }
});
function showStructureBlockInfo(player) { static showStructureBlockInfo(player) {
const block = Raycaster.getTargetedStructureBlock(player, { isFirst: true, collideWithWorldBlocks: true }); const block = Raycaster.getTargetedStructureBlock(player, { isFirst: true, collideWithWorldBlocks: true });
if (!block) if (!block)
return; return;
player.onScreenDisplay.setActionBar({ text: getFormattedBlockInfo(block.permutation) }); player.onScreenDisplay.setActionBar({ text: this.getFormattedBlockInfo(block.permutation) });
} }
function getFormattedBlockInfo(block) { static getFormattedBlockInfo(block) {
const states = block.getAllStates(); const states = block.getAllStates();
if (Object.keys(states).length === 0) if (Object.keys(states).length === 0)
return `Structure:\n§a${block.type.id}`; return `Structure:\n§a${block.type.id}`;
return `Structure:\n§a${block.type.id}\n§7${JSON.stringify(block.getAllStates())}`; return `Structure:\n§a${block.type.id}\n§7${JSON.stringify(block.getAllStates())}`;
} }
}
system.runInterval(() => BlockInfo.onTick());
+6 -6
View File
@@ -5,11 +5,11 @@ import { structureCollection } from '../classes/StructureCollection';
import { bannedBlocks, bannedToValidBlockMap, whitelistedBlockStates, resetToBlockStates, bannedDimensionBlocks, specialItemPlacementConversions, import { bannedBlocks, bannedToValidBlockMap, whitelistedBlockStates, resetToBlockStates, bannedDimensionBlocks, specialItemPlacementConversions,
blockIdToItemStackMap } from '../data'; blockIdToItemStackMap } from '../data';
const ARROW_SLOT = 35; const ACTION_SLOT = 35;
const easyPlace = new Rule({ const easyPlace = new Rule({
identifier: 'easyPlace', identifier: 'easyPlace',
description: { text: 'Simplifies placing blocks in a structure (arrow in bottom right inventory slot).' }, description: { text: 'Simplifies placing blocks in a structure (paper in bottom right inventory slot).' },
onEnableCallback: () => { world.beforeEvents.playerPlaceBlock.subscribe(onPlayerPlaceBlock); }, onEnableCallback: () => { world.beforeEvents.playerPlaceBlock.subscribe(onPlayerPlaceBlock); },
onDisableCallback: () => { world.beforeEvents.playerPlaceBlock.unsubscribe(onPlayerPlaceBlock); } onDisableCallback: () => { world.beforeEvents.playerPlaceBlock.unsubscribe(onPlayerPlaceBlock); }
}) })
@@ -17,19 +17,19 @@ extension.addRule(easyPlace);
function onPlayerPlaceBlock(event) { function onPlayerPlaceBlock(event) {
const { player, block } = event; const { player, block } = event;
if (!player || !block || !hasArrowInCorrectSlot(player)) return; if (!player || !block || !hasActionItemInCorrectSlot(player)) return;
const structureBlock = fetchStructureBlock(block.location); const structureBlock = fetchStructureBlock(block.location);
if (!structureBlock) if (!structureBlock)
return; return;
tryPlaceBlock(event, player, block, structureBlock); tryPlaceBlock(event, player, block, structureBlock);
} }
function hasArrowInCorrectSlot(player) { function hasActionItemInCorrectSlot(player) {
const inventory = player.getComponent(EntityComponentTypes.Inventory)?.container; const inventory = player.getComponent(EntityComponentTypes.Inventory)?.container;
if (!inventory) if (!inventory)
return false; return false;
const arrowSlot = inventory.getSlot(ARROW_SLOT); const actionSlot = inventory.getSlot(ACTION_SLOT);
return arrowSlot.hasItem() && arrowSlot.typeId === 'minecraft:arrow'; return actionSlot.hasItem() && actionSlot.typeId === 'minecraft:paper';
} }
function fetchStructureBlock(location) { function fetchStructureBlock(location) {
+4 -4
View File
@@ -8,7 +8,7 @@ import { Raycaster } from '../classes/Raycaster';
let runner = void 0; let runner = void 0;
const easyPlace = new Rule({ const easyPlace = new Rule({
identifier: 'fastEasyPlace', identifier: 'fastEasyPlace',
description: { text: 'Looking at structure blocks with an arrow in your hand will place them.' }, description: { text: 'Looking at structure blocks with paper in your hand will place them.' },
onEnableCallback: () => { runner = system.runInterval(onTick, 2); }, onEnableCallback: () => { runner = system.runInterval(onTick, 2); },
onDisableCallback: () => { system.clearRun(runner); } onDisableCallback: () => { system.clearRun(runner); }
}) })
@@ -23,7 +23,7 @@ function onTick() {
} }
function processEasyPlace(player) { function processEasyPlace(player) {
if (!player || !isHoldingArrow(player)) return; if (!player || !isHoldingActionItem(player)) return;
const structureBlock = Raycaster.getTargetedStructureBlock(player, { isFirst: true }); const structureBlock = Raycaster.getTargetedStructureBlock(player, { isFirst: true });
if (!structureBlock) if (!structureBlock)
return; return;
@@ -31,11 +31,11 @@ function processEasyPlace(player) {
tryPlaceBlock(player, worldBlock, structureBlock.permutation); tryPlaceBlock(player, worldBlock, structureBlock.permutation);
} }
function isHoldingArrow(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)
return false; return false;
return mainhandItemStack.typeId === 'minecraft:arrow'; return mainhandItemStack.typeId === 'minecraft:paper';
} }
function tryPlaceBlock(player, worldBlock, structureBlock) { function tryPlaceBlock(player, worldBlock, structureBlock) {