diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java index d83813d14..8a9831f22 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitPlatform.java @@ -103,7 +103,6 @@ import com.plotsquared.core.util.ConsoleColors; import com.plotsquared.core.util.EconHandler; import com.plotsquared.core.util.EventDispatcher; import com.plotsquared.core.util.FileUtils; -import com.plotsquared.core.util.PermHandler; import com.plotsquared.core.util.PlatformWorldManager; import com.plotsquared.core.util.PremiumVerification; import com.plotsquared.core.util.ReflectionUtils; @@ -181,7 +180,6 @@ import static com.plotsquared.core.util.ReflectionUtils.getRefClass; private boolean methodUnloadSetup = false; private boolean metricsStarted; private EconHandler econ; - private PermHandler perm; @Getter private Injector injector; @@ -340,10 +338,7 @@ import static com.plotsquared.core.util.ReflectionUtils.getRefClass; // Economy if (Settings.Enabled_Components.ECONOMY) { TaskManager.runTask(() -> { - final PermHandler permHandler = getInjector().getInstance(PermHandler.class); - if (permHandler != null) { - permHandler.init(); - } + this.getPermissionHandler().initialize(); final EconHandler econHandler = getInjector().getInstance(EconHandler.class); if (econHandler != null) { econHandler.init(); diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/inject/BukkitModule.java b/Bukkit/src/main/java/com/plotsquared/bukkit/inject/BukkitModule.java index b558aee4f..7599358bd 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/inject/BukkitModule.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/inject/BukkitModule.java @@ -29,7 +29,6 @@ import com.google.inject.AbstractModule; import com.google.inject.Provides; import com.google.inject.Singleton; import com.google.inject.assistedinject.FactoryModuleBuilder; -import com.google.inject.util.Providers; import com.plotsquared.bukkit.BukkitPlatform; import com.plotsquared.bukkit.player.BukkitPlayerManager; import com.plotsquared.bukkit.queue.BukkitLocalQueue; @@ -37,12 +36,9 @@ import com.plotsquared.bukkit.schematic.BukkitSchematicHandler; import com.plotsquared.bukkit.util.BukkitChunkManager; import com.plotsquared.bukkit.util.BukkitEconHandler; import com.plotsquared.bukkit.util.BukkitInventoryUtil; -import com.plotsquared.bukkit.util.BukkitPermHandler; import com.plotsquared.bukkit.util.BukkitRegionManager; import com.plotsquared.bukkit.util.BukkitSetupUtils; import com.plotsquared.bukkit.util.BukkitUtil; -import com.plotsquared.bukkit.util.task.PaperTimeConverter; -import com.plotsquared.bukkit.util.task.SpigotTimeConverter; import com.plotsquared.core.PlotPlatform; import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.generator.HybridGen; @@ -58,7 +54,6 @@ import com.plotsquared.core.queue.QueueProvider; import com.plotsquared.core.util.ChunkManager; import com.plotsquared.core.util.EconHandler; import com.plotsquared.core.util.InventoryUtil; -import com.plotsquared.core.util.PermHandler; import com.plotsquared.core.util.PlayerManager; import com.plotsquared.core.util.RegionManager; import com.plotsquared.core.util.SchematicHandler; @@ -70,7 +65,9 @@ import lombok.RequiredArgsConstructor; import org.bukkit.Bukkit; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.plugin.java.JavaPlugin; + import javax.annotation.Nonnull; +import javax.annotation.Nullable; @RequiredArgsConstructor public class BukkitModule extends AbstractModule { @@ -93,7 +90,6 @@ import javax.annotation.Nonnull; bind(ChunkManager.class).to(BukkitChunkManager.class); bind(RegionManager.class).to(BukkitRegionManager.class); bind(SchematicHandler.class).to(BukkitSchematicHandler.class); - this.setupVault(); if (Settings.Enabled_Components.WORLDS) { bind(PlotAreaManager.class).to(SinglePlotAreaManager.class); } else { @@ -102,25 +98,14 @@ import javax.annotation.Nonnull; install(new FactoryModuleBuilder().build(HybridPlotWorldFactory.class)); } - private void setupVault() { + @Provides @Singleton @Nullable EconHandler provideEconHandler() { if (Bukkit.getPluginManager().isPluginEnabled("Vault")) { - BukkitPermHandler bukkitPermHandler = null; try { - bukkitPermHandler = new BukkitPermHandler(); - bind(PermHandler.class).toInstance(bukkitPermHandler); + return new BukkitEconHandler(); } catch (final Exception ignored) { - bind(PermHandler.class).toProvider(Providers.of(null)); } - try { - final BukkitEconHandler bukkitEconHandler = new BukkitEconHandler(bukkitPermHandler); - bind(EconHandler.class).toInstance(bukkitEconHandler); - } catch (final Exception ignored) { - bind(EconHandler.class).toProvider(Providers.of(null)); - } - } else { - bind(PermHandler.class).toProvider(Providers.of(null)); - bind(EconHandler.class).toProvider(Providers.of(null)); } + return null; } } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/inject/PermissionModule.java b/Bukkit/src/main/java/com/plotsquared/bukkit/inject/PermissionModule.java index b22cb3648..de42d884a 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/inject/PermissionModule.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/inject/PermissionModule.java @@ -31,17 +31,13 @@ import com.google.inject.Singleton; import com.plotsquared.bukkit.permissions.BukkitPermissionHandler; import com.plotsquared.bukkit.permissions.VaultPermissionHandler; import com.plotsquared.core.permissions.PermissionHandler; -import net.milkbowl.vault.permission.Permission; import org.bukkit.Bukkit; -import org.bukkit.plugin.RegisteredServiceProvider; public class PermissionModule extends AbstractModule { @Provides @Singleton PermissionHandler providePermissionHandler() { try { - RegisteredServiceProvider permissionProvider = - Bukkit.getServer().getServicesManager().getRegistration(Permission.class); - if (permissionProvider != null) { + if (Bukkit.getPluginManager().isPluginEnabled("Vault")) { return new VaultPermissionHandler(); } } catch (final Exception ignored) { diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/permissions/BukkitPermissionHandler.java b/Bukkit/src/main/java/com/plotsquared/bukkit/permissions/BukkitPermissionHandler.java index bceecda3e..6aca7ea62 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/permissions/BukkitPermissionHandler.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/permissions/BukkitPermissionHandler.java @@ -35,6 +35,7 @@ import com.plotsquared.core.player.PlotPlayer; import org.bukkit.entity.Player; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.lang.ref.WeakReference; import java.util.EnumSet; import java.util.Optional; @@ -74,7 +75,8 @@ public class BukkitPermissionHandler implements PermissionHandler { this.playerReference = new WeakReference<>(player); } - @Override public boolean hasPermission(@Nonnull final String permission) { + @Override public boolean hasPermission(@Nullable final String world, + @Nonnull final String permission) { final Player player = this.playerReference.get(); return player != null && player.hasPermission(permission); } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/permissions/VaultPermissionHandler.java b/Bukkit/src/main/java/com/plotsquared/bukkit/permissions/VaultPermissionHandler.java index ec2724e6a..b7b08a49f 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/permissions/VaultPermissionHandler.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/permissions/VaultPermissionHandler.java @@ -26,19 +26,25 @@ package com.plotsquared.bukkit.permissions; import com.plotsquared.bukkit.player.BukkitOfflinePlayer; +import com.plotsquared.bukkit.player.BukkitPlayer; +import com.plotsquared.core.permissions.ConsolePermissionProfile; +import com.plotsquared.core.permissions.PermissionHandler; import com.plotsquared.core.permissions.PermissionProfile; +import com.plotsquared.core.player.ConsolePlayer; import com.plotsquared.core.player.OfflinePlotPlayer; +import com.plotsquared.core.player.PlotPlayer; import net.milkbowl.vault.permission.Permission; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.plugin.RegisteredServiceProvider; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.EnumSet; import java.util.Optional; import java.util.Set; -public class VaultPermissionHandler extends BukkitPermissionHandler { +public class VaultPermissionHandler implements PermissionHandler { private Permission permissions; @@ -53,6 +59,17 @@ public class VaultPermissionHandler extends BukkitPermissionHandler { } } + @Nonnull @Override public Optional getPermissionProfile( + @Nonnull PlotPlayer playerPlotPlayer) { + if (playerPlotPlayer instanceof BukkitPlayer) { + final BukkitPlayer bukkitPlayer = (BukkitPlayer) playerPlotPlayer; + return Optional.of(new VaultPermissionProfile(bukkitPlayer.getPlatformPlayer())); + } else if (playerPlotPlayer instanceof ConsolePlayer) { + return Optional.of(ConsolePermissionProfile.INSTANCE); + } + return Optional.empty(); + } + @Nonnull @Override public Optional getPermissionProfile( @Nonnull OfflinePlotPlayer offlinePlotPlayer) { if (offlinePlotPlayer instanceof BukkitOfflinePlayer) { @@ -62,7 +79,9 @@ public class VaultPermissionHandler extends BukkitPermissionHandler { } @Nonnull @Override public Set getCapabilities() { - return EnumSet.of(PermissionHandlerCapability.ONLINE_PERMISSIONS, PermissionHandlerCapability.OFFLINE_PERMISSIONS); + return EnumSet.of(PermissionHandlerCapability.PER_WORLD_PERMISSIONS, + PermissionHandlerCapability.ONLINE_PERMISSIONS, + PermissionHandlerCapability.OFFLINE_PERMISSIONS); } @@ -74,11 +93,15 @@ public class VaultPermissionHandler extends BukkitPermissionHandler { this.offlinePlayer = offlinePlayer; } - @Override public boolean hasPermission(@Nonnull final String permission) { + @Override public boolean hasPermission(@Nullable final String world, + @Nonnull final String permission) { if (permissions == null) { return false; } - return permissions.playerHas(null, offlinePlayer, permission); + if (world == null && offlinePlayer instanceof BukkitPlayer) { + return permissions.playerHas(((BukkitPlayer) offlinePlayer).getPlatformPlayer(), permission); + } + return permissions.playerHas(world, offlinePlayer, permission); } } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitOfflinePlayer.java b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitOfflinePlayer.java index e9c74dfb9..9226c5847 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitOfflinePlayer.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitOfflinePlayer.java @@ -31,6 +31,7 @@ import com.plotsquared.core.permissions.PermissionProfile; import com.plotsquared.core.player.OfflinePlotPlayer; import org.bukkit.OfflinePlayer; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.UUID; @@ -68,8 +69,9 @@ public class BukkitOfflinePlayer implements OfflinePlotPlayer { return this.player.getName(); } - @Override public boolean hasPermission(@Nonnull final String permission) { - return this.permissionProfile.hasPermission(permission); + @Override public boolean hasPermission(@Nullable final String world, + @Nonnull final String permission) { + return this.permissionProfile.hasPermission(world, permission); } } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java index 0fcc50fb0..1415c2840 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/player/BukkitPlayer.java @@ -54,6 +54,8 @@ import org.bukkit.event.EventException; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.plugin.RegisteredListener; + +import javax.annotation.Nonnegative; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -164,7 +166,8 @@ public class BukkitPlayer extends PlotPlayer { } } - @Override public int hasPermissionRange(final String stub, final int range) { + @Override @Nonnegative public int hasPermissionRange(@Nonnull final String stub, + @Nonnegative final int range) { if (hasPermission(Captions.PERMISSION_ADMIN.getTranslated())) { return Integer.MAX_VALUE; } @@ -224,10 +227,6 @@ public class BukkitPlayer extends PlotPlayer { return max; } - @Override public boolean isPermissionSet(final String permission) { - return this.player.isPermissionSet(permission); - } - @Override public void sendMessage(String message) { message = message.replace('\u2010', '%').replace('\u2020', '&').replace('\u2030', '&'); if (!StringMan.isEqual(this.getMeta("lastMessage"), message) || ( diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEconHandler.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEconHandler.java index c7356c13b..470fbbfbb 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEconHandler.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitEconHandler.java @@ -25,31 +25,21 @@ */ package com.plotsquared.bukkit.util; -import com.google.inject.Inject; import com.google.inject.Singleton; import com.plotsquared.bukkit.player.BukkitOfflinePlayer; import com.plotsquared.bukkit.player.BukkitPlayer; import com.plotsquared.core.player.OfflinePlotPlayer; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.util.EconHandler; -import com.plotsquared.core.util.PermHandler; import net.milkbowl.vault.economy.Economy; import org.bukkit.Bukkit; import org.bukkit.plugin.RegisteredServiceProvider; -import javax.annotation.Nullable; @Singleton public class BukkitEconHandler extends EconHandler { private Economy econ; - private final PermHandler permHandler; - - @Inject public BukkitEconHandler(@Nullable final PermHandler permHandler) { - this.permHandler = permHandler; - } - - @Override - public boolean init() { + @Override public boolean init() { if (this.econ == null) { setupEconomy(); } @@ -87,17 +77,6 @@ import javax.annotation.Nullable; this.econ.depositPlayer(((BukkitOfflinePlayer) player).player, amount); } - /** - * @deprecated Use {@link PermHandler#hasPermission(String, String, String)} instead - */ - @Deprecated @Override public boolean hasPermission(String world, String player, String perm) { - if (this.permHandler != null) { - return this.permHandler.hasPermission(world, player, perm); - } else { - return false; - } - } - @Override public double getBalance(PlotPlayer player) { return this.econ.getBalance(player.getName()); } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitPermHandler.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitPermHandler.java deleted file mode 100644 index 8987e3527..000000000 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/BukkitPermHandler.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * _____ _ _ _____ _ - * | __ \| | | | / ____| | | - * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | - * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | - * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | - * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| - * | | - * |_| - * PlotSquared plot management system for Minecraft - * Copyright (C) 2020 IntellectualSites - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.plotsquared.bukkit.util; - -import com.google.inject.Singleton; -import com.plotsquared.core.util.PermHandler; -import net.milkbowl.vault.permission.Permission; -import org.bukkit.Bukkit; -import org.bukkit.plugin.RegisteredServiceProvider; - -@Singleton public class BukkitPermHandler extends PermHandler { - - private Permission perms; - - @Override - public boolean init() { - if (this.perms == null) { - setupPermissions(); - } - return this.perms != null; - } - - private void setupPermissions() { - if (Bukkit.getServer().getPluginManager().getPlugin("Vault") == null) { - return; - } - RegisteredServiceProvider permissionProvider = - Bukkit.getServer().getServicesManager().getRegistration(Permission.class); - if (permissionProvider != null) { - this.perms = permissionProvider.getProvider(); - } - } - - @Override public boolean hasPermission(String world, String player, String perm) { - return this.perms.playerHas(world, Bukkit.getOfflinePlayer(player), perm); - } -} diff --git a/Core/src/main/java/com/plotsquared/core/command/Auto.java b/Core/src/main/java/com/plotsquared/core/command/Auto.java index ea6a6cf6c..211f5f22f 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Auto.java +++ b/Core/src/main/java/com/plotsquared/core/command/Auto.java @@ -37,6 +37,7 @@ import com.plotsquared.core.events.PlayerAutoPlotEvent; import com.plotsquared.core.events.PlotAutoMergeEvent; import com.plotsquared.core.events.Result; import com.plotsquared.core.events.TeleportCause; +import com.plotsquared.core.permissions.PermissionHandler; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.PlotArea; @@ -175,9 +176,11 @@ public class Auto extends SubCommand { @Override public boolean onCommand(final PlotPlayer player, String[] args) { PlotArea plotarea = player.getApplicablePlotArea(); if (plotarea == null) { - if (this.econHandler != null) { - for (PlotArea area : this.plotAreaManager.getAllPlotAreas()) { - if (this.econHandler.hasPermission(area.getWorldName(), player.getName(), "plots.auto")) { + final PermissionHandler permissionHandler = PlotSquared.platform().getPermissionHandler(); + if (permissionHandler.hasCapability( + PermissionHandler.PermissionHandlerCapability.PER_WORLD_PERMISSIONS)) { + for (final PlotArea area : this.plotAreaManager.getAllPlotAreas()) { + if (player.hasPermission(area.getWorldName(), "plots.auto")) { if (plotarea != null) { plotarea = null; break; diff --git a/Core/src/main/java/com/plotsquared/core/command/Command.java b/Core/src/main/java/com/plotsquared/core/command/Command.java index a0dbe9079..60512dd5a 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Command.java +++ b/Core/src/main/java/com/plotsquared/core/command/Command.java @@ -35,6 +35,7 @@ import com.plotsquared.core.util.StringComparison; import com.plotsquared.core.util.StringMan; import com.plotsquared.core.util.task.RunnableVal2; import com.plotsquared.core.util.task.RunnableVal3; +import com.plotsquared.core.permissions.PermissionHolder; import lombok.SneakyThrows; import javax.annotation.Nonnull; @@ -158,7 +159,7 @@ public abstract class Command { return this.allCommands; } - public boolean hasConfirmation(CommandCaller player) { + public boolean hasConfirmation(PermissionHolder player) { return this.confirmation && !player.hasPermission(getPermission() + ".confirm.bypass"); } diff --git a/Core/src/main/java/com/plotsquared/core/command/CommandCaller.java b/Core/src/main/java/com/plotsquared/core/command/CommandCaller.java index b26eed517..f7a6c6885 100644 --- a/Core/src/main/java/com/plotsquared/core/command/CommandCaller.java +++ b/Core/src/main/java/com/plotsquared/core/command/CommandCaller.java @@ -34,14 +34,6 @@ public interface CommandCaller { */ void sendMessage(String message); - /** - * Check the player's permissions. Will be cached if permission caching is enabled. - * - * @param permission the name of the permission - */ - boolean hasPermission(String permission); - - boolean isPermissionSet(String permission); - RequiredType getSuperCaller(); + } diff --git a/Core/src/main/java/com/plotsquared/core/permissions/ConsolePermissionProfile.java b/Core/src/main/java/com/plotsquared/core/permissions/ConsolePermissionProfile.java index 09857628c..783f605a7 100644 --- a/Core/src/main/java/com/plotsquared/core/permissions/ConsolePermissionProfile.java +++ b/Core/src/main/java/com/plotsquared/core/permissions/ConsolePermissionProfile.java @@ -26,11 +26,13 @@ package com.plotsquared.core.permissions; import javax.annotation.Nonnull; +import javax.annotation.Nullable; public enum ConsolePermissionProfile implements PermissionProfile { INSTANCE; - @Override public boolean hasPermission(@Nonnull final String permission) { + @Override public boolean hasPermission(@Nullable final String world, + @Nonnull final String permission) { return true; } diff --git a/Core/src/main/java/com/plotsquared/core/permissions/NullPermissionProfile.java b/Core/src/main/java/com/plotsquared/core/permissions/NullPermissionProfile.java index b59b5a78d..fbf7d8a03 100644 --- a/Core/src/main/java/com/plotsquared/core/permissions/NullPermissionProfile.java +++ b/Core/src/main/java/com/plotsquared/core/permissions/NullPermissionProfile.java @@ -26,11 +26,13 @@ package com.plotsquared.core.permissions; import javax.annotation.Nonnull; +import javax.annotation.Nullable; public enum NullPermissionProfile implements PermissionProfile { INSTANCE; - @Override public boolean hasPermission(@Nonnull final String permission) { + @Override public boolean hasPermission(@Nullable final String world, + @Nonnull final String permission) { return false; } diff --git a/Core/src/main/java/com/plotsquared/core/permissions/PermissionHandler.java b/Core/src/main/java/com/plotsquared/core/permissions/PermissionHandler.java index c88537f69..4a273c743 100644 --- a/Core/src/main/java/com/plotsquared/core/permissions/PermissionHandler.java +++ b/Core/src/main/java/com/plotsquared/core/permissions/PermissionHandler.java @@ -89,7 +89,11 @@ public interface PermissionHandler { /** * The ability to check for offline (player) permissions */ - OFFLINE_PERMISSIONS + OFFLINE_PERMISSIONS, + /** + * Per world permissions + */ + PER_WORLD_PERMISSIONS } } diff --git a/Core/src/main/java/com/plotsquared/core/permissions/PermissionHolder.java b/Core/src/main/java/com/plotsquared/core/permissions/PermissionHolder.java index a94bd0b39..abff13262 100644 --- a/Core/src/main/java/com/plotsquared/core/permissions/PermissionHolder.java +++ b/Core/src/main/java/com/plotsquared/core/permissions/PermissionHolder.java @@ -25,7 +25,12 @@ */ package com.plotsquared.core.permissions; +import com.plotsquared.core.configuration.Captions; +import com.plotsquared.core.configuration.Settings; + +import javax.annotation.Nonnegative; import javax.annotation.Nonnull; +import javax.annotation.Nullable; /** * Any object which can hold permissions @@ -33,11 +38,57 @@ import javax.annotation.Nonnull; public interface PermissionHolder { /** - * Check if the permission holder has the given permission node + * Check if the owner of the profile has a given (global) permission * - * @param permission Permission node - * @return {@code true} if the holder has the given permission node, else {@code false} + * @param permission Permission + * @return {@code true} if the owner has the given permission, else {@code false} */ - boolean hasPermission(@Nonnull String permission); + default boolean hasPermission(@Nonnull final String permission) { + return hasPermission(null ,permission); + } + + /** + * Check the the highest permission a PlotPlayer has within a specified range.
+ * - Excessively high values will lag
+ * - The default range that is checked is {@link Settings.Limit#MAX_PLOTS}
+ * + * @param stub The permission stub to check e.g. for `plots.plot.#` the stub is `plots.plot` + * @param range The range to check + * @return The highest permission they have within that range + */ + @Nonnegative default int hasPermissionRange(@Nonnull final String stub, + @Nonnegative final int range) { + if (hasPermission(Captions.PERMISSION_ADMIN.getTranslated())) { + return Integer.MAX_VALUE; + } + String[] nodes = stub.split("\\."); + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < (nodes.length - 1); i++) { + builder.append(nodes[i]).append("."); + if (!stub.equals(builder + Captions.PERMISSION_STAR.getTranslated())) { + if (hasPermission(builder + Captions.PERMISSION_STAR.getTranslated())) { + return Integer.MAX_VALUE; + } + } + } + if (hasPermission(stub + ".*")) { + return Integer.MAX_VALUE; + } + for (int i = range; i > 0; i--) { + if (hasPermission(stub + "." + i)) { + return i; + } + } + return 0; + } + + /** + * Check if the owner of the profile has a given permission + * + * @param world World name + * @param permission Permission + * @return {@code true} if the owner has the given permission, else {@code false} + */ + boolean hasPermission(@Nullable final String world, @Nonnull String permission); } diff --git a/Core/src/main/java/com/plotsquared/core/permissions/PermissionProfile.java b/Core/src/main/java/com/plotsquared/core/permissions/PermissionProfile.java index 912147980..b7aad8e63 100644 --- a/Core/src/main/java/com/plotsquared/core/permissions/PermissionProfile.java +++ b/Core/src/main/java/com/plotsquared/core/permissions/PermissionProfile.java @@ -26,6 +26,7 @@ package com.plotsquared.core.permissions; import javax.annotation.Nonnull; +import javax.annotation.Nullable; /** * A permission profile that can be used to check for permissions @@ -33,11 +34,22 @@ import javax.annotation.Nonnull; public interface PermissionProfile { /** - * Check if the owner of the profile has a given permission + * Check if the owner of the profile has a given (global) permission * * @param permission Permission * @return {@code true} if the owner has the given permission, else {@code false} */ - boolean hasPermission(@Nonnull String permission); + default boolean hasPermission(@Nonnull final String permission) { + return hasPermission(null ,permission); + } + + /** + * Check if the owner of the profile has a given permission + * + * @param world World name + * @param permission Permission + * @return {@code true} if the owner has the given permission, else {@code false} + */ + boolean hasPermission(@Nullable final String world, @Nonnull String permission); } diff --git a/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java b/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java index 2c1e67c8f..437e86491 100644 --- a/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java +++ b/Core/src/main/java/com/plotsquared/core/player/ConsolePlayer.java @@ -123,10 +123,6 @@ public class ConsolePlayer extends PlotPlayer { return 0; } - @Override public boolean isPermissionSet(String permission) { - return true; - } - @Override public void sendMessage(String message) { logger.info(message); } diff --git a/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java b/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java index d3c4fd369..666d5be7b 100644 --- a/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java +++ b/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java @@ -154,8 +154,9 @@ public abstract class PlotPlayer

implements CommandCaller, OfflinePlotPlayer return PlotSquared.platform().wrapPlayer(player); } - @Override public final boolean hasPermission(@Nonnull final String permission) { - return this.permissionProfile.hasPermission(permission); + @Override public final boolean hasPermission(@Nullable final String world, + @Nonnull final String permission) { + return this.permissionProfile.hasPermission(world, permission); } public abstract Actor toActor(); @@ -257,31 +258,6 @@ public abstract class PlotPlayer

implements CommandCaller, OfflinePlotPlayer return Permissions.hasPermissionRange(this, "plots.cluster", Settings.Limit.MAX_PLOTS); } - public int hasPermissionRange(String stub, int range) { - if (hasPermission(Captions.PERMISSION_ADMIN.getTranslated())) { - return Integer.MAX_VALUE; - } - String[] nodes = stub.split("\\."); - StringBuilder builder = new StringBuilder(); - for (int i = 0; i < (nodes.length - 1); i++) { - builder.append(nodes[i]).append("."); - if (!stub.equals(builder + Captions.PERMISSION_STAR.getTranslated())) { - if (hasPermission(builder + Captions.PERMISSION_STAR.getTranslated())) { - return Integer.MAX_VALUE; - } - } - } - if (hasPermission(stub + ".*")) { - return Integer.MAX_VALUE; - } - for (int i = range; i > 0; i--) { - if (hasPermission(stub + "." + i)) { - return i; - } - } - return 0; - } - /** * Get the number of plots this player owns. * diff --git a/Core/src/main/java/com/plotsquared/core/util/EconHandler.java b/Core/src/main/java/com/plotsquared/core/util/EconHandler.java index 69363079f..508fe8f57 100644 --- a/Core/src/main/java/com/plotsquared/core/util/EconHandler.java +++ b/Core/src/main/java/com/plotsquared/core/util/EconHandler.java @@ -48,16 +48,4 @@ public abstract class EconHandler { public abstract void depositMoney(OfflinePlotPlayer player, double amount); - /** - * @deprecated Use {@link PermHandler#hasPermission(String, String, String)} instead - */ - @Deprecated public abstract boolean hasPermission(String world, String player, String perm); - - /** - * @deprecated Use {@link PermHandler#hasPermission(String, String)} instead - */ - @Deprecated public boolean hasPermission(String player, String perm) { - return hasPermission(null, player, perm); - } - } diff --git a/Core/src/main/java/com/plotsquared/core/util/PermHandler.java b/Core/src/main/java/com/plotsquared/core/util/PermHandler.java deleted file mode 100644 index 1dec45fd4..000000000 --- a/Core/src/main/java/com/plotsquared/core/util/PermHandler.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * _____ _ _ _____ _ - * | __ \| | | | / ____| | | - * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | - * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | - * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | - * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| - * | | - * |_| - * PlotSquared plot management system for Minecraft - * Copyright (C) 2020 IntellectualSites - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.plotsquared.core.util; - -public abstract class PermHandler { - - public abstract boolean init(); - - public abstract boolean hasPermission(String world, String player, String perm); - - public boolean hasPermission(String player, String perm) { - return hasPermission(null, player, perm); - } -} diff --git a/Core/src/main/java/com/plotsquared/core/util/Permissions.java b/Core/src/main/java/com/plotsquared/core/util/Permissions.java index 34754e7c8..7e3b4622f 100644 --- a/Core/src/main/java/com/plotsquared/core/util/Permissions.java +++ b/Core/src/main/java/com/plotsquared/core/util/Permissions.java @@ -29,6 +29,7 @@ import com.plotsquared.core.command.CommandCaller; import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.permissions.PermissionHolder; import java.util.HashMap; @@ -39,7 +40,7 @@ import java.util.HashMap; */ public class Permissions { - public static boolean hasPermission(PlotPlayer player, Captions caption, boolean notify) { + public static boolean hasPermission(PlotPlayer player, Captions caption, boolean notify) { return hasPermission(player, caption.getTranslated(), notify); } @@ -50,7 +51,7 @@ public class Permissions { * @param caption * @return */ - public static boolean hasPermission(PlotPlayer player, Captions caption) { + public static boolean hasPermission(PlotPlayer player, Captions caption) { return hasPermission(player, caption.getTranslated()); } @@ -63,7 +64,7 @@ public class Permissions { */ public static boolean hasPermission(PlotPlayer player, String permission) { if (!Settings.Enabled_Components.PERMISSION_CACHE) { - return hasPermission((CommandCaller) player, permission); + return hasPermission((PermissionHolder) player, permission); } HashMap map = player.getMeta("perm"); if (map != null) { @@ -75,7 +76,7 @@ public class Permissions { map = new HashMap<>(); player.setMeta("perm", map); } - boolean result = hasPermission((CommandCaller) player, permission); + boolean result = hasPermission((PermissionHolder) player, permission); map.put(permission, result); return result; } @@ -87,12 +88,12 @@ public class Permissions { * @param permission * @return */ - public static boolean hasPermission(CommandCaller caller, String permission) { + public static boolean hasPermission(PermissionHolder caller, String permission) { if (caller.hasPermission(permission)) { return true; - } else if (caller.isPermissionSet(permission)) { + }/* TODO: DECIDE WHAT TO DO HERE; else if (caller.isPermissionSet(permission)) { return false; - } + }*/ if (caller.hasPermission(Captions.PERMISSION_ADMIN.getTranslated())) { return true; } @@ -105,9 +106,9 @@ public class Permissions { if (!permission.equals(combined)) { if (caller.hasPermission(combined)) { return true; - } else if (caller.isPermissionSet(combined)) { + }/* TODO: DECIDE WHAT TO DO HERE; else if (caller.isPermissionSet(combined)) { return false; - } + }*/ } } return false; @@ -121,7 +122,7 @@ public class Permissions { * @param notify * @return */ - public static boolean hasPermission(PlotPlayer player, String permission, boolean notify) { + public static boolean hasPermission(PlotPlayer player, String permission, boolean notify) { if (!hasPermission(player, permission)) { if (notify) { MainUtil.sendMessage(player, Captions.NO_PERMISSION_EVENT, permission); @@ -131,7 +132,7 @@ public class Permissions { return true; } - public static int hasPermissionRange(PlotPlayer player, Captions perm, int range) { + public static int hasPermissionRange(PlotPlayer player, Captions perm, int range) { return hasPermissionRange(player, perm.getTranslated(), range); } @@ -140,12 +141,12 @@ public class Permissions { * - Excessively high values will lag
* - The default range that is checked is {@link Settings.Limit#MAX_PLOTS}
* - * @param player + * @param player Player to check for * @param stub The permission stub to check e.g. for `plots.plot.#` the stub is `plots.plot` * @param range The range to check * @return The highest permission they have within that range */ - public static int hasPermissionRange(PlotPlayer player, String stub, int range) { + public static int hasPermissionRange(PlotPlayer player, String stub, int range) { return player.hasPermissionRange(stub, range); } }