Skip to content

Commit a2cdb4f

Browse files
committed
chore: added TeamStagesHelper#hasTeamStage variant for players
1 parent 91371ad commit a2cdb4f

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

common/src/main/java/dev/ftb/mods/ftbteams/api/TeamStagesHelper.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package dev.ftb.mods.ftbteams.api;
22

3+
import dev.ftb.mods.ftbteams.api.client.ClientTeamManager;
34
import dev.ftb.mods.ftbteams.api.event.TeamEvent;
45
import dev.ftb.mods.ftbteams.api.event.TeamPropertiesChangedEvent;
56
import dev.ftb.mods.ftbteams.api.property.TeamProperties;
67
import dev.ftb.mods.ftbteams.api.property.TeamPropertyCollection;
8+
import net.minecraft.server.level.ServerPlayer;
9+
import net.minecraft.world.entity.player.Player;
710

8-
import java.util.Collection;
9-
import java.util.Collections;
10-
import java.util.List;
11-
import java.util.Set;
11+
import java.util.*;
1212

1313
/**
1414
* Utility class providing some convenience methods for querying and modifying the team stages for a team. These methods
@@ -77,6 +77,25 @@ public static boolean hasTeamStage(Team team, String stage) {
7777
return team.getProperty(TeamProperties.TEAM_STAGES).contains(stage);
7878
}
7979

80+
/**
81+
* Check if a player has a particular team stage.
82+
*
83+
* @param player the player to check
84+
* @param stage the stage to check
85+
* @return true if the player's current team has the stage, false otherwise
86+
*/
87+
public static boolean hasTeamStage(Player player, String stage) {
88+
if (player instanceof ServerPlayer sp) {
89+
return FTBTeamsAPI.api().getManager().getTeamForPlayer(sp)
90+
.map(team -> team.getProperty(TeamProperties.TEAM_STAGES).contains(stage))
91+
.orElse(false);
92+
} else {
93+
return FTBTeamsAPI.api().getClientManager().getTeamForPlayer(player)
94+
.map(team -> team.getProperty(TeamProperties.TEAM_STAGES).contains(stage))
95+
.orElse(false);
96+
}
97+
}
98+
8099
/**
81100
* Get all the stages for a team.
82101
*

common/src/main/java/dev/ftb/mods/ftbteams/api/client/ClientTeamManager.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import dev.ftb.mods.ftbteams.api.FTBTeamsAPI;
44
import dev.ftb.mods.ftbteams.api.Team;
55
import net.minecraft.network.chat.Component;
6+
import net.minecraft.server.level.ServerPlayer;
7+
import net.minecraft.world.entity.player.Player;
68
import org.jetbrains.annotations.Nullable;
79

810
import java.util.Collection;
@@ -50,6 +52,15 @@ public interface ClientTeamManager {
5052
*/
5153
Optional<Team> getTeamByID(UUID teamId);
5254

55+
/**
56+
* Get the current team for the given player. This will only succeed if the details for the player in question
57+
* are in the known players list as returned by {@link #knownClientPlayers()}.
58+
*
59+
* @param player the player to check
60+
* @return the team, or {@code Optional.empty()} if no team could be found
61+
*/
62+
Optional<Team> getTeamForPlayer(Player player);
63+
5364
/**
5465
* Get the client team data for this client player (i.e. {@link net.minecraft.client.Minecraft#player})
5566
*

common/src/main/java/dev/ftb/mods/ftbteams/data/ClientTeamManagerImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.ftb.mods.ftbteams.data;
22

33
import dev.ftb.mods.ftbteams.FTBTeams;
4+
import dev.ftb.mods.ftbteams.api.FTBTeamsAPI;
45
import dev.ftb.mods.ftbteams.api.Team;
56
import dev.ftb.mods.ftbteams.api.client.ClientTeamManager;
67
import dev.ftb.mods.ftbteams.api.client.KnownClientPlayer;
@@ -14,6 +15,7 @@
1415
import net.minecraft.network.chat.Component;
1516
import net.minecraft.network.codec.ByteBufCodecs;
1617
import net.minecraft.network.codec.StreamCodec;
18+
import net.minecraft.world.entity.player.Player;
1719
import org.jetbrains.annotations.Nullable;
1820

1921
import java.util.*;
@@ -126,6 +128,11 @@ public Optional<Team> getTeamByID(UUID teamId) {
126128
return Optional.ofNullable(teamMap.get(teamId));
127129
}
128130

131+
@Override
132+
public Optional<Team> getTeamForPlayer(Player player) {
133+
return getKnownPlayer(player.getUUID()).flatMap(kcp -> getTeamByID(kcp.teamId()));
134+
}
135+
129136
@Override
130137
public ClientTeam selfTeam() {
131138
return selfTeam;

0 commit comments

Comments
 (0)