Skip to content

Commit

Permalink
Made potion IDs dynamic, so they shouldn't have to be updated manuall…
Browse files Browse the repository at this point in the history
…y again.
  • Loading branch information
LadyCailin committed Jul 9, 2013
1 parent b841bda commit d75094c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/laytonsmith/abstraction/MCLivingEntity.java
Expand Up @@ -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<MCEffect> getEffects();
public void damage(double amount);
public void damage(double amount, MCEntity source);
Expand Down
@@ -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;
Expand Down Expand Up @@ -181,13 +182,6 @@ private List<Block> getLineOfSight(HashSet<Short> 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{
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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."
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d75094c

Please sign in to comment.