From 1090550a3b3070154ed17766b0b61a1813262e82 Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Thu, 27 Nov 2014 03:37:14 -0600 Subject: [PATCH] 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