mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-06-11 20:13:55 +00:00
First attempt at integrating Piston as the only command system
This commit is contained in:
@ -19,26 +19,29 @@
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.inject.Key;
|
||||
import com.sk89q.bukkit.util.CommandInspector;
|
||||
import com.sk89q.minecraft.util.commands.CommandLocals;
|
||||
import com.sk89q.worldedit.extension.platform.Actor;
|
||||
import com.sk89q.worldedit.util.command.CommandMapping;
|
||||
import com.sk89q.worldedit.util.command.Description;
|
||||
import com.sk89q.worldedit.util.command.Dispatcher;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.enginehub.piston.CommandManager;
|
||||
import org.enginehub.piston.CommandParameters;
|
||||
import org.enginehub.piston.NoInputCommandParameters;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
class BukkitCommandInspector implements CommandInspector {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(BukkitCommandInspector.class);
|
||||
private final WorldEditPlugin plugin;
|
||||
private final Dispatcher dispatcher;
|
||||
private final CommandManager dispatcher;
|
||||
|
||||
BukkitCommandInspector(WorldEditPlugin plugin, Dispatcher dispatcher) {
|
||||
BukkitCommandInspector(WorldEditPlugin plugin, CommandManager dispatcher) {
|
||||
checkNotNull(plugin);
|
||||
checkNotNull(dispatcher);
|
||||
this.plugin = plugin;
|
||||
@ -47,9 +50,9 @@ class BukkitCommandInspector implements CommandInspector {
|
||||
|
||||
@Override
|
||||
public String getShortText(Command command) {
|
||||
CommandMapping mapping = dispatcher.get(command.getName());
|
||||
if (mapping != null) {
|
||||
return mapping.getDescription().getDescription();
|
||||
Optional<org.enginehub.piston.Command> mapping = dispatcher.getCommand(command.getName());
|
||||
if (mapping.isPresent()) {
|
||||
return mapping.get().getDescription();
|
||||
} else {
|
||||
logger.warn("BukkitCommandInspector doesn't know how about the command '" + command + "'");
|
||||
return "Help text not available";
|
||||
@ -58,10 +61,9 @@ class BukkitCommandInspector implements CommandInspector {
|
||||
|
||||
@Override
|
||||
public String getFullText(Command command) {
|
||||
CommandMapping mapping = dispatcher.get(command.getName());
|
||||
if (mapping != null) {
|
||||
Description description = mapping.getDescription();
|
||||
return "Usage: " + description.getUsage() + (description.getHelp() != null ? "\n" + description.getHelp() : "");
|
||||
Optional<org.enginehub.piston.Command> mapping = dispatcher.getCommand(command.getName());
|
||||
if (mapping.isPresent()) {
|
||||
return mapping.get().getFullHelp();
|
||||
} else {
|
||||
logger.warn("BukkitCommandInspector doesn't know how about the command '" + command + "'");
|
||||
return "Help text not available";
|
||||
@ -70,11 +72,14 @@ class BukkitCommandInspector implements CommandInspector {
|
||||
|
||||
@Override
|
||||
public boolean testPermission(CommandSender sender, Command command) {
|
||||
CommandMapping mapping = dispatcher.get(command.getName());
|
||||
if (mapping != null) {
|
||||
CommandLocals locals = new CommandLocals();
|
||||
locals.put(Actor.class, plugin.wrapCommandSender(sender));
|
||||
return mapping.getCallable().testPermission(locals);
|
||||
Optional<org.enginehub.piston.Command> mapping = dispatcher.getCommand(command.getName());
|
||||
if (mapping.isPresent()) {
|
||||
CommandParameters parameters = NoInputCommandParameters.builder()
|
||||
.injectedValues(ImmutableMap.of(
|
||||
Key.get(Actor.class), plugin.wrapCommandSender(sender)
|
||||
))
|
||||
.build();
|
||||
return mapping.get().getCondition().satisfied(parameters);
|
||||
} else {
|
||||
logger.warn("BukkitCommandInspector doesn't know how about the command '" + command + "'");
|
||||
return false;
|
||||
|
@ -22,27 +22,28 @@ package com.sk89q.worldedit.bukkit;
|
||||
import com.sk89q.bukkit.util.CommandInfo;
|
||||
import com.sk89q.bukkit.util.CommandRegistration;
|
||||
import com.sk89q.worldedit.LocalConfiguration;
|
||||
import com.sk89q.worldedit.command.util.PermissionCondition;
|
||||
import com.sk89q.worldedit.entity.Player;
|
||||
import com.sk89q.worldedit.extension.platform.Actor;
|
||||
import com.sk89q.worldedit.extension.platform.Capability;
|
||||
import com.sk89q.worldedit.extension.platform.MultiUserPlatform;
|
||||
import com.sk89q.worldedit.extension.platform.Preference;
|
||||
import com.sk89q.worldedit.util.command.CommandMapping;
|
||||
import com.sk89q.worldedit.util.command.Description;
|
||||
import com.sk89q.worldedit.util.command.Dispatcher;
|
||||
import com.sk89q.worldedit.world.registry.Registries;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.enginehub.piston.CommandManager;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class BukkitServerInterface implements MultiUserPlatform {
|
||||
public Server server;
|
||||
@ -116,20 +117,25 @@ public class BukkitServerInterface implements MultiUserPlatform {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerCommands(Dispatcher dispatcher) {
|
||||
List<CommandInfo> toRegister = new ArrayList<>();
|
||||
public void registerCommands(CommandManager dispatcher) {
|
||||
BukkitCommandInspector inspector = new BukkitCommandInspector(plugin, dispatcher);
|
||||
|
||||
for (CommandMapping command : dispatcher.getCommands()) {
|
||||
Description description = command.getDescription();
|
||||
List<String> permissions = description.getPermissions();
|
||||
String[] permissionsArray = new String[permissions.size()];
|
||||
permissions.toArray(permissionsArray);
|
||||
|
||||
toRegister.add(new CommandInfo(description.getUsage(), description.getDescription(), command.getAllAliases(), inspector, permissionsArray));
|
||||
}
|
||||
dynamicCommands.register(dispatcher.getAllCommands()
|
||||
.map(command -> {
|
||||
String[] permissionsArray = command.getCondition()
|
||||
.as(PermissionCondition.class)
|
||||
.map(PermissionCondition::getPermissions)
|
||||
.map(s -> s.toArray(new String[0]))
|
||||
.orElseGet(() -> new String[0]);
|
||||
|
||||
dynamicCommands.register(toRegister);
|
||||
String[] aliases = Stream.concat(
|
||||
Stream.of(command.getName()),
|
||||
command.getAliases().stream()
|
||||
).toArray(String[]::new);
|
||||
return new CommandInfo(command.getUsage(),
|
||||
command.getDescription(), aliases,
|
||||
inspector, permissionsArray);
|
||||
}).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -21,13 +21,13 @@
|
||||
|
||||
package com.sk89q.worldedit.bukkit;
|
||||
|
||||
import com.sk89q.minecraft.util.commands.CommandLocals;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.inject.Key;
|
||||
import com.sk89q.util.StringUtil;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.entity.Player;
|
||||
import com.sk89q.worldedit.extension.platform.Actor;
|
||||
import com.sk89q.worldedit.util.Location;
|
||||
import com.sk89q.worldedit.util.command.CommandMapping;
|
||||
import com.sk89q.worldedit.world.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.event.Event.Result;
|
||||
@ -40,9 +40,9 @@ import org.bukkit.event.player.PlayerCommandSendEvent;
|
||||
import org.bukkit.event.player.PlayerGameModeChangeEvent;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import org.enginehub.piston.CommandManager;
|
||||
import org.enginehub.piston.CommandParameters;
|
||||
import org.enginehub.piston.NoInputCommandParameters;
|
||||
|
||||
/**
|
||||
* Handles all events thrown in relation to a Player
|
||||
@ -87,7 +87,7 @@ public class WorldEditListener implements Listener {
|
||||
|
||||
if (split.length > 0) {
|
||||
split[0] = split[0].substring(1);
|
||||
split = plugin.getWorldEdit().getPlatformManager().getCommandManager().commandDetection(split);
|
||||
split = plugin.getWorldEdit().getPlatformManager().getPlatformCommandMananger().commandDetection(split);
|
||||
}
|
||||
|
||||
final String newMessage = "/" + StringUtil.joinString(split, " ");
|
||||
@ -108,13 +108,18 @@ public class WorldEditListener implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||
public void onPlayerCommand(PlayerCommandSendEvent event) {
|
||||
CommandLocals locals = new CommandLocals();
|
||||
locals.put(Actor.class, plugin.wrapCommandSender(event.getPlayer()));
|
||||
Set<String> toRemove = plugin.getWorldEdit().getPlatformManager().getCommandManager().getDispatcher().getCommands().stream()
|
||||
.filter(commandMapping -> !commandMapping.getCallable().testPermission(locals))
|
||||
.map(CommandMapping::getPrimaryAlias)
|
||||
.collect(Collectors.toSet());
|
||||
event.getCommands().removeIf(toRemove::contains);
|
||||
CommandParameters parameters = NoInputCommandParameters.builder()
|
||||
.injectedValues(ImmutableMap.of(
|
||||
Key.get(Actor.class), plugin.wrapCommandSender(event.getPlayer())
|
||||
))
|
||||
.build();
|
||||
CommandManager commandManager = plugin.getWorldEdit().getPlatformManager().getPlatformCommandMananger().getCommandManager();
|
||||
event.getCommands().removeIf(name ->
|
||||
// remove if in the manager and not satisfied
|
||||
commandManager.getCommand(name)
|
||||
.filter(command -> !command.getCondition().satisfied(parameters))
|
||||
.isPresent()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user