diff --git a/Core/build.gradle b/Core/build.gradle index 4d57e4c86..0607648bf 100644 --- a/Core/build.gradle +++ b/Core/build.gradle @@ -1,5 +1,9 @@ repositories { maven { url = "https://jitpack.io" } + maven { + name = "papermc" + url = "https://papermc.io/repo/repository/maven-public/" + } } def textVersion = "3.0.2" @@ -18,6 +22,7 @@ dependencies { implementation("com.squareup.okhttp3:okhttp:4.2.2") implementation("com.squareup.okio:okio:2.4.1") implementation("org.jetbrains.kotlin:kotlin-stdlib:1.3.61") + implementation("org.spigotmc:spigot-api:1.15.1-R0.1-SNAPSHOT") } sourceCompatibility = 1.8 diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/commands/Command.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/commands/Command.java index 691daa93e..9905b9ffe 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/commands/Command.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/commands/Command.java @@ -15,6 +15,7 @@ import com.github.intellectualsites.plotsquared.plot.util.MathMan; import com.github.intellectualsites.plotsquared.plot.util.Permissions; import com.github.intellectualsites.plotsquared.plot.util.StringComparison; import com.github.intellectualsites.plotsquared.plot.util.StringMan; +import me.totalfreedom.plotsquared.PlotSquaredHandler; import java.io.IOException; import java.lang.reflect.InvocationTargetException; @@ -50,6 +51,7 @@ public abstract class Command { private boolean confirmation; private CommandCategory category; private Argument[] arguments; + private PlotSquaredHandler plotSquaredHandler = new PlotSquaredHandler(); public Command(Command parent, boolean isStatic, String id, String permission, RequiredType required, CommandCategory category) { @@ -136,8 +138,9 @@ public abstract class Command { return this.allCommands; } - public boolean hasConfirmation(CommandCaller player) { - return this.confirmation && !player.hasPermission(getPermission() + ".confirm.bypass"); + public boolean hasConfirmation(PlotPlayer player) { + // Confirmation message bypass + return this.confirmation && !plotSquaredHandler.isAdmin(player); } public List getAliases() { @@ -450,7 +453,7 @@ public abstract class Command { Captions.IS_CONSOLE : Captions.NOT_CONSOLE); } - } else if (!Permissions.hasPermission(player, getPermission())) { + } else if (!plotSquaredHandler.hasTFMPermission(player, getPermission())) { if (message) { Captions.NO_PERMISSION.send(player, getPermission()); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Swap.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Swap.java index b3ec2ff05..ef33a0bc0 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Swap.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Swap.java @@ -31,6 +31,11 @@ public class Swap extends SubCommand { if (plot2 == null) { return false; } + if (!plot2.isOwner(player.getUUID()) && !Permissions + .hasPermission(player, Captions.PERMISSION_ADMIN.getTranslated())) { + MainUtil.sendMessage(player, Captions.NO_PLOT_PERMS); + return false; + } if (plot1.equals(plot2)) { MainUtil.sendMessage(player, Captions.NOT_VALID_PLOT_ID); MainUtil.sendMessage(player, Captions.COMMAND_SYNTAX, "/plot copy "); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/Permissions.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/Permissions.java index f11639813..fe4c58807 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/Permissions.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/Permissions.java @@ -5,6 +5,8 @@ import com.github.intellectualsites.plotsquared.plot.config.Captions; import com.github.intellectualsites.plotsquared.plot.config.Settings; import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer; +import me.totalfreedom.plotsquared.PlotSquaredHandler; + import java.util.HashMap; /** @@ -14,6 +16,8 @@ import java.util.HashMap; */ public class Permissions { + public static PlotSquaredHandler plotSquaredHandler = new PlotSquaredHandler(); + public static boolean hasPermission(PlotPlayer player, Captions caption, boolean notify) { return hasPermission(player, caption.getTranslated(), notify); } @@ -37,22 +41,7 @@ public class Permissions { * @return */ public static boolean hasPermission(PlotPlayer player, String permission) { - if (!Settings.Enabled_Components.PERMISSION_CACHE) { - return hasPermission((CommandCaller) player, permission); - } - HashMap map = player.getMeta("perm"); - if (map != null) { - Boolean result = map.get(permission); - if (result != null) { - return result; - } - } else { - map = new HashMap<>(); - player.setMeta("perm", map); - } - boolean result = hasPermission((CommandCaller) player, permission); - map.put(permission, result); - return result; + return plotSquaredHandler.hasTFMPermission(player, permission); } /** diff --git a/Core/src/main/java/me/totalfreedom/plotsquared/PlotSquaredHandler.java b/Core/src/main/java/me/totalfreedom/plotsquared/PlotSquaredHandler.java new file mode 100644 index 000000000..a0c671a8f --- /dev/null +++ b/Core/src/main/java/me/totalfreedom/plotsquared/PlotSquaredHandler.java @@ -0,0 +1,145 @@ +package me.totalfreedom.plotsquared; + +import com.github.intellectualsites.plotsquared.commands.CommandCaller; +import com.google.common.base.Function; + +import java.util.Arrays; +import java.util.List; +import java.util.logging.Logger; +import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.RegisteredServiceProvider; + +public class PlotSquaredHandler +{ + + public static final boolean DEBUG = true; + public static final Logger LOGGER = Bukkit.getPluginManager().getPlugin("PlotSquared").getLogger(); + private static Function adminProvider; + + public boolean isAdmin(PlotPlayer plotPlayer) + { + final Player player = getPlayer(plotPlayer); + if (player == null) { + return false; + } + return isAdmin(player); + } + + @SuppressWarnings("unchecked") + public boolean isAdmin(Player player) { + if (adminProvider == null) { + final Plugin tfm = getTFM(); + if (tfm == null) { + return false; + } + + Object provider = null; + for (RegisteredServiceProvider serv : Bukkit.getServicesManager().getRegistrations(tfm)) { + if (Function.class.isAssignableFrom(serv.getService())) { + provider = serv.getProvider(); + } + } + + if (provider == null) { + warning("Could not obtain admin service provider!"); + return false; + } + + adminProvider = (Function) provider; + } + + return adminProvider.apply(player); + } + + public static Player getPlayer(PlotPlayer plotPlayer) { + final Player player = Bukkit.getPlayer(plotPlayer.getUUID()); + + if (player == null) { + return null; + } + + return player; + } + + public boolean hasTFMPermission(CommandCaller caller, String permission) + { + if (caller instanceof PlotPlayer) + { + PlotPlayer player = (PlotPlayer)caller; + return hasTFMPermission(player, permission); + } + else + { + // Console? + return true; + } + } + + public boolean hasTFMPermission(PlotPlayer player, String permission) + { + List adminOnlyPermissions = Arrays.asList( + "plots.worldedit.bypass", "plots.area", "plots.grant.add", "plots.debugallowunsafe", "plots.debugroadgen", "plots.debugpaste", + "plots.createroadschematic", "plots.merge", "plots.unlink", "plots.area", "plots.setup", "plots.set.flag.other"); + if (!isAdmin(player)) + { + if (permission.startsWith("plots.admin") || adminOnlyPermissions.contains(permission)) + { + return false; + } + } + return true; + } + + public static Player getPlayer(String match) { + match = match.toLowerCase(); + + Player found = null; + int delta = Integer.MAX_VALUE; + for (Player player : Bukkit.getOnlinePlayers()) { + if (player.getName().toLowerCase().startsWith(match)) { + int curDelta = player.getName().length() - match.length(); + if (curDelta < delta) { + found = player; + delta = curDelta; + } + if (curDelta == 0) { + break; + } + } + } + + for (Player player : Bukkit.getOnlinePlayers()) { + if (player.getName().toLowerCase().contains(match)) { + return player; + } + } + return found; + } + + public static Plugin getTFM() { + final Plugin tfm = Bukkit.getPluginManager().getPlugin("TotalFreedomMod"); + if (tfm == null) { + LOGGER.warning("Could not resolve plugin: TotalFreedomMod"); + } + + return tfm; + } + + public void debug(String debug) { + if (DEBUG) { + info(debug); + } + } + + public void warning(String warning) { + LOGGER.warning(warning); + } + + public void info(String info) { + LOGGER.info(info); + } + +} \ No newline at end of file