| 1 | package net.minecraft.world.scores;
|
| 2 |
|
| 3 | import com.google.common.collect.Sets;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.lang.annotation.ElementType;
|
| 7 | import java.lang.annotation.Retention;
|
| 8 | import java.lang.annotation.RetentionPolicy;
|
| 9 | import java.lang.annotation.Target;
|
| 10 | import java.util.Collection;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Optional;
|
| 13 | import java.util.Set;
|
| 14 | import net.minecraft.network.chat.CommonComponents;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.network.chat.ComponentSerialization;
|
| 17 | import net.minecraft.network.chat.ComponentUtils;
|
| 18 | import net.minecraft.network.chat.HoverEvent;
|
| 19 | import net.minecraft.network.chat.MutableComponent;
|
| 20 | import net.minecraft.network.chat.Style;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | public class PlayerTeam extends Team {
|
| 24 | private static final byte BIT_FRIENDLY_FIRE = 1;
|
| 25 | private static final byte BIT_SEE_INVISIBLES = 2;
|
| 26 | private final Scoreboard scoreboard;
|
| 27 | private final String name;
|
| 28 | private final Set<String> players = Sets.newHashSet();
|
| 29 | private Component displayName;
|
| 30 | private Component playerPrefix = CommonComponents.EMPTY;
|
| 31 | private Component playerSuffix = CommonComponents.EMPTY;
|
| 32 | private boolean allowFriendlyFire = true;
|
| 33 | private boolean seeFriendlyInvisibles = true;
|
| 34 | private Team.Visibility nameTagVisibility = Team.Visibility.ALWAYS;
|
| 35 | private Team.Visibility deathMessageVisibility = Team.Visibility.ALWAYS;
|
| 36 | private Optional<TeamColor> color = Optional.empty();
|
| 37 | private Team.CollisionRule collisionRule = Team.CollisionRule.ALWAYS;
|
| 38 | private final Style displayNameStyle;
|
| 39 |
|
| 40 | public PlayerTeam(final Scoreboard scoreboard, final String name) {
|
| 41 | this.scoreboard = scoreboard;
|
| 42 | this.name = name;
|
| 43 | this.displayName = Component.literal(name);
|
| 44 | this.displayNameStyle = Style.EMPTY.withInsertion(name).withHoverEvent(new HoverEvent.ShowText(Component.literal(name)));
|
| 45 | }
|
| 46 |
|
| 47 | public PlayerTeam.Packed pack() {
|
| 48 | return new PlayerTeam.Packed(
|
| 49 | this.name,
|
| 50 | Optional.of(this.displayName),
|
| 51 | this.color,
|
| 52 | this.allowFriendlyFire,
|
| 53 | this.seeFriendlyInvisibles,
|
| 54 | this.playerPrefix,
|
| 55 | this.playerSuffix,
|
| 56 | this.nameTagVisibility,
|
| 57 | this.deathMessageVisibility,
|
| 58 | this.collisionRule,
|
| 59 | List.copyOf(this.players)
|
| 60 | );
|
| 61 | }
|
| 62 |
|
| 63 | public Scoreboard getScoreboard() {
|
| 64 | return this.scoreboard;
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public String getName() {
|
| 69 | return this.name;
|
| 70 | }
|
| 71 |
|
| 72 | public Component getDisplayName() {
|
| 73 | return this.displayName;
|
| 74 | }
|
| 75 |
|
| 76 | private MutableComponent applyColor(final MutableComponent result) {
|
| 77 | this.color.ifPresent(teamColor -> result.withColor(teamColor.textColor()));
|
| 78 | return result;
|
| 79 | }
|
| 80 |
|
| 81 | public MutableComponent getFormattedDisplayName() {
|
| 82 | return this.applyColor(ComponentUtils.wrapInSquareBrackets(this.displayName.copy().withStyle(this.displayNameStyle)));
|
| 83 | }
|
| 84 |
|
| 85 | public void setDisplayName(final Component displayName) {
|
| 86 | if (displayName == null) {
|
| 87 | throw new IllegalArgumentException("Name cannot be null");
|
| 88 | }
|
| 89 |
|
| 90 | this.displayName = displayName;
|
| 91 | this.scoreboard.onTeamChanged(this);
|
| 92 | }
|
| 93 |
|
| 94 | public void setPlayerPrefix(final @Nullable Component playerPrefix) {
|
| 95 | this.playerPrefix = playerPrefix == null ? CommonComponents.EMPTY : playerPrefix;
|
| 96 | this.scoreboard.onTeamChanged(this);
|
| 97 | }
|
| 98 |
|
| 99 | public Component getPlayerPrefix() {
|
| 100 | return this.playerPrefix;
|
| 101 | }
|
| 102 |
|
| 103 | public void setPlayerSuffix(final @Nullable Component playerSuffix) {
|
| 104 | this.playerSuffix = playerSuffix == null ? CommonComponents.EMPTY : playerSuffix;
|
| 105 | this.scoreboard.onTeamChanged(this);
|
| 106 | }
|
| 107 |
|
| 108 | public Component getPlayerSuffix() {
|
| 109 | return this.playerSuffix;
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public Collection<String> getPlayers() {
|
| 114 | return this.players;
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public MutableComponent getFormattedName(final Component teamMemberName) {
|
| 119 | return this.applyColor(Component.empty().append(this.playerPrefix).append(teamMemberName).append(this.playerSuffix));
|
| 120 | }
|
| 121 |
|
| 122 | public static MutableComponent formatNameForTeam(final @Nullable Team team, final Component name) {
|
| 123 | return team == null ? name.copy() : team.getFormattedName(name);
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public boolean isAllowFriendlyFire() {
|
| 128 | return this.allowFriendlyFire;
|
| 129 | }
|
| 130 |
|
| 131 | public void setAllowFriendlyFire(final boolean allowFriendlyFire) {
|
| 132 | this.allowFriendlyFire = allowFriendlyFire;
|
| 133 | this.scoreboard.onTeamChanged(this);
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | public boolean canSeeFriendlyInvisibles() {
|
| 138 | return this.seeFriendlyInvisibles;
|
| 139 | }
|
| 140 |
|
| 141 | public void setSeeFriendlyInvisibles(final boolean seeFriendlyInvisibles) {
|
| 142 | this.seeFriendlyInvisibles = seeFriendlyInvisibles;
|
| 143 | this.scoreboard.onTeamChanged(this);
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | public Team.Visibility getNameTagVisibility() {
|
| 148 | return this.nameTagVisibility;
|
| 149 | }
|
| 150 |
|
| 151 | @Override
|
| 152 | public Team.Visibility getDeathMessageVisibility() {
|
| 153 | return this.deathMessageVisibility;
|
| 154 | }
|
| 155 |
|
| 156 | public void setNameTagVisibility(final Team.Visibility visibility) {
|
| 157 | this.nameTagVisibility = visibility;
|
| 158 | this.scoreboard.onTeamChanged(this);
|
| 159 | }
|
| 160 |
|
| 161 | public void setDeathMessageVisibility(final Team.Visibility visibility) {
|
| 162 | this.deathMessageVisibility = visibility;
|
| 163 | this.scoreboard.onTeamChanged(this);
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public Team.CollisionRule getCollisionRule() {
|
| 168 | return this.collisionRule;
|
| 169 | }
|
| 170 |
|
| 171 | public void setCollisionRule(final Team.CollisionRule collisionRule) {
|
| 172 | this.collisionRule = collisionRule;
|
| 173 | this.scoreboard.onTeamChanged(this);
|
| 174 | }
|
| 175 |
|
| 176 | public @PlayerTeam.OptionFlags byte packOptions() {
|
| 177 | byte result = 0;
|
| 178 | if (this.isAllowFriendlyFire()) {
|
| 179 | result = (byte)(result | 1);
|
| 180 | }
|
| 181 |
|
| 182 | if (this.canSeeFriendlyInvisibles()) {
|
| 183 | result = (byte)(result | 2);
|
| 184 | }
|
| 185 |
|
| 186 | return result;
|
| 187 | }
|
| 188 |
|
| 189 | public void unpackOptions(final @PlayerTeam.OptionFlags byte options) {
|
| 190 | this.setAllowFriendlyFire((options & 1) != 0);
|
| 191 | this.setSeeFriendlyInvisibles((options & 2) != 0);
|
| 192 | }
|
| 193 |
|
| 194 | public void setColor(final Optional<TeamColor> color) {
|
| 195 | this.color = color;
|
| 196 | this.scoreboard.onTeamChanged(this);
|
| 197 | }
|
| 198 |
|
| 199 | @Override
|
| 200 | public Optional<TeamColor> getColor() {
|
| 201 | return this.color;
|
| 202 | }
|
| 203 |
|
| 204 | @Retention(RetentionPolicy.CLASS)
|
| 205 | @Target(ElementType.TYPE_USE)
|
| 206 | public @interface OptionFlags {
|
| 207 | }
|
| 208 |
|
| 209 | public record Packed(
|
| 210 | String name,
|
| 211 | Optional<Component> displayName,
|
| 212 | Optional<TeamColor> color,
|
| 213 | boolean allowFriendlyFire,
|
| 214 | boolean seeFriendlyInvisibles,
|
| 215 | Component memberNamePrefix,
|
| 216 | Component memberNameSuffix,
|
| 217 | Team.Visibility nameTagVisibility,
|
| 218 | Team.Visibility deathMessageVisibility,
|
| 219 | Team.CollisionRule collisionRule,
|
| 220 | List<String> players
|
| 221 | ) {
|
| 222 | public static final Codec<PlayerTeam.Packed> CODEC = RecordCodecBuilder.create(
|
| 223 | i -> i.group(
|
| 224 | Codec.STRING.fieldOf("Name").forGetter(PlayerTeam.Packed::name),
|
| 225 | ComponentSerialization.CODEC.optionalFieldOf("DisplayName").forGetter(PlayerTeam.Packed::displayName),
|
| 226 | TeamColor.CODEC.optionalFieldOf("TeamColor").forGetter(PlayerTeam.Packed::color),
|
| 227 | Codec.BOOL.optionalFieldOf("AllowFriendlyFire", true).forGetter(PlayerTeam.Packed::allowFriendlyFire),
|
| 228 | Codec.BOOL.optionalFieldOf("SeeFriendlyInvisibles", true).forGetter(PlayerTeam.Packed::seeFriendlyInvisibles),
|
| 229 | ComponentSerialization.CODEC.optionalFieldOf("MemberNamePrefix", CommonComponents.EMPTY).forGetter(PlayerTeam.Packed::memberNamePrefix),
|
| 230 | ComponentSerialization.CODEC.optionalFieldOf("MemberNameSuffix", CommonComponents.EMPTY).forGetter(PlayerTeam.Packed::memberNameSuffix),
|
| 231 | Team.Visibility.CODEC.optionalFieldOf("NameTagVisibility", Team.Visibility.ALWAYS).forGetter(PlayerTeam.Packed::nameTagVisibility),
|
| 232 | Team.Visibility.CODEC.optionalFieldOf("DeathMessageVisibility", Team.Visibility.ALWAYS).forGetter(PlayerTeam.Packed::deathMessageVisibility),
|
| 233 | Team.CollisionRule.CODEC.optionalFieldOf("CollisionRule", Team.CollisionRule.ALWAYS).forGetter(PlayerTeam.Packed::collisionRule),
|
| 234 | Codec.STRING.listOf().optionalFieldOf("Players", List.of()).forGetter(PlayerTeam.Packed::players)
|
| 235 | )
|
| 236 | .apply(i, PlayerTeam.Packed::new)
|
| 237 | );
|
| 238 | }
|
| 239 | }
|
| 240 |
|