From d75094c33c05840a857bdcd809ed103dbe1fb4c8 Mon Sep 17 00:00:00 2001 From: Cailin Smith Date: Mon, 8 Jul 2013 20:44:07 -0500 Subject: [PATCH] Made potion IDs dynamic, so they shouldn't have to be updated manually again. --- .../laytonsmith/abstraction/MCLivingEntity.java | 5 +++++ .../bukkit/BukkitMCLivingEntity.java | 17 ++++++++++------- .../core/functions/PlayerManagement.java | 9 ++++++++- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/laytonsmith/abstraction/MCLivingEntity.java b/src/main/java/com/laytonsmith/abstraction/MCLivingEntity.java index 02a32e4c4..48d267fe2 100644 --- a/src/main/java/com/laytonsmith/abstraction/MCLivingEntity.java +++ b/src/main/java/com/laytonsmith/abstraction/MCLivingEntity.java @@ -15,6 +15,11 @@ public interface MCLivingEntity extends MCEntity { public void addEffect(int potionID, int strength, int seconds, boolean ambient, Target t); public boolean removeEffect(int potionID); + /** + * Returns the maximum effect id, inclusive. + * @return + */ + public int getMaxEffect(); public List getEffects(); public void damage(double amount); public void damage(double amount, MCEntity source); diff --git a/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCLivingEntity.java b/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCLivingEntity.java index e3c0b3f79..c26e2474b 100644 --- a/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCLivingEntity.java +++ b/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCLivingEntity.java @@ -1,5 +1,6 @@ package com.laytonsmith.abstraction.bukkit; +import com.laytonsmith.PureUtilities.ReflectionUtils; import com.laytonsmith.abstraction.MCEntity; import com.laytonsmith.abstraction.MCEntityEquipment; import com.laytonsmith.abstraction.MCLivingEntity; @@ -181,13 +182,6 @@ private List getLineOfSight(HashSet transparent, int maxDistance, } public void addEffect(int potionID, int strength, int seconds, boolean ambient, Target t) { - //To work around a bug in bukkit/vanilla, if the effect is invalid, throw an exception - //otherwise the client crashes, and requires deletion of - //player data to fix. - if (potionID < 1 || potionID > 20) { - throw new ConfigRuntimeException("Invalid effect ID recieved, must be from 1-20", - ExceptionType.RangeException, t); - } PotionEffect pe = new PotionEffect(PotionEffectType.getById(potionID), (int)Static.msToTicks(seconds * 1000), strength, ambient); try{ @@ -221,6 +215,15 @@ public void addEffect(int potionID, int strength, int seconds, boolean ambient, // } // } } + + public int getMaxEffect(){ + try { + PotionEffectType[] arr = (PotionEffectType[])ReflectionUtils.get(PotionEffectType.class, "byId"); + return arr.length - 1; + } catch(ReflectionUtils.ReflectionException e){ + return Integer.MAX_VALUE; + } + } public boolean removeEffect(int potionID) { PotionEffectType t = PotionEffectType.getById(potionID); diff --git a/src/main/java/com/laytonsmith/core/functions/PlayerManagement.java b/src/main/java/com/laytonsmith/core/functions/PlayerManagement.java index ab070c389..4d35bfd0d 100644 --- a/src/main/java/com/laytonsmith/core/functions/PlayerManagement.java +++ b/src/main/java/com/laytonsmith/core/functions/PlayerManagement.java @@ -1640,7 +1640,7 @@ public Integer[] numArgs() { } public String docs() { - return "boolean {player, potionID, strength, [seconds], [ambient]} Effect is 1-19. Seconds defaults to 30." + return "boolean {player, potionID, strength, [seconds], [ambient]} Effect is 1-23. Seconds defaults to 30." + " If the potionID is out of range, a RangeException is thrown, because out of range potion effects" + " cause the client to crash, fairly hardcore. See http://www.minecraftwiki.net/wiki/Potion_effects" + " for a complete list of potions that can be added. To remove an effect, set the seconds to 0." @@ -1671,6 +1671,13 @@ public Construct exec(Target t, Environment env, Construct... args) throws Confi MCPlayer m = Static.GetPlayer(args[0].val(), t); int effect = Static.getInt32(args[1], t); + //To work around a bug in bukkit/vanilla, if the effect is invalid, throw an exception + //otherwise the client crashes, and requires deletion of + //player data to fix. + if (effect < 1 || effect > m.getMaxEffect()) { + throw new ConfigRuntimeException("Invalid effect ID recieved, must be from 1-" + m.getMaxEffect(), + ExceptionType.RangeException, t); + } int strength = Static.getInt32(args[2], t); int seconds = 30;