Solved Getting Location On PlayerInteractEvent

Discussion in 'Plugin Development' started by BajanAmerican, Aug 31, 2013.

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

    BajanAmerican

    Hey Guys, I came across some trouble trying to check the location of a clicked block in PlayerInteractEvent. This is my code:

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event){
    3. try{
    4. if(event.getAction() == Action.RIGHT_CLICK_BLOCK){
    5. Player player = event.getPlayer();
    6. if(Game.blue.contains(player.getName())){
    7. if(event.getClickedBlock().getLocation() == Locations.getRedCauldronLocation()){
    8. event.setCancelled(true);
    9. player.sendMessage(ChatColor.DARK_GREEN + "You Are Not Permitted To Drain The " + ChatColor.DARK_RED + "RED" + ChatColor.DARK_GREEN + " Team's Cauldron!");
    10. }
    11. }if(Game.red.contains(player.getName())){
    12. if(event.getClickedBlock().getLocation() == Locations.getRedCauldronLocation())){
    13. event.setCancelled(true);
    14. player.sendMessage(ChatColor.DARK_GREEN + "You Are Not Permitted To Drain The " + ChatColor.DARK_BLUE + "BLUE" + ChatColor.DARK_GREEN + " Team's Cauldron!");
    15. }
    16. }
    17. }
    18. }catch(Exception e){
    19. e.printStackTrace();
    20. }
    21. }


    Btw, the methods getBlueCauldronLocation() and getRedCauldronLocation() are just returning Locations.

    Code:java
    1. public static Location getBlueCauldronLocation() {
    2. return cauldronLocationBlue;
    3. }
    4.  
    5. public static Location getRedCauldronLocation() {
    6. return cauldronLocationRed;
    7. }


    If you could not tell, I have two locations. I want the blue team to not be able to interact with one location and vice-versa with the red team. Any help would be fantastic! Thanks!
     
  2. Offline

    metalhedd

    use .equals() instead of == when comparing locations.
     
  3. Offline

    BajanAmerican

    I changed it but it still is not working.
    Code:
                    if(Game.blue.contains(player.getName())){
                        if(event.getClickedBlock().getLocation().equals(Locations.getRedCauldronLocation())){
                            event.setCancelled(true);
                            player.sendMessage(ChatColor.DARK_GREEN + "You Are Not Permitted To Drain The " + ChatColor.DARK_RED + "RED" + ChatColor.DARK_GREEN + " Team's Cauldron!");
                        }
                    }if(Game.red.contains(player.getName())){
                        if(event.getClickedBlock().getLocation().equals(Locations.getBlueCauldronLocation())){
                            event.setCancelled(true);
                            player.sendMessage(ChatColor.DARK_GREEN + "You Are Not Permitted To Drain The " + ChatColor.DARK_BLUE + "BLUE" + ChatColor.DARK_GREEN + " Team's Cauldron!");
                        }
                    }
     
  4. Offline

    metalhedd

    add some logging to determine which condition is false that you think is true.
     
  5. Offline

    BajanAmerican

    What do you mean?
     
  6. Offline

    metalhedd

    Code:java
    1.  
    2. if(Game.blue.contains(player.getName())) {
    3. if(event.getClickedBlock().getLocation().equals(Locations.getRedCauldronLocation())){
    4. event.setCancelled(true);
    5. player.sendMessage(ChatColor.DARK_GREEN + "You Are Not Permitted To Drain The " + ChatColor.DARK_RED + "RED" + ChatColor.DARK_GREEN + " Team's Cauldron!");
    6. } else {
    7. plugin.getLogger().info("Clicked block was NOT the red cauldron (" + event.getClickedBlock().getLocation() + " vs. " + Locations.getRedCauldronLocation() + ")");
    8. }
    9. } else if (Game.red.contains(player.getName())) {
    10. if(event.getClickedBlock().getLocation().equals(Locations.getBlueCauldronLocation())){
    11. event.setCancelled(true);
    12. player.sendMessage(ChatColor.DARK_GREEN + "You Are Not Permitted To Drain The " + ChatColor.DARK_BLUE + "BLUE" + ChatColor.DARK_GREEN + " Team's Cauldron!");
    13. } else {
    14. plugin.getLogger().info("Clicked block was NOT the blue cauldron (" + event.getClickedBlock().getLocation() + " vs. " + Locations.getBlueCauldronLocation() + ")");
    15. }
    16. } else {
    17. plugin.getLogger().info("Player wasn't on either team?!");
    18. }
    19.  


    Then check your server log to see which of those get's printed.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  7. Offline

    BajanAmerican

    Okay so I was on the blue team. I clicked on the red cauldron and it gave me the message that I did not click on the red cauldron. I checked the cords and they are the same as the location I am checking.
     
  8. Offline

    metalhedd


    can you paste the exact output you got? also your current code would be helpful incase there is some typo
     
  9. Offline

    BajanAmerican

    I found the error, it was in another class involving cases with the different maps. Thank you very much! :)
     
    metalhedd likes this.
Thread Status:
Not open for further replies.

Share This Page