Skip to content

Commit

Permalink
Check if faction has alliances/truces
Browse files Browse the repository at this point in the history
  • Loading branch information
Draycia authored and jpenilla committed Nov 14, 2023
1 parent c5bfdbd commit ae1ac89
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand All @@ -41,4 +42,10 @@ abstract class AbstractFactionsChannel extends ConfigChatChannel {
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
Expand Up @@ -67,22 +67,22 @@ public AllianceChannel() {
@Override
public ChannelPermissionResult speechPermitted(final CarbonPlayer player) {
return channelPermissionResult(
this.hasAllies(player),
this.hasRelations(player, Relation.ALLY),
() -> this.messages.cannotUseFactionAllianceChannel(player)
);
}

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

@Override
public List<Audience> recipients(final CarbonPlayer sender) {
if (!this.hasAllies(sender)) {
if (!this.hasRelations(sender, Relation.ALLY)) {
if (sender.online()) {
sender.sendMessage(this.messages.cannotUseFactionAllianceChannel(sender));
}
Expand Down Expand Up @@ -124,10 +124,4 @@ private List<Player> alliedPlayersTo(final CarbonPlayer player) {
return alliedPlayers;
}

private boolean hasAllies(final CarbonPlayer player) {
final @Nullable Faction faction = this.faction(player);

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,22 @@ public TruceChannel() {
@Override
public ChannelPermissionResult speechPermitted(final CarbonPlayer player) {
return channelPermissionResult(
this.faction(player) != null,
this.hasRelations(player, Relation.TRUCE),
() -> this.messages.cannotUseTruceChannel(player)
);
}

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

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

if (faction == null) {
if (!this.hasRelations(sender, Relation.TRUCE)) {
if (sender.online()) {
sender.sendMessage(this.messages.cannotUseTruceChannel(sender));
}
Expand Down

0 comments on commit ae1ac89

Please sign in to comment.