Solved Left click on block doesn't work (PlayerInteractEvent)

Discussion in 'Spigot Plugin Development' started by DevelopmentHigh, Jun 28, 2020.

  1. Hello, i have little problem with player interact event right click on block doesn't work and i do not have any error on console, there is my information:

    stats.yml :
    Code (YAML):
    n1:
      world
    : world
      x
    : 1492.0
      y
    : 52.0
      z
    : -1800.0
    code:
    Code (Java):
                    World world1 = Bukkit.getWorld(config.getString("n1.world"));
                    double x1 = config.getDouble("n1.x");
                    double y1 = config.getDouble("n1.y");
                    double z1 = config.getDouble("n1.z");

                    Location loc1 = new Location(world1,x1,y1,z1);


                        if (is.equals(loc1) && event.getClickedBlock().getType() == Material.STONE_BUTTON) {
                     
                            player.sendMessage("radi");
                            RandomC.getRandom(1, player);
                     

                            return;
    }
     
     
  2. Can you post the code for the full method.
     
  3. Try adding debug statements throughout all of the code. Check if your listener is being accessed, and any other conditional statements you might have in the method. The entire section of code would also be helpful.
     
  4. You can store and get a location from a configuration file, not the answer to your solution, but a helpful tip.
     
  5. How?
     
  6. Just like any other type of variable :)

    Set the location:
    Code (Text):
    Location loc = new Location(world, x, y, z);
    getConfig.set("location", loc);
    Get the location:
    Code (Text):
    Location loc = getConfig.getLocation("location");
     
  7. My thoughts are just that the locations are not quite equal. You might try getting the block at the location of both locations and testing block equality.
     
  8. This might just be the problem. What you could try doing is rounding the locations to the location in the config. With this kept in mind you could either round up (ceiling) or round down (floor).
     
  9. Or you can just convert the locations to vectors, then take their difference and whenever the length of that is smaller then approximately 1 you should probably see it as the same location.
     
  10. I always forget about vectors in my Minecraft haha. Yep this would work too.
     
  11. Code (Java):


    When put location on stats.yml

                String world = player.getWorld().getName().toLowerCase();
                double x = event.getBlock().getLocation().getX();
                double y = event.getBlock().getLocation().getY();
                double z = event.getBlock().getLocation().getZ();
               
                config.set("n1." + "world", world);
                config.set("n1." + "x", Double.valueOf(x));
                config.set("n1." + "y", Double.valueOf(y));
                config.set("n1." + "z", Double.valueOf(z));
               
                try {
                    config.save(file);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
               
                player.sendMessage(Enums.getMessage(Enums.PREFIX) + ChatColor.GREEN + "Suscessfully!");
               
                return;
               
               
               
    Stats.yml:
    Code (YAML):
    n1:
      world
    : world
      x
    : 1492.0
      y
    : 52.0
      z
    : -1800.0
    PlayerInteractEvent right click on block
    Code (Java):

    @EventHandler
        public void leftCl(PlayerInteractEvent event){

                if(event.getClickedBlock() == null || event.getClickedBlock().getType() == Material.AIR){
                 return;
             }
           
            Location is = event.getClickedBlock().getLocation();
           
            Player player = event.getPlayer();

    if (event.getAction() == Action.LEFT_CLICK_BLOCK) {

        World world1 = Bukkit.getWorld(config.getString("n1.world"));
                    double x1 = config.getDouble("n1.x");
                    double y1 = config.getDouble("n1.y");
                    double z1 = config.getDouble("n1.z");
                   
    Location loc1 = new Location(world1,x1,y1,z1);


                        if (is.equals(loc1) && event.getClickedBlock().getType() == Material.STONE_BUTTON) {
                       
                            player.sendMessage("radi");
                            RandomC.getRandom(1, player);
                       

                            return;

                        }


    }
    }
     
  12. I am a beginner for real. I didn't know .yaml could store objects like that.
     
  13. I would first of all check if:
    1) is.getX() == loc1.getX()
    2) is.getY() == loc1.getY()
    3) is.getZ() == loc1.getZ()
    by printing out e.g. is.getX() and loc1.getX()
     
    #13 S0urc3C0d3, Jun 28, 2020
    Last edited: Jun 28, 2020
  14. doesn't work
     
  15. What didn't work? Printing out 6 values?

    EDIT:

    Do
    Code (Java):

    System.out.println("is-X:" + is.getX());
    System.out.println("loc1-X:" + loc1.getX());
    ...
     
    and then send what the server printed out
     
  16. Fixed: problem is

    Code (Java):
             if(event.getAction() == Action.LEFT_CLICK_BLOCK){



     
     
  17. A YAML file can store most kinds of objects. You can just call the get method for non supported objects and cast the return value's object type to the object you're returning to store it.
     
    • Agree Agree x 1
  18. I tried this with an ItemStack and a Location and it wouldn't save in the config. Is there something I need to do special?
     
  19. You need to save config if you using custom config
     
  20. Nah I was using default config, but using .set(ItemStack); wasnt working