Skip to content

Commit

Permalink
The teleporter can now ignore the teams and teleport one player per s…
Browse files Browse the repository at this point in the history
…pot even with teams.

Syntax: /uh start [slow:true] [ignoreTeams:true]

Closes #62.
  • Loading branch information
AmauryCarrade committed Mar 26, 2015
1 parent fc0e678 commit 023a3d9
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 88 deletions.
29 changes: 13 additions & 16 deletions src/main/java/eu/carrade/amaury/UHCReloaded/UHGameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,15 @@ public void run() {
*
* @param sender The player who launched the game.
* @param slow If true, the slow mode is enabled.
* With the slow mode, the players are, at first, teleported team by team with a configurable delay,
* and with the fly.
* Then, the fly is removed and the game starts.
* With the slow mode, the players are, at first, teleported team by team
* with a configurable delay, and with the fly.
* Then, the fly is removed and the game starts.
* @param ignoreTeams If true, the players will be teleported in individual teleportation spots,
* just like without teams, even with teams.
*
* @throws IllegalStateException if the game is running.
*/
public void start(CommandSender sender, Boolean slow) throws IllegalStateException {
public void start(CommandSender sender, Boolean slow, Boolean ignoreTeams) throws IllegalStateException {

if(isGameRunning()) {
throw new IllegalStateException("The game is currently running!");
Expand Down Expand Up @@ -271,7 +273,9 @@ public void start(CommandSender sender, Boolean slow) throws IllegalStateExcepti
if(!team.isEmpty()) aliveTeamsCount++;
}

if(p.getSpawnsManager().getSpawnPoints().size() < aliveTeamsCount) {
int spawnPointsNeeded = ignoreTeams ? alivePlayersCount : aliveTeamsCount;

if(p.getSpawnsManager().getSpawnPoints().size() < spawnPointsNeeded) {
sender.sendMessage(i.t("start.notEnoughTP"));

// We clears the teams if the game was in solo-mode, to avoid a team-counter to be displayed on the next start
Expand All @@ -293,17 +297,14 @@ public void start(CommandSender sender, Boolean slow) throws IllegalStateExcepti
/** Teleportation **/

// Standard mode
if(slow == false) {
if(!slow) {
List<Location> unusedTP = p.getSpawnsManager().getSpawnPoints();

for (final UHTeam t : tm.getTeams()) {
if(t.isEmpty()) continue;

final Location lo = unusedTP.get(this.random.nextInt(unusedTP.size()));

BukkitRunnable teamStartTask = new TeamStartTask(p, t, lo);
BukkitRunnable teamStartTask = new TeamStartTask(p, t, unusedTP, ignoreTeams);
teamStartTask.runTaskLater(p, 10L);

unusedTP.remove(lo);
}


Expand Down Expand Up @@ -338,14 +339,10 @@ public void start(CommandSender sender, Boolean slow) throws IllegalStateExcepti
for (UHTeam t : tm.getTeams()) {
if(t.isEmpty()) continue;

Location lo = unusedTP.get(random.nextInt(unusedTP.size()));

BukkitRunnable teamStartTask = new TeamStartTask(p, t, lo, true, sender, teamsTeleported);
BukkitRunnable teamStartTask = new TeamStartTask(p, t, unusedTP, ignoreTeams, true, sender, teamsTeleported);
teamStartTask.runTaskLater(p, 20L * teamsTeleported * delayBetweenTP);

teamsTeleported++;

unusedTP.remove(lo);
}

// The end is handled by this.finalizeStartSlow().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
*/
package eu.carrade.amaury.UHCReloaded.commands.commands.uh;

import eu.carrade.amaury.UHCReloaded.commands.commands.categories.Category;
import eu.carrade.amaury.UHCReloaded.commands.core.exceptions.CannotExecuteCommandException;
import eu.carrade.amaury.UHCReloaded.UHCReloaded;
import eu.carrade.amaury.UHCReloaded.commands.commands.categories.Category;
import eu.carrade.amaury.UHCReloaded.commands.core.AbstractCommand;
import eu.carrade.amaury.UHCReloaded.commands.core.annotations.Command;
import eu.carrade.amaury.UHCReloaded.i18n.I18n;
import eu.carrade.amaury.UHCReloaded.commands.core.exceptions.CannotExecuteCommandException;
import eu.carrade.amaury.UHCReloaded.commands.core.utils.CommandUtils;
import eu.carrade.amaury.UHCReloaded.i18n.I18n;
import eu.carrade.amaury.UHCReloaded.utils.UHUtils;
import org.bukkit.command.CommandSender;

import java.util.Arrays;
import java.util.List;
import java.util.*;


/**
Expand Down Expand Up @@ -57,32 +57,29 @@ public UHStartCommand(UHCReloaded plugin) {
*/
@Override
public void run(CommandSender sender, String[] args) throws CannotExecuteCommandException {
if(args.length == 0) { // /uh start (standard mode)
try {
p.getGameManager().start(sender, false);
} catch(IllegalStateException e) {
sender.sendMessage(i.t("start.already"));
} catch(Exception e) {
e.printStackTrace();
}
}

else if(args.length == 1 && args[0].equalsIgnoreCase("slow")) { // /uh start slow
try {
p.getGameManager().start(sender, true);
} catch(IllegalStateException e) {
sender.sendMessage(i.t("start.already"));
} catch(Exception e) {
e.printStackTrace();
}
if(args.length == 1 && args[0].equalsIgnoreCase("help")) {
throw new CannotExecuteCommandException(CannotExecuteCommandException.Reason.NEED_DOC);
}

else if(args.length == 2 && args[0].equalsIgnoreCase("slow") && args[1].equalsIgnoreCase("go")) { // /uh start slow go
p.getGameManager().finalizeStartSlow(sender);
}

else {
throw new CannotExecuteCommandException(CannotExecuteCommandException.Reason.BAD_USE);
Map<String, String> defaultTags = new HashMap<>();
defaultTags.put("slow", "false");
defaultTags.put("ignoreTeams", "false");

Map<String, String> tags = CommandUtils.getTagsInArgs(args, defaultTags);

try {
p.getGameManager().start(sender, UHUtils.stringToBoolean(tags.get("slow")), UHUtils.stringToBoolean(tags.get("ignoreTeams")));
} catch(IllegalStateException e) {
sender.sendMessage(i.t("start.already"));
} catch(Exception e) {
e.printStackTrace();
}
}
}

Expand All @@ -97,15 +94,23 @@ else if(args.length == 2 && args[0].equalsIgnoreCase("slow") && args[1].equalsIg
@Override
public List<String> tabComplete(CommandSender sender, String[] args) {

if(args.length == 1) { // /uh start <?>
return CommandUtils.getAutocompleteSuggestions(args[0], Arrays.asList("slow"));
}

else if(args.length == 2 && args[0].equalsIgnoreCase("slow")) { // /uh start slow <?>
if(args.length == 2 && args[0].equalsIgnoreCase("slow")) { // /uh start slow <?>
return CommandUtils.getAutocompleteSuggestions(args[1], Arrays.asList("go"));
}

else return null;
else {
// Can be improved

List<String> suggestions = new ArrayList<>();
suggestions.add("slow:true");
suggestions.add("ignoreTeams:true");

if(args.length == 1) {
suggestions.add("slow");
}

return CommandUtils.getAutocompleteSuggestions(args[args.length - 1], suggestions);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@
import eu.carrade.amaury.UHCReloaded.commands.core.AbstractCommand;
import eu.carrade.amaury.UHCReloaded.commands.core.annotations.Command;
import eu.carrade.amaury.UHCReloaded.utils.UHUtils;
import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.*;


public class CommandUtils {
Expand Down Expand Up @@ -84,6 +82,47 @@ public static String[] getSubcommandArguments(String[] args) {
}


/**
* Returns the tags in the arguments, following the format "tagname:value".
*
* <p>
* If a tag is defined multiple times, the value used is the last one.
* </p>
* <p>
* Invalid tags (other format that « key:value ») are ignored.
* </p>
*
* @param args The args.
* @param defaults The defaults values. The values defined here will always be in the returned map,
* with the same value if the key is not in the arguments.
* {@code null} if no default values are needed.
*
* @return A map tagname -> value.
*/
public static Map<String, String> getTagsInArgs(String[] args, Map<String, String> defaults) {
Map<String, String> tagsCollected;

if(defaults != null) {
tagsCollected = new HashMap<>(defaults);
}
else {
tagsCollected = new HashMap<>();
}

for(String arg : args) {
String[] argSpilt = arg.split(":");
if(argSpilt.length >= 2) { // valid
String key = argSpilt[0];
String value = StringUtils.join(Arrays.copyOfRange(argSpilt, 1, argSpilt.length), ":");

tagsCollected.put(key, value);
}
}

return tagsCollected;
}


/**
* Returns a list of autocompletion suggestions based on what the user typed and on a list of
* available commands.
Expand Down
Loading

0 comments on commit 023a3d9

Please sign in to comment.