#Detecting if user is looking at top or bottom of block

1 messages · Page 1 of 1 (latest)

stray trail
#

Is there a way I can detect where a user is looking on a block? I'm trying to manually place a slab but i can only place a bottom half slab.

world.beforeEvents.worldInitialize.subscribe(initEvent => {
    initEvent.blockTypeRegistry.registerCustomComponent('quark:trowel_buffer', {
        beforeOnPlayerPlace: e => {
            if(!e.player) return;
            let blocks = getHotBarBlocks(e.player);
            if(blocks.length === 0) {
                e.cancel = true;
                return;
            }
            let blockToPlace = pickRandom(blocks);
            let permutation = BlockPermutation.resolve(blockToPlace.typeId);
            let cardinalDirState = permutation.getState('minecraft:cardinal_direction');
            world.sendMessage(JSON.stringify(permutation.getAllStates()));
            if(cardinalDirState) {
                let cardinalDir = getCardinalDirection(e.player);
                e.permutationToPlace = permutation.withState('minecraft:cardinal_direction', cardinalDir.toLowerCase());
            } else {
                e.permutationToPlace = permutation;
            }
            
        }
    });
});

my current code. Assume blockToPlace is a slab itemStack

stray trail
#

unfortunately no

#

that just gets the block, not where on the block

#

since i need to know if i can make the slab a top slab or not

#

let me record a video

willow inlet
#

You could use PlayerInteractWithBlockAfterEvent and use the faceLocation and a little bit of math to see if it's the top or bottom

stray trail
#

hm

#

really unfortunate the event im using doesnt have that...

#

ill see if i can make it work

willow inlet
#
world.afterEvents.playerInteractWithBlock.subscribe(function (data) {
  let player = data.player;
  let block = data.block;
  let location = data.faceLocation;
  player.sendMessage(JSON.stringify(Vector3Utils.subtract(location, block.location)));
});
#

And then instead of player.sendMessage Just run your code

#

Make sure to subtract the block location from the face location otherwise you'll get numbers much larger than expected. Found that out the hard way

stray trail
# willow inlet ```typescript world.afterEvents.playerInteractWithBlock.subscribe(function (data...
function getUV(blockFace: string, faceLocation: Vector3) {
    let u = 0;
    let v = 0;
    switch(blockFace.toLowerCase()) {
        case 'up':
            u = faceLocation.x;
            v = faceLocation.z;
            break;
        case 'down':
            u = faceLocation.x;
            v = faceLocation.z;
            break;
        case 'north':
            u = faceLocation.x;
            v = faceLocation.y;
            break;
        case 'south':
            u = 1 - faceLocation.x;
            v = faceLocation.y;
            break;
        case 'west':
            u = 1 - faceLocation.z;
            v = faceLocation.y;
            break;
        case 'east':
            u = faceLocation.z;
            v = faceLocation.y;
            break;
    }
    return { u, v };
}

function subtractVector3(v1: Vector3, v2: Vector3) {
    return { x: v1.x - v2.x, y: v1.y - v2.y, z: v1.z - v2.z };
}
world.afterEvents.playerInteractWithBlock.subscribe(data => {
    let block = data.block;
    let location = data.faceLocation;
    let uv = getUV(data.blockFace, subtractVector3(location, block.location));
    data.player.onScreenDisplay.setActionBar("U: " + uv.u + " V: " + uv.v + " " + data.blockFace);
});

made a nice function to get the uv coordinates