From 1ed9f4441e26432c3a0e5657058273d14c7b7004 Mon Sep 17 00:00:00 2001 From: Josua Parks Date: Mon, 13 Jan 2025 02:52:29 -0800 Subject: [PATCH] Add faction mod channel (#590) --- .../common/messages/CarbonMessages.java | 4 + .../locale/messages-en_US.properties | 1 + .../fuuid/AbstractFactionsChannel.java | 17 ++- .../integration/fuuid/FactionModChannel.java | 116 ++++++++++++++++++ .../fuuid/FactionsIntegration.java | 4 + 5 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/FactionModChannel.java diff --git a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java index 192f59dc..265ee864 100644 --- a/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java +++ b/common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java @@ -634,4 +634,8 @@ public interface CarbonMessages { @Message("integrations.fuuid.cannot_use_truce_channel") Component cannotUseTruceChannel(Audience audience); + + @Message("integrations.fuuid.cannot_use_mod_channel") + Component cannotUseFactionModChannel(Audience audience); + } diff --git a/common/src/main/resources/locale/messages-en_US.properties b/common/src/main/resources/locale/messages-en_US.properties index ee459b28..0ba11977 100644 --- a/common/src/main/resources/locale/messages-en_US.properties +++ b/common/src/main/resources/locale/messages-en_US.properties @@ -169,3 +169,4 @@ integrations.mcmmo.cannot_use_party_channel=You must join an mcMMO party to integrations.fuuid.cannot_use_faction_channel=You must join a faction to use this channel. integrations.fuuid.cannot_use_alliance_channel=You must join an alliance to use this channel. integrations.fuuid.cannot_use_truce_channel=You must have a truce with another faction to use this channel. +integrations.fuuid.cannot_use_mod_channel=You must be a faction mod/admin to use this channel. diff --git a/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/AbstractFactionsChannel.java b/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/AbstractFactionsChannel.java index d649d9db..bdd69d97 100644 --- a/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/AbstractFactionsChannel.java +++ b/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/AbstractFactionsChannel.java @@ -23,6 +23,7 @@ import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.FPlayers; import com.massivecraft.factions.Faction; import com.massivecraft.factions.perms.Relation; +import com.massivecraft.factions.perms.Role; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.common.channels.ConfigChatChannel; import org.checkerframework.checker.nullness.qual.Nullable; @@ -33,7 +34,7 @@ import org.jspecify.annotations.NonNull; abstract class AbstractFactionsChannel extends ConfigChatChannel { protected final @Nullable Faction faction(final CarbonPlayer player) { - final FPlayer fPlayer = FPlayers.getInstance().getById(player.uuid().toString()); + final @Nullable FPlayer fPlayer = this.factionPlayer(player); if (fPlayer == null || !fPlayer.hasFaction()) { return null; @@ -42,6 +43,20 @@ abstract class AbstractFactionsChannel extends ConfigChatChannel { return fPlayer.getFaction(); } + protected final @Nullable FPlayer factionPlayer(final CarbonPlayer player) { + return FPlayers.getInstance().getById(player.uuid().toString()); + } + + protected final @Nullable Role factionRole(final CarbonPlayer player) { + final @Nullable FPlayer fPlayer = this.factionPlayer(player); + + if (fPlayer == null || !fPlayer.hasFaction()) { + return null; + } + + return fPlayer.getRole(); + } + protected final boolean hasRelations(final CarbonPlayer player, final Relation relation) { final @Nullable Faction faction = this.faction(player); diff --git a/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/FactionModChannel.java b/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/FactionModChannel.java new file mode 100644 index 00000000..239baa2a --- /dev/null +++ b/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/FactionModChannel.java @@ -0,0 +1,116 @@ +/* + * CarbonChat + * + * Copyright (c) 2024 Josua Parks (Vicarious) + * Contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.draycia.carbon.paper.integration.fuuid; + +import com.google.inject.Inject; +import com.massivecraft.factions.FPlayer; +import com.massivecraft.factions.Faction; +import com.massivecraft.factions.perms.Role; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import net.draycia.carbon.api.channels.ChannelPermissions; +import net.draycia.carbon.api.users.CarbonPlayer; +import net.draycia.carbon.api.users.UserManager; +import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.key.Key; +import org.bukkit.entity.Player; +import org.checkerframework.checker.nullness.qual.MonotonicNonNull; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.checkerframework.framework.qual.DefaultQualifier; +import org.spongepowered.configurate.objectmapping.ConfigSerializable; + +import static net.draycia.carbon.api.channels.ChannelPermissionResult.channelPermissionResult; + +@DefaultQualifier(NonNull.class) +@ConfigSerializable +public class FactionModChannel extends AbstractFactionsChannel { + + public static final String FILE_NAME = "factionsuuid-factionmodchat.conf"; + + private transient @MonotonicNonNull @Inject UserManager users; + + // We could check if the player doesn't have the normal role, but this list may be configurable in the future? + private transient final List validRoles = List.of(Role.ADMIN, Role.MODERATOR, Role.COLEADER); + + public FactionModChannel() { + this.key = Key.key("carbon", "factionmodchat"); + this.commandAliases = List.of("mc"); + + this.messageSource = new ConfigChannelMessageSource(); + this.messageSource.defaults = Map.of( + "default_format", "(fmod: %factionsuuid_faction_name%) : ", + "console", "[fmod: %factionsuuid_faction_name%] : " + ); + } + + @Override + public ChannelPermissions permissions() { + return ChannelPermissions.uniformDynamic(player -> channelPermissionResult( + this.validRoles.contains(this.factionRole(player)), + () -> this.messages.cannotUseFactionModChannel(player) + )); + } + + @Override + public List recipients(final CarbonPlayer sender) { + if (!this.validRoles.contains(this.factionRole(sender))) { + if (sender.online()) { + sender.sendMessage(this.messages.cannotUseFactionModChannel(sender)); + } + + return Collections.emptyList(); + } + + final List recipients = new ArrayList<>(); + for (final Player player : this.factionMods(sender)) { + final @Nullable CarbonPlayer carbon = this.users.user(player.getUniqueId()).getNow(null); + if (carbon != null) { + recipients.add(carbon); + } + } + + recipients.add(this.server.console()); + + return recipients; + } + + private List factionMods(final CarbonPlayer player) { + final @Nullable Faction faction = this.faction(player); + + if (faction == null) { + return List.of(); + } + + final List factionMods = new ArrayList<>(); + + for (final FPlayer onlinePlayer : faction.getFPlayersWhereOnline(true)) { + if (this.validRoles.contains(onlinePlayer.getRole())) { + factionMods.add(onlinePlayer.getPlayer()); + } + } + + return factionMods; + } + +} diff --git a/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/FactionsIntegration.java b/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/FactionsIntegration.java index 13887e89..ea4908f5 100644 --- a/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/FactionsIntegration.java +++ b/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/FactionsIntegration.java @@ -59,6 +59,9 @@ public final class FactionsIntegration implements Integration { if (this.config.truceChannel) { this.channelRegistry.registerSpecialConfigChannel(TruceChannel.FILE_NAME, TruceChannel.class); } + if (this.config.factionModChannel) { + this.channelRegistry.registerSpecialConfigChannel(FactionModChannel.FILE_NAME, FactionModChannel.class); + } } public static ConfigMeta configMeta() { @@ -73,6 +76,7 @@ public final class FactionsIntegration implements Integration { boolean factionChannel = true; boolean allianceChannel = true; boolean truceChannel = true; + boolean factionModChannel = false; }