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 5 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
44 changes: 44 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,44 @@
package tc.oc.pgm.api.map;

public enum Gamemode {
ATTACK_DEFEND("ad", "Attack/Defend"),
CoWinkKeyDinkInc marked this conversation as resolved.
Show resolved Hide resolved
ARCADE("arcade", "Arcade"),
BLITZ("blitz", "Blitz"),
BLITZ_RAGE("br", "Blitz: Rage"),
CAPTURE_THE_FLAG("ctf", "Capture the Flag"),
CONTROL_THE_POINT("cp", "Control the Point"),
CAPTURE_THE_WOOL("ctw", "Capture the Wool"),
DESTROY_THE_CORE("dtc", "Destroy the Core"),
DESTROY_THE_MONUMENT("dtm", "Destroy the Monument"),
FREE_FOR_ALL("ffa", "Free For All"),
FLAG_FOOTBALL("ffb", "Flag Football"),
KING_OF_THE_HILL("koth", "King of the Hill"),
KING_OF_THE_FLAG("kotf", "King of the Flag"),
MIXED("mixed", "Mixed"),
RAGE("rage", "Rage"),
RACE_FOR_WOOL("rfw", "Race for Wool"),
SCOREBOX("scorebox", "Scorebox"),
DEATHMATCH("tdm", "Deathmatch");

private final String id;
private final String name;
private final MapTag mapTag;

Gamemode(String id, String name) {
this.id = id;
this.name = name;
this.mapTag = new MapTag(id, name, true, false);
CoWinkKeyDinkInc marked this conversation as resolved.
Show resolved Hide resolved
}

public String getId() {
return id;
}

public String getName() {
return name;
}

public MapTag getMapTag() {
return mapTag;
}
}
9 changes: 8 additions & 1 deletion 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.
*/
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
29 changes: 26 additions & 3 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.WorldInfo;
Expand Down Expand Up @@ -45,6 +46,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 @@ -60,7 +62,8 @@ public MapInfoImpl(
@Nullable Collection<MapTag> tags,
@Nullable Collection<Integer> players,
@Nullable WorldInfo world,
@Nullable Component gamemode) {
@Nullable Component gamemode,
@Nullable Collection<Gamemode> gamemodes) {
this.name = checkNotNull(name);
this.id = checkNotNull(MapInfo.normalizeName(id == null ? name : id));
this.proto = checkNotNull(proto);
Expand All @@ -75,6 +78,7 @@ public MapInfoImpl(
this.players = players == null ? new LinkedList<>() : players;
this.world = world == null ? new WorldInfoImpl() : world;
this.gamemode = gamemode;
this.gamemodes = gamemodes;
}

public MapInfoImpl(MapInfo info) {
Expand All @@ -92,7 +96,8 @@ public MapInfoImpl(MapInfo info) {
info.getTags(),
info.getMaxPlayers(),
info.getWorld(),
info.getGamemode());
info.getGamemode(),
info.getGamemodes());
}

public MapInfoImpl(Element root) throws InvalidXMLException {
Expand All @@ -115,7 +120,8 @@ public MapInfoImpl(Element root) throws InvalidXMLException {
null,
null,
parseWorld(root),
XMLUtils.parseFormattedText(root, "game"));
XMLUtils.parseFormattedText(root, "game"),
parseGamemodes(root));
}

@Override
Expand Down Expand Up @@ -183,6 +189,11 @@ public Component getGamemode() {
return gamemode;
}

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

@Override
public WorldInfo getWorld() {
return world;
Expand Down Expand Up @@ -243,6 +254,18 @@ 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")) {
for (Gamemode gamemode : Gamemode.values()) {
if (gamemode.getId().equals(gamemodeEl.getText())) {
CoWinkKeyDinkInc marked this conversation as resolved.
Show resolved Hide resolved
gamemodes.add(gamemode);
}
}
}
return gamemodes;
}

private static List<Contributor> parseContributors(Element root, String tag)
throws InvalidXMLException {
List<Contributor> contributors = null;
Expand Down
28 changes: 25 additions & 3 deletions core/src/main/java/tc/oc/pgm/scoreboard/SidebarMatchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand All @@ -30,6 +31,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 +260,29 @@ 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()) {
Iterator<Gamemode> gamemodeIterator = gamemodes.iterator();
String firstgamemodeId = gamemodeIterator.next().getId();
if (gamemodeIterator.hasNext()) {
String secondgamemodeId = gamemodeIterator.next().getId();
Component firstComponent =
Component.translatable("gamemode." + firstgamemodeId + ".acronym");
Component secondComponent =
Component.translatable("gamemode." + secondgamemodeId + ".acronym");
return firstComponent
.append(Component.text(" & "))
.append(secondComponent)
.colorIfAbsent(NamedTextColor.AQUA);
} else {
return Component.translatable("gamemode." + firstgamemodeId + ".name")
.colorIfAbsent(NamedTextColor.AQUA);
}
}
CoWinkKeyDinkInc marked this conversation as resolved.
Show resolved Hide resolved

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