functional on 1.21.90
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { system, EntityComponentTypes } from '@minecraft/server';
|
||||
import { FormCancelationReason } from '@minecraft/server-ui';
|
||||
import { specialItemPlacementConversions } from './data';
|
||||
import { blocks } from './blocks';
|
||||
|
||||
export async function forceShow(player, form, timeout = Infinity) {
|
||||
const startTick = system.currentTick;
|
||||
@@ -24,4 +26,36 @@ export function fetchMatchingItemSlot(entity, itemToMatchId) {
|
||||
if (itemSlot.hasItem() && itemSlot?.typeId === itemToMatchId)
|
||||
return itemSlot;
|
||||
}
|
||||
}
|
||||
|
||||
export function placeBlock(player, placedBlock, blockToPlace, itemSlotToConsume = void 0) {
|
||||
system.run(() => {
|
||||
if (itemSlotToConsume)
|
||||
consumeItem(itemSlotToConsume);
|
||||
placedBlock.setPermutation(blockToPlace);
|
||||
playBlockPlacementSound(player, placedBlock, blockToPlace);
|
||||
});
|
||||
}
|
||||
|
||||
function consumeItem(itemSlot) {
|
||||
if (specialItemPlacementConversions[itemSlot.typeId.replace('minecraft:', '')]) {
|
||||
itemSlot.setItem(new ItemStack(specialItemPlacementConversions[itemSlot.typeId.replace('minecraft:', '')]));
|
||||
} else {
|
||||
if (itemSlot.amount === 1)
|
||||
itemSlot.setItem(void 0);
|
||||
else
|
||||
itemSlot.amount--;
|
||||
}
|
||||
}
|
||||
|
||||
function playBlockPlacementSound(player, block, structureBlock) {
|
||||
const blockId = structureBlock.type.id.replace('minecraft:', '');
|
||||
const blockData = blocks[blockId];
|
||||
let blockSound = blockData['sound'] || 'stone';
|
||||
if (['glass', 'terracotta'].includes(blockSound))
|
||||
blockSound = 'stone';
|
||||
let blockSoundId = 'dig.' + blockSound;
|
||||
if (['iron', 'calcite'].includes(blockSound))
|
||||
blockSoundId = 'place.' + blockSound;
|
||||
player.dimension.playSound(blockSoundId, block.location);
|
||||
}
|
||||
Reference in New Issue
Block a user