[WorldGuardApi] How can i check if at a given Location building is allowed?

Discussion in 'Spigot Plugin Development' started by ENCOM, Aug 14, 2019.

  1. hi there im trying for 3 days to find a way to check if at a given location, players can build.
    i want to create some structures at random locations but i dont want them to be
    generated in lets say regions like the spawn area and its buildings.

    i found a way to check if a player can build at given location but i couldnt find a way to check
    without a player as param, im so frustrated:/ could someone help me?
    (i use 1.14)
     
    #1 ENCOM, Aug 14, 2019
    Last edited: Aug 14, 2019
  2. If you're going by the wiki, you should have something along the lines of
    Code (Java):

    LocalPlayer localPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
    Location loc = new Location(world, 10, 64, 100);
    RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
    RegionQuery query = container.createQuery();

    if (!query.testState(loc, localPlayer, Flags.BUILD)) {
       // Can't build
    }
     
    However, if you look at the source, you'll see:
    Code (Java):

    /**
    * Test whether the (effective) value for a list of state flags equals
    * {@code ALLOW}.
    *
    * <p>{@code player} can be non-null to satisfy region group requirements,
    * otherwise it will be assumed that the caller that is not a member of any
    * regions. (Flags on a region can be changed so that they only apply
    * to certain users.) The player argument is required if the
    * {@link Flags#BUILD} flag is in the list of flags.</p>
    *
    * <p>This method does not check the region bypass permission. That must
    * be done by the calling code.</p>
    *
    * @param location the location
    * @param player an optional player, which would be used to determine the region group to apply
    * @param flag the flag
    * @return true if the result was {@code ALLOW}
    * @see RegionResultSet#queryValue(RegionAssociable, Flag)
    */

    public boolean testState(Location location, @Nullable LocalPlayer player, StateFlag... flag) {
    return StateFlag.test(queryState(location, player, flag));
    }
     
    Note the @ Nullable annotation before LocalPlayer. You should be able to pass null instead of the player.
     
    • Friendly Friendly x 1
  3. thanks! but i cant use null:/ 1.PNG 2.PNG
     
  4. What version of WorldGuard are you building against exactly?
     
    • Friendly Friendly x 1
  5. worldguard-bukkit-7.0.1-rc2-dist
     
    • Useful Useful x 1
  6. "Outdated" is a key feature of the worldguard Wiki...
     
  7. 1.PNG
    okay this must it be, how can i convert a bukkit location to a worldguard one?
     
  8. I sent you the link to com.sk89q.worldedit.util.Location. Pick a constructor. You'll need an instance of Extent, I posted a link to the different implementations, one of which you need to use. I would try NullExtent first, but I really don't know enough about the inner workings to tell you whether or not it'll work. If it's not documented, it'll be trial and error.
     
  9. Read before replying please.
     
  10. Oh my bad, wiki is definitely outdated, didn't see that earlier comment. Unfortunate that it is, worldedit/guard is a great plugin, needs better documentation since the 7.0 update imo. I've only used 6.1 because it's what most servers use and it has a lot of documentation still fortunately.