#Getting direction of a block and then setting it (getAxis();)

1 messages · Page 1 of 1 (latest)

wet spruce
#
    public static void playerInteract(PlayerInteractEvent event){
        Player player = event.getPlayer();
        if(event.getHand() == EquipmentSlot.HAND && cmdClass.stripToggle.get(player.getName())){
            Material clickedBlock = event.getClickedBlock().getType();
            if (clickedBlock == Material.SPRUCE_LOG) {
                Location loc = event.getClickedBlock().getLocation();
                Block block = loc.getBlock();
                block.setType(Material.STRIPPED_SPRUCE_LOG);
                player.playSound(player.getLocation(), Sound.ITEM_AXE_STRIP, 2, 1);
            }
        }
    }```


In this code, a player can strip a log just by right/left clicking, except it doesn't keep the orientation of the log, and the new block is always facing up. How do I get the "axis" property of a block and then set it to the new block? Thanks!
quiet sand
#

getBlockData and cast appropriately

wet spruce
#

so I get the block data and then set it with .setData()?

quiet sand
#

You’ll have to copy the property you want

#

Actually I think you can just setType in the block data, not sure

wet spruce
quiet sand
#

NewData.setWhatever(OldData.getWhatever())

wet spruce
quiet sand
#

Gonna need to see the code

#

And error

wet spruce
#

Cannot resolve method 'setAxis' in 'BlockData'

#
    public static void playerInteract(PlayerInteractEvent event){
        Player player = event.getPlayer();
        if(event.getHand() == EquipmentSlot.HAND && cmdClass.stripToggle.get(player.getName())){
            Material clickedBlock = event.getClickedBlock().getType();
            if (clickedBlock == Material.SPRUCE_LOG) {
                Location loc = event.getClickedBlock().getLocation();
                Block block = loc.getBlock();
                BlockData oldData = block.getBlockData();
                block.setType(Material.STRIPPED_SPRUCE_LOG);
                BlockData newData = loc.getBlock().getBlockData();
                newData.setAxis();
                player.playSound(player.getLocation(), Sound.ITEM_AXE_STRIP, 2, 1);
            }
        }
    }```
quiet sand
#

You gotta cast

wet spruce
#

cast?

quiet sand
#

Yes

wet spruce
#

what's that

quiet sand
wet spruce
#

Okay, what do I cast it to? Orientable?

quiet sand
#

Yes

wet spruce
#
                Block block = loc.getBlock();
                BlockData oldData = block.getBlockData();
                block.setType(Material.STRIPPED_SPRUCE_LOG);
                BlockData newData = loc.getBlock().getBlockData();
                (Orientable) newData.setAxis();
                player.playSound(player.getLocation(), Sound.ITEM_AXE_STRIP, 2, 1);```

Gives same error. Did I do something wrong?
quiet sand
#

You haven’t casted properly

#

If you want to cast Inline you need to do ((Class)object).method

wet spruce
#

oh alright

#

thanks