refactor: Split permission register and checking, and more DRYing

This commit is contained in:
Ben Woo 2023-03-01 15:01:22 +08:00
parent 36cb515b86
commit a6eea018ac
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
13 changed files with 317 additions and 377 deletions

View File

@ -38,10 +38,8 @@ import com.onarandombox.MultiverseCore.commands.LoadCommand;
import com.onarandombox.MultiverseCore.commands.RegenCommand;
import com.onarandombox.MultiverseCore.commands.ReloadCommand;
import com.onarandombox.MultiverseCore.commands.RemoveCommand;
import com.onarandombox.MultiverseCore.commands.RootCommand;
import com.onarandombox.MultiverseCore.commands.TeleportCommand;
import com.onarandombox.MultiverseCore.commands.UnloadCommand;
import com.onarandombox.MultiverseCore.commands.UsageCommand;
import com.onarandombox.MultiverseCore.commandtools.MVCommandManager;
import com.onarandombox.MultiverseCore.destination.DestinationsProvider;
import com.onarandombox.MultiverseCore.destination.core.AnchorDestination;
@ -62,7 +60,7 @@ import com.onarandombox.MultiverseCore.listeners.MVWorldListener;
import com.onarandombox.MultiverseCore.teleportation.SimpleBlockSafety;
import com.onarandombox.MultiverseCore.teleportation.SimpleLocationManipulation;
import com.onarandombox.MultiverseCore.teleportation.SimpleSafeTTeleporter;
import com.onarandombox.MultiverseCore.utils.PermissionsTool;
import com.onarandombox.MultiverseCore.utils.permission.PermissionsRegistrar;
import com.onarandombox.MultiverseCore.utils.player.PlayerActionChecker;
import com.onarandombox.MultiverseCore.utils.TestingMode;
import com.onarandombox.MultiverseCore.utils.UnsafeCallWrapper;
@ -91,7 +89,6 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
private DestinationsProvider destinationsProvider;
private MVEconomist economist;
private LocationManipulation locationManipulation = new SimpleLocationManipulation();
private final PermissionsTool permissionsTool = new PermissionsTool(this);
private PlayerActionChecker playerActionChecker;
private SafeTTeleporter safeTTeleporter = new SimpleSafeTTeleporter(this);
private final UnsafeCallWrapper unsafeCallWrapper = new UnsafeCallWrapper(this);
@ -139,7 +136,7 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
*/
@Override
public void onEnable() {
this.permissionsTool.setUpPermissions();
PermissionsRegistrar.setup();
this.playerActionChecker = new PlayerActionChecker(this);
// Load our configs first as we need them for everything else.
@ -280,14 +277,6 @@ public class MultiverseCore extends JavaPlugin implements MVCore {
return economist;
}
/**
* {@inheritDoc}
*/
@Override
public PermissionsTool getPermissionsTool() {
return permissionsTool;
}
/**
* {@inheritDoc}
*/

View File

@ -14,7 +14,6 @@ import com.onarandombox.MultiverseCore.economy.MVEconomist;
import com.onarandombox.MultiverseCore.teleportation.SimpleBlockSafety;
import com.onarandombox.MultiverseCore.teleportation.SimpleLocationManipulation;
import com.onarandombox.MultiverseCore.teleportation.SimpleSafeTTeleporter;
import com.onarandombox.MultiverseCore.utils.PermissionsTool;
import com.onarandombox.MultiverseCore.utils.UnsafeCallWrapper;
import com.onarandombox.MultiverseCore.utils.player.PlayerActionChecker;
@ -106,13 +105,6 @@ public interface MVCore extends MVPlugin {
*/
int getPluginCount();
/**
* Gets the {@link PermissionsTool} instance.
*
* @return The {@link PermissionsTool} instance.
*/
PermissionsTool getPermissionsTool();
/**
* Gets the {@link PlayerActionChecker} instance.
*

View File

@ -35,7 +35,7 @@ public class CheckCommand extends MultiverseCoreCommand {
) {
issuer.sendMessage("Checking " + player + " to " + destination + "...");
//TODO More detailed output on permissions required.
if (!this.plugin.getPlayerActionChecker().canUseDestinationToTeleport(issuer.getIssuer(), player, destination).asBoolean()) {
if (!this.plugin.getPlayerActionChecker().canUseParsedDestination(issuer.getIssuer(), player, destination).asBoolean()) {
issuer.sendMessage("You don't have permission to use this destination.");
}
if (!this.plugin.getPlayerActionChecker().canGoToDestination(issuer.getIssuer(), player, destination).asBoolean()) {

View File

@ -35,7 +35,7 @@ public class TeleportCommand extends MultiverseCoreCommand {
) {
// TODO Add warning if teleporting too many players at once.
for (Player player : players) {
if (!this.plugin.getPlayerActionChecker().canUseDestinationToTeleport(issuer.getIssuer(), player, destination).asBoolean()) {
if (!this.plugin.getPlayerActionChecker().canUseParsedDestination(issuer.getIssuer(), player, destination).asBoolean()) {
issuer.sendMessage("You don't have permission to use this destination.");
continue;
}
@ -48,6 +48,7 @@ public class TeleportCommand extends MultiverseCoreCommand {
@Override
public boolean hasPermission(CommandIssuer issuer) {
return this.plugin.getPermissionsTool().hasAnyDestinationTeleportPermissions(issuer.getIssuer());
return this.plugin.getDestinationsProvider().getRegisteredDestinations().stream()
.anyMatch(destination -> this.plugin.getPlayerActionChecker().canUseDestinationType(issuer.getIssuer(), destination).asBoolean());
}
}

View File

@ -11,6 +11,7 @@ import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.Destination;
import com.onarandombox.MultiverseCore.api.DestinationInstance;
import com.onarandombox.MultiverseCore.api.Teleporter;
import com.onarandombox.MultiverseCore.utils.permission.PermissionsRegistrar;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@ -42,7 +43,7 @@ public class DestinationsProvider {
*/
public void registerDestination(@NotNull Destination<?> destination) {
this.destinationMap.put(destination.getIdentifier(), destination);
this.plugin.getPermissionsTool().registerDestinationTeleportPermissions(destination);
PermissionsRegistrar.registerDestinationPermissions(destination);
}
/**
@ -56,7 +57,7 @@ public class DestinationsProvider {
@Nullable String deststring
) {
return destinationMap.values().stream()
.filter(destination -> hasSelfOrOtherTeleportPermission(issuer, destination))
//.filter(destination -> this.plugin.getPlayerActionChecker().canUseDestinationToTeleport(issuer, destination))
.map(destination -> destination.suggestDestinations(issuer, deststring).stream()
.map(s -> destination.getIdentifier() + SEPARATOR + s)
.collect(Collectors.toList()))
@ -64,11 +65,6 @@ public class DestinationsProvider {
.collect(Collectors.toList());
}
private boolean hasSelfOrOtherTeleportPermission(@NotNull BukkitCommandIssuer issuer, Destination<?> destination) {
return this.plugin.getPermissionsTool().hasDestinationTeleportPermission(issuer.getIssuer(), destination)
|| this.plugin.getPermissionsTool().hasDestinationTeleportPermission(issuer.getIssuer(), this.plugin.getServer().getConsoleSender(), destination);
}
/**
* Converts a destination string to a destination object.
*

View File

@ -1,329 +0,0 @@
package com.onarandombox.MultiverseCore.utils;
import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.Destination;
import com.onarandombox.MultiverseCore.api.MVWorld;
import com.onarandombox.MultiverseCore.destination.ParsedDestination;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.PluginManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* A utility class for registering and checking permissions.
*/
public class PermissionsTool {
private static final String TELEPORT_PERM_PREFIX = "multiverse.teleport.";
private final MultiverseCore plugin;
private PluginManager pluginManager;
private Permission worldAccessPermission;
private Permission bypassGameModePermission;
private Permission bypassEntryFeePermission;
private Permission bypassPlayerLimitPermission;
public PermissionsTool(MultiverseCore plugin) {
this.plugin = plugin;
}
/**
* Sets up the permissions for Multiverse.
*/
public void setUpPermissions() {
this.pluginManager = this.plugin.getServer().getPluginManager();
setUpWorldPermissionWildcards();
}
/**
* Set up the world permissions wildcards.
*/
private void setUpWorldPermissionWildcards() {
this.worldAccessPermission = new Permission("multiverse.access.*",
"Allows access to all worlds",
PermissionDefault.OP);
this.bypassGameModePermission = new Permission("mv.bypass.gamemode.*",
"Allows players with this permission to ignore gamemode changes.",
PermissionDefault.FALSE);
this.bypassEntryFeePermission = new Permission("multiverse.exempt.*",
"A player who has this does not pay to enter this world, or use any MV portals in it",
PermissionDefault.OP);
this.bypassPlayerLimitPermission = new Permission("mv.bypass.playerlimit.*",
"A player who can enter this world regardless of whether its full",
PermissionDefault.OP);
try {
pluginManager.addPermission(this.worldAccessPermission);
pluginManager.addPermission(this.bypassGameModePermission);
pluginManager.addPermission(this.bypassEntryFeePermission);
pluginManager.addPermission(this.bypassPlayerLimitPermission);
} catch (IllegalArgumentException e) {
Logging.finer("World permissions wildcard already registered.");
}
Logging.finer("Registered world permissions wildcard.");
}
/**
* Registers the permissions for a world.
*
* @param world The world to register permissions for.
*/
public void registerMVWorldPermissions(MVWorld world) {
Permission accessPermission = new Permission("multiverse.access." + world.getName(),
"Allows access to " + world.getName(),
PermissionDefault.OP);
Permission ignorePermission = new Permission("mv.bypass.gamemode." + world.getName(),
"Allows players with this permission to ignore gamemode changes.",
PermissionDefault.FALSE);
Permission exemptPermission = new Permission("multiverse.exempt." + world.getName(),
"A player who has this does not pay to enter this world, or use any MV portals in it " + world.getName(),
PermissionDefault.OP);
Permission playerLimitPermissions = new Permission("mv.bypass.playerlimit." + world.getName(),
"A player who can enter this world regardless of whether its full",
PermissionDefault.OP);
try {
pluginManager.addPermission(accessPermission);
pluginManager.addPermission(ignorePermission);
pluginManager.addPermission(exemptPermission);
pluginManager.addPermission(playerLimitPermissions);
} catch (IllegalArgumentException e) {
Logging.finer("World permissions already registered for '" + world.getName() + "'");
return;
}
accessPermission.addParent(this.worldAccessPermission, true);
ignorePermission.addParent(this.bypassGameModePermission, true);
exemptPermission.addParent(this.bypassEntryFeePermission, true);
playerLimitPermissions.addParent(this.bypassPlayerLimitPermission, true);
Logging.finer("Registered permissions for '" + world.getName() + "'");
}
/**
* Unregisters the permissions for a world.
*
* @param world The world to remove permissions for.
*/
public void removeMVWorldPermissions(MVWorld world) {
Permission accessPermission = pluginManager.getPermission("multiverse.access." + world.getName());
if (accessPermission != null) {
this.worldAccessPermission.getChildren().remove(accessPermission.getName());
pluginManager.removePermission(accessPermission);
}
Permission ignorePermission = pluginManager.getPermission("mv.bypass.gamemode." + world.getName());
if (ignorePermission != null) {
this.bypassGameModePermission.getChildren().remove(ignorePermission.getName());
pluginManager.removePermission(ignorePermission);
}
Permission exemptPermission = pluginManager.getPermission("multiverse.exempt." + world.getName());
if (exemptPermission != null) {
this.bypassEntryFeePermission.getChildren().remove(exemptPermission.getName());
pluginManager.removePermission(exemptPermission);
}
Permission playerLimitPermissions = pluginManager.getPermission("mv.bypass.playerlimit." + world.getName());
if (playerLimitPermissions != null) {
this.bypassPlayerLimitPermission.getChildren().remove(playerLimitPermissions.getName());
pluginManager.removePermission(playerLimitPermissions);
}
}
/**
* Removes all Multiverse World related permissions.
*/
public void removeAllMVWorldPermissions() {
this.worldAccessPermission.getChildren().keySet().forEach(pluginManager::removePermission);
this.bypassGameModePermission.getChildren().keySet().forEach(pluginManager::removePermission);
this.bypassEntryFeePermission.getChildren().keySet().forEach(pluginManager::removePermission);
this.bypassPlayerLimitPermission.getChildren().keySet().forEach(pluginManager::removePermission);
pluginManager.removePermission(this.worldAccessPermission);
pluginManager.removePermission(this.bypassGameModePermission);
pluginManager.removePermission(this.bypassEntryFeePermission);
pluginManager.removePermission(this.bypassPlayerLimitPermission);
setUpWorldPermissionWildcards();
}
/**
* Registers the permissions for a destination.
*
* @param destination The destination to register permissions for.
*/
public void registerDestinationTeleportPermissions(Destination<?> destination) {
try {
pluginManager.addPermission(new Permission(TELEPORT_PERM_PREFIX + "self." + destination.getIdentifier(), PermissionDefault.OP));
pluginManager.addPermission(new Permission(TELEPORT_PERM_PREFIX + "other." + destination.getIdentifier(), PermissionDefault.OP));
pluginManager.addPermission(new Permission(TELEPORT_PERM_PREFIX + "self." + destination.getIdentifier() + ".*", PermissionDefault.OP));
pluginManager.addPermission(new Permission(TELEPORT_PERM_PREFIX + "other." + destination.getIdentifier() + ".*", PermissionDefault.OP));
} catch (IllegalArgumentException e) {
Logging.finer("Destination permissions already registered for '" + destination.getIdentifier() + "'");
}
Logging.finer("Registered permissions for '" + destination.getIdentifier() + "'");
}
/**
* Registers the finer permissions for a destination.
*
* @param destination The destination to register permissions for.
* @param finerSuffix The finer suffix to register.
*/
public void registerFinerDestinationTeleportPermissions(Destination<?> destination, String finerSuffix) {
String finerPermissionName = TELEPORT_PERM_PREFIX + "self." + destination.getIdentifier() + "." + finerSuffix;
if (pluginManager.getPermission(finerPermissionName) != null) {
return;
}
Permission permission = pluginManager.getPermission(TELEPORT_PERM_PREFIX + "self." + destination.getIdentifier() + ".*");
if (permission == null) {
return;
}
Permission finerPermission = new Permission(finerPermissionName, PermissionDefault.OP);
try {
pluginManager.addPermission(finerPermission);
return;
} catch (IllegalArgumentException e) {
Logging.finer("Finer destination permissions already registered for '" + destination.getIdentifier() + "'");
}
finerPermission.addParent(permission, true);
Logging.finer("Registered finer permissions '" + finerPermissionName + "' for '" + destination.getIdentifier() + "'");
}
/**
* Checks if a player has the permission to bypass the player limit.
*
* @param sender The player to check.
* @param toWorld The world to check.
* @return True if the player has the permission, false otherwise.
*/
public boolean hasBypassPlayerLimit(@NotNull CommandSender sender, @NotNull MVWorld toWorld) {
return hasPermission(sender, "mv.bypass.playerlimit." + toWorld.getName());
}
/**
* Checks if a player has the permission to bypass the entry fee.
*
* @param sender The player to check.
* @param toWorld The world to check.
* @return True if the player has the permission, false otherwise.
*/
public boolean hasBypassEntryFee(@NotNull CommandSender sender, @NotNull MVWorld toWorld) {
return hasPermission(sender, "multiverse.exempt." + toWorld.getName());
}
/**
* Checks if a player has the permission to bypass the game mode enforcement.
*
* @param sender The player to check.
* @param toWorld The world to check.
* @return True if the player has the permission, false otherwise.
*/
public boolean hasBypassGameModeEnforcement(@NotNull CommandSender sender, @NotNull MVWorld toWorld) {
return hasPermission(sender, "mv.bypass.gamemode." + toWorld.getName());
}
/**
* Checks if a player has the permission to access the world.
*
* @param sender The player to check.
* @param toWorld The world to check.
* @return True if the player has the permission, false otherwise.
*/
public boolean hasWorldAccess(@NotNull CommandSender sender, @NotNull MVWorld toWorld) {
return hasPermission(sender, "multiverse.access." + toWorld.getName());
}
/**
* Checks if a player has the permission to teleport to a destination.
*
* @param teleportee The player to check.
* @param destination The destination to check.
* @return True if the player has the permission, false otherwise.
*/
public boolean hasDestinationTeleportPermission(@NotNull CommandSender teleportee, @NotNull Destination<?> destination) {
return hasDestinationTeleportPermission(null, teleportee, destination);
}
/**
* Checks if a player has the permission to teleport to a destination.
*
* @param teleporter The player who is teleporting the other player.
* @param teleportee The player to check.
* @param destination The destination to check.
* @return True if the player has the permission, false otherwise.
*/
public boolean hasDestinationTeleportPermission(@Nullable CommandSender teleporter,
@NotNull CommandSender teleportee,
@NotNull Destination<?> destination
) {
if (teleporter == null || teleportee.equals(teleporter)) {
return hasPermission(teleportee, TELEPORT_PERM_PREFIX + "self." + destination.getIdentifier());
}
return hasPermission(teleporter, TELEPORT_PERM_PREFIX + "other." + destination.getIdentifier());
}
/**
* Checks if a player has the permission to teleport to a destination.
*
* @param teleportee The player to check.
* @param destination The destination to check.
* @return True if the player has the permission, false otherwise.
*/
public boolean hasFinerDestinationTeleportPermission(@NotNull CommandSender teleportee, @NotNull ParsedDestination<?> destination) {
return hasFinerDestinationTeleportPermission(null, teleportee, destination);
}
/**
* Checks if a player has the permission to teleport to a destination.
*
* @param teleporter The player who is teleporting the other player.
* @param teleportee The player to check.
* @param destination The destination to check.
* @return True if the player has the permission, false otherwise.
*/
public boolean hasFinerDestinationTeleportPermission(@Nullable CommandSender teleporter,
@NotNull CommandSender teleportee,
@NotNull ParsedDestination<?> destination
) {
if (teleporter == null || teleportee.equals(teleporter)) {
return hasPermission(teleportee, TELEPORT_PERM_PREFIX + "self."
+ destination.getDestination().getIdentifier() + "."
+ destination.getDestinationInstance().getFinerPermissionSuffix());
}
return hasPermission(teleporter, TELEPORT_PERM_PREFIX + "other."
+ destination.getDestination().getIdentifier() + "."
+ destination.getDestinationInstance().getFinerPermissionSuffix());
}
/**
* Checks if a player has the permission to teleport to at least 1 destination type.
*
* @param sender The player to check.
* @return True if the player has the permission, false otherwise.
*/
public boolean hasAnyDestinationTeleportPermissions(@NotNull CommandSender sender) {
return this.plugin.getDestinationsProvider().getRegisteredDestinations().stream()
.anyMatch(destination -> hasDestinationTeleportPermission(sender, this.plugin.getServer().getConsoleSender(), destination)
|| hasDestinationTeleportPermission(sender, destination));
}
/**
* Internal method to check if a player has a permission and does logging.
*
* @param sender The sender to check.
* @param permission The permission to check.
* @return True if the player has the permission, false otherwise.
*/
private boolean hasPermission(@NotNull CommandSender sender, @NotNull String permission) {
if (sender.hasPermission(permission)) {
Logging.finer("Checking to see if sender [" + sender.getName() + "] has permission [" + permission + "]... YES");
return true;
}
Logging.finer("Checking to see if sender [" + sender.getName() + "] has permission [" + permission + "]... NO");
return false;
}
}

View File

@ -0,0 +1,81 @@
package com.onarandombox.MultiverseCore.utils.permission;
import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.api.Destination;
import com.onarandombox.MultiverseCore.api.MVWorld;
import com.onarandombox.MultiverseCore.destination.ParsedDestination;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
public class PermissionsChecker {
public static boolean hasWorldAccessPermission(@NotNull CommandSender sender, @NotNull MVWorld world) {
return hasPermission(sender, PermissionsRegistrar.worldAccessPermission, world.getName());
}
public static boolean hasWorldGamemodeBypassPermission(@NotNull CommandSender sender, @NotNull MVWorld world) {
return hasPermission(sender, PermissionsRegistrar.worldGamemodeBypassPermission, world.getName());
}
public static boolean hasWorldExemptPermission(@NotNull CommandSender sender, @NotNull MVWorld world) {
return hasPermission(sender, PermissionsRegistrar.worldExemptPermission, world.getName());
}
public static boolean hasWorldPlayerLimitBypassPermission(@NotNull CommandSender sender, @NotNull MVWorld world) {
return hasPermission(sender, PermissionsRegistrar.worldPlayerLimitBypassPermission, world.getName());
}
public static boolean hasTeleportSelfPermission(@NotNull CommandSender sender, @NotNull ParsedDestination<?> destination) {
return hasTeleportSelfPermission(sender, destination.getDestination());
}
public static boolean hasTeleportSelfPermission(@NotNull CommandSender sender, @NotNull Destination<?> destination) {
return hasPermission(sender, PermissionsRegistrar.teleportSelfPermission, destination.getIdentifier());
}
public static boolean hasTeleportSelfFinerPermission(@NotNull CommandSender sender, @NotNull ParsedDestination<?> destination) {
return hasPermission(sender, PermissionsRegistrar.teleportSelfPermission,
destination.getIdentifier() + "." + destination.getFinerPermissionSuffix());
}
public static boolean hasTeleportOtherPermission(@NotNull CommandSender sender, @NotNull ParsedDestination<?> destination) {
return hasTeleportOtherPermission(sender, destination.getDestination());
}
public static boolean hasTeleportOtherPermission(@NotNull CommandSender sender, @NotNull Destination<?> destination) {
return hasPermission(sender, PermissionsRegistrar.teleportOtherPermission, destination.getIdentifier());
}
public static boolean hasTeleportOtherFinerPermission(@NotNull CommandSender sender, @NotNull ParsedDestination<?> destination) {
return hasPermission(sender, PermissionsRegistrar.teleportOtherPermission,
destination.getIdentifier() + "." + destination.getFinerPermissionSuffix());
}
/**
* Internal method to check if a player has a permission and does logging.
*
* @param sender The sender to check.
* @param permission The permission to check.
* @param permissionSuffix The suffix to append to the permission.
* @return True if the player has the permission, false otherwise.
*/
private static boolean hasPermission(@NotNull CommandSender sender, @NotNull PrefixPermission permission, @NotNull String permissionSuffix) {
return hasPermission(sender, permission.getPermissionName(permissionSuffix));
}
/**
* Internal method to check if a player has a permission and does logging.
*
* @param sender The sender to check.
* @param permission The permission to check.
* @return True if the player has the permission, false otherwise.
*/
private static boolean hasPermission(@NotNull CommandSender sender, @NotNull String permission) {
if (sender.hasPermission(permission)) {
Logging.finer("Checking to see if sender [" + sender.getName() + "] has permission [" + permission + "]... YES");
return true;
}
Logging.finer("Checking to see if sender [" + sender.getName() + "] has permission [" + permission + "]... NO");
return false;
}
}

View File

@ -0,0 +1,77 @@
package com.onarandombox.MultiverseCore.utils.permission;
import java.util.ArrayList;
import java.util.List;
import com.onarandombox.MultiverseCore.api.Destination;
import com.onarandombox.MultiverseCore.api.MVWorld;
import org.bukkit.permissions.PermissionDefault;
public class PermissionsRegistrar {
private static List<PrefixPermission> worldPermissions;
private static List<PrefixPermission> destinationPermissions;
static PrefixPermission worldAccessPermission;
static PrefixPermission worldGamemodeBypassPermission;
static PrefixPermission worldExemptPermission;
static PrefixPermission worldPlayerLimitBypassPermission;
static PrefixPermission teleportSelfPermission;
static PrefixPermission teleportOtherPermission;
public static void setup() {
worldPermissions = new ArrayList<>() {{
worldAccessPermission = new PrefixPermission("multiverse.access.", "Allows access to a world.");
worldGamemodeBypassPermission = new PrefixPermission("mv.bypass.gamemode.", "Allows bypassing of gamemode restrictions.", PermissionDefault.FALSE);
worldExemptPermission = new PrefixPermission("multiverse.exempt.", "A player who has this does not pay to enter this world.");
worldPlayerLimitBypassPermission = new PrefixPermission("mv.bypass.playerlimit.", "Allows bypassing of player limit restrictions.");
}};
destinationPermissions = new ArrayList<>() {{
teleportSelfPermission = new PrefixPermission("multiverse.teleport.self.", "Allows teleporting to a world.");
teleportOtherPermission = new PrefixPermission("multiverse.teleport.other.", "Allows teleporting other players to a world.");
}};
}
public static void registerWorldPermissions(MVWorld world) {
registerPrefixPermissionList(worldPermissions, world.getName());
}
public static void registerDestinationPermissions(Destination<?> destination) {
registerPrefixPermissionList(destinationPermissions, destination.getIdentifier());
}
public static void removeWorldPermissions(MVWorld world) {
removePrefixPermissions(worldPermissions, world.getName());
}
public static void removeDestinationPermissions(Destination<?> destination) {
removePrefixPermissions(destinationPermissions, destination.getIdentifier());
}
public static void removeAllWorldPermissions() {
removeAllPrefixPermissions(worldPermissions);
}
public static void removeAllDestinationPermissions() {
removeAllPrefixPermissions(destinationPermissions);
}
private static void removePrefixPermissions(List<PrefixPermission> worldPermissions, String name) {
for (PrefixPermission permission : worldPermissions) {
permission.removePermission(name);
}
}
private static void registerPrefixPermissionList(List<PrefixPermission> permissions, String permissionSuffix) {
for (PrefixPermission permission : permissions) {
permission.registerWildcardPermission();
}
}
private static void removeAllPrefixPermissions(List<PrefixPermission> worldPermissions) {
for (PrefixPermission permission : worldPermissions) {
permission.removeAllPermissions();
}
}
}

View File

@ -0,0 +1,102 @@
package com.onarandombox.MultiverseCore.utils.permission;
import org.bukkit.Bukkit;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.PluginManager;
public class PrefixPermission {
private final PluginManager pluginManager;
private final String permissionPrefix;
private final String description;
private final PermissionDefault permissionDefault;
private Permission wildcardPermission;
public PrefixPermission(String permissionPrefix, String description) {
this(permissionPrefix, description, PermissionDefault.OP);
}
public PrefixPermission(String permissionPrefix, String description, PermissionDefault permissionDefault) {
this.pluginManager = Bukkit.getServer().getPluginManager();
this.permissionPrefix = permissionPrefix;
this.description = description;
this.permissionDefault = permissionDefault;
}
public Permission registerPermission(String permissionSuffix) {
String permissionName = permissionPrefix + permissionSuffix;
Permission permission = pluginManager.getPermission(permissionName);
if (permission != null) {
return permission;
}
permission = new Permission(permissionName, description, permissionDefault);
pluginManager.addPermission(permission);
if (wildcardPermission == null) {
registerWildcardPermission();
}
permission.addParent(wildcardPermission, true);
return permission;
}
public void registerWildcardPermission() {
String permissionName = getPermissionName("*");
wildcardPermission = pluginManager.getPermission(permissionName);
if (wildcardPermission != null) {
return;
}
wildcardPermission = new Permission(permissionName, description, permissionDefault);
pluginManager.addPermission(wildcardPermission);
}
public boolean removePermission(String permissionSuffix) {
String permissionName = getPermissionName(permissionSuffix);
Permission permission = pluginManager.getPermission(permissionName);
if (permission == null) {
return false;
}
try {
wildcardPermission.getChildren().remove(permission.getName());
pluginManager.removePermission(permission);
} catch (IllegalArgumentException e) {
return false;
}
return true;
}
public boolean removeAllPermissions() {
try {
wildcardPermission.getChildren().forEach((child, value) -> pluginManager.removePermission(child));
pluginManager.removePermission(wildcardPermission);
} catch (IllegalArgumentException e) {
return false;
}
return true;
}
public Permission getPermission(String permissionSuffix) {
return pluginManager.getPermission(permissionPrefix + permissionSuffix);
}
public Permission getWildcardPermission() {
return wildcardPermission;
}
public String getPermissionName(String permissionSuffix) {
return permissionPrefix + permissionSuffix;
}
public String getPermissionPrefix() {
return permissionPrefix;
}
public String getDescription() {
return description;
}
public PermissionDefault getPermissionDefault() {
return permissionDefault;
}
}

View File

@ -0,0 +1 @@
package com.onarandombox.MultiverseCore.utils.permission;

View File

@ -1,11 +1,12 @@
package com.onarandombox.MultiverseCore.utils.player;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.Destination;
import com.onarandombox.MultiverseCore.api.MVWorld;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.api.operation.OperationResultChain;
import com.onarandombox.MultiverseCore.destination.ParsedDestination;
import com.onarandombox.MultiverseCore.utils.PermissionsTool;
import com.onarandombox.MultiverseCore.utils.permission.PermissionsChecker;
import com.onarandombox.MultiverseCore.utils.player.checkresult.BlacklistResult;
import com.onarandombox.MultiverseCore.utils.player.checkresult.EntryFeeResult;
import com.onarandombox.MultiverseCore.utils.player.checkresult.GameModeResult;
@ -29,7 +30,6 @@ import org.jetbrains.annotations.Nullable;
public class PlayerActionChecker {
private final MultiverseCore plugin;
private final MVWorldManager worldManager;
private final PermissionsTool permissionsTool;
/**
* Creates a new PlayerActionChecker.
@ -39,7 +39,30 @@ public class PlayerActionChecker {
public PlayerActionChecker(@NotNull MultiverseCore plugin) {
this.plugin = plugin;
this.worldManager = plugin.getMVWorldManager();
this.permissionsTool = plugin.getPermissionsTool();
}
public UseDestinationResult canUseDestinationType(@NotNull CommandSender teleportee,
@NotNull Destination<?> destination
) {
return canUseDestinationType(teleportee, teleportee, destination);
}
public UseDestinationResult canUseDestinationType(@Nullable CommandSender teleporter,
@NotNull CommandSender teleportee,
@NotNull Destination<?> destination
) {
if (teleporter == null || teleporter.equals(teleportee)) {
return PermissionsChecker.hasTeleportSelfPermission(teleportee, destination)
? UseDestinationResult.CAN_USE_DESTINATION : UseDestinationResult.NO_DESTINATION_PERMISSION;
}
return PermissionsChecker.hasTeleportOtherPermission(teleporter, destination)
? UseDestinationResult.CAN_USE_DESTINATION : UseDestinationResult.NO_DESTINATION_PERMISSION;
}
public UseDestinationResult canUseParsedDestination(@NotNull CommandSender teleportee,
@NotNull ParsedDestination<?> destination
) {
return canUseParsedDestination(teleportee, teleportee, destination);
}
/**
@ -50,16 +73,21 @@ public class PlayerActionChecker {
* @param destination The destination to teleport to.
* @return The result of the check.
*/
public UseDestinationResult canUseDestinationToTeleport(@NotNull CommandSender teleporter,
@NotNull CommandSender teleportee,
@NotNull ParsedDestination<?> destination
public UseDestinationResult canUseParsedDestination(@Nullable CommandSender teleporter,
@NotNull CommandSender teleportee,
@NotNull ParsedDestination<?> destination
) {
if (!permissionsTool.hasDestinationTeleportPermission(teleporter, teleportee, destination.getDestination())) {
return UseDestinationResult.NO_DESTINATION_PERMISSION;
UseDestinationResult result = canUseDestinationType(teleporter, teleportee, destination.getDestination());
if (!result.asBoolean()) {
return result;
}
//TODO Config whether to use finer permission
return permissionsTool.hasFinerDestinationTeleportPermission(teleporter, teleportee, destination)
? UseDestinationResult.CAN_USE_DESTINATION : UseDestinationResult.NO_DESTINATION_PERMISSION;
if (teleporter == null || teleporter.equals(teleportee)) {
return PermissionsChecker.hasTeleportSelfFinerPermission(teleportee, destination)
? UseDestinationResult.CAN_USE_DESTINATION : UseDestinationResult.NO_DESTINATION_PERMISSION_FINER;
}
return PermissionsChecker.hasTeleportOtherFinerPermission(teleporter, destination)
? UseDestinationResult.CAN_USE_DESTINATION : UseDestinationResult.NO_DESTINATION_PERMISSION_FINER;
}
/**
@ -156,7 +184,7 @@ public class PlayerActionChecker {
if (!this.plugin.getMVConfig().getEnforceAccess()) {
return WorldAccessResult.NO_ENFORCE_WORLD_ACCESS;
}
return permissionsTool.hasWorldAccess(sender, toWorld)
return PermissionsChecker.hasWorldAccessPermission(sender, toWorld)
? WorldAccessResult.HAS_WORLD_ACCESS : WorldAccessResult.NO_WORLD_ACCESS;
}
@ -183,7 +211,7 @@ public class PlayerActionChecker {
return EntryFeeResult.FREE_ENTRY;
}
Player player = (Player) sender;
if (permissionsTool.hasBypassEntryFee(player, toWorld)) {
if (PermissionsChecker.hasWorldExemptPermission(player, toWorld)) {
return EntryFeeResult.EXEMPT_FROM_ENTRY_FEE;
}
return this.plugin.getEconomist().isPlayerWealthyEnough(player, price, currency)
@ -204,7 +232,7 @@ public class PlayerActionChecker {
if (toWorld.getPlayerLimit() > toWorld.getCBWorld().getPlayers().size()) {
return PlayerLimitResult.WITHIN_PLAYERLIMIT;
}
return permissionsTool.hasBypassPlayerLimit(sender, toWorld)
return PermissionsChecker.hasWorldPlayerLimitBypassPermission(sender, toWorld)
? PlayerLimitResult.BYPASS_PLAYERLIMIT : PlayerLimitResult.EXCEED_PLAYERLIMIT;
}
@ -236,7 +264,7 @@ public class PlayerActionChecker {
return GameModeResult.KEEP_GAME_MODE;
}
//TODO: Add config option disable game mode enforcement
return permissionsTool.hasBypassGameModeEnforcement(player, toWorld)
return PermissionsChecker.hasWorldGamemodeBypassPermission(player, toWorld)
? GameModeResult.KEEP_GAME_MODE : GameModeResult.ENFORCE_GAME_MODE;
}
}

View File

@ -19,6 +19,7 @@ import com.onarandombox.MultiverseCore.api.BlockSafety;
import com.onarandombox.MultiverseCore.api.MVWorld;
import com.onarandombox.MultiverseCore.api.SafeTTeleporter;
import com.onarandombox.MultiverseCore.exceptions.PropertyDoesNotExistException;
import com.onarandombox.MultiverseCore.utils.permission.PermissionsRegistrar;
import com.onarandombox.MultiverseCore.world.configuration.AllowedPortalType;
import com.onarandombox.MultiverseCore.world.configuration.EnglishChatColor;
import com.onarandombox.MultiverseCore.world.configuration.SpawnLocation;
@ -85,7 +86,7 @@ public class SimpleMVWorld implements MVWorld {
this.props.environment = world.getEnvironment();
this.props.seed = world.getSeed();
this.plugin.getPermissionsTool().registerMVWorldPermissions(this);
PermissionsRegistrar.registerWorldPermissions(this);
this.props.flushChanges();

View File

@ -33,6 +33,7 @@ import com.onarandombox.MultiverseCore.api.SafeTTeleporter;
import com.onarandombox.MultiverseCore.api.WorldPurger;
import com.onarandombox.MultiverseCore.event.MVWorldDeleteEvent;
import com.onarandombox.MultiverseCore.utils.file.FileUtils;
import com.onarandombox.MultiverseCore.utils.permission.PermissionsRegistrar;
import org.bukkit.Bukkit;
import org.bukkit.GameRule;
import org.bukkit.Location;
@ -723,7 +724,7 @@ public class SimpleMVWorldManager implements MVWorldManager {
// Force the worlds to be loaded, ie don't just load new worlds.
if (forceLoad) {
this.plugin.getPermissionsTool().removeAllMVWorldPermissions();
PermissionsRegistrar.removeAllWorldPermissions();
this.worlds.clear();
}