From 0cf0024e557eee978542922721e817261be4e2f7 Mon Sep 17 00:00:00 2001 From: taoneill Date: Tue, 20 Dec 2011 01:59:13 -0500 Subject: [PATCH] Closes gh-326. Potion effects are cleared at respawn and saved with player state so, for example, you get re-poisonned when you exit a warzone if you entered with the effect. Thanks @mahoutsukaii for the MobEffect trick. --- .../java/com/tommytony/war/PotionEffect.java | 132 ++++++++++++++++++ .../main/java/com/tommytony/war/Warzone.java | 12 +- .../com/tommytony/war/utils/PlayerState.java | 13 +- 3 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 war/src/main/java/com/tommytony/war/PotionEffect.java diff --git a/war/src/main/java/com/tommytony/war/PotionEffect.java b/war/src/main/java/com/tommytony/war/PotionEffect.java new file mode 100644 index 0000000..4c46b1a --- /dev/null +++ b/war/src/main/java/com/tommytony/war/PotionEffect.java @@ -0,0 +1,132 @@ +package com.tommytony.war; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; + +import net.minecraft.server.MobEffect; +import net.minecraft.server.MobEffectList; + +import org.bukkit.craftbukkit.entity.CraftPlayer; +import org.bukkit.entity.Player; + +import bukkit.tommytony.war.War; + +public class PotionEffect { + + private int id; + private int amplifier; + private int time; + + public PotionEffect(int id, int amplifier, int time) { + this.setId(id); + this.setAmplifier(amplifier); + this.setTime(time); + } + + public void setId(int id) { + this.id = id; + } + + public int getId() { + return id; + } + + public void setAmplifier(int amplifier) { + this.amplifier = amplifier; + } + + public int getAmplifier() { + return amplifier; + } + + public void setTime(int time) { + this.time = time; + } + + public int getTime() { + return time; + } + + public static List getCurrentPotionEffects(Player player) { + List effects = new ArrayList(); + + for(int i = 1; i < 20; i++) + { + if(((CraftPlayer)player).getHandle().hasEffect(enchantIdToList(i))) + { + MobEffect mobEffect = ((CraftPlayer)player).getHandle().getEffect(enchantIdToList(i)); + effects.add(new PotionEffect(mobEffect.getEffectId(), mobEffect.getAmplifier(), mobEffect.getDuration())); + War.war.log("Stored " + mobEffect.getEffectId(), Level.INFO); + } + } + + return effects; + } + + public static void restorePotionEffects(Player player, List potionEffects) { + clearPotionEffects(player); + for (PotionEffect effect : potionEffects) { + ((CraftPlayer)player).getHandle().addEffect(new MobEffect(effect.getId(), effect.getTime(), effect.getAmplifier())); + War.war.log("Restored " + effect.getId(), Level.INFO); + } + } + + public static void clearPotionEffects(Player player) { + for (int i = 1; i < 20; i++) { + if(((CraftPlayer)player).getHandle().hasEffect(enchantIdToList(i))) + { + int amplifier = ((CraftPlayer)player).getHandle().getEffect(enchantIdToList(i)).getAmplifier(); + ((CraftPlayer)player).getHandle().addEffect(new MobEffect(i, -1, amplifier + 1)); + + War.war.log("Cleared " + i, Level.INFO); + } + } + } + + private static MobEffectList enchantIdToList(int id) { + switch (id) { + case 1: + return MobEffectList.FASTER_MOVEMENT; + case 2: + return MobEffectList.SLOWER_MOVEMENT; + case 3: + return MobEffectList.FASTER_DIG; + case 4: + return MobEffectList.SLOWER_DIG; + case 5: + return MobEffectList.INCREASE_DAMAGE; + case 6: + return MobEffectList.HEAL; + case 7: + return MobEffectList.HARM; + case 8: + return MobEffectList.JUMP; + case 9: + return MobEffectList.CONFUSION; + case 10: + return MobEffectList.REGENERATION; + case 11: + return MobEffectList.RESISTANCE; + case 12: + return MobEffectList.FIRE_RESISTANCE; + case 13: + return MobEffectList.WATER_BREATHING; + case 14: + return MobEffectList.INVISIBILITY; + case 15: + return MobEffectList.BLINDNESS; + case 16: + return MobEffectList.NIGHT_VISION; + case 17: + return MobEffectList.HUNGER; + case 18: + return MobEffectList.WEAKNESS; + case 19: + return MobEffectList.POISON; + default: + 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 9e8245e..80efc39 100644 --- a/war/src/main/java/com/tommytony/war/Warzone.java +++ b/war/src/main/java/com/tommytony/war/Warzone.java @@ -7,6 +7,9 @@ import java.util.List; import java.util.Map; import java.util.logging.Level; +import net.minecraft.server.MobEffect; +import net.minecraft.server.MobEffectList; + import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.Location; @@ -15,6 +18,7 @@ import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.craftbukkit.entity.CraftItem; +import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.entity.Entity; import org.bukkit.entity.Item; import org.bukkit.entity.Player; @@ -336,6 +340,9 @@ public class Warzone { if (player.getGameMode() == GameMode.CREATIVE) { player.setGameMode(GameMode.SURVIVAL); } + // clear potion effects + PotionEffect.clearPotionEffects(player); + if (!this.getLoadoutSelections().keySet().contains(player.getName())) { this.getLoadoutSelections().put(player.getName(), new LoadoutSelection(true, 0)); } else { @@ -469,9 +476,11 @@ public class Warzone { public void keepPlayerState(Player player) { PlayerInventory inventory = player.getInventory(); ItemStack[] contents = inventory.getContents(); + List potionEffects = PotionEffect.getCurrentPotionEffects(player); this.playerStates.put(player.getName(), new PlayerState(player.getGameMode(), contents, inventory.getHelmet(), inventory.getChestplate(), inventory.getLeggings(), inventory.getBoots(), - player.getHealth(), player.getExhaustion(), player.getSaturation(), player.getFoodLevel())); + player.getHealth(), player.getExhaustion(), player.getSaturation(), + player.getFoodLevel(), potionEffects)); } public void restorePlayerState(Player player) { @@ -484,6 +493,7 @@ public class Warzone { player.setExhaustion(originalContents.getExhaustion()); player.setSaturation(originalContents.getSaturation()); player.setFoodLevel(originalContents.getFoodLevel()); + PotionEffect.restorePotionEffects(player, originalContents.getPotionEffects()); } } diff --git a/war/src/main/java/com/tommytony/war/utils/PlayerState.java b/war/src/main/java/com/tommytony/war/utils/PlayerState.java index ce086f7..9f14be8 100644 --- a/war/src/main/java/com/tommytony/war/utils/PlayerState.java +++ b/war/src/main/java/com/tommytony/war/utils/PlayerState.java @@ -1,8 +1,12 @@ package com.tommytony.war.utils; +import java.util.List; + import org.bukkit.GameMode; import org.bukkit.inventory.ItemStack; +import com.tommytony.war.PotionEffect; + public class PlayerState { private ItemStack[] contents; private ItemStack helmet; @@ -14,14 +18,15 @@ public class PlayerState { private final int foodLevel; private final int health; private final GameMode gamemode; + private final List potionEffects; - - public PlayerState(GameMode gamemode, ItemStack[] contents, ItemStack helmet, ItemStack chest, ItemStack legs, ItemStack feet, int health, float exhaustion, float saturation, int foodLevel) { + public PlayerState(GameMode gamemode, ItemStack[] contents, ItemStack helmet, ItemStack chest, ItemStack legs, ItemStack feet, int health, float exhaustion, float saturation, int foodLevel, List potionEffects) { this.gamemode = gamemode; this.health = health; this.exhaustion = exhaustion; this.saturation = saturation; this.foodLevel = foodLevel; + this.potionEffects = potionEffects; this.setContents(contents); this.setHelmet(helmet); this.setChest(chest); @@ -89,4 +94,8 @@ public class PlayerState { return gamemode; } + public List getPotionEffects() { + return potionEffects; + } + }