Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Called when a player left or right clicks.
- *
- * @param event The event for said left or right clicks.
- */
- @EventHandler(priority = EventPriority.LOWEST)
- public final void playerInteract(final PlayerInteractEvent event) {
- // Check if this is an event the plugin should be interested in, a right click with a
- // spawner egg, if it isn't stop here.
- if (!event.hasItem()
- || event.getItem().getTypeId() != SPAWN_EGG
- || !event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
- return;
- }
- Player player = event.getPlayer();
- ItemStack itemInHand = event.getItem();
- EntityType entityType = EntityType.fromId(itemInHand.getDurability());
- // Check if the Metadata on the egg being placed is valid for a spawner, if it
- // isn't stop here.
- if (entityType == null || !entityType.isAlive() || !entityType.isSpawnable()) {
- return;
- }
- // Check if the user has the permission to place the egg they're attempting to
- // place based on sss.place.*, if not stop here.
- if (!player.hasPermission(PLACE_SPECIFIC.get(entityType))) {
- return;
- }
- // Prevent the normal spawener egg behaviours
- event.setCancelled(true);
- event.setUseItemInHand(Event.Result.DENY);
- // Get the block that the player is attempting to place on and ensure
- // that the block is valid
- Block targetBlock = event.getClickedBlock();
- if (targetBlock.getType().equals(Material.AIR)) {
- return;
- }
- // Select the block we should be changing into a spawner and ensure it's air,
- // water, or lava which are blocks you can normally put items in.
- Block placedBlock = event.getClickedBlock().getRelative(event.getBlockFace());
- if (placedBlock.getType() != Material.AIR &&
- placedBlock.getType() != Material.WATER &&
- placedBlock.getType() != Material.LAVA) {
- return;
- }
- // Save the previous state of the block being manipulated, just in case.
- BlockState previousState = placedBlock.getState();
- // Replace the placed block with a spawner cage.
- placedBlock.setType(Material.MOB_SPAWNER);
- CreatureSpawner spawner = (CreatureSpawner) placedBlock.getState();
- // Create a block place event for compatibility, then call it.
- BlockPlaceEvent bpEvent = new BlockPlaceEvent(placedBlock, previousState, targetBlock,
- new ItemStack(Material.MOB_SPAWNER, 1, entityType.getTypeId()),
- player, canBuild(player, placedBlock.getX(), placedBlock.getZ()));
- Bukkit.getPluginManager().callEvent(bpEvent);
- bpEvent.canBuild();
- // Ensure that there were no errors when attempting to call the event.
- ItemStack itemInHandEvent = bpEvent.getItemInHand();
- if (bpEvent.isCancelled() || itemInHandEvent == null ||
- itemInHandEvent.getType() != Material.MOB_SPAWNER) {
- previousState.update(true);
- return;
- }
- entityType = EntityType.fromId(bpEvent.getItemInHand().getDurability());
- if (entityType == null) {
- previousState.update(true);
- return;
- }
- // Update the spawner to spawn the entity type of the egg held in hand.
- spawner.setSpawnedType(entityType);
- spawner.update(true);
- // Remove one monster spawner from the players hand if they're not in creative mode.
- if (placedBlock.getState() instanceof CreatureSpawner
- && player.getGameMode().equals(GameMode.SURVIVAL)) {
- if (itemInHand.getAmount() > 1) {
- itemInHand.setAmount(itemInHand.getAmount() - 1);
- } else {
- player.getInventory().setItemInHand(null);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment