diff --git a/src/main/java/tc/oc/pgm/PGMImpl.java b/src/main/java/tc/oc/pgm/PGMImpl.java index bf1ab997fe..18508538a1 100644 --- a/src/main/java/tc/oc/pgm/PGMImpl.java +++ b/src/main/java/tc/oc/pgm/PGMImpl.java @@ -114,8 +114,8 @@ import tc.oc.pgm.regions.RegionModule; import tc.oc.pgm.renewable.RenewableModule; import tc.oc.pgm.restart.RestartManager; -import tc.oc.pgm.rotation.FixedPGMMapOrderManager; import tc.oc.pgm.rotation.RandomPGMMapOrder; +import tc.oc.pgm.rotation.RotationManager; import tc.oc.pgm.score.ScoreModule; import tc.oc.pgm.scoreboard.ScoreboardModule; import tc.oc.pgm.scoreboard.SidebarModule; @@ -237,8 +237,7 @@ public void onEnable() { if (Config.Rotations.areEnabled()) { matchManager.setMapOrder( - new FixedPGMMapOrderManager( - matchManager, logger, new File(getDataFolder(), Config.Rotations.getPath()))); + new RotationManager(logger, new File(getDataFolder(), Config.Rotations.getPath()))); } else { matchManager.setMapOrder(new RandomPGMMapOrder(matchManager)); } diff --git a/src/main/java/tc/oc/pgm/commands/RotationCommands.java b/src/main/java/tc/oc/pgm/commands/RotationCommands.java index f1b081877b..afc1c33106 100644 --- a/src/main/java/tc/oc/pgm/commands/RotationCommands.java +++ b/src/main/java/tc/oc/pgm/commands/RotationCommands.java @@ -3,6 +3,7 @@ import app.ashcon.intake.Command; import app.ashcon.intake.CommandException; import app.ashcon.intake.parametric.annotation.Default; +import app.ashcon.intake.parametric.annotation.Switch; import java.util.List; import net.md_5.bungee.api.ChatColor; import org.bukkit.command.CommandSender; @@ -11,151 +12,75 @@ import tc.oc.pgm.api.chat.Audience; import tc.oc.pgm.api.match.MatchManager; import tc.oc.pgm.map.PGMMap; -import tc.oc.pgm.rotation.FixedPGMMapOrder; -import tc.oc.pgm.rotation.FixedPGMMapOrderManager; +import tc.oc.pgm.rotation.Rotation; +import tc.oc.pgm.rotation.RotationManager; import tc.oc.pgm.util.PrettyPaginatedResult; public class RotationCommands { @Command( aliases = {"rotation", "rot"}, desc = "Shows the maps in the active rotation", - usage = "[page]", + usage = "[page] [-r rotation]", help = "Shows all the maps that are currently in the active rotation.") public static void rotation( - Audience audience, CommandSender sender, MatchManager matchManager, @Default("1") int page) - throws CommandException { - - if (matchManager.getMapOrder() instanceof FixedPGMMapOrderManager) { - FixedPGMMapOrderManager fixedPGMMapOrderManager = - (FixedPGMMapOrderManager) matchManager.getMapOrder(); - FixedPGMMapOrder rotation = fixedPGMMapOrderManager.getActiveRotation(); - - List maps; - if (rotation != null) maps = rotation.getMaps(); - else { - sender.sendMessage( - ChatColor.RED + AllTranslations.get().translate("command.rotation.noRotation", sender)); - return; - } - - int resultsPerPage = 8; - int pages = (maps.size() + resultsPerPage - 1) / resultsPerPage; - - String listHeader = - ChatColor.BLUE.toString() - + ChatColor.STRIKETHROUGH - + "-----------" - + ChatColor.RESET - + " " - + AllTranslations.get().translate("command.rotation.currentRotation.title", sender) - + ChatColor.DARK_AQUA - + " (" - + ChatColor.AQUA - + rotation.getName() - + ChatColor.DARK_AQUA - + ")" - + " (" - + ChatColor.AQUA - + page - + ChatColor.DARK_AQUA - + " of " - + ChatColor.AQUA - + pages - + ChatColor.DARK_AQUA - + ") " - + ChatColor.translateAlternateColorCodes('&', "&9&m-----------"); - - new PrettyPaginatedResult(listHeader, resultsPerPage) { - @Override - public String format(PGMMap map, int index) { - index++; - String indexString = - rotation.getPosition() + 1 == index - ? ChatColor.DARK_AQUA.toString() + index - : String.valueOf(index); - return (indexString) + ". " + ChatColor.RESET + map.getInfo().getShortDescription(sender); - } - }.display(audience, maps, page); - } else { - sender.sendMessage( - ChatColor.RED - + AllTranslations.get().translate("command.rotation.rotationsDisabled", sender)); - } - } - - @Command( - aliases = { - "rotationcheckout", - "rotcheck", - }, - desc = "Shows the maps in the specified rotation", - usage = "[rotation] [page]", - help = "Shows all the maps that are currently in a specific rotation.") - public static void rotationCheckout( Audience audience, CommandSender sender, MatchManager matchManager, - String rotationName, - @Default("1") int page) + @Default("1") int page, + @Switch('r') String rotationName) throws CommandException { - if (matchManager.getMapOrder() instanceof FixedPGMMapOrderManager) { - List maps; - FixedPGMMapOrderManager fixedPGMMapOrderManager = - (FixedPGMMapOrderManager) matchManager.getMapOrder(); - FixedPGMMapOrder rotation = fixedPGMMapOrderManager.getRotationByName(rotationName); + RotationManager rotationManager = getRotationManager(sender, matchManager); + Rotation rotation = + rotationName == null + ? rotationManager.getActiveRotation() + : rotationManager.getRotationByName(rotationName); - if (rotation != null) maps = rotation.getMaps(); - else { - sender.sendMessage( - ChatColor.RED - + AllTranslations.get().translate("command.rotation.noRotationMatch", sender)); - return; - } - - int resultsPerPage = 8; - int pages = (maps.size() + resultsPerPage - 1) / resultsPerPage; - - String listHeader = - ChatColor.BLUE.toString() - + ChatColor.STRIKETHROUGH - + "-----------" - + ChatColor.RESET - + " " - + AllTranslations.get().translate("command.rotation.currentRotation.title", sender) - + ChatColor.DARK_AQUA - + " (" - + ChatColor.AQUA - + rotation.getName() - + ChatColor.DARK_AQUA - + ")" - + " (" - + ChatColor.AQUA - + page - + ChatColor.DARK_AQUA - + " of " - + ChatColor.AQUA - + pages - + ChatColor.DARK_AQUA - + ") " - + ChatColor.translateAlternateColorCodes('&', "&9&m-----------"); - - new PrettyPaginatedResult(listHeader, resultsPerPage) { - @Override - public String format(PGMMap map, int index) { - index++; - String indexString = - rotation.getPosition() + 1 == index - ? ChatColor.DARK_AQUA.toString() + index - : String.valueOf(index); - return (indexString) + ". " + ChatColor.RESET + map.getInfo().getShortDescription(sender); - } - }.display(audience, maps, page); - } else { + if (rotation == null) { sender.sendMessage( - ChatColor.RED - + AllTranslations.get().translate("command.rotation.rotationsDisabled", sender)); + ChatColor.RED + AllTranslations.get().translate("command.rotation.noRotation", sender)); + return; } + List maps = rotation.getMaps(); + + int resultsPerPage = 8; + int pages = (maps.size() + resultsPerPage - 1) / resultsPerPage; + + String listHeader = + ChatColor.BLUE.toString() + + ChatColor.STRIKETHROUGH + + "-----------" + + ChatColor.RESET + + " " + + AllTranslations.get().translate("command.rotation.currentRotation.title", sender) + + ChatColor.DARK_AQUA + + " (" + + ChatColor.AQUA + + rotation.getName() + + ChatColor.DARK_AQUA + + ")" + + " (" + + ChatColor.AQUA + + page + + ChatColor.DARK_AQUA + + " of " + + ChatColor.AQUA + + pages + + ChatColor.DARK_AQUA + + ") " + + ChatColor.translateAlternateColorCodes('&', "&9&m-----------"); + + new PrettyPaginatedResult(listHeader, resultsPerPage) { + @Override + public String format(PGMMap map, int index) { + index++; + String indexString = + rotation.getNextPosition() == index + ? ChatColor.DARK_AQUA.toString() + index + : String.valueOf(index); + return (indexString) + ". " + ChatColor.RESET + map.getInfo().getShortDescription(sender); + } + }.display(audience, maps, page); } @Command( @@ -166,67 +91,57 @@ public static void rotations( Audience audience, CommandSender sender, MatchManager matchManager, @Default("1") int page) throws CommandException { - if (matchManager.getMapOrder() instanceof FixedPGMMapOrderManager) { - FixedPGMMapOrderManager fixedPGMMapOrderManager = - (FixedPGMMapOrderManager) matchManager.getMapOrder(); - - List rotations; - if (!fixedPGMMapOrderManager.getRotations().isEmpty()) { - rotations = fixedPGMMapOrderManager.getRotations(); - } else { - sender.sendMessage( - ChatColor.RED - + AllTranslations.get().translate("command.rotation.noRotations", sender)); - return; - } - - int resultsPerPage = 8; - int pages = (rotations.size() + resultsPerPage - 1) / resultsPerPage; + RotationManager rotationManager = getRotationManager(sender, matchManager); - String listHeader = - ChatColor.BLUE.toString() - + ChatColor.STRIKETHROUGH - + "-----------" - + ChatColor.RESET - + " " - + AllTranslations.get().translate("command.rotation.rotationList.title", sender) - + ChatColor.DARK_AQUA - + " (" - + ChatColor.AQUA - + page - + ChatColor.DARK_AQUA - + " of " - + ChatColor.AQUA - + pages - + ChatColor.DARK_AQUA - + ") " - + ChatColor.translateAlternateColorCodes('&', "&9&m-----------"); - - new PrettyPaginatedResult(listHeader, resultsPerPage) { - @Override - public String format(FixedPGMMapOrder rotation, int index) { - String arrow = - fixedPGMMapOrderManager.getActiveRotation().getName().equals(rotation.getName()) - ? ChatColor.GREEN + "» " - : "» "; - return arrow - + ChatColor.GOLD - + rotation.getName() - + ChatColor.DARK_AQUA - + " (" - + ChatColor.AQUA - + "Players: " - + ChatColor.WHITE - + rotation.getPlayers() - + ChatColor.DARK_AQUA - + ")"; - } - }.display(audience, rotations, page); - } else { + List rotations = rotationManager.getRotations(); + if (rotations.isEmpty()) { sender.sendMessage( - ChatColor.RED - + AllTranslations.get().translate("command.rotation.rotationsDisabled", sender)); + ChatColor.RED + AllTranslations.get().translate("command.rotation.noRotations", sender)); + return; } + + int resultsPerPage = 8; + int pages = (rotations.size() + resultsPerPage - 1) / resultsPerPage; + + String listHeader = + ChatColor.BLUE.toString() + + ChatColor.STRIKETHROUGH + + "-----------" + + ChatColor.RESET + + " " + + AllTranslations.get().translate("command.rotation.rotationList.title", sender) + + ChatColor.DARK_AQUA + + " (" + + ChatColor.AQUA + + page + + ChatColor.DARK_AQUA + + " of " + + ChatColor.AQUA + + pages + + ChatColor.DARK_AQUA + + ") " + + ChatColor.translateAlternateColorCodes('&', "&9&m-----------"); + + new PrettyPaginatedResult(listHeader, resultsPerPage) { + @Override + public String format(Rotation rotation, int index) { + String arrow = + rotationManager.getActiveRotation().getName().equals(rotation.getName()) + ? ChatColor.GREEN + "» " + : "» "; + return arrow + + ChatColor.GOLD + + rotation.getName() + + ChatColor.DARK_AQUA + + " (" + + ChatColor.AQUA + + "Players: " + + ChatColor.WHITE + + rotation.getPlayers() + + ChatColor.DARK_AQUA + + ")"; + } + }.display(audience, rotations, page); } @Command( @@ -235,7 +150,8 @@ public String format(FixedPGMMapOrder rotation, int index) { usage = "[positions]", perms = Permissions.SETNEXT) public static void skip( - CommandSender sender, MatchManager matchManager, @Default("1") int positions) { + CommandSender sender, MatchManager matchManager, @Default("1") int positions) + throws CommandException { if (positions < 0) { sender.sendMessage( @@ -244,34 +160,36 @@ public static void skip( return; } - if (matchManager.getMapOrder() instanceof FixedPGMMapOrderManager) { - FixedPGMMapOrderManager fixedPGMMapOrderManager = - (FixedPGMMapOrderManager) matchManager.getMapOrder(); + RotationManager rotationManager = getRotationManager(sender, matchManager); + + rotationManager.getActiveRotation().advance(positions); + rotationManager.saveRotations(); + sender.sendMessage( + ChatColor.WHITE + + "[" + + ChatColor.GOLD + + "Rotations" + + ChatColor.WHITE + + "] " + + "[" + + ChatColor.AQUA + + rotationManager.getActiveRotation().getName() + + ChatColor.WHITE + + "] " + + ChatColor.GREEN + + AllTranslations.get() + .translate( + "command.rotation.skip.message", + sender, + (ChatColor.AQUA.toString() + positions + ChatColor.GREEN))); + } - fixedPGMMapOrderManager.getActiveRotation().movePosition(positions); - fixedPGMMapOrderManager.saveCurrentPosition(); - sender.sendMessage( - ChatColor.WHITE - + "[" - + ChatColor.GOLD - + "Rotations" - + ChatColor.WHITE - + "] " - + "[" - + ChatColor.AQUA - + fixedPGMMapOrderManager.getActiveRotation().getName() - + ChatColor.WHITE - + "] " - + ChatColor.GREEN - + AllTranslations.get() - .translate( - "command.rotation.skip.message", - sender, - (ChatColor.AQUA.toString() + positions + ChatColor.GREEN))); - } else { - sender.sendMessage( - ChatColor.RED - + AllTranslations.get().translate("command.rotation.rotationsDisabled", sender)); - } + private static RotationManager getRotationManager(CommandSender sender, MatchManager matchManager) + throws CommandException { + if (matchManager.getMapOrder() instanceof RotationManager) + return (RotationManager) matchManager.getMapOrder(); + + throw new CommandException( + AllTranslations.get().translate("command.rotation.rotationsDisabled", sender)); } } diff --git a/src/main/java/tc/oc/pgm/commands/provider/PGMMapProvider.java b/src/main/java/tc/oc/pgm/commands/provider/PGMMapProvider.java index 268015a660..1f79f8f139 100644 --- a/src/main/java/tc/oc/pgm/commands/provider/PGMMapProvider.java +++ b/src/main/java/tc/oc/pgm/commands/provider/PGMMapProvider.java @@ -38,10 +38,10 @@ public PGMMap get(CommandSender sender, CommandArgs args, List getMapByNameOrId(String nameOrId) { Set maps = mapsByName.get(nameOrId); if (maps.isEmpty()) { - return Optional.fromNullable(mapsById.get(nameOrId)); + return Optional.ofNullable(mapsById.get(nameOrId)); } PGMMap best = null; diff --git a/src/main/java/tc/oc/pgm/rotation/FixedPGMMapOrder.java b/src/main/java/tc/oc/pgm/rotation/FixedPGMMapOrder.java deleted file mode 100644 index 5759128849..0000000000 --- a/src/main/java/tc/oc/pgm/rotation/FixedPGMMapOrder.java +++ /dev/null @@ -1,150 +0,0 @@ -package tc.oc.pgm.rotation; - -import com.google.common.base.Optional; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.logging.Level; -import org.bukkit.configuration.ConfigurationSection; -import tc.oc.pgm.api.PGM; -import tc.oc.pgm.map.PGMMap; - -/** In practice, a {@link FixedPGMMapOrder} is a rotation of maps, a type of {@link PGMMapOrder} */ -public class FixedPGMMapOrder implements PGMMapOrder { - private ConfigurationSection configurationSection; - - private String name; - private boolean enabled; - private List maps = new ArrayList<>(); - private int players; - private int position; - - FixedPGMMapOrder(ConfigurationSection configurationSection, String name) { - this.configurationSection = configurationSection; - this.name = name; - this.enabled = configurationSection.getBoolean("enabled"); - this.players = configurationSection.getInt("players"); - } - - public String getName() { - return name; - } - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public List getMaps() { - return Collections.unmodifiableList(maps); - } - - public int getPlayers() { - return players; - } - - public void setPlayers(int players) { - this.players = players; - } - - public void setPosition(int position) { - this.position = position; - } - - public int getPosition() { - return position; - } - - public int getNextPosition() { - return (position + 1) % maps.size(); - } - - public void load() { - configurationSection - .getStringList("maps") - .forEach( - map -> { - final Optional mapByNameOrId = - PGM.get().getMapLibrary().getMapByNameOrId(map); - if (mapByNameOrId.isPresent()) maps.add(mapByNameOrId.get()); - else { - PGM.get() - .getLogger() - .warning( - "[Rotation] " - + "[" - + name - + "] " - + map - + " not found in map repo. Ignoring..."); - } - }); - - Optional nextMap = - PGM.get().getMapLibrary().getMapByNameOrId(configurationSection.getString("next_map")); - if (!nextMap.isPresent()) { - PGM.get() - .getLogger() - .log( - Level.SEVERE, - "Could not resolve next map from rotations. Resuming on initial position: 0"); - this.position = 0; - } else { - this.position = getMapPosition(nextMap.get()); - } - } - - private int getMapPosition(PGMMap map) { - int count = 0; - - for (PGMMap pgmMap : maps) { - if (pgmMap.getName().equals(map.getName())) break; - count++; - } - - return count; - } - - private void rotate() { - movePosition(1); - } - - public void movePosition(int positions) { - position = (position + positions) % maps.size(); - } - - PGMMap getMapInPosition(int position) { - if (position >= maps.size()) { - PGM.get() - .getLogger() - .log( - Level.WARNING, - "An unexpected call to map in position " - + position - + " from rotation with size " - + maps.size() - + " has been issued. Returning map in position 0 instead."); - return getMapInPosition(0); - } - - return maps.get(position); - } - - @Override - public PGMMap popNextMap() { - PGMMap nextMap = maps.get(position); - rotate(); - return nextMap; - } - - @Override - public PGMMap getNextMap() { - return maps.get(position); - } - - @Override - public void setNextMap(PGMMap map) {} -} diff --git a/src/main/java/tc/oc/pgm/rotation/PGMMapOrder.java b/src/main/java/tc/oc/pgm/rotation/PGMMapOrder.java index e09cad4154..4842106a50 100644 --- a/src/main/java/tc/oc/pgm/rotation/PGMMapOrder.java +++ b/src/main/java/tc/oc/pgm/rotation/PGMMapOrder.java @@ -1,15 +1,41 @@ package tc.oc.pgm.rotation; +import tc.oc.pgm.api.match.Match; import tc.oc.pgm.map.PGMMap; /** - * An order of {@link PGMMap}s, which can either be a {@link FixedPGMMapOrder} or a {@link - * RandomPGMMapOrder} + * A provider of {@link PGMMap} ordering order. It is responsible for providing the next map the + * server should play. */ public interface PGMMapOrder { + /** + * Returns next map to play, and removes it from the queue. + * + * @return The next map to play + */ PGMMap popNextMap(); + /** + * Returns the next map that will be played. Returning a null value is allowed and specifies the + * map order hasn't picked the next map yet. + * + * @return The next map to play, if present. + */ PGMMap getNextMap(); + /** + * Forces a specific map to be played next. The underlying {@link PGMMapOrder} may ignore this, + * but it is recommended not to. + * + * @param map The map to set next + */ void setNextMap(PGMMap map); + + /** + * Notify the {@link PGMMapOrder} that a match just ended, to allow it to run actions before cycle + * starts. + * + * @param match The match that just ended + */ + default void matchEnded(Match match) {} } diff --git a/src/main/java/tc/oc/pgm/rotation/RandomPGMMapOrder.java b/src/main/java/tc/oc/pgm/rotation/RandomPGMMapOrder.java index 219744be22..2f90e22071 100644 --- a/src/main/java/tc/oc/pgm/rotation/RandomPGMMapOrder.java +++ b/src/main/java/tc/oc/pgm/rotation/RandomPGMMapOrder.java @@ -4,7 +4,6 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; -import java.util.Objects; import tc.oc.pgm.api.PGM; import tc.oc.pgm.api.match.Match; import tc.oc.pgm.api.match.MatchManager; @@ -28,14 +27,12 @@ private PGMMap getRandomMap() { Iterator iterator = matchManager.getMatches().iterator(); PGMMap current = iterator.hasNext() ? iterator.next().getMap() : null; List maps = new ArrayList<>(PGM.get().getMapLibrary().getMaps()); + Collections.shuffle(maps); - PGMMap randomMap = null; - do { - Collections.shuffle(maps); - randomMap = maps.get(0); - } while (maps.size() > 1 && Objects.equals(current, randomMap)); - - return randomMap; + for (PGMMap map : maps) { + if (map != current) return map; + } + return maps.get(0); } @Override diff --git a/src/main/java/tc/oc/pgm/rotation/Rotation.java b/src/main/java/tc/oc/pgm/rotation/Rotation.java new file mode 100644 index 0000000000..c97332b12b --- /dev/null +++ b/src/main/java/tc/oc/pgm/rotation/Rotation.java @@ -0,0 +1,150 @@ +package tc.oc.pgm.rotation; + +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.logging.Level; +import java.util.stream.Collectors; +import org.bukkit.configuration.ConfigurationSection; +import tc.oc.pgm.api.PGM; +import tc.oc.pgm.map.MapLibrary; +import tc.oc.pgm.map.PGMMap; + +/** Rotation of maps, a type of {@link PGMMapOrder} */ +public class Rotation implements PGMMapOrder, Comparable { + private final RotationManager manager; + private final ConfigurationSection configSection; + + private final String name; + private final boolean enabled; + private final List maps; + private final int players; + + private int position; + + Rotation(RotationManager manager, ConfigurationSection configSection, String name) { + this.manager = manager; + this.configSection = configSection; + this.name = name; + this.enabled = configSection.getBoolean("enabled"); + this.players = configSection.getInt("players"); + + MapLibrary library = PGM.get().getMapLibrary(); + List mapList = + configSection.getStringList("maps").stream() + .map( + mapName -> { + Optional optMap = library.getMapByNameOrId(mapName); + if (optMap.isPresent()) return optMap.get(); + PGM.get() + .getLogger() + .warning( + "[Rotation] [" + + name + + "] " + + mapName + + " not found in map repo. Ignoring..."); + return null; + }) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + this.maps = Collections.unmodifiableList(mapList); + + Optional nextMap = library.getMapByNameOrId(configSection.getString("next_map")); + if (nextMap.isPresent()) this.position = getMapPosition(nextMap.get()); + else { + PGM.get() + .getLogger() + .log( + Level.SEVERE, + "Could not resolve next map from rotations. Resuming on initial position: 0"); + } + } + + public String getName() { + return name; + } + + public boolean isEnabled() { + return enabled; + } + + public List getMaps() { + return Collections.unmodifiableList(maps); + } + + public int getPlayers() { + return players; + } + + public void setPosition(int position) { + this.position = position % maps.size(); + } + + public int getPosition() { + return position; + } + + public int getNextPosition() { + return (position + 1) % maps.size(); + } + + private int getMapPosition(PGMMap map) { + int count = 0; + + for (PGMMap pgmMap : maps) { + if (pgmMap.getName().equals(map.getName())) break; + count++; + } + + return count; + } + + private PGMMap getMapInPosition(int position) { + if (position < 0 || position >= maps.size()) { + PGM.get() + .getLogger() + .log( + Level.WARNING, + "An unexpected call to map in position " + + position + + " from rotation with size " + + maps.size() + + " has been issued. Returning map in position 0 instead."); + return maps.get(0); + } + + return maps.get(position); + } + + private void advance() { + advance(1); + } + + public void advance(int positions) { + position = (position + positions) % maps.size(); + configSection.set("next_map", getMapInPosition(position).getName()); + manager.saveRotations(); + } + + @Override + public PGMMap popNextMap() { + PGMMap nextMap = getMapInPosition(position); + advance(); + return nextMap; + } + + @Override + public PGMMap getNextMap() { + return maps.get(position); + } + + @Override + public void setNextMap(PGMMap map) {} + + @Override + public int compareTo(Rotation o) { + return Integer.compare(this.position, o.position); + } +} diff --git a/src/main/java/tc/oc/pgm/rotation/FixedPGMMapOrderManager.java b/src/main/java/tc/oc/pgm/rotation/RotationManager.java similarity index 50% rename from src/main/java/tc/oc/pgm/rotation/FixedPGMMapOrderManager.java rename to src/main/java/tc/oc/pgm/rotation/RotationManager.java index e2b145c3d7..9656cad4b9 100644 --- a/src/main/java/tc/oc/pgm/rotation/FixedPGMMapOrderManager.java +++ b/src/main/java/tc/oc/pgm/rotation/RotationManager.java @@ -5,38 +5,33 @@ import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; import org.apache.commons.io.FileUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; import tc.oc.pgm.AllTranslations; import tc.oc.pgm.api.PGM; import tc.oc.pgm.api.match.Match; -import tc.oc.pgm.api.match.MatchManager; import tc.oc.pgm.map.PGMMap; /** - * Manages all the existing {@link FixedPGMMapOrder}s (Rotations), as for maintaining their order, - * and updating the one {@link PGM} will use after every match depending on the player count - * (Dynamic Rotations) + * Manages all the existing {@link Rotation}s, as for maintaining their order, and updating the one + * {@link PGM} will use after every match depending on the player count (Dynamic Rotations) */ -public class FixedPGMMapOrderManager implements PGMMapOrder { +public class RotationManager implements PGMMapOrder { - private MatchManager matchManager; private Logger logger; private File rotationsFile; private FileConfiguration rotationsFileConfiguration; - private List rotations = new ArrayList<>(); - private FixedPGMMapOrder activeRotation; - private boolean isEvaluatingPlayerCount = true; + private List rotations = new ArrayList<>(); + private Rotation activeRotation; /** When a {@link PGMMap} is manually set next, it overrides the rotation order * */ private PGMMap overriderMap; - public FixedPGMMapOrderManager(MatchManager matchManager, Logger logger, File rotationsFile) { - this.matchManager = matchManager; + public RotationManager(Logger logger, File rotationsFile) { this.rotationsFile = rotationsFile; this.logger = logger; @@ -54,59 +49,23 @@ public FixedPGMMapOrderManager(MatchManager matchManager, Logger logger, File ro } private void loadRotations() { - rotationsFileConfiguration - .getConfigurationSection("rotations") - .getKeys(false) - .forEach( - key -> - rotations.add( - new FixedPGMMapOrder( + rotations = + rotationsFileConfiguration.getConfigurationSection("rotations").getKeys(false).stream() + .map( + key -> + new Rotation( + this, rotationsFileConfiguration.getConfigurationSection("rotations." + key), - key))); - - rotations.forEach(FixedPGMMapOrder::load); - - rotations.forEach( - rotation -> { - if (!rotation.isEnabled()) rotations.remove(rotation); - }); - - FixedPGMMapOrder lastActiveRotation = - rotations.stream() - .map(FixedPGMMapOrder::getName) - .filter(name -> name.equals(rotationsFileConfiguration.getString("last_active"))) - .map(this::getRotationByName) - .findFirst() - .orElse(null); + key)) + .filter(Rotation::isEnabled) + .collect(Collectors.toList()); + Rotation lastActiveRotation = + getRotationByName(rotationsFileConfiguration.getString("last_active")); setActiveRotation(lastActiveRotation); } - private void setActiveRotation(FixedPGMMapOrder activeRotation) { - this.activeRotation = activeRotation; - } - - public FixedPGMMapOrder getActiveRotation() { - return activeRotation; - } - - void setEvaluatingPlayerCount(boolean evaluatingPlayerCount) { - this.isEvaluatingPlayerCount = evaluatingPlayerCount; - } - - public boolean isEvaluatingPlayerCount() { - return isEvaluatingPlayerCount; - } - - public List getRotations() { - return rotations; - } - - public void saveCurrentPosition() { - rotationsFileConfiguration.set( - "rotations." + activeRotation.getName() + ".next_map", - activeRotation.getMapInPosition(activeRotation.getNextPosition()).getName()); - + public void saveRotations() { try { rotationsFileConfiguration.save(rotationsFile); } catch (IOException e) { @@ -114,40 +73,34 @@ public void saveCurrentPosition() { } } - public void recalculateActiveRotation() { - int activePlayers = getActivePlayers(); + private void setActiveRotation(Rotation activeRotation) { + this.activeRotation = activeRotation; + } - rotations.stream() - .filter(FixedPGMMapOrder::isEnabled) - .map(FixedPGMMapOrder::getPlayers) - .filter(playerCount -> playerCount >= activePlayers) - .min(Integer::compareTo) - .map(this::getRotationByPlayerCount) - .ifPresent(this::updateActiveRotation); + public Rotation getActiveRotation() { + return activeRotation; } - private int getActivePlayers() { - Collection onlinePlayers = Bukkit.getOnlinePlayers(); - if (onlinePlayers.isEmpty()) return 0; + public List getRotations() { + return rotations; + } - Match match = matchManager.getMatch((Player) onlinePlayers.toArray()[0]); + public void matchEnded(Match match) { + int activePlayers = match.getPlayers().size() - (match.getObservers().size() / 2); - assert match != null; - return onlinePlayers.size() - match.getObservers().size() / 2; + rotations.stream() + .filter(rot -> activePlayers >= rot.getPlayers()) + .min(Rotation::compareTo) + .ifPresent(this::updateActiveRotation); } - private void updateActiveRotation(FixedPGMMapOrder rotation) { + private void updateActiveRotation(Rotation rotation) { if (rotation == activeRotation) return; setActiveRotation(rotation); rotationsFileConfiguration.set("last_active", activeRotation.getName()); - - try { - rotationsFileConfiguration.save(rotationsFile); - } catch (IOException e) { - logger.log(Level.SEVERE, "Could not save last active rotation for future reference.", e); - } + saveRotations(); Bukkit.broadcastMessage( ChatColor.WHITE @@ -164,7 +117,7 @@ private void updateActiveRotation(FixedPGMMapOrder rotation) { (ChatColor.AQUA + rotation.getName() + ChatColor.GREEN))); } - private FixedPGMMapOrder getRotationByPlayerCount(int playerCount) { + private Rotation getRotationByPlayerCount(int playerCount) { return rotations.stream() .filter(rotation -> rotation.getPlayers() == playerCount) .findFirst() @@ -176,9 +129,9 @@ private FixedPGMMapOrder getRotationByPlayerCount(int playerCount) { * things. * * @param name The name of the desired rotation - * @return The {@link FixedPGMMapOrder} (Rotation) which matches the input name + * @return The {@link Rotation} which matches the input name */ - public FixedPGMMapOrder getRotationByName(String name) { + public Rotation getRotationByName(String name) { return rotations.stream() .filter(rot -> rot.getName().equalsIgnoreCase(name)) .findFirst() @@ -191,10 +144,8 @@ public PGMMap popNextMap() { PGMMap overrider = overriderMap; overriderMap = null; return overrider; - } else { - saveCurrentPosition(); - return activeRotation.popNextMap(); } + return activeRotation.popNextMap(); } @Override