Add teleport permission checking support.

This commit is contained in:
benwoo1110 2020-12-20 14:33:25 +08:00
parent 1302a1efb8
commit 3ca9de69f0
4 changed files with 106 additions and 37 deletions

View File

@ -245,11 +245,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
// Setup Permissions, we'll do an initial check for the Permissions plugin then fall back on isOP().
this.ph = new MVPermissions(this);
// Setup commands
//TODO: Should init commands after config
this.commandManager = new MVCommandManager(this);
// Initialize the Destination factor AFTER the commands
// Initialize the Destination factory
this.initializeDestinationFactory();
this.playerSessions = new HashMap<String, MVPlayerSession>();
@ -295,6 +291,9 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core {
this.initializeBuscript();
this.setupMetrics();
// Setup commands
this.commandManager = new MVCommandManager(this);
// Output a little snippet to show it's enabled.
Logging.config("Version %s (API v%s) Enabled - By %s", this.getDescription().getVersion(), PROTOCOL, getAuthors());

View File

@ -1,8 +1,10 @@
package com.onarandombox.MultiverseCore.commands_acf;
import co.aikar.commands.BaseCommand;
import co.aikar.commands.CommandIssuer;
import co.aikar.commands.annotation.CommandAlias;
import co.aikar.commands.annotation.CommandCompletion;
import co.aikar.commands.annotation.CommandPermission;
import co.aikar.commands.annotation.Description;
import co.aikar.commands.annotation.Flags;
import co.aikar.commands.annotation.Single;
@ -24,6 +26,8 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.Set;
public class TeleportCommand extends MultiverseCommand {
private final SafeTTeleporter playerTeleporter;
@ -37,36 +41,53 @@ public class TeleportCommand extends MultiverseCommand {
@CommandAlias("mv")
public class Teleport extends BaseCommand {
@Subcommand("tp|teleport")
@Subcommand("teleport")
@CommandPermission("@destinations")
@Syntax("[player] <destination>")
@CommandCompletion("@players|@MVWorlds @MVWorlds")
@Description("Allows you to the teleport to a location on your server!")
public void doTeleportCommand(@NotNull CommandSender sender,
@NotNull @Flags("other,defaultself,fallbackself") Player player,
@NotNull @Single String destinationName) {
@NotNull @Flags("other,defaultself,fallbackself") Player player,
@Single String destinationName) {
doTeleport(sender, player, destinationName);
}
@Override
public Set<String> getRequiredPermissions() {
return destPerms();
}
}
public class AliasTeleport extends BaseCommand {
@CommandAlias("mvtp")
@CommandPermission("@destinations")
@Syntax("[player] <destination>")
//TODO: playerOnly flag
@CommandCompletion("@players|@MVWorlds @MVWorlds")
@Description("Alias for /mv tp")
public void doTeleportCommand(@NotNull CommandSender sender,
@NotNull @Flags("other,defaultself,fallbackself") Player player,
@NotNull @Single String destinationName) {
@Single String destinationName) {
doTeleport(sender, player, destinationName);
}
@Override
public Set<String> getRequiredPermissions() {
return destPerms();
}
}
private Set<String> destPerms() {
return this.plugin.getDestFactory().getPermissions();
}
private void doTeleport(@NotNull CommandSender teleporter,
@NotNull Player teleportee,
@NotNull String destinationName) {
String destinationName) {
destinationName = parseCannonDestination(teleportee, destinationName);
MVDestination destination = this.plugin.getDestFactory().getDestination(destinationName);

View File

@ -5,9 +5,11 @@ import co.aikar.commands.BukkitCommandExecutionContext;
import co.aikar.commands.BukkitCommandIssuer;
import co.aikar.commands.CommandCompletions;
import co.aikar.commands.CommandContexts;
import co.aikar.commands.CommandIssuer;
import co.aikar.commands.ConditionContext;
import co.aikar.commands.ConditionFailedException;
import co.aikar.commands.PaperCommandManager;
import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.commands_acf.BedCommand;
@ -43,6 +45,7 @@ import com.onarandombox.MultiverseCore.enums.AddProperties;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import sun.rmi.runtime.Log;
import java.io.File;
import java.util.Collections;
@ -120,6 +123,39 @@ public class MVCommandManager extends PaperCommandManager {
return this.completions;
}
/**
* Change default implementation to OR instead of AND
*/
@Override
public boolean hasPermission(CommandIssuer issuer, Set<String> permissions) {
if (permissions == null || permissions.isEmpty()) {
return true;
}
for (String permission : permissions) {
if (hasPermission(issuer, permission)) {
return true;
}
}
return false;
}
/**
* Change default implementation to OR instead of AND
*/
@Override
public boolean hasPermission(CommandIssuer issuer, String permission) {
if (permission == null || permission.isEmpty()) {
return true;
}
for (String perm : permission.split(",")) {
if (!perm.isEmpty() && issuer.hasPermission(perm)) {
return true;
}
}
return false;
}
public CommandQueueManager getQueueManager() {
return commandQueueManager;
}

View File

@ -7,6 +7,7 @@
package com.onarandombox.MultiverseCore.destination;
import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MVDestination;
import com.onarandombox.MultiverseCore.utils.PermissionTools;
@ -14,24 +15,27 @@ import com.pneumaticraft.commandhandler.Command;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/** A factory class that will create destinations from specific strings. */
public class DestinationFactory {
private MultiverseCore plugin;
private Map<String, Class<? extends MVDestination>> destList;
private Command teleportCommand;
private final MultiverseCore plugin;
private final Map<String, Class<? extends MVDestination>> destList;
private final Set<String> destPermissions;
private final PermissionTools permTools;
public DestinationFactory(MultiverseCore plugin) {
this.plugin = plugin;
this.destList = new HashMap<String, Class<? extends MVDestination>>();
// List<Command> cmds = this.plugin.getCommandHandler().getAllCommands();
// for (Command c : cmds) {
// if (c instanceof TeleportCommand) {
// this.teleportCommand = c;
// }
// }
this.destList = new HashMap<>();
this.destPermissions = new HashSet<>();
this.permTools = new PermissionTools(plugin);
}
/**
@ -80,23 +84,32 @@ public class DestinationFactory {
if (identifier.equals("")) {
identifier = "w";
}
Permission self = this.plugin.getServer().getPluginManager().getPermission("multiverse.teleport.self." + identifier);
Permission other = this.plugin.getServer().getPluginManager().getPermission("multiverse.teleport.other." + identifier);
PermissionTools pt = new PermissionTools(this.plugin);
if (self == null) {
self = new Permission("multiverse.teleport.self." + identifier,
"Permission to teleport yourself for the " + identifier + " destination.", PermissionDefault.OP);
this.plugin.getServer().getPluginManager().addPermission(self);
pt.addToParentPerms("multiverse.teleport.self." + identifier);
}
if (other == null) {
other = new Permission("multiverse.teleport.other." + identifier,
"Permission to teleport others for the " + identifier + " destination.", PermissionDefault.OP);
this.plugin.getServer().getPluginManager().addPermission(other);
pt.addToParentPerms("multiverse.teleport.other." + identifier);
}
// this.teleportCommand.addAdditonalPermission(self);
// this.teleportCommand.addAdditonalPermission(other);
addDestPerm("multiverse.teleport.self." + identifier,
"Permission to teleport yourself for the " + identifier + " destination.");
addDestPerm("multiverse.teleport.other." + identifier,
"Permission to teleport other for the " + identifier + " destination.");
return true;
}
private void addDestPerm(String permNode, String description) {
if (this.plugin.getServer().getPluginManager().getPermission(permNode) != null) {
Logging.fine("Destination permission node " + permNode + " already added.");
return;
}
Permission perm = new Permission(permNode, description, PermissionDefault.OP);
this.plugin.getServer().getPluginManager().addPermission(perm);
permTools.addToParentPerms(permNode);
destPermissions.add(permNode);
}
public Collection<String> getIdentifiers() {
return destList.keySet();
}
public Set<String> getPermissions() {
return destPermissions;
}
}