Skip to content

Commit

Permalink
Use color names for team names by default
Browse files Browse the repository at this point in the history
Fixes #251
  • Loading branch information
haykam821 authored and Patbox committed Nov 11, 2024
1 parent fc95a28 commit 4f417c9
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import xyz.nucleoid.plasmid.api.util.ItemStackBuilder;
import xyz.nucleoid.plasmid.api.util.PlasmidCodecs;

import java.util.Optional;
import java.util.function.Function;

/**
Expand All @@ -38,14 +39,14 @@ public final record GameTeamConfig(

public static final MapCodec<GameTeamConfig> MAP_CODEC = RecordCodecBuilder.mapCodec(instance -> {
return instance.group(
PlasmidCodecs.TEXT.fieldOf("name").forGetter(GameTeamConfig::name),
PlasmidCodecs.TEXT.optionalFieldOf("name").forGetter(config -> Optional.of(config.name)),
Colors.CODEC.optionalFieldOf("color", Colors.NONE).forGetter(GameTeamConfig::colors),
Codec.BOOL.optionalFieldOf("friendly_fire", true).forGetter(GameTeamConfig::friendlyFire),
COLLISION_CODEC.optionalFieldOf("collision", AbstractTeam.CollisionRule.ALWAYS).forGetter(GameTeamConfig::collision),
VISIBILITY_CODEC.optionalFieldOf("name_tag_visibility", AbstractTeam.VisibilityRule.ALWAYS).forGetter(GameTeamConfig::nameTagVisibility),
PlasmidCodecs.TEXT.optionalFieldOf("prefix", ScreenTexts.EMPTY).forGetter(GameTeamConfig::prefix),
PlasmidCodecs.TEXT.optionalFieldOf("suffix", ScreenTexts.EMPTY).forGetter(GameTeamConfig::suffix)
).apply(instance, GameTeamConfig::new);
).apply(instance, GameTeamConfig::of);
});

public static final Codec<GameTeamConfig> CODEC = MAP_CODEC.codec();
Expand Down Expand Up @@ -107,8 +108,22 @@ public void applyToScoreboard(Team scoreboardTeam) {
scoreboardTeam.setSuffix(this.suffix());
}

public static GameTeamConfig of(Optional<Text> name, Colors colors, boolean friendlyFire, AbstractTeam.CollisionRule collision, AbstractTeam.VisibilityRule nameTagVisibility, Text prefix, Text suffix) {
return new GameTeamConfig(getNameWithColorFallback(name, colors), colors, friendlyFire, collision, nameTagVisibility, prefix, suffix);
}

private static Text getNameWithColorFallback(Optional<Text> name, Colors colors) {
return name.orElseGet(() -> {
if (colors == Colors.NONE) {
return Text.literal("Team");
} else {
return Text.translatable("color.minecraft." + colors.blockDyeColor().getName());
}
});
}

public static final class Builder {
private Text name = Text.literal("Team");
private Optional<Text> name = Optional.empty();
private Colors colors = Colors.NONE;
private boolean friendlyFire = true;
private AbstractTeam.CollisionRule collision = AbstractTeam.CollisionRule.ALWAYS;
Expand All @@ -120,7 +135,7 @@ public static final class Builder {
}

Builder(GameTeamConfig config) {
this.name = config.name;
this.name = Optional.of(config.name);
this.colors = config.colors;
this.friendlyFire = config.friendlyFire;
this.collision = config.collision;
Expand All @@ -130,7 +145,7 @@ public static final class Builder {
}

public Builder setName(Text name) {
this.name = name;
this.name = Optional.of(name);
return this;
}

Expand Down Expand Up @@ -165,7 +180,7 @@ public Builder setSuffix(Text suffix) {
}

public GameTeamConfig build() {
return new GameTeamConfig(
return GameTeamConfig.of(
this.name, this.colors,
this.friendlyFire, this.collision, this.nameTagVisibility,
this.prefix, this.suffix
Expand Down
116 changes: 116 additions & 0 deletions src/test/java/xyz/nucleoid/plasmid/test/GameTeamTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package xyz.nucleoid.plasmid.test;

import com.google.gson.JsonParser;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import net.minecraft.Bootstrap;
import net.minecraft.SharedConstants;
import net.minecraft.scoreboard.AbstractTeam;
import net.minecraft.text.Text;
import net.minecraft.util.DyeColor;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import xyz.nucleoid.plasmid.api.game.common.team.GameTeamConfig;
import xyz.nucleoid.plasmid.api.game.common.team.GameTeamConfig.Colors;

import static org.junit.jupiter.api.Assertions.*;

public class GameTeamTests {
@BeforeAll
public static void beforeAll() {
SharedConstants.createGameVersion();
Bootstrap.initialize();
}

@Test
public void testConfigParsing() {
var json = """
{
"name": {
"text": "Team Name"
},
"color": "blue",
"friendly_fire": true,
"collision": "never",
"name_tag_visibility": "never"
}
""";

var expected = new GameTeamConfig(
Text.literal("Team Name"),
Colors.from(DyeColor.BLUE),
true,
AbstractTeam.CollisionRule.NEVER,
AbstractTeam.VisibilityRule.NEVER,
Text.empty(),
Text.empty()
);

assertParsedEquals(json, expected, GameTeamConfig.CODEC);
}

@Test
public void testConfigParsingWithoutName() {
var json = """
{
"color": "red",
"friendly_fire": false,
"collision": "pushOtherTeams",
"name_tag_visibility": "hideForOtherTeams",
"prefix": {
"text": "Prefix"
},
"suffix": {
"text": "Suffix"
}
}
""";

var expected = new GameTeamConfig(
Text.translatable("color.minecraft.red"),
Colors.from(DyeColor.RED),
false,
AbstractTeam.CollisionRule.PUSH_OTHER_TEAMS,
AbstractTeam.VisibilityRule.HIDE_FOR_OTHER_TEAMS,
Text.literal("Prefix"),
Text.literal("Suffix")
);

assertParsedEquals(json, expected, GameTeamConfig.CODEC);
}

@Test
public void testConfigParsingWithoutColor() {
var json = """
{
"friendly_fire": false,
"collision": "always",
"name_tag_visibility": "hideForOwnTeam"
}
""";

var expected = new GameTeamConfig(
Text.literal("Team"),
Colors.NONE,
false,
AbstractTeam.CollisionRule.ALWAYS,
AbstractTeam.VisibilityRule.HIDE_FOR_OWN_TEAM,
Text.empty(),
Text.empty()
);

assertParsedEquals(json, expected, GameTeamConfig.CODEC);
}

private static <T> void assertParsedEquals(String json, T expected, Codec<T> codec) {
var actual = parse(json, codec);
assertEquals(expected, actual);
}

private static <T> T parse(String json, Codec<T> codec) {
var element = JsonParser.parseString(json);
var result = codec.decode(JsonOps.INSTANCE, element);

return result.getOrThrow().getFirst();
}
}

0 comments on commit 4f417c9

Please sign in to comment.