Skip to content

Commit

Permalink
Fix and rewrite potion system
Browse files Browse the repository at this point in the history
  • Loading branch information
bensku committed Mar 13, 2016
1 parent 9d99179 commit e0f8968
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 124 deletions.
7 changes: 7 additions & 0 deletions src/main/java/ch/njol/skript/aliases/Aliases.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
@SuppressWarnings("deprecation")
public abstract class Aliases {

private final static boolean newPotions = Skript.isRunningMinecraft(1, 9);

/**
* Note to self: never use this, use {@link #getAlias_i(String)} instead.
*/
Expand Down Expand Up @@ -181,6 +183,11 @@ private final static String concatenate(final String... parts) {
*/
static LinkedHashMap<String, ItemType> getAliases(final String name, final ItemType value, final Variations variations) {
final LinkedHashMap<String, ItemType> r = new LinkedHashMap<String, ItemType>(); // LinkedHashMap to preserve order for item names

if (name.contains("potion") && newPotions) { // 1.9 new potions hack
return r;
}

for (int i = 0; i < name.length(); i++) {
final char c = name.charAt(i);
if ("[({".indexOf(c) != -1) {
Expand Down
112 changes: 0 additions & 112 deletions src/main/java/ch/njol/skript/classes/data/BukkitClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -1116,117 +1116,5 @@ public boolean mustSyncDeserialization() {
return false;
}
}));

if (Skript.classExists("org.bukkit.potion.PotionData")) {
Classes.registerClass(new ClassInfo<PotionData>(PotionData.class, "potiondata")
.user("potion datas?")
.name("Potion Data")
.description("A potion type, which contains one <a href='#potioneffecttype'>effect type</a>. It also tells if the potion is strong (level 2 effect) or extended (increased effect length)")
.usage("") //Not usable directly due to the fact you might want to say "strong potion of swiftness" ("potion of" is not part of type)
.examples("")
.since("2.2-Fixes-V10")
.before("potioneffecttype")
.parser(new Parser<PotionData>() {

@Override
@Nullable
public PotionData parse(final String s, final ParseContext context) {
String[] words = s.split(" ");

boolean extended = false;
boolean strong = false;
PotionType checked = null;
for (int i = 0; i < words.length; i++) {
switch (words[i]) {
case "extended":
case "long":
extended = true;
break;
case "strong":
case "level 2":
case "upgraded":
strong = true;
break;
case "potion":
case "of":
break; // These words are most certainly NOT potion types
default:
PotionType type;
if (i == words.length - 1) type = PotionEffectUtils.checkPotionType(words[i]);
else type = PotionEffectUtils.checkPotionType(words[i] + " " + words[i + 1]);

if (type != null) checked = type;
}
}

// Sanity checks...
if (checked == null) return null;
if (extended && !checked.isExtendable()) return null;
if (strong && !checked.isUpgradeable()) return null;
if (strong && extended) return null;

return new PotionData(checked, extended, strong);
}

@Override
public String toString(final PotionData o, final int flags) {
String ret = "";
if (o.isExtended()) ret += "extended";
else if (o.isUpgraded()) ret += "strong";

ret += " potion of ";
ret += o.getType().name().replace("_", " ").toLowerCase(); // Might return clunky (but working) string representations...
return ret;
}

@Override
public String toVariableNameString(PotionData o) {
return toString(o, 0);
}

@Override
public String getVariableNamePattern() {
return "[a-z ]+";
}

})
.serializer(new Serializer<PotionData>() {

@Override
public Fields serialize(final PotionData o) throws NotSerializableException {
Fields f = new Fields();
f.putObject("type", o.getType());
f.putPrimitive("extended", o.isExtended());
f.putPrimitive("strong", o.isUpgraded());

return f;
}

@Override
public void deserialize(final PotionData o, final Fields f) throws StreamCorruptedException {
assert false;
}

@Override
protected PotionData deserialize(final Fields f) throws StreamCorruptedException {
PotionType type = f.getObject("type", PotionType.class);
boolean extended = f.getPrimitive("extended", Boolean.class);
boolean strong = f.getPrimitive("strong", Boolean.class);

return new PotionData(type, extended, strong);
}

@Override
public boolean mustSyncDeserialization() {
return false;
}

@Override
protected boolean canBeInstantiated() {
return false;
}
})
);
}
}
}
31 changes: 19 additions & 12 deletions src/main/java/ch/njol/skript/expressions/ExprPotionItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,58 +21,65 @@

package ch.njol.skript.expressions;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.util.PotionEffectUtils;
import ch.njol.util.Kleenean;

/**
* Simple interface for creating vanilla potions (if supported by server).
* @author bensku
*/
public class ExprPotionItem extends SimpleExpression<ItemStack> {
public class ExprPotionItem extends SimpleExpression<ItemType> {

public static final String POTION_MODS = "[(0¦(regular|normal)|1¦(strong|upgraded|level 2)|2¦(extended|long)) ]";

static {
if (Skript.classExists("org.bukkit.potion.PotionData")) {
Skript.registerExpression(ExprPotionItem.class, ItemStack.class, ExpressionType.PROPERTY,
"%potiondata%");
Skript.registerExpression(ExprPotionItem.class, ItemType.class, ExpressionType.SIMPLE,
POTION_MODS + "potion of %potioneffecttype%");
}
}

@Nullable
private Expression<PotionData> data;
private Expression<PotionEffectType> type;
private int mod = 0; // 1=upgraded, 2=extended

@SuppressWarnings("unchecked")
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
data = (Expression<PotionData>) exprs[0];
if (data == null) return false;
type = (Expression<PotionEffectType>) exprs[0];
mod = parseResult.mark;
if (exprs.length == 0) return false;
return true;
}

@SuppressWarnings("null")
@Override
@Nullable
protected ItemStack[] get(final Event e) {
if (data == null) return new ItemStack[] {};
PotionData potion = data.getSingle(e);
protected ItemType[] get(final Event e) {
PotionData potion = new PotionData(PotionEffectUtils.effectToType(type.getSingle(e)), mod == 2, mod == 1);
ItemStack item = new ItemStack(Material.POTION);
PotionMeta meta = (PotionMeta) item.getItemMeta();
meta.setBasePotionData(potion);
item.setItemMeta(meta);

return new ItemStack[] {item};
return new ItemType[] {new ItemType(item)};
}

@Override
Expand All @@ -81,8 +88,8 @@ public boolean isSingle() {
}

@Override
public Class<? extends ItemStack> getReturnType() {
return ItemStack.class;
public Class<? extends ItemType> getReturnType() {
return ItemType.class;
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/ch/njol/skript/util/PotionEffectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.bukkit.entity.ThrownPotion;
import org.bukkit.potion.Potion;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
Expand Down Expand Up @@ -105,6 +106,7 @@ public static short guessData(final ThrownPotion p) {

/**
* Checks if given string represents a known potion type and returns that type.
* Unused currently, will be used soon (TM).
* @param name Name of potion type
* @return
*/
Expand Down Expand Up @@ -159,4 +161,15 @@ public static PotionType checkPotionType(String name) {

return null;
}

/**
* Wrapper around deprecated API function, in case it gets removed.
* Changing one method is easier that changing loads of them from different expressions.
* @param effect Type.
* @return Potion type.
*/
@SuppressWarnings("null")
public static PotionType effectToType(PotionEffectType effect) {
return PotionType.getByEffect(effect);
}
}

0 comments on commit e0f8968

Please sign in to comment.