|
| 1 | +/* |
| 2 | + * CarbonChat |
| 3 | + * |
| 4 | + * Copyright (c) 2024 Josua Parks (Vicarious) |
| 5 | + * 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 <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | +package net.draycia.carbon.paper.integration.fuuid; |
| 21 | + |
| 22 | +import com.google.inject.Inject; |
| 23 | +import com.massivecraft.factions.FPlayer; |
| 24 | +import com.massivecraft.factions.Faction; |
| 25 | +import com.massivecraft.factions.perms.Role; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import net.draycia.carbon.api.channels.ChannelPermissions; |
| 31 | +import net.draycia.carbon.api.users.CarbonPlayer; |
| 32 | +import net.draycia.carbon.api.users.UserManager; |
| 33 | +import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource; |
| 34 | +import net.kyori.adventure.audience.Audience; |
| 35 | +import net.kyori.adventure.key.Key; |
| 36 | +import org.bukkit.entity.Player; |
| 37 | +import org.checkerframework.checker.nullness.qual.MonotonicNonNull; |
| 38 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 39 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 40 | +import org.checkerframework.framework.qual.DefaultQualifier; |
| 41 | +import org.spongepowered.configurate.objectmapping.ConfigSerializable; |
| 42 | + |
| 43 | +import static net.draycia.carbon.api.channels.ChannelPermissionResult.channelPermissionResult; |
| 44 | + |
| 45 | +@DefaultQualifier(NonNull.class) |
| 46 | +@ConfigSerializable |
| 47 | +public class FactionModChannel extends AbstractFactionsChannel { |
| 48 | + |
| 49 | + public static final String FILE_NAME = "factionsuuid-factionmodchat.conf"; |
| 50 | + |
| 51 | + private transient @MonotonicNonNull @Inject UserManager<?> users; |
| 52 | + |
| 53 | + // We could check if the player doesn't have the normal role, but this list may be configurable in the future? |
| 54 | + private transient final List<Role> validRoles = List.of(Role.ADMIN, Role.MODERATOR, Role.COLEADER); |
| 55 | + |
| 56 | + public FactionModChannel() { |
| 57 | + this.key = Key.key("carbon", "factionmodchat"); |
| 58 | + this.commandAliases = List.of("mc"); |
| 59 | + |
| 60 | + this.messageSource = new ConfigChannelMessageSource(); |
| 61 | + this.messageSource.defaults = Map.of( |
| 62 | + "default_format", "(fmod: %factionsuuid_faction_name%) <display_name>: <message>", |
| 63 | + "console", "[fmod: %factionsuuid_faction_name%] <username>: <message>" |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public ChannelPermissions permissions() { |
| 69 | + return ChannelPermissions.uniformDynamic(player -> channelPermissionResult( |
| 70 | + this.validRoles.contains(this.factionRole(player)), |
| 71 | + () -> this.messages.cannotUseFactionModChannel(player) |
| 72 | + )); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public List<Audience> recipients(final CarbonPlayer sender) { |
| 77 | + if (!this.validRoles.contains(this.factionRole(sender))) { |
| 78 | + if (sender.online()) { |
| 79 | + sender.sendMessage(this.messages.cannotUseFactionModChannel(sender)); |
| 80 | + } |
| 81 | + |
| 82 | + return Collections.emptyList(); |
| 83 | + } |
| 84 | + |
| 85 | + final List<Audience> recipients = new ArrayList<>(); |
| 86 | + for (final Player player : this.factionMods(sender)) { |
| 87 | + final @Nullable CarbonPlayer carbon = this.users.user(player.getUniqueId()).getNow(null); |
| 88 | + if (carbon != null) { |
| 89 | + recipients.add(carbon); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + recipients.add(this.server.console()); |
| 94 | + |
| 95 | + return recipients; |
| 96 | + } |
| 97 | + |
| 98 | + private List<Player> factionMods(final CarbonPlayer player) { |
| 99 | + final @Nullable Faction faction = this.faction(player); |
| 100 | + |
| 101 | + if (faction == null) { |
| 102 | + return List.of(); |
| 103 | + } |
| 104 | + |
| 105 | + final List<Player> factionMods = new ArrayList<>(); |
| 106 | + |
| 107 | + for (final FPlayer onlinePlayer : faction.getFPlayersWhereOnline(true)) { |
| 108 | + if (this.validRoles.contains(onlinePlayer.getRole())) { |
| 109 | + factionMods.add(onlinePlayer.getPlayer()); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return factionMods; |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments