From 72a2f08d70352970cba5230d82c3744c055d0532 Mon Sep 17 00:00:00 2001 From: cmastudios Date: Sat, 22 Feb 2014 18:12:11 -0600 Subject: [PATCH] Config to apply potions on spawn. Closes #615. The new APPLYPOTION configuration option allows warzone makers to give potion effects ot players every time they spawn. Use case: Resistance 5 for a few seconds on player spawn to prevent them from being "spawn camped" - killed when they step out by other players waiting nearby. Format of APPLYPOTION: effect_type:duration:amplification effect_type: For a valid list of values that can be used for this type param, please see http://jd.bukkit.org/rb/apidocs/org/bukkit/potion/PotionEffectType.html duration: Time in ticks (1/20 sec) that the potion will apply for. amplification: Amplifier to the potion's power. @taoneill @cmastudios documentate how to tune this option before 1.9 release on the wiki. Also please test use of this option in game (pulling a @grinning here). --- war/src/main/java/com/tommytony/war/War.java | 22 +++++++++++++++++++ .../main/java/com/tommytony/war/Warzone.java | 8 +++++++ .../com/tommytony/war/config/TeamConfig.java | 3 ++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/war/src/main/java/com/tommytony/war/War.java b/war/src/main/java/com/tommytony/war/War.java index 112e6ba..17ad4fa 100644 --- a/war/src/main/java/com/tommytony/war/War.java +++ b/war/src/main/java/com/tommytony/war/War.java @@ -57,6 +57,8 @@ import com.tommytony.war.utility.PlayerState; import com.tommytony.war.utility.SizeCounter; import com.tommytony.war.utility.WarLogFormatter; import com.tommytony.war.volume.Volume; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; /** * Main class of War @@ -225,6 +227,7 @@ public class War extends JavaPlugin { teamDefaultConfig.put(TeamConfig.KILLSTREAK, false); teamDefaultConfig.put(TeamConfig.BLOCKWHITELIST, "all"); teamDefaultConfig.put(TeamConfig.PLACEBLOCK, true); + teamDefaultConfig.put(TeamConfig.APPLYPOTION, ""); this.getDefaultInventories().clearLoadouts(); HashMap defaultLoadout = new HashMap(); @@ -1318,4 +1321,23 @@ public class War extends JavaPlugin { public Locale getLoadedLocale() { return messages.getLocale(); } + + /** + * Convert serialized effect to actual effect. + * @param serializedEffect String stored in configuration. + * Format: TYPE:DURATION:AMPLIFY + * @return Potion effect or null otherwise + */ + public PotionEffect getPotionEffect(String serializedEffect) { + String[] arr = serializedEffect.split(":"); + if (arr.length != 3) return null; + try { + PotionEffectType type = PotionEffectType.getByName(arr[0]); + int duration = Integer.parseInt(arr[1]); + int amplification = Integer.parseInt(arr[2]); + return new PotionEffect(type, duration, amplification); + } catch (RuntimeException ex) { + return null; + } + } } diff --git a/war/src/main/java/com/tommytony/war/Warzone.java b/war/src/main/java/com/tommytony/war/Warzone.java index 92337b2..5287c6b 100644 --- a/war/src/main/java/com/tommytony/war/Warzone.java +++ b/war/src/main/java/com/tommytony/war/Warzone.java @@ -36,6 +36,7 @@ import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.permissions.Permissible; +import org.bukkit.potion.PotionEffect; import org.bukkit.scoreboard.DisplaySlot; import org.bukkit.scoreboard.Objective; import org.bukkit.scoreboard.Scoreboard; @@ -451,6 +452,13 @@ public class Warzone { // clear potion effects PotionEffectHelper.clearPotionEffects(player); + + if (!team.getTeamConfig().getString(TeamConfig.APPLYPOTION).isEmpty()) { + PotionEffect effect = War.war.getPotionEffect(team.getTeamConfig().getString(TeamConfig.APPLYPOTION)); + if (effect != null) { + player.addPotionEffect(effect); + } + } boolean isFirstRespawn = false; if (!this.getLoadoutSelections().keySet().contains(player.getName())) { diff --git a/war/src/main/java/com/tommytony/war/config/TeamConfig.java b/war/src/main/java/com/tommytony/war/config/TeamConfig.java index 222c0d5..807e2de 100644 --- a/war/src/main/java/com/tommytony/war/config/TeamConfig.java +++ b/war/src/main/java/com/tommytony/war/config/TeamConfig.java @@ -17,7 +17,8 @@ public enum TeamConfig { XPKILLMETER (Boolean.class), KILLSTREAK (Boolean.class), BLOCKWHITELIST (String.class), - PLACEBLOCK (Boolean.class); + PLACEBLOCK (Boolean.class), + APPLYPOTION(String.class); private final Class configType;