Add faction mod channel (#590)

This commit is contained in:
Josua Parks authored and GitHub committed 2025-01-13 02:52:29 -08:00
1 parent a58c745725
commit 1ed9f4441e
5 files changed
+141 -1

No files matched your search

@@ -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);
}
@@ -169,3 +169,4 @@ integrations.mcmmo.cannot_use_party_channel=<red>You must join an mcMMO party to
integrations.fuuid.cannot_use_faction_channel=<red>You must join a faction to use this channel.
integrations.fuuid.cannot_use_alliance_channel=<red>You must join an alliance to use this channel.
integrations.fuuid.cannot_use_truce_channel=<red>You must have a truce with another faction to use this channel.
integrations.fuuid.cannot_use_mod_channel=<red>You must be a faction mod/admin to use this channel.
@@ -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);
@@ -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 <https://www.gnu.org/licenses/>.
*/
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<Role> 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%) <display_name>: <message>",
"console", "[fmod: %factionsuuid_faction_name%] <username>: <message>"
);
}
@Override
public ChannelPermissions permissions() {
return ChannelPermissions.uniformDynamic(player -> channelPermissionResult(
this.validRoles.contains(this.factionRole(player)),
() -> this.messages.cannotUseFactionModChannel(player)
));
}
@Override
public List<Audience> 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<Audience> 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<Player> factionMods(final CarbonPlayer player) {
final @Nullable Faction faction = this.faction(player);
if (faction == null) {
return List.of();
}
final List<Player> factionMods = new ArrayList<>();
for (final FPlayer onlinePlayer : faction.getFPlayersWhereOnline(true)) {
if (this.validRoles.contains(onlinePlayer.getRole())) {
factionMods.add(onlinePlayer.getPlayer());
}
}
return factionMods;
}
}
@@ -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;
}