|
| 1 | +/* |
| 2 | + * WorldEdit, a Minecraft world manipulation toolkit |
| 3 | + * Copyright (C) sk89q <http://www.sk89q.com> |
| 4 | + * Copyright (C) WorldEdit team and contributors |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +package com.sk89q.worldedit.sponge; |
| 21 | + |
| 22 | +import com.sk89q.worldedit.command.util.PermissionCondition; |
| 23 | +import com.sk89q.worldedit.sponge.internal.LocaleResolver; |
| 24 | +import net.kyori.adventure.text.Component; |
| 25 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 26 | +import org.enginehub.piston.Command; |
| 27 | +import org.spongepowered.api.command.CommandCause; |
| 28 | + |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.Optional; |
| 31 | +import java.util.Set; |
| 32 | + |
| 33 | +public abstract class CommandAdapter implements org.spongepowered.api.command.Command.Raw { |
| 34 | + private final Command command; |
| 35 | + |
| 36 | + protected CommandAdapter(Command command) { |
| 37 | + this.command = command; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public boolean canExecute(CommandCause cause) { |
| 42 | + Set<String> permissions = command.getCondition().as(PermissionCondition.class) |
| 43 | + .map(PermissionCondition::getPermissions) |
| 44 | + .orElseGet(Collections::emptySet); |
| 45 | + |
| 46 | + // Allow commands without permission nodes to always execute. |
| 47 | + if (permissions.isEmpty()) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + for (String perm : permissions) { |
| 52 | + if (cause.hasPermission(perm)) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + } |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Optional<Component> shortDescription(CommandCause cause) { |
| 61 | + return Optional.of(command.getDescription()) |
| 62 | + .map(desc -> SpongeTextAdapter.convert(desc, LocaleResolver.resolveLocale(cause.audience()))); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Optional<Component> extendedDescription(CommandCause cause) { |
| 67 | + return command.getFooter() |
| 68 | + .map(footer -> SpongeTextAdapter.convert(footer, LocaleResolver.resolveLocale(cause.audience()))); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Optional<Component> help(@NonNull CommandCause cause) { |
| 73 | + return Optional.of(command.getFullHelp()) |
| 74 | + .map(help -> SpongeTextAdapter.convert(help, LocaleResolver.resolveLocale(cause.audience()))); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public Component usage(CommandCause cause) { |
| 79 | + return SpongeTextAdapter.convert(command.getUsage(), LocaleResolver.resolveLocale(cause.audience())); |
| 80 | + } |
| 81 | +} |
0 commit comments