I can't get my plugin to register the RIGHT/LEFT_CLICK_AIR action?

Discussion in 'Plugin Development' started by Lemoncakecake, Jan 5, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Lemoncakecake

    So basically, I want my plugin so that when you left click with a blaze rod, it will teleport you to the block you targeted, but I can only get it to teleport you when you click on a block... Here's my code:

    Code:
    package me.lemoncakecake.SpellCraft;
    
    import org.bukkit.GameMode;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.material.MaterialData;
    
    public class SpellClickListener extends PlayerListener {
    
        public static SpellCraft plugin;
    
        public SpellClickListener(SpellCraft instance) {
            plugin = instance;
        }
    
        public void onPlayerInteract(PlayerInteractEvent event)
        {
            //Event Variables
            Player player = event.getPlayer();
            String playerDisplayName = player.getDisplayName();
            World playerWorld = player.getWorld();
            PlayerInventory playerInventory = player.getInventory();
    
            Block clickedBlock = event.getClickedBlock();
            Block targetedBlock = player.getTargetBlock(null, 0);
    
            ItemStack itemInHand = player.getItemInHand();
            Material itemInHandType = itemInHand.getType();
            int itemInHandAmount = itemInHand.getAmount();
            MaterialData itemInHandData = itemInHand.getData();
    
            int playerHealth = player.getHealth();
            int playerHunger = player.getFoodLevel();
            Location playerBedLocation = player.getBedSpawnLocation();
            GameMode playerGameMode = player.getGameMode();
            float playerYaw = player.getEyeLocation().getYaw();
            float playerPitch = player.getEyeLocation().getPitch();
    
            Material clickedBlockType = clickedBlock.getType();
            int clickedBlockData = clickedBlock.getData();
            int clickedBlockX = clickedBlock.getX();
            int clickedBlockY = clickedBlock.getY();
            int clickedBlockZ = clickedBlock.getZ();
            Location clickedBlockLocation = clickedBlock.getLocation();
    
            Location clickedBlockLoc = clickedBlockLocation;
            clickedBlockLoc.setY(clickedBlockY + 1);
    
            Material targetedBlockType = targetedBlock.getType();
            int targetedBlockData = targetedBlock.getData();
            int targetedBlockX = targetedBlock.getX();
            int targetedBlockY = targetedBlock.getY();
            int targetedBlockZ = targetedBlock.getZ();
            Location targetedBlockLocation = targetedBlock.getLocation();
    
            Location targetedBlockLoc = targetedBlockLocation;
            targetedBlockLoc.setX(targetedBlockX + .5);
            targetedBlockLoc.setZ(targetedBlockZ + .5);
            targetedBlockLoc.setY(targetedBlockY + 1);
            targetedBlockLoc.setPitch(playerPitch);
            targetedBlockLoc.setYaw(playerYaw);
    
            boolean noRightInteract = false;
            boolean noLeftInteract = false;
    
            if (event.hasBlock()) {
                Material m = event.getClickedBlock().getType();
                if (m == Material.WOODEN_DOOR || m == Material.TRAP_DOOR || m == Material.BED || m == Material.WORKBENCH || m == Material.CHEST || m == Material.FURNACE || m == Material.LEVER || m == Material.STONE_BUTTON || m == Material.BREWING_STAND || (m == Material.CAULDRON && itemInHandType == Material.GLASS_BOTTLE) || (m == Material.CAULDRON && itemInHandType == Material.WATER_BUCKET)) {
                    noRightInteract = true;
                }
                if (m == Material.WOODEN_DOOR || m == Material.TRAP_DOOR) {
                    noLeftInteract = true;
                }
            }
    
            //Lightning Strike
            if (itemInHandType ==  Material.getMaterial(369) && event.getAction() == Action.RIGHT_CLICK_AIR) {
                playerWorld.strikeLightning(targetedBlockLocation);
            }
            if (itemInHandType ==  Material.getMaterial(369) && event.getAction() == Action.RIGHT_CLICK_BLOCK && noRightInteract != true) {
                playerWorld.strikeLightning(clickedBlockLocation);
            }
    
            //Teleport
            if (itemInHandType ==  Material.getMaterial(369) && (event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_AIR) && noLeftInteract != true) {
                player.teleport(targetedBlockLoc);
            }
    
        }
    }
    
    Any help would be great, thanks
     
  2. Offline

    theguynextdoor

    Block targetedBlock = player.getTargetBlock(null, 0);
    Im not 100% sure, but i think the second value is the max range, and so try increasing that abit, to like 100 or so
     
  3. Offline

    knightidus

    Yeah, i use the lightning thing on mine and i have set it to 300, it seem's to work

    I do this:
    Code:
    public void strikeLightning(Player p){
            Block block = p.getTargetBlock(null, 300);
            Location coords = block.getLocation();
            coords.getWorld().strikeLightning(coords);
    
        }
     
  4. Offline

    Lemoncakecake

    Setting the target location higher seems to have no effect.
    I've changed the RIGHT_CLICK_BLOCK to strike at the targeted location, and that works fine, so something tells me it's the RIGHT_CLICK_AIR that isn't passing for some reason.

    Does anyone know how to fix this?

    yes, my suspicions were correct, as I put the code at the top as:

    event.getPlayer().getTargetBlock(null, 0).getWorld().strikeLightning(event.getPlayer().getTargetBlock(null, 0).getLocation());

    and it worked fine...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  5. Offline

    Taco

    Stupid question, but do you have the PlayerInteractEvent registered?
     
Thread Status:
Not open for further replies.

Share This Page