Skip to content

Commit

Permalink
ExprYawPitch - Fix changer issues and merge with ExprVectorYawPitch (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneBeee authored Jun 9, 2021
1 parent e9d15bd commit 9547172
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 193 deletions.
124 changes: 0 additions & 124 deletions src/main/java/ch/njol/skript/expressions/ExprVectorYawPitch.java

This file was deleted.

179 changes: 110 additions & 69 deletions src/main/java/ch/njol/skript/expressions/ExprYawPitch.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package ch.njol.skript.expressions;

import ch.njol.util.VectorMath;
import org.bukkit.Location;
import org.bukkit.event.Event;

Expand All @@ -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<Location, Number> {
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<Object, Number> {

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<? extends Number> 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<? extends Number> getReturnType() {
return Number.class;
}

@Override
protected String getPropertyName() {
return usesYaw ? "yaw" : "pitch";
}

}

0 comments on commit 9547172

Please sign in to comment.