Search in sources :

Example 1 with BlockPlacerPlaceEvent

use of io.github.thebusybiscuit.slimefun4.api.events.BlockPlacerPlaceEvent in project Slimefun4 by Slimefun.

the class BlockPlacer method placeBlock.

@ParametersAreNonnullByDefault
private void placeBlock(ItemStack item, Block facedBlock, Dispenser dispenser) {
    BlockPlacerPlaceEvent e = new BlockPlacerPlaceEvent(dispenser.getBlock(), item, facedBlock);
    Bukkit.getPluginManager().callEvent(e);
    if (!e.isCancelled()) {
        schedulePlacement(facedBlock, dispenser.getInventory(), item, () -> {
            facedBlock.setType(item.getType());
            if (item.hasItemMeta()) {
                ItemMeta meta = item.getItemMeta();
                if (meta.hasDisplayName()) {
                    BlockStateSnapshotResult blockState = PaperLib.getBlockState(facedBlock, false);
                    if ((blockState.getState() instanceof Nameable)) {
                        Nameable nameable = ((Nameable) blockState.getState());
                        nameable.setCustomName(meta.getDisplayName());
                        if (blockState.isSnapshot()) {
                            // Update block state after changing name
                            blockState.getState().update(true, false);
                        }
                    }
                }
            }
        });
    }
}
Also used : Nameable(org.bukkit.Nameable) BlockPlacerPlaceEvent(io.github.thebusybiscuit.slimefun4.api.events.BlockPlacerPlaceEvent) BlockStateSnapshotResult(io.papermc.lib.features.blockstatesnapshot.BlockStateSnapshotResult) ItemMeta(org.bukkit.inventory.meta.ItemMeta) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault)

Example 2 with BlockPlacerPlaceEvent

use of io.github.thebusybiscuit.slimefun4.api.events.BlockPlacerPlaceEvent in project Slimefun4 by Slimefun.

the class BlockPlacer method placeSlimefunBlock.

@ParametersAreNonnullByDefault
private void placeSlimefunBlock(SlimefunItem sfItem, ItemStack item, Block block, Dispenser dispenser) {
    BlockPlacerPlaceEvent e = new BlockPlacerPlaceEvent(dispenser.getBlock(), item, block);
    Bukkit.getPluginManager().callEvent(e);
    if (!e.isCancelled()) {
        boolean hasItemHandler = sfItem.callItemHandler(BlockPlaceHandler.class, handler -> {
            if (handler.isBlockPlacerAllowed()) {
                schedulePlacement(block, dispenser.getInventory(), item, () -> {
                    block.setType(item.getType());
                    BlockStorage.store(block, sfItem.getId());
                    handler.onBlockPlacerPlace(e);
                });
            }
        });
        if (!hasItemHandler) {
            schedulePlacement(block, dispenser.getInventory(), item, () -> {
                block.setType(item.getType());
                BlockStorage.store(block, sfItem.getId());
            });
        }
    }
}
Also used : BlockPlacerPlaceEvent(io.github.thebusybiscuit.slimefun4.api.events.BlockPlacerPlaceEvent) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault)

Example 3 with BlockPlacerPlaceEvent

use of io.github.thebusybiscuit.slimefun4.api.events.BlockPlacerPlaceEvent in project Slimefun4 by Slimefun.

the class RepairedSpawner method onPlace.

@Nonnull
private BlockPlaceHandler onPlace() {
    return new BlockPlaceHandler(true) {

        @Override
        public void onPlayerPlace(BlockPlaceEvent e) {
            onPlace(e.getItemInHand(), e);
        }

        @Override
        public void onBlockPlacerPlace(BlockPlacerPlaceEvent e) {
            onPlace(e.getItemStack(), e);
        }

        @ParametersAreNonnullByDefault
        private void onPlace(ItemStack item, BlockEvent e) {
            /**
             * This may no longer be needed at some point but for legacy items
             * we still need to set the spawned EntityType manually
             */
            if (e.getBlock().getType() == Material.SPAWNER) {
                getEntityType(item).ifPresent(entity -> {
                    CreatureSpawner spawner = (CreatureSpawner) e.getBlock().getState();
                    spawner.setSpawnedType(entity);
                    spawner.update(true, false);
                });
            }
        }
    };
}
Also used : BlockPlaceHandler(io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler) BlockPlaceEvent(org.bukkit.event.block.BlockPlaceEvent) BlockPlacerPlaceEvent(io.github.thebusybiscuit.slimefun4.api.events.BlockPlacerPlaceEvent) ItemStack(org.bukkit.inventory.ItemStack) SlimefunItemStack(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack) BlockEvent(org.bukkit.event.block.BlockEvent) CreatureSpawner(org.bukkit.block.CreatureSpawner) Nonnull(javax.annotation.Nonnull)

Aggregations

BlockPlacerPlaceEvent (io.github.thebusybiscuit.slimefun4.api.events.BlockPlacerPlaceEvent)3 ParametersAreNonnullByDefault (javax.annotation.ParametersAreNonnullByDefault)2 SlimefunItemStack (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack)1 BlockPlaceHandler (io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler)1 BlockStateSnapshotResult (io.papermc.lib.features.blockstatesnapshot.BlockStateSnapshotResult)1 Nonnull (javax.annotation.Nonnull)1 Nameable (org.bukkit.Nameable)1 CreatureSpawner (org.bukkit.block.CreatureSpawner)1 BlockEvent (org.bukkit.event.block.BlockEvent)1 BlockPlaceEvent (org.bukkit.event.block.BlockPlaceEvent)1 ItemStack (org.bukkit.inventory.ItemStack)1 ItemMeta (org.bukkit.inventory.meta.ItemMeta)1