package com.archivesmc.painter.integrations;

import com.archivesmc.painter.Painter;
import com.worldcretornica.plotme_core.Plot;
import com.worldcretornica.plotme_core.PlotId;
import com.worldcretornica.plotme_core.PlotMeCoreManager;
import com.worldcretornica.plotme_core.api.ILocation;
import com.worldcretornica.plotme_core.bukkit.api.BukkitBlock;
import com.worldcretornica.plotme_core.bukkit.api.BukkitLocation;
import com.worldcretornica.plotme_core.bukkit.api.BukkitWorld;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.util.HashMap;
import java.util.Map;

public class PlotMeIntegration implements Integration {
    private final Painter plugin;

    public PlotMeIntegration(final Painter plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean canEdit(final Block block, final Player player) {
        // This API is pretty silly
        PlotMeCoreManager manager = PlotMeCoreManager.getInstance();

        if (! manager.isPlotWorld(new BukkitBlock(block))) {
            // Not a plot world
            return true;
        }

        if (player.hasPermission("plotme.admin.buildanywhere")) {
            // Allowed to build anywhere
            return true;
        }

        ILocation location = new BukkitLocation(block.getLocation());
        PlotId plotId = manager.getPlotId(location);

        if (plotId == null) {
            // On the road
            return false;
        }

        Plot plot = manager.getPlotById(plotId, new BukkitWorld(block.getWorld()));

        // On the road also
        return plot != null && plot.isAllowed(player.getUniqueId());

    }

    @Override
    public String getPluginName() {
        return "PlotMe";
    }

    @Override
    public boolean setUp() {
        Plugin plotMePlugin = this.plugin.getServer().getPluginManager().getPlugin("PlotMe");

        return plotMePlugin != null && plotMePlugin.isEnabled();

    }

    @Override
    public void notifyNotAllowed(final Block block, final Player player) {
        // This API is pretty silly
        PlotMeCoreManager manager = PlotMeCoreManager.getInstance();
        ILocation location = new BukkitLocation(block.getLocation());
        Plot plot = manager.getPlotById(manager.getPlotId(location), new BukkitWorld(block.getWorld()));

        Map<String, String> args = new HashMap<>();

        args.put("name", plot.getPlotName());
        args.put("owner", plot.getOwner());

        this.plugin.sendMessage(player, "plotme_not_allowed", args);
    }

    @Override
    public void blockReplaced(final Block block, final Player player) {
        // Honey badger don't care
    }
}

@@ -1659,25 +1659,24 @@
 niqueId());%0A
-%0A
     %7D%0A%0A    @
@@ -2157,16 +2157,17 @@
 ance();%0A
+%0A
         
@@ -2232,37 +2232,41 @@
 ));%0A        Plot
+Id
  plot
+Id
  = manager.getPl
@@ -2267,21 +2267,139 @@
 .getPlot
-By
 Id(
+location);%0A%0A        if (plotId == null) %7B%0A            // On the road%0A            return;%0A        %7D%0A%0A        Plot plot = 
 manager.
@@ -2405,28 +2405,27 @@
 .getPlot
+By
 Id(
+p
 lo
-cation)
+tId
 , new Bu
@@ -2447,32 +2447,118 @@
 .getWorld()));%0A%0A
+        if (plot == null) %7B%0A            // On the road%0A            return;%0A        %7D%0A%0A
         Map%3CStri

Update PlotMe integration