Solved PlotSquared test location is inside plot + perms

Discussion in 'Spigot Plugin Development' started by TeamBergerhealer, Feb 2, 2020.

  1. TeamBergerhealer

    Supporter

    I'm currently busy integrating PlotSquared support into one of my plugins. With PlotSquared I have the following questions to answer:
    • Is this a World where PlotSquared is used?
    • What plot (plots?) are active at a given position
    • Does the player have permission to modify this plot? Build here? Some permission rule?
    Basically, this is all I got:
    Code (Java):
    /**
    * Handles node editing events using PlotSquared permissions
    */

    public class PlotSquaredHandler implements Listener {
        private final PlotAPI api = new PlotAPI();

        @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
        public void onCoasterNodeEvent(CoasterNodeEvent event) {
            event.setCancelled(!checkAllowed(event.getPlayer(), event.getNode().getPosition()));
        }

        @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
        public void onCoasterConnectionEvent(CoasterConnectionEvent event) {
            event.setCancelled(!checkAllowed(event.getPlayer(), event.getConnection().getNodeA().getPosition()) ||
                               !checkAllowed(event.getPlayer(), event.getConnection().getNodeB().getPosition()));
        }

        // Checks whether the node at the position can be modified by the player
        private boolean checkAllowed(Player player, Vector position) {
            //TODO!
            return true;
        }
    }
    There are a lot of nodes, and those events can fire quite frequently. I assume a plot world can have thousands of plots over the years, so iterating all plots of a world and checking whether the x/z position is contained inside seems very inefficient.

    I have found this method:
    Code (Java):
    PlotArea area = api.getPlotSquared().getApplicablePlotArea(Location);
    But its in the implementation code, and Im not sure if its OK to use this. In the implementation it has a few questionable things where it seems to assume plots never overlap. (is this true?)

    How do you obtain a plot area at x/y/z and whether a player has build permissions/permission node? How to know what worlds PlotSquared is actually active?
     
    • Like Like x 1
  2. TeamBergerhealer

    Supporter

    @Nutty101 Ah Plot.getPlot(Location) is a thing, that helps a lot. It probably just calls the method I mentioned, but it's good that it is inside the API. Then the only question remains how to check whether a world is a 'plot world' and whether a player has a given permission / build permission in a plot.

    I see use of getTrusted(), getMembers() and isOwner(), using player UUID to check. Is the getMembers() the players that are added to a plot that only have permission when a trusted owner is online? Do I check that an owner is online myself?
     
    • Informative Informative x 1
  3. Shouldn't really matter, just pass the Users UUID. OfflinePlayer or Player will have them.
     
  4. If her plot returns null it’s most likely not a plot world
     
  5. TeamBergerhealer

    Supporter

    @Nutty101 Im only worried that if I do getMembers().contains(uuid) to check, it will allows players to modify the plot when no owners/trusted are online. Is this something I should implement myself?

    @Maxx_Qc fair point, I guess on a plot world every area is a plot. I can add a default permission for outside plots.
     
  6. Nah, this is just a readonly list.
     
  7. TeamBergerhealer

    Supporter

    I looked a bit more closely at the Plot API and found out they have a method I needed: isAdded(UUID). This method checks whether a Player is an owner/trusted, denied, and if a member, that another player is online. No further implementation needed on my end.

    In summary, I ended up doing:
    Code (Java):
        // Checks whether the node at the position can be modified by the player
        private boolean checkAllowed(Player player, Vector position) {
            // If player has global USE permission, the player can use the plugin anywhere
            if (TCCoastersPermissions.USE.has(player)) {
                return true;
            }

            // Note: TCCCoastersListener already checks whether player has the plotsquared use permission
            //       At event priority LOW the event would already be cancelled if that permission was absent.

            // Re-use Location
            location.setWorld(player.getWorld().getName());
            location.setX(position.getBlockX());
            location.setY(position.getBlockY());
            location.setZ(position.getBlockZ());

            // Find plot area at Location
            Plot plot = Plot.getPlot(location);
            if (plot == null) {
                //TODO: Message can not edit outside own plots
                return false;
            }

            // Check whether the player in question is an Owner
            if (!plot.isAdded(player.getUniqueId())) {
                //TODO: Message you have no permission in plot
                return false;
            }

            return true;
        }
     
    • Informative Informative x 1