diff --git a/src/main/java/ch/njol/skript/expressions/ExprVectorYawPitch.java b/src/main/java/ch/njol/skript/expressions/ExprVectorYawPitch.java deleted file mode 100644 index 902770e2af0..00000000000 --- a/src/main/java/ch/njol/skript/expressions/ExprVectorYawPitch.java +++ /dev/null @@ -1,124 +0,0 @@ -/** - * 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 . - * - * Copyright Peter Güttinger, SkriptLang team and contributors - */ -package ch.njol.skript.expressions; - -import org.bukkit.event.Event; -import org.bukkit.util.Vector; -import org.eclipse.jdt.annotation.Nullable; - -import ch.njol.skript.classes.Changer; -import ch.njol.skript.classes.Changer.ChangeMode; -import ch.njol.skript.doc.Description; -import ch.njol.skript.doc.Examples; -import ch.njol.skript.doc.Name; -import ch.njol.skript.doc.Since; -import ch.njol.skript.expressions.base.SimplePropertyExpression; -import ch.njol.skript.lang.Expression; -import ch.njol.skript.lang.SkriptParser.ParseResult; -import ch.njol.util.Kleenean; -import ch.njol.util.VectorMath; -import ch.njol.util.coll.CollectionUtils; - -/** - * @author bi0qaw - */ -@Name("Vectors - Yaw and Pitch") -@Description("Gets or sets the yaw or pitch value of a vector.") -@Examples({"set {_v} to vector -1, 1, 1", - "send \"%vector yaw of {_v}%, %vector pitch of {_v}%\"", - "add 45 to vector yaw of {_v}", - "subtract 45 from vector pitch of {_v}", - "send \"%vector yaw of {_v}%, %vector pitch of {_v}%\"", - "set vector yaw of {_v} to -45", - "set vector pitch of {_v} to 45", - "send \"%vector yaw of {_v}%, %vector pitch of {_v}%\"",}) -@Since("2.2-dev28") -public class ExprVectorYawPitch extends SimplePropertyExpression { - - static { - register(ExprVectorYawPitch.class, Number.class, "[vector] (0¦yaw|1¦pitch)", "vectors"); - } - - private boolean usesYaw; - - @Override - public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { - super.init(exprs, matchedPattern, isDelayed, parseResult); - usesYaw = parseResult.mark == 0; - return true; - } - - @Override - public Number convert(Vector vector) { - if (usesYaw) - return VectorMath.skriptYaw(VectorMath.getYaw(vector)); - return VectorMath.skriptPitch(VectorMath.getPitch(vector)); - } - - @Override - @SuppressWarnings("null") - public Class[] acceptChange(ChangeMode mode) { - if ((mode == ChangeMode.SET || mode == ChangeMode.ADD || mode == ChangeMode.REMOVE) - && getExpr().isSingle() && Changer.ChangerUtils.acceptsChange(getExpr(), ChangeMode.SET, Vector.class)) - return CollectionUtils.array(Number.class); - return null; - } - - @Override - public void change(Event e, @Nullable Object[] delta, ChangeMode mode) { - assert delta != null; - Vector v = getExpr().getSingle(e); - if (v == null) - return; - float n = ((Number) delta[0]).floatValue(); - float yaw = VectorMath.getYaw(v); - float pitch = VectorMath.getPitch(v); - switch (mode) { - case ADD: - if (usesYaw) - yaw += n; - else - pitch -= n; // Negative because of Minecraft's / Skript's upside down pitch - v = VectorMath.fromYawAndPitch(yaw, pitch); - getExpr().change(e, new Vector[]{v}, ChangeMode.SET); - break; - case REMOVE: - n = -n; - //$FALL-THROUGH$ - case SET: - if (usesYaw) - yaw = VectorMath.fromSkriptYaw(n); - else - pitch = VectorMath.fromSkriptPitch(n); - v = VectorMath.fromYawAndPitch(yaw, pitch); - getExpr().change(e, new Vector[]{v}, ChangeMode.SET); - } - } - - @Override - protected String getPropertyName() { - return usesYaw ? "yaw" : "pitch"; - } - - @Override - public Class getReturnType() { - return Number.class; - } - -} diff --git a/src/main/java/ch/njol/skript/expressions/ExprYawPitch.java b/src/main/java/ch/njol/skript/expressions/ExprYawPitch.java index 91ed60e1693..e7aa8a5eef3 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprYawPitch.java +++ b/src/main/java/ch/njol/skript/expressions/ExprYawPitch.java @@ -18,6 +18,7 @@ */ package ch.njol.skript.expressions; +import ch.njol.util.VectorMath; import org.bukkit.Location; import org.bukkit.event.Event; @@ -31,90 +32,130 @@ import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.util.Kleenean; import ch.njol.util.coll.CollectionUtils; +import org.bukkit.util.Vector; -/** - * @author Peter Güttinger - */ @Name("Yaw / Pitch") -@Description("The yaw or pitch of a location. You likely won't need this expression ever if you don't know what this means.") -@Examples("log \"%player%: %location of player%, %player's yaw%, %player's pitch%\" to \"playerlocs.log\"") -@Since("2.0") -public class ExprYawPitch extends SimplePropertyExpression { - - public static boolean randomSK = true; - +@Description("The yaw or pitch of a location or vector.") +@Examples({"log \"%player%: %location of player%, %player's yaw%, %player's pitch%\" to \"playerlocs.log\"", + "set {_yaw} to yaw of player", + "set {_p} to pitch of target entity"}) +@Since("2.0, 2.2-dev28 (vector yaw/pitch)") +public class ExprYawPitch extends SimplePropertyExpression { + static { - register(ExprYawPitch.class, Number.class, "(0¦yaw|1¦pitch)", "locations"); + register(ExprYawPitch.class, Number.class, "(0¦yaw|1¦pitch)", "locations/vectors"); } - - private boolean yaw; - + + private boolean usesYaw; + @Override public boolean init(final Expression[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { - yaw = parseResult.mark == 0; + usesYaw = parseResult.mark == 0; return super.init(exprs, matchedPattern, isDelayed, parseResult); } - + @Override - public Number convert(final Location l) { - return yaw ? convertToPositive(l.getYaw()) : l.getPitch(); + public Number convert(final Object object) { + if (object instanceof Location) { + Location l = ((Location) object); + return usesYaw ? convertToPositive(l.getYaw()) : l.getPitch(); + } else if (object instanceof Vector) { + Vector vector = ((Vector) object); + if (usesYaw) + return VectorMath.skriptYaw(VectorMath.getYaw(vector)); + return VectorMath.skriptPitch(VectorMath.getPitch(vector)); + } + return null; } - + + @SuppressWarnings({"null"}) @Override - public Class getReturnType() { - return Number.class; + public Class[] acceptChange(final ChangeMode mode) { + if (mode == ChangeMode.SET || mode == ChangeMode.ADD || mode == ChangeMode.REMOVE) + return CollectionUtils.array(Number.class); + return null; } - + + @SuppressWarnings("null") @Override - protected String getPropertyName() { - return yaw ? "yaw" : "pitch"; - } - - @SuppressWarnings({"null"}) - @Override - public Class[] acceptChange(final ChangeMode mode) { - if (mode == ChangeMode.SET || mode == ChangeMode.ADD || mode == ChangeMode.REMOVE) - return CollectionUtils.array(Number.class); - return null; + public void change(Event e, Object[] delta, ChangeMode mode) { + if (delta == null) + return; + float value = ((Number) delta[0]).floatValue(); + Object single = getExpr().getSingle(e); + if (single instanceof Location) { + changeLocation(((Location) single), value, mode); + } else if (single instanceof Vector) { + changeVector(e, ((Vector) single), value, mode); } - - @SuppressWarnings("null") - @Override - public void change(Event e, Object[] delta, ChangeMode mode) { - Location l = getExpr().getSingle(e); - if(delta[0] == null || l == null) - return; - float f = ((Number) delta[0]).floatValue(); - switch (mode) { - case SET: - if (yaw) - l.setYaw(convertToPositive(f)); - else - l.setPitch(f); - break; - case ADD: - if (yaw) - l.setYaw(convertToPositive(l.getYaw()) + f); - else - l.setPitch(l.getPitch() + f); - break; - case REMOVE: - if (yaw) - l.setYaw(convertToPositive(l.getYaw()) - f); - else - l.setPitch(l.getPitch() - f); - break; + } + + private void changeLocation(Location l, float value, ChangeMode mode) { + switch (mode) { + case SET: + if (usesYaw) + l.setYaw(convertToPositive(value)); + else + l.setPitch(value); + break; + case ADD: + if (usesYaw) + l.setYaw(convertToPositive(l.getYaw()) + value); + else + l.setPitch(l.getPitch() + value); + break; + case REMOVE: + if (usesYaw) + l.setYaw(convertToPositive(l.getYaw()) - value); + else + l.setPitch(l.getPitch() - value); + break; default: break; - } } - - - //Some random method decided to use for converting to positive values. - public float convertToPositive(float f) { - if (f != 0 && f * -1 == Math.abs(f)) - return 360 + f; - return f; - } - + } + + private void changeVector(Event e, Vector vector, float n, ChangeMode mode) { + float yaw = VectorMath.getYaw(vector); + float pitch = VectorMath.getPitch(vector); + switch (mode) { + case REMOVE: + n = -n; + //$FALL-THROUGH$ + case ADD: + if (usesYaw) + yaw += n; + else + pitch -= n; // Negative because of Minecraft's / Skript's upside down pitch + vector = VectorMath.fromYawAndPitch(yaw, pitch); + getExpr().change(e, new org.bukkit.util.Vector[]{vector}, ChangeMode.SET); + break; + case SET: + if (usesYaw) + yaw = VectorMath.fromSkriptYaw(n); + else + pitch = VectorMath.fromSkriptPitch(n); + vector = VectorMath.fromYawAndPitch(yaw, pitch); + getExpr().change(e, new org.bukkit.util.Vector[]{vector}, ChangeMode.SET); + } + } + + + //Some random method decided to use for converting to positive values. + public float convertToPositive(float f) { + if (f != 0 && f * -1 == Math.abs(f)) + return 360 + f; + return f; + } + + @Override + public Class getReturnType() { + return Number.class; + } + + @Override + protected String getPropertyName() { + return usesYaw ? "yaw" : "pitch"; + } + }