From 1090550a3b3070154ed17766b0b61a1813262e82 Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Thu, 27 Nov 2014 03:37:14 -0600 Subject: [PATCH 1/6] Add potion effects to bosses when they spawn. With this feature you can add potions to the bosses when they spawn, adding the ability to add strength to the boss for the first 30 seconds making it harder for the first few seconds and once the effect wears goes away the boss becomes easier. The syntax is in the same style as potion items, except it uses either the potion effect enum name or the potion effect id. POTION_EFFECT:duration in seconds:amplifer SLOW:360:4;INVISIBILITY:36000:0 If no duration is given, the plugin will default to sixty seconds. If the amplifer was not provided, the plugin will default to zero. The potion effect amplifcation starts at 0 being level 1. --- .../garbagemule/MobArena/MASpawnThread.java | 1 + .../MobArena/util/PotionEffectParser.java | 123 ++++++++++++++++++ .../MobArena/waves/WaveParser.java | 10 ++ .../MobArena/waves/types/BossWave.java | 12 ++ 4 files changed, 146 insertions(+) create mode 100644 src/com/garbagemule/MobArena/util/PotionEffectParser.java diff --git a/src/com/garbagemule/MobArena/MASpawnThread.java b/src/com/garbagemule/MobArena/MASpawnThread.java index 3044890..8efe99b 100644 --- a/src/com/garbagemule/MobArena/MASpawnThread.java +++ b/src/com/garbagemule/MobArena/MASpawnThread.java @@ -183,6 +183,7 @@ public class MASpawnThread implements Runnable boss.setDrops(bw.getDrops()); bw.addMABoss(boss); bw.activateAbilities(arena); + e.addPotionEffects(bw.getPotions()); if (bw.getBossName() != null) { e.setCustomName(bw.getBossName()); e.setCustomNameVisible(true); diff --git a/src/com/garbagemule/MobArena/util/PotionEffectParser.java b/src/com/garbagemule/MobArena/util/PotionEffectParser.java new file mode 100644 index 0000000..887ff01 --- /dev/null +++ b/src/com/garbagemule/MobArena/util/PotionEffectParser.java @@ -0,0 +1,123 @@ +package com.garbagemule.MobArena.util; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; + +import com.garbagemule.MobArena.Messenger; + +public class PotionEffectParser +{ + private static final int TICKS_PER_SECOND = 20; + private static final int DEFAULT_POTION_DURATION = 1200; + private static final int DEFAULT_POTION_AMPLIFIER = 0; + + public static List parsePotionEffects(String s) { + if (s == null || s.isEmpty()) + return null; + + List potions = new ArrayList(); + for (String potion : s.split(",")) { + PotionEffect eff = parsePotionEffect(potion); + if (eff != null) { + potions.add(eff); + } + } + + return potions; + } + + public static PotionEffect parsePotionEffect(String p) { + if (p == null || p.isEmpty()) + return null; + + String[] parts = p.split(":"); + PotionEffect result = null; + + switch (parts.length) { + case 1: + result = parseSingle(parts[0]); + break; + case 2: + result = withDuration(parts[0], parts[1]); + break; + case 3: + result = withDurationAndAmplifier(parts[0], parts[1], parts[2]); + break; + } + + if (result == null) { + Messenger.warning("Failed to parse potion effect: " + p); + return null; + } + + return result; + } + + private static PotionEffect parseSingle(String type) { + PotionEffectType effect = getType(type); + + if (effect == null) { + return null; + } else { + return new PotionEffect(effect, DEFAULT_POTION_DURATION, DEFAULT_POTION_AMPLIFIER); + } + } + + private static PotionEffect withDuration(String type, String duration) { + PotionEffectType effect = getType(type); + int dur = getDuration(duration); + + if (effect == null || dur == -1) { + return null; + } else { + return new PotionEffect(effect, dur * TICKS_PER_SECOND, DEFAULT_POTION_AMPLIFIER); + } + } + + private static PotionEffect withDurationAndAmplifier(String type, String duration, String amplifier) { + PotionEffectType effect = getType(type); + int dur = getDuration(duration); + int amp = getAmplification(amplifier); + + if (effect == null || dur == -1 || amp == -1) { + return null; + } else { + return new PotionEffect(effect, dur * TICKS_PER_SECOND, amp); + } + } + + private static PotionEffectType getType(String type) { + PotionEffectType effect = null; + + if (type.matches("[0-9]*")) { + effect = PotionEffectType.getById(Integer.parseInt(type)); + } else { + effect = PotionEffectType.getByName(type); + } + + return effect; + } + + private static int getDuration(String duration) { + int dur = -1; + + if (duration.matches("[0-9]*")) { + dur = Integer.parseInt(duration); + } + + return dur; + } + + private static int getAmplification(String amplifier) { + int amp = -1; + + if (amplifier.matches("[0-9]*")) { + amp = Integer.parseInt(amplifier); + } + + return amp; + } +} diff --git a/src/com/garbagemule/MobArena/waves/WaveParser.java b/src/com/garbagemule/MobArena/waves/WaveParser.java index b42244a..476c82e 100644 --- a/src/com/garbagemule/MobArena/waves/WaveParser.java +++ b/src/com/garbagemule/MobArena/waves/WaveParser.java @@ -3,14 +3,17 @@ package com.garbagemule.MobArena.waves; import java.util.*; import com.garbagemule.MobArena.ArenaClass; + import org.bukkit.Location; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; import com.garbagemule.MobArena.Messenger; import com.garbagemule.MobArena.framework.Arena; import com.garbagemule.MobArena.region.ArenaRegion; import com.garbagemule.MobArena.util.ItemParser; +import com.garbagemule.MobArena.util.PotionEffectParser; import com.garbagemule.MobArena.waves.ability.Ability; import com.garbagemule.MobArena.waves.ability.AbilityManager; import com.garbagemule.MobArena.waves.enums.*; @@ -297,6 +300,13 @@ public class WaveParser List drops = ItemParser.parseItems(drp); result.setDrops(drops); + // Potions! + String pots = config.getString("potions"); + if (pots != null) { + List potions = PotionEffectParser.parsePotionEffects(pots); + if (potions != null) result.setPotions(potions); + } + return result; } diff --git a/src/com/garbagemule/MobArena/waves/types/BossWave.java b/src/com/garbagemule/MobArena/waves/types/BossWave.java index f0bc283..54c27d9 100644 --- a/src/com/garbagemule/MobArena/waves/types/BossWave.java +++ b/src/com/garbagemule/MobArena/waves/types/BossWave.java @@ -8,6 +8,7 @@ import java.util.Map; import java.util.Set; import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; import com.garbagemule.MobArena.Messenger; import com.garbagemule.MobArena.Msg; @@ -34,11 +35,13 @@ public class BossWave extends AbstractWave private ItemStack reward; private List drops; + private List potions; public BossWave(MACreature monster) { this.monster = monster; this.bosses = new HashSet(); this.abilities = new ArrayList(); + this.potions = new ArrayList(); this.activated = false; this.abilityAnnounce = false; this.setType(WaveType.BOSS); @@ -130,6 +133,14 @@ public class BossWave extends AbstractWave this.drops = drops; } + public List getPotions() { + return potions; + } + + public void setPotions(List potions) { + this.potions = potions; + } + public void activateAbilities(Arena arena) { if (activated) { return; @@ -159,6 +170,7 @@ public class BossWave extends AbstractWave result.flatHealth = this.flatHealth; result.reward = this.reward; result.drops = this.drops; + result.potions = this.potions; result.bossName = this.bossName; // From AbstractWave From 47f22f75ab3912bb7303b1a1ee5e6de49ff101fe Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Thu, 27 Nov 2014 22:05:38 -0600 Subject: [PATCH 2/6] If no duration provided, potion effects on bosses will last forever. --- src/com/garbagemule/MobArena/util/PotionEffectParser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/garbagemule/MobArena/util/PotionEffectParser.java b/src/com/garbagemule/MobArena/util/PotionEffectParser.java index 887ff01..aaa2e6a 100644 --- a/src/com/garbagemule/MobArena/util/PotionEffectParser.java +++ b/src/com/garbagemule/MobArena/util/PotionEffectParser.java @@ -11,7 +11,7 @@ import com.garbagemule.MobArena.Messenger; public class PotionEffectParser { private static final int TICKS_PER_SECOND = 20; - private static final int DEFAULT_POTION_DURATION = 1200; + private static final int DEFAULT_POTION_DURATION = Integer.MAX_VALUE; private static final int DEFAULT_POTION_AMPLIFIER = 0; public static List parsePotionEffects(String s) { From 409c3f6c14e728b9a4bacf91cf869e1730e6cd6d Mon Sep 17 00:00:00 2001 From: garbagemule Date: Mon, 8 Dec 2014 16:38:33 +0100 Subject: [PATCH 3/6] Swap amplifier and duration parameters in effect parser. This changes the format from: :: to a (pressumably) more user-friendly format of: :: The reason for this swap is that the duration is more likely to be wanted left at default than the amplifier. From previous user input on the matter, it sounds like users generally want the bosses to have "permanent" or "infinite duration" effects, and this change makes it a little easier on them, not having to specify a duration. --- .../MobArena/util/PotionEffectParser.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/com/garbagemule/MobArena/util/PotionEffectParser.java b/src/com/garbagemule/MobArena/util/PotionEffectParser.java index aaa2e6a..4fd4f25 100644 --- a/src/com/garbagemule/MobArena/util/PotionEffectParser.java +++ b/src/com/garbagemule/MobArena/util/PotionEffectParser.java @@ -11,8 +11,8 @@ import com.garbagemule.MobArena.Messenger; public class PotionEffectParser { private static final int TICKS_PER_SECOND = 20; - private static final int DEFAULT_POTION_DURATION = Integer.MAX_VALUE; private static final int DEFAULT_POTION_AMPLIFIER = 0; + private static final int DEFAULT_POTION_DURATION = Integer.MAX_VALUE; public static List parsePotionEffects(String s) { if (s == null || s.isEmpty()) @@ -41,10 +41,10 @@ public class PotionEffectParser result = parseSingle(parts[0]); break; case 2: - result = withDuration(parts[0], parts[1]); + result = withAmplifier(parts[0], parts[1]); break; case 3: - result = withDurationAndAmplifier(parts[0], parts[1], parts[2]); + result = withAmplifierAndDuration(parts[0], parts[1], parts[2]); break; } @@ -66,21 +66,21 @@ public class PotionEffectParser } } - private static PotionEffect withDuration(String type, String duration) { + private static PotionEffect withAmplifier(String type, String amplifier) { PotionEffectType effect = getType(type); - int dur = getDuration(duration); + int amp = getAmplification(amplifier); - if (effect == null || dur == -1) { + if (effect == null || amp == -1) { return null; } else { - return new PotionEffect(effect, dur * TICKS_PER_SECOND, DEFAULT_POTION_AMPLIFIER); + return new PotionEffect(effect, DEFAULT_POTION_DURATION, amp); } } - private static PotionEffect withDurationAndAmplifier(String type, String duration, String amplifier) { + private static PotionEffect withAmplifierAndDuration(String type, String amplifier, String duration) { PotionEffectType effect = getType(type); - int dur = getDuration(duration); int amp = getAmplification(amplifier); + int dur = getDuration(duration); if (effect == null || dur == -1 || amp == -1) { return null; From 55e0500bae619d3e8e4853d65322a782cc541d2c Mon Sep 17 00:00:00 2001 From: garbagemule Date: Mon, 8 Dec 2014 16:43:15 +0100 Subject: [PATCH 4/6] Allow for lower case potion effect names. --- src/com/garbagemule/MobArena/util/PotionEffectParser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/garbagemule/MobArena/util/PotionEffectParser.java b/src/com/garbagemule/MobArena/util/PotionEffectParser.java index 4fd4f25..594fa6e 100644 --- a/src/com/garbagemule/MobArena/util/PotionEffectParser.java +++ b/src/com/garbagemule/MobArena/util/PotionEffectParser.java @@ -95,7 +95,7 @@ public class PotionEffectParser if (type.matches("[0-9]*")) { effect = PotionEffectType.getById(Integer.parseInt(type)); } else { - effect = PotionEffectType.getByName(type); + effect = PotionEffectType.getByName(type.toUpperCase()); } return effect; From 243c5ea6ea408e33faa14081b58c63298a8e8381 Mon Sep 17 00:00:00 2001 From: garbagemule Date: Mon, 8 Dec 2014 16:45:05 +0100 Subject: [PATCH 5/6] Make numeric regexes require at least one number to match. --- src/com/garbagemule/MobArena/util/PotionEffectParser.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/com/garbagemule/MobArena/util/PotionEffectParser.java b/src/com/garbagemule/MobArena/util/PotionEffectParser.java index 594fa6e..a86f96d 100644 --- a/src/com/garbagemule/MobArena/util/PotionEffectParser.java +++ b/src/com/garbagemule/MobArena/util/PotionEffectParser.java @@ -92,7 +92,7 @@ public class PotionEffectParser private static PotionEffectType getType(String type) { PotionEffectType effect = null; - if (type.matches("[0-9]*")) { + if (type.matches("[0-9]+")) { effect = PotionEffectType.getById(Integer.parseInt(type)); } else { effect = PotionEffectType.getByName(type.toUpperCase()); @@ -104,7 +104,7 @@ public class PotionEffectParser private static int getDuration(String duration) { int dur = -1; - if (duration.matches("[0-9]*")) { + if (duration.matches("[0-9]+")) { dur = Integer.parseInt(duration); } @@ -114,7 +114,7 @@ public class PotionEffectParser private static int getAmplification(String amplifier) { int amp = -1; - if (amplifier.matches("[0-9]*")) { + if (amplifier.matches("[0-9]+")) { amp = Integer.parseInt(amplifier); } From bca1d387a4f76ea0dc3de7cbcc65d41b64fab236 Mon Sep 17 00:00:00 2001 From: garbagemule Date: Mon, 8 Dec 2014 16:48:53 +0100 Subject: [PATCH 6/6] Allow for spaces between comma-separated potion effects. --- src/com/garbagemule/MobArena/util/PotionEffectParser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/garbagemule/MobArena/util/PotionEffectParser.java b/src/com/garbagemule/MobArena/util/PotionEffectParser.java index a86f96d..fbd9aa5 100644 --- a/src/com/garbagemule/MobArena/util/PotionEffectParser.java +++ b/src/com/garbagemule/MobArena/util/PotionEffectParser.java @@ -20,7 +20,7 @@ public class PotionEffectParser List potions = new ArrayList(); for (String potion : s.split(",")) { - PotionEffect eff = parsePotionEffect(potion); + PotionEffect eff = parsePotionEffect(potion.trim()); if (eff != null) { potions.add(eff); }