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.

This commit is contained in:
taoneill 2011-12-20 01:59:13 -05:00
parent 007344a63e
commit 0cf0024e55
3 changed files with 154 additions and 3 deletions

View File

@ -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<PotionEffect> getCurrentPotionEffects(Player player) {
List<PotionEffect> effects = new ArrayList<PotionEffect>();
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<PotionEffect> 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;
}
}
}

View File

@ -7,6 +7,9 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import net.minecraft.server.MobEffect;
import net.minecraft.server.MobEffectList;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Location; import org.bukkit.Location;
@ -15,6 +18,7 @@ import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.entity.CraftItem; import org.bukkit.craftbukkit.entity.CraftItem;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Item; import org.bukkit.entity.Item;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -336,6 +340,9 @@ public class Warzone {
if (player.getGameMode() == GameMode.CREATIVE) { if (player.getGameMode() == GameMode.CREATIVE) {
player.setGameMode(GameMode.SURVIVAL); player.setGameMode(GameMode.SURVIVAL);
} }
// clear potion effects
PotionEffect.clearPotionEffects(player);
if (!this.getLoadoutSelections().keySet().contains(player.getName())) { if (!this.getLoadoutSelections().keySet().contains(player.getName())) {
this.getLoadoutSelections().put(player.getName(), new LoadoutSelection(true, 0)); this.getLoadoutSelections().put(player.getName(), new LoadoutSelection(true, 0));
} else { } else {
@ -469,9 +476,11 @@ public class Warzone {
public void keepPlayerState(Player player) { public void keepPlayerState(Player player) {
PlayerInventory inventory = player.getInventory(); PlayerInventory inventory = player.getInventory();
ItemStack[] contents = inventory.getContents(); ItemStack[] contents = inventory.getContents();
List<PotionEffect> potionEffects = PotionEffect.getCurrentPotionEffects(player);
this.playerStates.put(player.getName(), new PlayerState(player.getGameMode(), this.playerStates.put(player.getName(), new PlayerState(player.getGameMode(),
contents, inventory.getHelmet(), inventory.getChestplate(), inventory.getLeggings(), inventory.getBoots(), 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) { public void restorePlayerState(Player player) {
@ -484,6 +493,7 @@ public class Warzone {
player.setExhaustion(originalContents.getExhaustion()); player.setExhaustion(originalContents.getExhaustion());
player.setSaturation(originalContents.getSaturation()); player.setSaturation(originalContents.getSaturation());
player.setFoodLevel(originalContents.getFoodLevel()); player.setFoodLevel(originalContents.getFoodLevel());
PotionEffect.restorePotionEffects(player, originalContents.getPotionEffects());
} }
} }

View File

@ -1,8 +1,12 @@
package com.tommytony.war.utils; package com.tommytony.war.utils;
import java.util.List;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import com.tommytony.war.PotionEffect;
public class PlayerState { public class PlayerState {
private ItemStack[] contents; private ItemStack[] contents;
private ItemStack helmet; private ItemStack helmet;
@ -14,14 +18,15 @@ public class PlayerState {
private final int foodLevel; private final int foodLevel;
private final int health; private final int health;
private final GameMode gamemode; private final GameMode gamemode;
private final List<PotionEffect> potionEffects;
public PlayerState(GameMode gamemode, ItemStack[] contents, ItemStack helmet, ItemStack chest, ItemStack legs, ItemStack feet, int health, float exhaustion, float saturation, int foodLevel, List<PotionEffect> potionEffects) {
public PlayerState(GameMode gamemode, ItemStack[] contents, ItemStack helmet, ItemStack chest, ItemStack legs, ItemStack feet, int health, float exhaustion, float saturation, int foodLevel) {
this.gamemode = gamemode; this.gamemode = gamemode;
this.health = health; this.health = health;
this.exhaustion = exhaustion; this.exhaustion = exhaustion;
this.saturation = saturation; this.saturation = saturation;
this.foodLevel = foodLevel; this.foodLevel = foodLevel;
this.potionEffects = potionEffects;
this.setContents(contents); this.setContents(contents);
this.setHelmet(helmet); this.setHelmet(helmet);
this.setChest(chest); this.setChest(chest);
@ -89,4 +94,8 @@ public class PlayerState {
return gamemode; return gamemode;
} }
public List<PotionEffect> getPotionEffects() {
return potionEffects;
}
} }