Skip to content

Commit

Permalink
New expression, potion fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
bensku committed Mar 14, 2016
1 parent e0f8968 commit a00c792
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/main/java/ch/njol/skript/aliases/ItemType.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.RandomAccess;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.Inventory;
Expand Down Expand Up @@ -74,6 +75,7 @@ public class ItemType implements Unit, Iterable<ItemData>, Container<ItemStack>,

// Minecraft < 1.9 (1.9 has bug fixed)
public final static boolean invSizeWorkaround = !Skript.isRunningMinecraft(1, 9);
public final static boolean rawNamesSupported = Skript.isRunningMinecraft(1, 8);

/**
* Note to self: use {@link #add_(ItemData)} to add item datas, don't add them directly to this list.
Expand Down Expand Up @@ -1127,4 +1129,21 @@ public void deserialize(final Fields fields) throws StreamCorruptedException, No
fields.setFields(this);
}

/**
* Gets raw item names ("minecraft:some_item"). Only works for Minecraft 1.8+.
* @return
*/
public List<String> getRawNames() {
if (!rawNamesSupported) Skript.error("Raw Minecraft names are not supported. It requires Minecraft 1.8+!");

List<String> rawNames = new ArrayList<String>();
for (ItemData data : types) {
rawNames.add("minecraft:" + Material.getMaterial(data.typeid).toString().toLowerCase()
.replace("leash", "lead") // Add hacky code here :)
.replace("wood", "planks")
);
}

return rawNames;
}
}
2 changes: 2 additions & 0 deletions src/main/java/ch/njol/skript/events/EvtClick.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ private boolean checkOffHandUse(ItemStack mainHand, ItemStack offHand, int click
case ENDER_PEARL:
offUsable = true;
break;
//$CASES-OMITTED$
default:
offUsable = false;
}
Expand Down Expand Up @@ -240,6 +241,7 @@ private boolean checkOffHandUse(ItemStack mainHand, ItemStack offHand, int click
case ENDER_PEARL:
mainUsable = true;
break;
//$CASES-OMITTED$
default:
mainUsable = false;
}
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/ch/njol/skript/expressions/ExprPotionItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
*
* Copyright 2011-2013 Peter Güttinger
*
*/

package ch.njol.skript.expressions;
Expand Down Expand Up @@ -52,17 +49,23 @@ public class ExprPotionItem extends SimpleExpression<ItemType> {
static {
if (Skript.classExists("org.bukkit.potion.PotionData")) {
Skript.registerExpression(ExprPotionItem.class, ItemType.class, ExpressionType.SIMPLE,
POTION_MODS + "potion of %potioneffecttype%");
POTION_MODS + "potion of %potioneffecttype%", POTION_MODS + "%potioneffecttype% potion", "potion");
}
}

@Nullable
private Expression<PotionEffectType> type;
private int mod = 0; // 1=upgraded, 2=extended
private boolean water = false;

@SuppressWarnings("unchecked")
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
if (matchedPattern == 2) {
water = true;
return true;
}

type = (Expression<PotionEffectType>) exprs[0];
mod = parseResult.mark;
if (exprs.length == 0) return false;
Expand All @@ -73,8 +76,9 @@ public boolean init(final Expression<?>[] exprs, final int matchedPattern, final
@Override
@Nullable
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);
if (water) return new ItemType[] {new ItemType(item)};
PotionData potion = new PotionData(PotionEffectUtils.effectToType(type.getSingle(e)), mod == 2, mod == 1);
PotionMeta meta = (PotionMeta) item.getItemMeta();
meta.setBasePotionData(potion);
item.setItemMeta(meta);
Expand Down
94 changes: 94 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprRawName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
*/

package ch.njol.skript.expressions;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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.PotionEffectType;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.expressions.base.PropertyExpression;
import ch.njol.skript.expressions.base.SimplePropertyExpression;
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;

public class ExprRawName extends SimpleExpression<String> {

static {
if (Skript.isRunningMinecraft(1, 8)) {
Skript.registerExpression(ExprRawName.class, String.class, ExpressionType.SIMPLE, "(raw|minecraft|vanilla) name of %itemtypes%");
}
}

@Nullable
private Expression<ItemType> types;

@SuppressWarnings("unchecked")
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
this.types = (Expression<ItemType>) exprs[0];
return true;
}

@SuppressWarnings("null")
@Override
@Nullable
protected String[] get(final Event e) {
if (types == null) return null;

ItemType[] items = types.getAll(e);
List<String> names = new ArrayList<String>();
for (int i = 0; i < items.length; i++) {
names.addAll(items[i].getRawNames());
}

return names.toArray(new String[names.size()]);
}

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

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

@SuppressWarnings("null")
@Override
public String toString(final @Nullable Event e, final boolean debug) {
String[] strs = get(e);
if (strs == null) return "";
return Arrays.toString(strs);
}
}

0 comments on commit a00c792

Please sign in to comment.