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 <gamemode> module #873

Merged
merged 15 commits into from
Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions core/src/main/java/tc/oc/pgm/api/map/Gamemode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package tc.oc.pgm.api.map;

public enum Gamemode {
ATTACK_DEFEND("ad"),
ARCADE("arcade"),
BLITZ("blitz"),
BLITZ_RAGE("br"),
CAPTURE_THE_FLAG("ctf"),
CONTROL_THE_POINT("cp"),
CAPTURE_THE_WOOL("ctw"),
DESTROY_THE_CORE("dtc"),
DESTROY_THE_MONUMENT("dtm"),
FREE_FOR_ALL("ffa"),
FLAG_FOOTBALL("ffb"),
KING_OF_THE_HILL("koth"),
KING_OF_THE_FLAG("kotf"),
MIXED("mixed"),
RAGE("rage"),
RACE_FOR_WOOL("rfw"),
SCOREBOX("scorebox"),
DEATHMATCH("tdm");

private final String id;

Gamemode(String id) {
this.id = id;
}

public static Gamemode byId(String gamemodeId) {
for (Gamemode gamemode : Gamemode.values()) {
if (gamemode.getId().equals(gamemodeId)) {
return gamemode;
}
}
return null;
}

public String getId() {
return id;
}
}
11 changes: 9 additions & 2 deletions core/src/main/java/tc/oc/pgm/api/map/MapInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,19 @@ default String getStyledNameLegacy(MapNameStyle style, @Nullable CommandSender s
Collection<MapTag> getTags();

/**
* Get a {@link Component} that represents this map's gamemode name.
* Get a {@link Component} that represents this map's custom game title.
*
* @return A component of the gamemode name if defined or null.
* @return Returns the defined gamemode title, empty if not defined.
*/
Component getGamemode();

/**
* Get a {@link Collection<Gamemode>} that represents this map's gamemodes.
*
* @return A component of gamemodes if defined or null.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return A component of gamemodes if defined or null.
* @return The list of defined gamemodes, empty list if none are defined.

Don't lie in the javadocs, makes the APIs hard to use

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look resolved

*/
Collection<Gamemode> getGamemodes();

/**
* Get the maximum number of players that can participate on each team.
*
Expand Down
21 changes: 21 additions & 0 deletions core/src/main/java/tc/oc/pgm/map/MapInfoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.bukkit.Difficulty;
import org.jdom2.Element;
import tc.oc.pgm.api.map.Contributor;
import tc.oc.pgm.api.map.Gamemode;
import tc.oc.pgm.api.map.MapInfo;
import tc.oc.pgm.api.map.MapTag;
import tc.oc.pgm.api.map.Phase;
Expand Down Expand Up @@ -47,6 +48,7 @@ public class MapInfoImpl implements MapInfo {

protected final Collection<MapTag> tags;
protected final Collection<Integer> players;
protected final Collection<Gamemode> gamemodes;

public MapInfoImpl(
@Nullable String id,
Expand All @@ -63,6 +65,7 @@ public MapInfoImpl(
@Nullable Collection<Integer> players,
@Nullable WorldInfo world,
@Nullable Component gamemode,
@Nullable Collection<Gamemode> gamemodes,
Phase phase) {
this.name = checkNotNull(name);
this.id = checkNotNull(MapInfo.normalizeName(id == null ? name : id));
Expand All @@ -78,6 +81,7 @@ public MapInfoImpl(
this.players = players == null ? new LinkedList<>() : players;
this.world = world == null ? new WorldInfoImpl() : world;
this.gamemode = gamemode;
this.gamemodes = gamemodes;
this.phase = phase;
}

Expand All @@ -97,6 +101,7 @@ public MapInfoImpl(MapInfo info) {
info.getMaxPlayers(),
info.getWorld(),
info.getGamemode(),
info.getGamemodes(),
info.getPhase());
}

Expand All @@ -121,6 +126,7 @@ public MapInfoImpl(Element root) throws InvalidXMLException {
null,
parseWorld(root),
XMLUtils.parseFormattedText(root, "game"),
parseGamemodes(root),
XMLUtils.parseEnum(
Node.fromLastChildOrAttr(root, "phase"), Phase.class, "phase", Phase.PRODUCTION));
}
Expand Down Expand Up @@ -190,6 +196,11 @@ public Component getGamemode() {
return gamemode;
}

@Override
public Collection<Gamemode> getGamemodes() {
return gamemodes;
}

@Override
public WorldInfo getWorld() {
return world;
Expand Down Expand Up @@ -255,6 +266,16 @@ private static List<String> parseRules(Element root) {
return rules;
}

private static List<Gamemode> parseGamemodes(Element root) throws InvalidXMLException {
List<Gamemode> gamemodes = new ArrayList<>();
for (Element gamemodeEl : root.getChildren("gamemode")) {
Gamemode gm = Gamemode.byId(gamemodeEl.getText());
if (gm == null) throw new InvalidXMLException("Unknown gamemode", gamemodeEl);
gamemodes.add(gm);
}
return gamemodes;
}

private static List<Contributor> parseContributors(Element root, String tag)
throws InvalidXMLException {
List<Contributor> contributors = null;
Expand Down
19 changes: 16 additions & 3 deletions core/src/main/java/tc/oc/pgm/scoreboard/SidebarMatchModule.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tc.oc.pgm.scoreboard;

import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.Component.translatable;

import com.google.common.collect.ImmutableList;
Expand All @@ -18,6 +19,7 @@
import java.util.UUID;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
Expand All @@ -30,6 +32,7 @@
import org.bukkit.event.Listener;
import tc.oc.pgm.api.Config;
import tc.oc.pgm.api.PGM;
import tc.oc.pgm.api.map.Gamemode;
import tc.oc.pgm.api.map.MapInfo;
import tc.oc.pgm.api.map.MapTag;
import tc.oc.pgm.api.match.Match;
Expand Down Expand Up @@ -258,9 +261,19 @@ private Component renderTitle(final Config config, final MapInfo map) {
return header.colorIfAbsent(NamedTextColor.AQUA);
}

final Component game = map.getGamemode();
if (game != null) {
return game.colorIfAbsent(NamedTextColor.AQUA);
final Component gamemode = map.getGamemode();
if (gamemode != null) {
return gamemode.colorIfAbsent(NamedTextColor.AQUA);
}

final Collection<Gamemode> gamemodes = map.getGamemodes();
if (!gamemodes.isEmpty()) {
String suffix = gamemodes.size() <= 1 ? ".name" : ".acronym";
List<Component> gmComponents =
gamemodes.stream()
.map(gm -> translatable("gamemode." + gm.getId() + suffix))
.collect(Collectors.toList());
return TextFormatter.list(gmComponents, NamedTextColor.AQUA);
}

final List<Component> games = new LinkedList<>();
Expand Down
18 changes: 17 additions & 1 deletion util/src/main/i18n/templates/gamemode.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ gamemode.ctw.name = Capture the Wool

gamemode.ctf.name = Capture the Flag

gamemode.ffb.name = Flag Football

gamemode.kotf.name = King of the Flag

gamemode.tdm.name = Deathmatch

gamemode.ad.name = Attack/Defend
Expand All @@ -20,6 +24,10 @@ gamemode.blitz.name = Blitz

gamemode.rage.name = Rage

gamemode.br.name = Blitz: Rage

gamemode.rfw.name = Race for Wool

gamemode.scorebox.name = Scorebox

gamemode.arcade.name = Arcade
Expand All @@ -36,18 +44,26 @@ gamemode.ctw.acronym = CTW

gamemode.ctf.acronym = CTF

gamemode.ffb.acronym = FFB

gamemode.kotf.acronym = KotF

gamemode.tdm.acronym = TDM

gamemode.ad.acronym = A/D

gamemode.cp.acronym = CP

gamemode.koth.acronym = KoTH
gamemode.koth.acronym = KotH

gamemode.blitz.acronym = Blitz

gamemode.rage.acronym = Rage

gamemode.br.acronym = Blitz: Rage

gamemode.rfw.acronym = RFW

gamemode.scorebox.acronym = Scorebox

gamemode.arcade.acronym = Arcade
Expand Down