Advertisement
nicka101

SuperSimpleSpawners PlayerInteract

Apr 26th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.21 KB | None | 0 0
  1. @EventHandler(priority = EventPriority.LOWEST)
  2.     public final void playerInteract(final PlayerInteractEvent event) {
  3.         Player player = event.getPlayer();
  4.         // Check if this is an event the plugin should be interested in, a right click with a
  5.         // spawn egg, if it isn't stop here.
  6.         if (!event.hasItem()
  7.                 || event.getItem().getTypeId() != SPAWN_EGG
  8.                 || !event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.isCancelled()) {
  9.             return;
  10.         }
  11.         ItemStack itemInHand = player.getItemInHand();
  12.         EntityType entityType = EntityType.fromId(itemInHand.getDurability());
  13.  
  14.         // Check if the Metadata on the egg being placed is valid for a spawner, if it
  15.         // isn't stop here.
  16.         if (entityType == null
  17.                 || !entityType.isAlive()
  18.                 || !entityType.isSpawnable()) {
  19.             return;
  20.         }
  21.         // Check if the user has the permission to place the egg they're attempting to
  22.         // place based on sss.place.entity_type, if not stop here.
  23.         if (!player.hasPermission(PLACE_SPECIFIC.get(entityType))) {
  24.             return;
  25.         }
  26.         // Prevent the normal spawn egg behaviours
  27.         event.setCancelled(true);
  28.         event.setUseItemInHand(Event.Result.DENY);
  29.         // Get the block that the player is attempting to place on and ensure
  30.         // that the block is valid
  31.         Block targetBlock = player.getTargetBlock(null, PLAYER_REACH);
  32.         if (targetBlock.getType().equals(Material.AIR)) {
  33.             return;
  34.         }
  35.         // Ensure that the player is not placing the block in their feet.
  36.         Location playerLocation = player.getLocation();
  37.         if (playerLocation.getWorld().getBlockAt(
  38.                 playerLocation.getBlockX(),
  39.                 playerLocation.getBlockY() - 1,
  40.                 playerLocation.getBlockZ()).equals(targetBlock)) {
  41.             return;
  42.         }
  43.         // Select the block we should be changing into a spawner and ensure it's air,
  44.         // water, or lava which are blocks you can normally put items in.
  45.         Block placedBlock = targetBlock.getRelative(event.getBlockFace());
  46.         if (placedBlock.getType() != Material.AIR
  47.                 && placedBlock.getType() != Material.WATER
  48.                 && placedBlock.getType() != Material.LAVA
  49.                 && placedBlock.getType() != Material.STATIONARY_WATER
  50.                 && placedBlock.getType() != Material.STATIONARY_LAVA
  51.         && placedBlock.getType() != Material.VINE
  52.         && placedBlock.getTypeId() != 78
  53.         && placedBlock.getType() != Material.FIRE
  54.         && placedBlock.getTypeId() != 106
  55.         && placedBlock.getTypeId() != 111) {
  56.             return;
  57.         }
  58.         // Save the previous state of the block being manipulated, in case the fake block place
  59.         // event we throw is canceled.
  60.         BlockState previousState = placedBlock.getState();
  61.         // Replace the placed block with a spawner cage.
  62.         placedBlock.setType(Material.MOB_SPAWNER);
  63.         CreatureSpawner spawner = (CreatureSpawner) placedBlock.getState();
  64.         // We're going to change the item type to a monster spawner so it looks like that's what the
  65.         // player is placing for the fake block place event.
  66.         // Create a block place event for compatibility, then call it.
  67.         BlockPlaceEvent bpEvent = new BlockPlaceEvent(placedBlock, previousState, targetBlock,
  68.                 itemInHand, player, canBuild(player, placedBlock.getX(), placedBlock.getZ()));
  69.         Bukkit.getPluginManager().callEvent(bpEvent);
  70.         // Now we'll switch that monster spawner back to spawn eggs so the player sees no change
  71.         // If the block place event was cancelled, the item was changed to something other than
  72.         // a spawner, or the player is trying to place in spawn protection we need to stop here.
  73.         if (bpEvent.isCancelled()
  74.                 || itemInHand.getType() != Material.MONSTER_EGG
  75.                 || !bpEvent.canBuild()) {
  76.             // We'll revert the block back to what it was by updating the previous state.
  77.             previousState.update(true);
  78.             return;
  79.         }
  80.         entityType = EntityType.fromId(itemInHand.getDurability());
  81.         // We need to ensure that the item in their hand is still a valid monster spawner egg.
  82.         if (entityType == null
  83.                 || !entityType.isAlive()
  84.                 || !entityType.isSpawnable()) {
  85.             // We'll revert the block back to what it was by updating the previous state.
  86.             previousState.update(true);
  87.             return;
  88.         }
  89.         // Update the spawner to spawn the entity type of the egg held in hand.
  90.         spawner.setSpawnedType(entityType);
  91.         spawner.update(true);
  92.         // Remove one spawn egg from the players hand if they're not in creative mode.
  93.         if (placedBlock.getState() instanceof CreatureSpawner
  94.                 && player.getGameMode().equals(GameMode.SURVIVAL)) {
  95.             if (itemInHand.getAmount() > 1) {
  96.                 itemInHand.setAmount(itemInHand.getAmount() - 1);
  97.             } else {
  98.                 player.getInventory().setItemInHand(null);
  99.             }
  100.             player.updateInventory();
  101.         }
  102.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement