From 4c6afdb6f57cf34aecfbf55fa069330a3888e257 Mon Sep 17 00:00:00 2001 From: Amaury Carrade Date: Tue, 12 Jan 2016 00:42:55 +0100 Subject: [PATCH] Added configurable no-PvP period. (#106, closes #88) * 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. --- .../UHCReloaded/game/UHGameManager.java | 31 ++++++++++++++++--- .../amaury/UHCReloaded/utils/UHUtils.java | 4 +-- src/main/resources/config.yml | 10 ++++-- src/main/resources/i18n/en_US.yml | 5 ++- src/main/resources/i18n/fr_FR.yml | 3 ++ 5 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/main/java/eu/carrade/amaury/UHCReloaded/game/UHGameManager.java b/src/main/java/eu/carrade/amaury/UHCReloaded/game/UHGameManager.java index 9bb5342..e8c3108 100644 --- a/src/main/java/eu/carrade/amaury/UHCReloaded/game/UHGameManager.java +++ b/src/main/java/eu/carrade/amaury/UHCReloaded/game/UHGameManager.java @@ -77,6 +77,7 @@ 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; @@ -84,7 +85,7 @@ public class UHGameManager private I18n i = null; private Random random = null; - private Boolean damageIsOn = false; + private Boolean damagesEnabled = false; private HashSet players = new HashSet<>(); // Will be converted to UUID when a built-in API for name->UUID conversion will be available private HashSet alivePlayers = new HashSet<>(); @@ -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")); } @@ -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); + } } /** @@ -959,7 +980,7 @@ public boolean isGameWithTeams() */ public boolean isTakingDamage() { - return damageIsOn; + return damagesEnabled; } /** diff --git a/src/main/java/eu/carrade/amaury/UHCReloaded/utils/UHUtils.java b/src/main/java/eu/carrade/amaury/UHCReloaded/utils/UHUtils.java index 091f403..b96f17c 100644 --- a/src/main/java/eu/carrade/amaury/UHCReloaded/utils/UHUtils.java +++ b/src/main/java/eu/carrade/amaury/UHCReloaded/utils/UHUtils.java @@ -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."); } @@ -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; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 4fbf10d..0d5b5a9 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -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 diff --git a/src/main/resources/i18n/en_US.yml b/src/main/resources/i18n/en_US.yml index 1c3b015..ec6d02b 100644 --- a/src/main/resources/i18n/en_US.yml +++ b/src/main/resources/i18n/en_US.yml @@ -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}] --------" diff --git a/src/main/resources/i18n/fr_FR.yml b/src/main/resources/i18n/fr_FR.yml index 021ee0e..6879b8e 100644 --- a/src/main/resources/i18n/fr_FR.yml +++ b/src/main/resources/i18n/fr_FR.yml @@ -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}] --------"