PlayerInteractEvent Button

Discussion in 'Plugin Development' started by KingPhantom213, Jul 17, 2019.

Thread Status:
Not open for further replies.
  1. Hey, I will programm a Plguin on Spigot 1.8.8. When I press a Button on Location 1 then the Button will teleport me to location 3, wehn i press Button 2 then I will teleport to location 4.

    My Code is this:

    Code:
        @EventHandler
        public boolean onPlayerInteract(PlayerInteractEvent event) {
            //Define the player
            Player p = event.getPlayer();
        //If Cord in Config Button 1
        File file = new File("plugins//Talentshow//Button1.yml");
    
        if(!(file.exists())) {
        p.sendMessage(Main.prefix + "§4Es ist noch keine Location für Button 1 gesetzt!");
        return true;
        }
        //If Cord in Config Button 2
        File file1 = new File("plugins//Talentshow//Button2.yml");
    
        if(!(file1.exists())) {
        p.sendMessage(Main.prefix + "§4Es ist noch keine Location für Button 2 gesetzt!");
        return true;
        }
        //Become loc Button 1
        YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
        Location loc = p.getLocation();
        loc.setX(cfg.getDouble("Button1.loc.X"));
        loc.setY(cfg.getDouble("Button1.loc.Y"));
        loc.setZ(cfg.getDouble("Button1.loc.Z"));
        World welt = Bukkit.getWorld(cfg.getString("Button1.loc.Weltname"));
        loc.setWorld(welt);
        //If it Button 1 or 2
        if((file.exists())) {
        //Richt Click on Block
        if(event.getAction() == Action.RIGHT_CLICK_BLOCK) {
        //Check if it's a button
        if(event.getClickedBlock().getType() == Material.STONE_BUTTON) {
          p.teleport(loc);
            p.sendMessage("§2Teleport.");
            }
        }
        } else {
            event.setCancelled(true);
        }
        return false;
    }
    Thanks for the Help.
    Soory for my Bad english.
     
    Last edited: Jul 17, 2019
  2. Offline

    timtower Administrator Administrator Moderator

  3. It don't work, when i press they teleport me to button1 and not to button1loc and all buttons do the same and i will that the button on possiton 1 will teleport me to position 3 and button 2 on positon 4 and at the moment button 1 teleport me to 3 and button 2 teleport me to 4.
     
  4. Offline

    timtower Administrator Administrator Moderator

    @KingPhantom213 You do realize that you are only loading the variable file right?
    So you will always be teleported to the same place.
    Not to mention that the provided code never teleports.
     
  5. @timtower Yes I see I edit the Code with the New Teleport Line.
    Yes but it's the first code i will that the code now that is button on on positon 1, but the code think all buttons are do the same.
     
  6. Offline

    timtower Administrator Administrator Moderator

    @KingPhantom213 Probably has to do with the fact that you are not checking where the button is.
    And you are setting the location to the button location.
     
  7. @timtower Hier my new Code but at the moment he skip the teleport. and give me Test out because he mean the button is not on loc1

    Code:
        @EventHandler
        public boolean onPlayerInteract(PlayerInteractEvent event) {
            //Define the player
            Player p = event.getPlayer();
        //If Cord in Config Button 1
        File file = new File("plugins//Talentshow//Button1.yml");
    
        if(!(file.exists())) {
        p.sendMessage(Main.prefix + "§4Es ist noch keine Location für Button 1 gesetzt!");
        return true;
        }
        YamlConfiguration cfg1 = YamlConfiguration.loadConfiguration(file);
        Location loc1 = p.getLocation();
        loc1.setX(cfg1.getDouble("Button1.loc.X"));
        loc1.setY(cfg1.getDouble("Button1.loc.Y"));
        loc1.setZ(cfg1.getDouble("Button1.loc.Z"));
        World welt1 = Bukkit.getWorld(cfg1.getString("Button1.loc.Weltname"));
        loc1.setWorld(welt1);
        //If Cord in Config Button 2
        File file1 = new File("plugins//Talentshow//Button2.yml");
    
        if(!(file1.exists())) {
        p.sendMessage(Main.prefix + "§4Es ist noch keine Location für Button 2 gesetzt!");
        return true;
        }
        //Become loc Button 1
        File file2 = new File("plugins//Talentshow//Button1loc.yml");
    
        if(!(file2.exists())) {
        p.sendMessage(Main.prefix + "§4Es ist noch keine Location für Button 1 gesetzt!");
        return true;
        }
        YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file2);
        Location loc = p.getLocation();
        loc.setX(cfg.getDouble("Button1.loc.X"));
        loc.setY(cfg.getDouble("Button1.loc.Y"));
        loc.setZ(cfg.getDouble("Button1.loc.Z"));
        World welt = Bukkit.getWorld(cfg.getString("Button1.loc.Weltname"));
        loc.setWorld(welt);
        //If it Button 1 or 2
        if((file2.exists())) {
        //Richt Click on Block
        if(event.getAction() == Action.RIGHT_CLICK_BLOCK) {
            //Check if it's a button
        if(event.getClickedBlock().getType() == Material.STONE_BUTTON) {
            if(event.getClickedBlock().getLocation() == loc1) {
            p.teleport(loc);
            p.sendMessage("§2Teleport.");
            }else {
                p.sendMessage(Main.prefix + "§5Test");
            }
        }
        }
        } else {
            event.setCancelled(true);
        }
        return false;
    }
     
  8. Offline

    KarimAKL

    @KingPhantom213 That's because you are comparing the locations with '==', check if the X, Y and Z is the same.
     
  9. What did you mean which line?
     
  10. Offline

    timtower Administrator Administrator Moderator

  11. Yes when i put ther if(event.getClickedBlock().getLocation() = loc1) { then i become the left-hand side of an assignment must be a variable or waht you mean?
     
  12. Offline

    timtower Administrator Administrator Moderator

  13. Can you send me a example, because i dont know what you mean.

    I'm a Beginner in programming
     
  14. Offline

    KarimAKL

    @KingPhantom213 Make these checks:
    Code:Java
    1. if (block location world name) is equal to (loc1 world name)
    2. if (block location x) is equal to (loc1 x)
    3. if (block location y) is equal to (loc1 y)
    4. if (block location z) is equal to (loc1 z)
     
  15. Offline

    gnomik12

    Location loc=event.getClickedBlock().getLocation();

    if(loc.getBlockX() == loc1.getBlockX() && loc.getBlockY() == loc1.getBlockY() && loc.getBlockZ() == loc1.getBlockZ()) {

    }
     
Thread Status:
Not open for further replies.

Share This Page