Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Faction, Alliance, and Truce chats from FactionsUUID #349

Merged
merged 6 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -549,4 +549,13 @@ void errorCommandCommandExecution(
@Message("integrations.mcmmo.cannot_use_party_channel")
Component cannotUseMcmmoPartyChannel(Audience audience);

@Message("integrations.fuuid.cannot_use_faction_channel")
Component cannotUseFactionChannel(Audience audience);

@Message("integrations.fuuid.cannot_use_alliance_channel")
Component cannotUseFactionAllianceChannel(Audience audience);

@Message("integrations.fuuid.cannot_use_truce_channel")
Component cannotUseTruceChannel(Audience audience);

}
3 changes: 3 additions & 0 deletions common/src/main/resources/locale/messages-en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,6 @@ integrations.towny.cannot_use_alliance_channel=<red>You must join an alliance to
integrations.towny.cannot_use_nation_channel=<red>You must join a nation to use this channel.
integrations.towny.cannot_use_town_channel=<red>You must join a town to use this channel.
integrations.mcmmo.cannot_use_party_channel=<red>You must join an mcMMO party to use this channel.
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.
jpenilla marked this conversation as resolved.
Show resolved Hide resolved
integrations.fuuid.cannot_use_truce_channel=<red>You must have a truce with another faction to use this channel.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ nats = "2.16.14"
h2 = "2.2.224"
towny = "0.100.0.2"
mcmmo = "2.1.225"
fuuid = "1.6.9.5-U0.6.33"

# synced with version used by lowest supported mc (currently 1.19.4 on paper)
gson = "2.10"
Expand Down Expand Up @@ -139,3 +140,4 @@ placeholderapi = { group = "me.clip", name = "placeholderapi", version.ref = "pl
miniplaceholders = { group = "io.github.miniplaceholders", name = "miniplaceholders-api", version.ref = "miniplaceholders" }
towny = { group = "com.palmergames.bukkit.towny", name = "towny", version.ref = "towny" }
mcmmo = { group = "com.gmail.nossr50.mcMMO", name = "mcMMO", version.ref = "mcmmo" }
factionsUuid = { group = "com.massivecraft", name = "Factions", version.ref = "fuuid" }
3 changes: 3 additions & 0 deletions paper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {
compileOnly(libs.mcmmo) {
isTransitive = false
}
compileOnly(libs.factionsUuid)

runtimeDownload(libs.guice) {
exclude("com.google.guava")
Expand All @@ -43,6 +44,7 @@ dependencies {

configurablePlugins.dependency(libs.towny)
configurablePlugins.dependency(libs.mcmmo)
configurablePlugins.dependency(libs.factionsUuid)

tasks {
shadowJar {
Expand Down Expand Up @@ -86,6 +88,7 @@ paper {
// Integrations
dependency("Towny", PaperPluginDescription.Load.BEFORE, false)
dependency("mcMMO", PaperPluginDescription.Load.BEFORE, false)
dependency("Factions", PaperPluginDescription.Load.BEFORE, false)
}

bukkit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import net.draycia.carbon.common.util.CloudUtils;
import net.draycia.carbon.paper.command.PaperCommander;
import net.draycia.carbon.paper.command.PaperPlayerCommander;
import net.draycia.carbon.paper.integration.fuuid.FactionsIntegration;
import net.draycia.carbon.paper.integration.mcmmo.McmmoIntegration;
import net.draycia.carbon.paper.integration.towny.TownyIntegration;
import net.draycia.carbon.paper.listeners.PaperChatListener;
Expand Down Expand Up @@ -132,6 +133,9 @@ protected void configureIntegrations(final Multibinder<Integration> integrations

integrations.addBinding().to(McmmoIntegration.class);
configs.addBinding().toInstance(McmmoIntegration.configMeta());

integrations.addBinding().to(FactionsIntegration.class);
configs.addBinding().toInstance(FactionsIntegration.configMeta());
}

private void configureListeners() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* CarbonChat
*
* Copyright (c) 2023 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.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.perms.Relation;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.channels.ConfigChatChannel;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.jspecify.annotations.NonNull;

@DefaultQualifier(NonNull.class)
abstract class AbstractFactionsChannel extends ConfigChatChannel {

protected final @Nullable Faction faction(final CarbonPlayer player) {
final FPlayer fPlayer = FPlayers.getInstance().getById(player.uuid().toString());

if (fPlayer == null || !fPlayer.hasFaction()) {
return null;
}

return fPlayer.getFaction();
}

protected final boolean hasRelations(final CarbonPlayer player, final Relation relation) {
final @Nullable Faction faction = this.faction(player);

return faction != null && faction.getRelationCount(relation) > 0;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* CarbonChat
*
* Copyright (c) 2023 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.FPlayers;
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.perms.Relation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import net.draycia.carbon.api.channels.ChannelPermissionResult;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
import net.draycia.carbon.common.messages.CarbonMessages;
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 AllianceChannel extends AbstractFactionsChannel {

public static final String FILE_NAME = "factionsuuid-alliancechat.conf";

private transient @MonotonicNonNull @Inject CarbonMessages messages;
private transient @MonotonicNonNull @Inject UserManager<?> users;

public AllianceChannel() {
this.key = Key.key("carbon", "alliancechat");
this.commandAliases = List.of("ac");

this.messageSource = new ConfigChannelMessageSource();
this.messageSource.defaults = Map.of(
"default_format", "(alliance: %factionsuuid_faction_name%) <display_name>: <message>",
"console", "[alliance: %factionsuuid_faction_name%] <username>: <message>"
);
}

@Override
public ChannelPermissionResult speechPermitted(final CarbonPlayer player) {
return channelPermissionResult(
this.hasRelations(player, Relation.ALLY),
() -> this.messages.cannotUseFactionAllianceChannel(player)
);
}

@Override
public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) {
return channelPermissionResult(
this.hasRelations(player, Relation.ALLY),
() -> this.messages.cannotUseFactionAllianceChannel(player)
);
}

@Override
public List<Audience> recipients(final CarbonPlayer sender) {
if (!this.hasRelations(sender, Relation.ALLY)) {
if (sender.online()) {
sender.sendMessage(this.messages.cannotUseFactionAllianceChannel(sender));
}

return Collections.emptyList();
}

final List<Audience> recipients = new ArrayList<>();
for (final Player player : this.alliedPlayersTo(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> alliedPlayersTo(final CarbonPlayer player) {
final @Nullable Faction faction = this.faction(player);

if (faction == null) {
return List.of();
}

final List<Player> alliedPlayers = new ArrayList<>();

for (final FPlayer onlinePlayer : FPlayers.getInstance().getOnlinePlayers()) {
final Relation relation = faction.getRelationTo(onlinePlayer);

// Not sure if checking MEMBER is required, or if we only need to check ALLY
if (relation == Relation.MEMBER || relation == Relation.ALLY) {
jpenilla marked this conversation as resolved.
Show resolved Hide resolved
alliedPlayers.add(onlinePlayer.getPlayer());
}
}

return alliedPlayers;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* CarbonChat
*
* Copyright (c) 2023 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.Faction;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import net.draycia.carbon.api.channels.ChannelPermissionResult;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
import net.draycia.carbon.common.messages.CarbonMessages;
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 FactionChannel extends AbstractFactionsChannel {

public static final String FILE_NAME = "factionsuuid-factionchat.conf";

private transient @MonotonicNonNull @Inject CarbonMessages messages;
private transient @MonotonicNonNull @Inject UserManager<?> users;

public FactionChannel() {
this.key = Key.key("carbon", "factionchat");
this.commandAliases = List.of("fc");

this.messageSource = new ConfigChannelMessageSource();
this.messageSource.defaults = Map.of(
"default_format", "(faction: %factionsuuid_faction_name%) <display_name>: <message>",
"console", "[faction: %factionsuuid_faction_name%] <username>: <message>"
);
}

@Override
public ChannelPermissionResult speechPermitted(final CarbonPlayer player) {
return channelPermissionResult(
this.faction(player) != null,
() -> this.messages.cannotUseFactionChannel(player)
);
}

@Override
public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) {
return channelPermissionResult(
this.faction(player) != null,
() -> this.messages.cannotUseFactionChannel(player)
);
}

@Override
public List<Audience> recipients(final CarbonPlayer sender) {
final @Nullable Faction faction = this.faction(sender);

if (faction == null) {
if (sender.online()) {
sender.sendMessage(this.messages.cannotUseFactionChannel(sender));
}

return Collections.emptyList();
}

final List<Audience> recipients = new ArrayList<>();
for (final Player player : faction.getOnlinePlayers()) {
final @Nullable CarbonPlayer carbon = this.users.user(player.getUniqueId()).getNow(null);
if (carbon != null) {
recipients.add(carbon);
}
}

recipients.add(this.server.console());

return recipients;
}

}