Skip to content

Commit

Permalink
Added configurable no-PvP period. (#106, closes #88)
Browse files Browse the repository at this point in the history
* NEW: added a configurable PvP-less period, disabled by default (i.e. PvP enabled when the grace period expires).
* OPT: updated the grace period time format, from seconds to the user-friendly format.
* OPT: the version of UhUtils.string2Time with a default value now accepts null values, instead of throwing a NPE.
  • Loading branch information
AmauryCarrade committed Jan 11, 2016
1 parent 5c13593 commit 4c6afdb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ public class UHGameManager
private final Boolean RANDOM_COLORS_IN_SOLO;
private final Boolean BROADCAST_SLOW_START_PROGRESS;
private final Long GRACE_PERIOD;
private final Long PEACE_PERIOD;
private final UHSound DEATH_SOUND;

private UHCReloaded p = null;
private TeamManager tm = null;
private I18n i = null;
private Random random = null;

private Boolean damageIsOn = false;
private Boolean damagesEnabled = false;

private HashSet<String> players = new HashSet<>(); // Will be converted to UUID when a built-in API for name->UUID conversion will be available
private HashSet<UUID> alivePlayers = new HashSet<>();
Expand Down Expand Up @@ -127,7 +128,8 @@ public UHGameManager(UHCReloaded plugin)
// Loads the config
RANDOM_COLORS_IN_SOLO = p.getConfig().getBoolean("teams-options.randomColors");
BROADCAST_SLOW_START_PROGRESS = p.getConfig().getBoolean("start.slow.broadcastProgress");
GRACE_PERIOD = (long) Math.min(p.getConfig().getDouble("start.gracePeriod", 30), 15) * 20l;
GRACE_PERIOD = (long) Math.min(UHUtils.string2Time(p.getConfig().getString("start.gracePeriod"), 30), 15) * 20l;
PEACE_PERIOD = (long) UHUtils.string2Time(p.getConfig().getString("start.peacePeriod"), 0) * 20l;
DEATH_SOUND = new UHSound(p.getConfig().getConfigurationSection("death.announcements.sound"));
}

Expand Down Expand Up @@ -589,15 +591,34 @@ private void startTimer()
*/
private void scheduleDamages()
{
// 30 seconds later, damages are enabled.
// When the grace period is over, damages are enabled.
RunTask.later(new Runnable()
{
@Override
public void run()
{
damageIsOn = true;
damagesEnabled = true;
}
}, GRACE_PERIOD);

// When the peace period is over, PVP is enabled
if (PEACE_PERIOD > 0)
{
for (World world : Bukkit.getWorlds())
world.setPVP(false);

RunTask.later(new Runnable()
{
@Override
public void run()
{
for (World world : Bukkit.getWorlds())
world.setPVP(true);

Bukkit.broadcastMessage(i.t("pvp.enabled"));
}
}, PEACE_PERIOD);
}
}

/**
Expand Down Expand Up @@ -959,7 +980,7 @@ public boolean isGameWithTeams()
*/
public boolean isTakingDamage()
{
return damageIsOn;
return damagesEnabled;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static int string2Time(String text)
{
String[] split = text.split(":");

if (split.length > 3)
if (text.isEmpty() || split.length > 3)
{
throw new IllegalArgumentException("Badly formatted string in string2time, formats allowed are mm, mm:ss or hh:mm:ss.");
}
Expand Down Expand Up @@ -143,7 +143,7 @@ public static int string2Time(String text, Integer defaultValue)
{
return string2Time(text);
}
catch (IllegalArgumentException e)
catch (IllegalArgumentException | NullPointerException e)
{
PluginLogger.warning("Invalid duration '{0}', using {1} seconds instead.", text, defaultValue);
return defaultValue;
Expand Down
10 changes: 8 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,15 @@ start:
# Display a title to everyone when the game begins
displayTitle: true

# Damages are enabled after this amount of seconds.
# Damages are enabled after this amount of time.
# Cannot be less than 15 seconds, to avoid initial-fall-related problems.
gracePeriod: 30
# Format: "mm", "mm:ss", "hh:mm:ss"
gracePeriod: "00:30"

# PVP is enabled after this amount of time.
# If 0, enabled immediately.
# Format: "mm", "mm:ss", "hh:mm:ss"
peacePeriod: 0



Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/i18n/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ keys:
broadcast:
withTeams: "{darkgreen}{obfuscated}--{green} Congratulations to {0} (team {1}{green}) for their victory! {darkgreen}{obfuscated}--"
withoutTeams: "{darkgreen}{obfuscated}--{green} Congratulations to {0} for his victory! {darkgreen}{obfuscated}--"


pvp:
enabled: "{red}{bold}Warning!{white} PvP is now enabled."

episodes:
end: "{aqua}-------- End of episode {0} --------"
endForced: "{aqua}-------- End of episode {0} [forced by {1}] --------"
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/i18n/fr_FR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ keys:
withTeams: "{darkgreen}{obfuscated}--{green} Félicitations à {0} (équipe {1}{green}) pour leur victoire ! {darkgreen}{obfuscated}--"
withoutTeams: "{darkgreen}{obfuscated}--{green} Félicitation à {0} pour sa victoire ! {darkgreen}{obfuscated}--"

pvp:
enabled: "{red}{bold}Attention !{white} Le PvP est désormais actif."

episodes:
end: "{aqua}-------- Fin de l'épisode {0} --------"
endForced: "{aqua}-------- Fin de l'épisode {0} [forcé par {1}] --------"
Expand Down

0 comments on commit 4c6afdb

Please sign in to comment.