Trying to execute code when a player is standing on a location

Discussion in 'Plugin Development' started by xpaintall, Jul 24, 2021.

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

    xpaintall

    So basically I have a PlayerMoveEvent and I'm trying to detect if a player is standing in a location, or one of the locations specified in the config.
    Code:
    @EventHandler
        public void onWalk(PlayerMoveEvent event) {
            Player player = event.getPlayer();
            for(String s : f.getConfig().getStringList("Levels.")) {
                Location loc = player.getLocation();
                Location lo = (Location) f.getConfig().get("Levels." + s + ".FinishLocation");
                if(loc.getBlockX() == lo.getBlockX() && loc.getBlockY() == lo.getBlockY() && loc.getBlockZ() == lo.getBlockZ()) {
                    f.finishLevel(player, s, (int) f.getConfig().get("Levels." + s + ".CompletionReward"), m.getLevel(s).getCompleted().add(player.getUniqueId()), (Location) f.getConfig().get("Levels." + s + ".LobbyLocation"));
                }
            }
        }
     
  2. Offline

    Shqep

    @xpaintall
    PHP:
    for(String s f.getConfig().getStringList("Levels.")) {}
    The getStringList("Levels.") is probably your problem. You will see how this works here:

    My test YAML
    Code:
    List:
      - "never"
      - "gonna"
      - "give"
      - "you"
      - "up"
    Test code:
    PHP:
    System.out.println(config.getStringList("List."));
    System.out.println(config.getStringList("List"));
    Test results:
    Code:
    []
    ["never", "gonna", "give", "you", "up"]
    This probably covered it well enough already.

    EDIT: In case you want to get the KEYS instead:

    Test YAML
    Code:
    List:
      a:
        - "Rick Astley"
      b:
        - "I'm not rickrolling you"
    Test code:
    PHP:
    System.out.println(config.getConfigurationSection("List").getKeys(false));
    System.out.println(config.getString("List.a"));
    Test results:
    Code:
    ["a", "b"]
    Rick Astley
     
  3. Offline

    Tim_M

    If you want to be sure then just create a setter and getter for a location, so that your config looks like this:
    Code:
    location1:
       world: "world"
       x: 2
       y: 4
       z: 5
    
     
  4. Offline

    davidclue

    @xpaintall
    Use getTo and getFrom to check if they move a block over so it doesn't impact your performance as much, or you could just use a repeating task that just gets nearby entities within a 1 block radius from your location and checks if they are a player, this is the best method I know of for this kind of thing.
     
Thread Status:
Not open for further replies.

Share This Page