|
| 1 | +/* |
| 2 | + * This file is part of Skript. |
| 3 | + * |
| 4 | + * Skript is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * Skript is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with Skript. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + * |
| 18 | + * Copyright 2011-2013 Peter Güttinger |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +package ch.njol.skript.expressions; |
| 23 | + |
| 24 | +import org.bukkit.entity.HumanEntity; |
| 25 | +import org.bukkit.entity.LivingEntity; |
| 26 | +import org.bukkit.entity.Player; |
| 27 | +import org.bukkit.entity.Projectile; |
| 28 | +import org.bukkit.event.Event; |
| 29 | +import org.eclipse.jdt.annotation.Nullable; |
| 30 | + |
| 31 | +import ch.njol.skript.Skript; |
| 32 | +import ch.njol.skript.bukkitutil.ProjectileUtils; |
| 33 | +import ch.njol.skript.classes.Converter; |
| 34 | +import ch.njol.skript.classes.Changer.ChangeMode; |
| 35 | +import ch.njol.skript.doc.Description; |
| 36 | +import ch.njol.skript.doc.Examples; |
| 37 | +import ch.njol.skript.doc.Name; |
| 38 | +import ch.njol.skript.doc.Since; |
| 39 | +import ch.njol.skript.expressions.base.PropertyExpression; |
| 40 | +import ch.njol.skript.expressions.base.SimplePropertyExpression; |
| 41 | +import ch.njol.skript.lang.Expression; |
| 42 | +import ch.njol.skript.lang.ExpressionType; |
| 43 | +import ch.njol.skript.lang.SkriptParser.ParseResult; |
| 44 | +import ch.njol.util.Kleenean; |
| 45 | + |
| 46 | +/** |
| 47 | + * Used to set saturation of players. Number is used in case something changes in future... |
| 48 | + * @author bensku |
| 49 | + */ |
| 50 | +@Name("Saturation") |
| 51 | +@Description("The saturation of a player.") |
| 52 | +@Examples({"saturation of player is 20 #Not hungry!"}) |
| 53 | +@Since("2.2-Fixes-V10") |
| 54 | +public class ExprSaturation extends PropertyExpression<Player, Number> { |
| 55 | + static { |
| 56 | + Skript.registerExpression(ExprSaturation.class, Number.class, ExpressionType.SIMPLE, "[the] saturation [of %players%]"); |
| 57 | + } |
| 58 | + |
| 59 | + @SuppressWarnings({"unchecked", "null"}) |
| 60 | + @Override |
| 61 | + public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { |
| 62 | + setExpr((Expression<? extends Player>) exprs[0]); |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected Number[] get(final Event e, final Player[] source) { |
| 68 | + return get(source, new Converter<Player, Number>() { |
| 69 | + @Override |
| 70 | + @Nullable |
| 71 | + public Number convert(final Player p) { |
| 72 | + return p.getSaturation(); |
| 73 | + } |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + @Nullable |
| 79 | + public Class<?>[] acceptChange(final ChangeMode mode) { |
| 80 | + if (mode == ChangeMode.SET) |
| 81 | + return new Class[] {Number.class}; |
| 82 | + return super.acceptChange(mode); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void change(final Event e, final @Nullable Object[] delta, final ChangeMode mode) { |
| 87 | + if (mode == ChangeMode.SET) { |
| 88 | + assert delta != null; |
| 89 | + for (final Player p : getExpr().getArray(e)) { |
| 90 | + assert p != null : getExpr(); |
| 91 | + p.setSaturation(((Number) delta[0]).floatValue()); |
| 92 | + } |
| 93 | + } else { |
| 94 | + super.change(e, delta, mode); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public Class<Number> getReturnType() { |
| 100 | + return Number.class; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public String toString(final @Nullable Event e, final boolean debug) { |
| 105 | + return "the saturation" + (getExpr().isDefault() ? "" : " of " + getExpr().toString(e, debug)); |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments