Java Code Examples for org.bukkit.event.player.PlayerInteractEvent#getBlockFace()

The following examples show how to use org.bukkit.event.player.PlayerInteractEvent#getBlockFace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlockListener.java    From Carbon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void slabInteract(PlayerInteractEvent event) {
	if (event.getItem() == null || event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getItem().getType() != Carbon.injector().redSandstoneSlabMat)
		return;
	Block clickedBlock = event.getClickedBlock();
	if (clickedBlock.getType() == Carbon.injector().redSandstoneSlabMat)
		if ((clickedBlock.getData() == 0 && event.getBlockFace() == BlockFace.UP) || (clickedBlock.getData() == 8 && event.getBlockFace() == BlockFace.DOWN)) {
			setDoubleSlab(event.getPlayer(), clickedBlock);
			event.setCancelled(true);
			return;
		}
	Block adjacent = clickedBlock.getRelative(event.getBlockFace());
	if (adjacent.getType() == Carbon.injector().redSandstoneSlabMat) {
		setDoubleSlab(event.getPlayer(), adjacent);
		event.setCancelled(true);
	}
}
 
Example 2
Source File: PlayerInteract.java    From AdditionsAPI with MIT License 5 votes vote down vote up
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
	Player player = event.getPlayer();
	PlayerInventory inv = player.getInventory();
	ItemStack item = inv.getItemInMainHand();
	if (event.getHand() != null)
		switch (event.getHand()) {
		case OFF_HAND:
			item = inv.getItemInOffHand();
			break;
		default:
			break;
		}

	if (AdditionsAPI.isCustomItem(item)) {
		CustomItemPlayerInteractEvent customEvent = new CustomItemPlayerInteractEvent(event,
				new CustomItemStack(item));
		Bukkit.getServer().getPluginManager().callEvent(customEvent);
	}
	if (event.getAction() != null && event.getAction() == Action.RIGHT_CLICK_BLOCK
			&& ToolType.getToolType(item.getType()) != null
			&& ToolType.getToolType(item.getType()).equals(ToolType.HOE)) {
		Block block = event.getClickedBlock();
		Material material = block.getType();
		Location blockLoc = block.getLocation();
		blockLoc.setY(blockLoc.getBlockY() + 1);
		Material materialUp = blockLoc.getBlock().getType();
		@SuppressWarnings("deprecation")
		byte data = block.getData();

		BlockFace face = event.getBlockFace();
		if (materialUp == Material.AIR && (face == BlockFace.UP || face == BlockFace.EAST || face == BlockFace.NORTH
				|| face == BlockFace.SOUTH || face == BlockFace.WEST))
			if (shouldPlaySound(material, item, data, player))
				player.playSound(block.getLocation(), "additionsapi.hoe.till", 1.0F, 1.0F);
	}
}
 
Example 3
Source File: BonusGoodieManager.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.LOW)
public void OnPlayerInteractEvent(PlayerInteractEvent event) {
	
	ItemStack item = event.getPlayer().getItemInHand();
	BonusGoodie goodie = CivGlobal.getBonusGoodie(item);
	if (goodie == null) {
		return;
	}

	if (event.getClickedBlock() == null) {
		event.setCancelled(true);
		return;
	}
	
	BlockCoord bcoord = new BlockCoord(event.getClickedBlock());		
	ItemFrameStorage clickedFrame = ItemFrameStorage.attachedBlockMap.get(bcoord);
	
	if (clickedFrame != null) {
		if (clickedFrame.getItemFrame() != null) {
			if (clickedFrame.getItemFrame().getAttachedFace().getOppositeFace() ==
					event.getBlockFace()) {
				onPlayerProtectedFrameInteract(event.getPlayer(), clickedFrame,	goodie, event);
				event.setCancelled(true);
			}
		}
		return;
	}
	
	if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {				
		CivMessage.sendError(event.getPlayer(), "Cannot use bonus goodie as a normal item.");
		event.setCancelled(true);
		return;
	}
}
 
Example 4
Source File: PlayerRightClickEvent.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
public PlayerRightClickEvent(PlayerInteractEvent e) {
    event = e;
    player = e.getPlayer();
    clickedBlock = Optional.ofNullable(e.getClickedBlock());
    face = e.getBlockFace();
    hand = e.getHand();

    if (e.getItem() == null || e.getItem().getType() == Material.AIR || e.getItem().getAmount() == 0) {
        itemStack = Optional.empty();
    }
    else {
        itemStack = Optional.of(e.getItem());
    }
}
 
Example 5
Source File: PlayerInteractListener.java    From IridiumSkyblock with GNU General Public License v2.0 4 votes vote down vote up
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
    try {
        final Player player = event.getPlayer();
        final Location playerLocation = player.getLocation();
        final IslandManager islandManager = IridiumSkyblock.getIslandManager();
        if (!islandManager.isIslandWorld(playerLocation)) return;

        final User user = User.getUser(player);
        final Block block = event.getClickedBlock();

        if (event.getAction().toString().startsWith("RIGHT_CLICK")) {
            if (player.getItemInHand() != null) {
                int crystals = Utils.getCrystals(player.getItemInHand()) * player.getItemInHand().getAmount();
                if (crystals != 0) {
                    player.setItemInHand(null);
                    user.getIsland().setCrystals(user.getIsland().getCrystals() + crystals);
                    player.sendMessage(Utils.color(IridiumSkyblock.getMessages().depositedCrystals.replace("%amount%", crystals + "").replace("%prefix%", IridiumSkyblock.getConfiguration().prefix)));
                }
            }
        }

        if (block != null) {
            final Location location = block.getLocation();
            final Island island = islandManager.getIslandViaLocation(location);
            if (island != null) {
                if (!island.getPermissions(user).interact) {
                    event.setCancelled(true);
                    return;
                }
                final ItemStack itemInHand = player.getItemInHand();
                if (itemInHand.getType().equals(Material.BUCKET) && island.failedGenerators.remove(location)) {
                    if (itemInHand.getAmount() == 1)
                        itemInHand.setType(Material.LAVA_BUCKET);
                    else {
                        player.getInventory().addItem(new ItemStack(Material.LAVA_BUCKET));
                        player.getItemInHand().setAmount(itemInHand.getAmount() - 1);
                    }
                    block.setType(Material.AIR);
                }
            } else if (!user.bypassing) {
                event.setCancelled(true);
                return;
            }
        }

        final ItemStack item = event.getItem();
        if (IridiumSkyblock.getConfiguration().allowWaterInNether
                && event.getAction().equals(Action.RIGHT_CLICK_BLOCK)
                && item != null
                && block != null) {
            final World world = block.getWorld();
            if (!world.getEnvironment().equals(World.Environment.NETHER)) return;
            if (!item.getType().equals(Material.WATER_BUCKET)) return;

            event.setCancelled(true);

            final BlockFace face = event.getBlockFace();
            block.getRelative(face).setType(Material.WATER);

            final Block relative = block.getRelative(face);
            final BlockPlaceEvent blockPlaceEvent = new BlockPlaceEvent(relative, relative.getState(), block, item, player, false);
            if (blockPlaceEvent.isCancelled()) {
                block.getRelative(face).setType(Material.AIR);
            } else if (player.getGameMode().equals(GameMode.SURVIVAL)) {
                if (item.getAmount() == 1) {
                    item.setType(Material.BUCKET);
                } else {
                    item.setAmount(item.getAmount() - 1);
                    player.getInventory().addItem(new ItemStack(Material.BUCKET));
                }
            }
        }
    } catch (Exception e) {
        IridiumSkyblock.getInstance().sendErrorMessage(e);
    }
}
 
Example 6
Source File: SitListener.java    From NyaaUtils with MIT License 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onClickBlock(PlayerInteractEvent event) {
    if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.hasBlock() && !event.hasItem()) {
        Block block = event.getClickedBlock();
        BlockFace face = event.getBlockFace();
        if (face == BlockFace.DOWN || block.isLiquid() || !plugin.cfg.sit_blocks.contains(block.getType())) {
            return;
        }
        Block relative = block.getRelative(0, 1, 0);
        Player player = event.getPlayer();
        if (messageCooldown.getIfPresent(player.getUniqueId()) != null) {
            return;
        }
        messageCooldown.put(player.getUniqueId(), true);
        if (!player.hasPermission("nu.sit") || !enabledPlayers.contains(player.getUniqueId()) || player.isInsideVehicle() || !player.getPassengers().isEmpty() || player.getGameMode() == GameMode.SPECTATOR || !player.isOnGround()) {
            return;
        }
        if (relative.isLiquid() || !(relative.isEmpty() || relative.isPassable())) {
            player.sendMessage(I18n.format("user.sit.invalid_location"));
            return;
        }
        Vector vector = block.getBoundingBox().getCenter().clone();
        Location loc = vector.setY(block.getBoundingBox().getMaxY()).toLocation(player.getWorld()).clone();
        for (SitLocation sl : plugin.cfg.sit_locations.values()) {
            if (sl.blocks != null && sl.x != null && sl.y != null && sl.z != null && sl.blocks.contains(block.getType().name())) {
                loc.add(sl.x, sl.y, sl.z);
            }
        }
        if (block.getBlockData() instanceof Directional) {
            face = ((Directional) block.getBlockData()).getFacing();
            if (face == BlockFace.EAST) {
                loc.setYaw(90);
            } else if (face == BlockFace.WEST) {
                loc.setYaw(-90);
            } else if (face == BlockFace.NORTH) {
                loc.setYaw(0);
            } else if (face == BlockFace.SOUTH) {
                loc.setYaw(-180);
            }
        } else {
            if (face == BlockFace.WEST) {
                loc.setYaw(90);
            } else if (face == BlockFace.EAST) {
                loc.setYaw(-90);
            } else if (face == BlockFace.SOUTH) {
                loc.setYaw(0);
            } else if (face == BlockFace.NORTH) {
                loc.setYaw(-180);
            } else {
                loc.setYaw(player.getEyeLocation().getYaw());
            }
        }
        for (Entity e : loc.getWorld().getNearbyEntities(loc, 0.5, 0.7, 0.5)) {
            if (e instanceof LivingEntity) {
                if (e.hasMetadata(metadata_key) || (e instanceof Player && e.isInsideVehicle() && e.getVehicle().hasMetadata(metadata_key))) {
                    player.sendMessage(I18n.format("user.sit.invalid_location"));
                    return;
                }
            }
        }
        Location safeLoc = player.getLocation().clone();
        ArmorStand armorStand = loc.getWorld().spawn(loc, ArmorStand.class, (e) -> {
            e.setVisible(false);
            e.setPersistent(false);
            e.setCanPickupItems(false);
            e.setBasePlate(false);
            e.setArms(false);
            e.setMarker(true);
            e.setInvulnerable(true);
            e.setGravity(false);
        });
        if (armorStand != null) {
            armorStand.setMetadata(metadata_key, new FixedMetadataValue(plugin, true));
            if (armorStand.addPassenger(player)) {
                safeLocations.put(player.getUniqueId(), safeLoc);
            } else {
                armorStand.remove();
            }
        }
    }
}
 
Example 7
Source File: ItemListener.java    From Carbon with GNU Lesser General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
   public void onCauldronClick(PlayerInteractEvent evt) {
       if (evt.getAction() == Action.RIGHT_CLICK_BLOCK) {
           if (evt.getBlockFace() == BlockFace.UP && evt.getClickedBlock().getType() == Material.CAULDRON && evt.getItem() != null && evt.getItem().getType() == Carbon.injector().bannerItemMat) {
           	evt.setCancelled(true);
           	ItemStack originalBanner = evt.getItem();
           	//create new banner with latest pattern removed
               net.minecraft.server.v1_7_R4.ItemStack nmsNewBanner = CraftItemStack.asNMSCopy(originalBanner);
               NBTTagCompound tag = nmsNewBanner.getTag();
               byte waterLevel = evt.getClickedBlock().getData();
               if (waterLevel > 0 && tag != null && tag.hasKey("BlockEntityTag") && tag.getCompound("BlockEntityTag").hasKey("Patterns")) {
                NBTTagCompound compound = tag.getCompound("BlockEntityTag");
                NBTTagList list = compound.getList("Patterns", 10);
                NBTTagList newList = new NBTTagList();
                for (int n = 0; n < list.size() - 1; n++) {
                    newList.add(list.get(n));
                }
                if (newList.size() > 0) {
                	compound.set("Patterns", newList);
                } else {
                	compound.remove("Patterns");
                }
                ItemStack newBannerItem = CraftItemStack.asCraftMirror(nmsNewBanner);
                newBannerItem.setAmount(1);
                //update cauldron
                evt.getClickedBlock().setData(--waterLevel);
                //update used itemstack
                if (originalBanner.getAmount() > 1) {
                	evt.getItem().setAmount(originalBanner.getAmount() - 1);
                } else {
                	evt.getItem().setAmount(0);
                	evt.getPlayer().setItemInHand(null);
                }
                //add new banner
                evt.getPlayer().getInventory().addItem(newBannerItem);
                evt.getPlayer().updateInventory();
               }
           }
       }
   }