Skip to content

Commit 1ed9f44

Browse files
authored
Add faction mod channel (#590)
1 parent a58c745 commit 1ed9f44

File tree

5 files changed

+141
-1
lines changed

5 files changed

+141
-1
lines changed

common/src/main/java/net/draycia/carbon/common/messages/CarbonMessages.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,4 +634,8 @@ void partySpy(
634634

635635
@Message("integrations.fuuid.cannot_use_truce_channel")
636636
Component cannotUseTruceChannel(Audience audience);
637+
638+
@Message("integrations.fuuid.cannot_use_mod_channel")
639+
Component cannotUseFactionModChannel(Audience audience);
640+
637641
}

common/src/main/resources/locale/messages-en_US.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,4 @@ integrations.mcmmo.cannot_use_party_channel=<red>You must join an mcMMO party to
169169
integrations.fuuid.cannot_use_faction_channel=<red>You must join a faction to use this channel.
170170
integrations.fuuid.cannot_use_alliance_channel=<red>You must join an alliance to use this channel.
171171
integrations.fuuid.cannot_use_truce_channel=<red>You must have a truce with another faction to use this channel.
172+
integrations.fuuid.cannot_use_mod_channel=<red>You must be a faction mod/admin to use this channel.

paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/AbstractFactionsChannel.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.massivecraft.factions.FPlayers;
2424
import com.massivecraft.factions.Faction;
2525
import com.massivecraft.factions.perms.Relation;
26+
import com.massivecraft.factions.perms.Role;
2627
import net.draycia.carbon.api.users.CarbonPlayer;
2728
import net.draycia.carbon.common.channels.ConfigChatChannel;
2829
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -33,7 +34,7 @@
3334
abstract class AbstractFactionsChannel extends ConfigChatChannel {
3435

3536
protected final @Nullable Faction faction(final CarbonPlayer player) {
36-
final FPlayer fPlayer = FPlayers.getInstance().getById(player.uuid().toString());
37+
final @Nullable FPlayer fPlayer = this.factionPlayer(player);
3738

3839
if (fPlayer == null || !fPlayer.hasFaction()) {
3940
return null;
@@ -42,6 +43,20 @@ abstract class AbstractFactionsChannel extends ConfigChatChannel {
4243
return fPlayer.getFaction();
4344
}
4445

46+
protected final @Nullable FPlayer factionPlayer(final CarbonPlayer player) {
47+
return FPlayers.getInstance().getById(player.uuid().toString());
48+
}
49+
50+
protected final @Nullable Role factionRole(final CarbonPlayer player) {
51+
final @Nullable FPlayer fPlayer = this.factionPlayer(player);
52+
53+
if (fPlayer == null || !fPlayer.hasFaction()) {
54+
return null;
55+
}
56+
57+
return fPlayer.getRole();
58+
}
59+
4560
protected final boolean hasRelations(final CarbonPlayer player, final Relation relation) {
4661
final @Nullable Faction faction = this.faction(player);
4762

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
}

paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/FactionsIntegration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public void register() {
5959
if (this.config.truceChannel) {
6060
this.channelRegistry.registerSpecialConfigChannel(TruceChannel.FILE_NAME, TruceChannel.class);
6161
}
62+
if (this.config.factionModChannel) {
63+
this.channelRegistry.registerSpecialConfigChannel(FactionModChannel.FILE_NAME, FactionModChannel.class);
64+
}
6265
}
6366

6467
public static ConfigMeta configMeta() {
@@ -73,6 +76,7 @@ public static final class Config {
7376
boolean factionChannel = true;
7477
boolean allianceChannel = true;
7578
boolean truceChannel = true;
79+
boolean factionModChannel = false;
7680

7781
}
7882

0 commit comments

Comments
 (0)