|
| 1 | +/* |
| 2 | + * This file is part of spark. |
| 3 | + * |
| 4 | + * Copyright (c) lucko (Luck) <luck@lucko.me> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package me.lucko.spark.minestom; |
| 22 | + |
| 23 | +import java.util.function.BiPredicate; |
| 24 | +import me.lucko.spark.common.command.sender.AbstractCommandSender; |
| 25 | +import net.kyori.adventure.text.Component; |
| 26 | +import net.minestom.server.command.CommandSender; |
| 27 | +import net.minestom.server.command.ConsoleSender; |
| 28 | +import net.minestom.server.entity.Player; |
| 29 | + |
| 30 | +import java.util.UUID; |
| 31 | +import org.jetbrains.annotations.NotNull; |
| 32 | + |
| 33 | +final class MinestomCommandSender extends AbstractCommandSender<CommandSender> { |
| 34 | + |
| 35 | + private final @NotNull BiPredicate<CommandSender, String> permissionHandler; |
| 36 | + |
| 37 | + public MinestomCommandSender(CommandSender delegate, @NotNull BiPredicate<CommandSender, String> permissionHandler) { |
| 38 | + super(delegate); |
| 39 | + this.permissionHandler = permissionHandler; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public String getName() { |
| 44 | + if (this.delegate instanceof Player player) return player.getUsername(); |
| 45 | + else if (this.delegate instanceof ConsoleSender) return "Console"; |
| 46 | + return "unknown:" + this.delegate.getClass().getSimpleName(); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public UUID getUniqueId() { |
| 51 | + if (super.delegate instanceof Player player) return player.getUuid(); |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void sendMessage(Component message) { |
| 57 | + this.delegate.sendMessage(message); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean hasPermission(String permission) { |
| 62 | + return this.permissionHandler.test(this.delegate, permission); |
| 63 | + } |
| 64 | + |
| 65 | +} |
0 commit comments