regarding PlayerInteractEvent

Discussion in 'Plugin Development' started by JoltTheBolt, Jan 28, 2021.

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

    JoltTheBolt

    I want an item to get the location of clicked block when (action == Action.RIGHT_CLICK_AIR) and get the location of another clicked block when (action == Action.LEFT_CLICK_AIR).

    Just like the world edit plugin, yeah.

    If I get the location with one action, I have to use a condition which will get me the location within the brackets {}. This makes it unable to use the location outside of the brackets. As shown:

    Code:
    ItemStack itemCheck = p.getInventory().getItemInHand();
            if (p.getItemInHand().getType() == Material.BLAZE_ROD) {
                if(itemCheck.hasItemMeta() &&
                        itemCheck.getItemMeta().hasDisplayName() &&
                        itemCheck.getItemMeta().getDisplayName().equals(ChatColor.GOLD.toString() + ChatColor.BOLD.toString() + "Area Selector")) {
                   
                    if (action == Action.RIGHT_CLICK_BLOCK) {
                    Location point1 = p.getTargetBlock((Set<Material>) null, 10).getLocation();
                    p.sendMessage(ChatColor.LIGHT_PURPLE.toString() + "First Location Selected");
                    }
                    if (action == Action.LEFT_CLICK_BLOCK) {
                    Location point2 = p.getTargetBlock((Set<Material>) null, 10).getLocation();
                    p.sendMessage(ChatColor.LIGHT_PURPLE.toString() + "Second Location Selected");
                    }
                   
                    if (action == Action.LEFT_CLICK_BLOCK) {
                    Location point2 = p.getTargetBlock((Set<Material>) null, 10).getLocation();
                    p.sendMessage(ChatColor.LIGHT_PURPLE.toString() + "Second Location Selected");
    I upgraded it to make it so that you always have to select the first location first by right-clicking. This allowed me to put the next "if action equals" within the same brackets so I can use that location. As shown:


    Code:
    ItemStack itemCheck = p.getInventory().getItemInHand();
            if (p.getItemInHand().getType() == Material.BLAZE_ROD) {
                if(itemCheck.hasItemMeta() &&
                        itemCheck.getItemMeta().hasDisplayName() &&
                        itemCheck.getItemMeta().getDisplayName().equals(ChatColor.GOLD.toString() + ChatColor.BOLD.toString() + "Area Selector")) {
                   
                    if (action == Action.RIGHT_CLICK_BLOCK) {
                    Location point1 = p.getTargetBlock((Set<Material>) null, 10).getLocation();
                    p.sendMessage(ChatColor.LIGHT_PURPLE.toString() + "First Location Selected");
                   
                    if (action == Action.LEFT_CLICK_BLOCK) {
                    Location point2 = p.getTargetBlock((Set<Material>) null, 10).getLocation();
                    p.sendMessage(ChatColor.LIGHT_PURPLE.toString() + "Second Location Selected");
    However, this doesn't quite work. The second statement does not get called at all and I don't understand why. Please help.

    Thanks for any help!
     
  2. Offline

    Chr0mosom3

    A couple of thingis before I get to your problem

    1) You don't need to do ChatColor.LIGHT_PURPLE.toString(), just do ChatColor.LIGHT_PURPLE and it will work

    Now onto your problem:

    1) Are you doing anything with the point2 variable afterwards? If you want to use it at a later time, you are going to have to save it to a variable outside the scope of the event
    2) Left clicking and then right clicking is 2 seperate events
    3) What exactly is your problem?
    4) Did you do any debugging, what have you tried and what works?
    5) Have you register the event properly?
     
  3. Offline

    Kars

    @JoltTheBolt declare the objects uninitialized outside of the brackets and set them inside the brackets like so:
    PHP:
    Location point1;
    Location point2;

    if (
    action == Action.RIGHT_CLICK_BLOCK) {
        
    point1 p.getTargetBlock((Set<Material>) null10).getLocation();
    } else if (
    action == Action.LEFT_CLICK_BLOCK) {
        
    point2 p.getTargetBlock((Set<Material>) null10).getLocation();
    }
    And check for nulls afterward.
     
    JoltTheBolt likes this.
Thread Status:
Not open for further replies.

Share This Page