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..fbd9aa5 --- /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_AMPLIFIER = 0; + private static final int DEFAULT_POTION_DURATION = Integer.MAX_VALUE; + + 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.trim()); + 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 = withAmplifier(parts[0], parts[1]); + break; + case 3: + result = withAmplifierAndDuration(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 withAmplifier(String type, String amplifier) { + PotionEffectType effect = getType(type); + int amp = getAmplification(amplifier); + + if (effect == null || amp == -1) { + return null; + } else { + return new PotionEffect(effect, DEFAULT_POTION_DURATION, amp); + } + } + + private static PotionEffect withAmplifierAndDuration(String type, String amplifier, String duration) { + PotionEffectType effect = getType(type); + int amp = getAmplification(amplifier); + int dur = getDuration(duration); + + 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.toUpperCase()); + } + + 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