commit 93b170b4047e2f27001cdd8ac951a6a75a2fe2c3 Author: bi0qaw Date: Wed Nov 15 21:14:06 2017 +0100 Initial commit diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7b6157e --- /dev/null +++ b/pom.xml @@ -0,0 +1,71 @@ + + + 4.0.0 + + io.github.bi0qaw + biosphere2 + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.6 + 1.6 + + + + + + + + bensku-repo + https://raw.githubusercontent.com/bensku/mvn-repo/master + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + true + daily + + + + sk89q-repo + http://maven.sk89q.com/repo + + + destroystokyo-repo + https://repo.destroystokyo.com/repository/maven-public/ + + + vault-repo + http://nexus.hc.to/content/repositories/pub_releases + + + jitpack-repo + https://jitpack.io + + + + + + + ch.njol + skript + 2.2-dev32 + compile + + + + org.spigotmc + spigot-api + 1.12.2-R0.1-SNAPSHOT + provided + + + + \ No newline at end of file diff --git a/src/main/java/io/github/bi0qaw/biosphere/Biosphere.java b/src/main/java/io/github/bi0qaw/biosphere/Biosphere.java new file mode 100644 index 0000000..4d8ef4a --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/Biosphere.java @@ -0,0 +1,74 @@ +package io.github.bi0qaw.biosphere; + +import io.github.bi0qaw.biosphere.expression.*; +import ch.njol.skript.Skript; +import ch.njol.skript.lang.ExpressionType; +import org.bukkit.Location; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.type.FrameType; +import io.github.bi0qaw.biosphere.util.Frame; + +public class Biosphere extends JavaPlugin { + + private static Biosphere plugin; + + public void onEnable(){ + + plugin = this; + Skript.registerAddon(this); + + new FrameType(); + + Skript.registerExpression(ExprDegToRad.class, Number.class, ExpressionType.PROPERTY, "%number% [in] rad[ian]"); + Skript.registerExpression(ExprRadToDeg.class, Number.class, ExpressionType.PROPERTY, "%number% [in] deg[ree]"); + + Skript.registerExpression(ExprFrameFromEntity.class, Frame.class, ExpressionType.SIMPLE, new String[]{"frame of %entity%", "%entity%['s] frame"}); + Skript.registerExpression(ExprFrameFromYawPitch.class, Frame.class, ExpressionType.SIMPLE, "frame with yaw %number% and pitch %number%"); + Skript.registerExpression(ExprFrameYawPitch.class, Number.class, ExpressionType.PROPERTY, "frame (0¦yaw|1¦pitch) of %frame%"); + + Skript.registerExpression(ExprLocationCircle.class, Location.class, ExpressionType.SIMPLE, "circle[s] at %locations%[ with] radius %number%(,| and) density %number%"); + Skript.registerExpression(ExprLocationCube.class, Location.class, ExpressionType.SIMPLE, "cube[s] at %locations% with radius %number%"); + Skript.registerExpression(ExprLocationCubeOutline.class, Location.class, ExpressionType.SIMPLE, "cube[s] outline[s] at %locations% with radius %number%(,| and) density %number%"); + Skript.registerExpression(ExprLocationCylinderLoc.class, Location.class, ExpressionType.SIMPLE, "cylinder coordinate[s] at %locations% with radius %number%(,| and) yaw %number%(,| and) height %number%"); + Skript.registerExpression(ExprLocationHelix.class, Location.class, ExpressionType.SIMPLE, "heli(x|xes|ces) at %locations% with radius %number%(,| and) height %number%(,| and) step[(height|size)] %number%(,| and) density %number%"); + Skript.registerExpression(ExprLocationLine.class, Location.class, ExpressionType.SIMPLE, "line[s] from %locations% to %location% with density %number%"); + Skript.registerExpression(ExprLocationLineLoc.class, Location.class, ExpressionType.SIMPLE, "line[ar] (coordinate[s]|position[s]|location[s]) %number% from %locations% to %location%"); + Skript.registerExpression(ExprLocationLinkAll.class, Location.class, ExpressionType.SIMPLE, "%locations% (linked|connected) with density %number%"); + Skript.registerExpression(ExprLocationMidpoint.class, Location.class, ExpressionType.SIMPLE, "[location ]midpoint of %locations%"); + Skript.registerExpression(ExprLocationMove.class, Location.class, ExpressionType.SIMPLE, new String[]{"%locations% with center %location% (moved|shifted) to %location%", "%locations% with center %location% (moved|shifted) to %location%"}); + Skript.registerExpression(ExprLocationOffset.class, Location.class, ExpressionType.SIMPLE, "%locations% offset by %vectors%"); + Skript.registerExpression(ExprLocationPolygon.class, Location.class, ExpressionType.SIMPLE, "polygon[s] at %locations% with %integer% (vertex|vertices|vertexes|points)(,| and) radius %number%"); + Skript.registerExpression(ExprLocationPolygonOutline.class, Location.class, ExpressionType.SIMPLE, "polygon[s] outline[s] at %locations% with %integer% (vertex|vertices|vertexes|points)(,| and) radius %number%(,| and) density %number%"); + Skript.registerExpression(ExprLocationReflection.class, Location.class, ExpressionType.SIMPLE, "%locations% (mirrored|reflected) at %location%[ (in|with) direction [of ]%-vector%]"); + Skript.registerExpression(ExprLocationRotate.class, Location.class, ExpressionType.SIMPLE, "%locations% rotated around %vector% at %location% (with angle|by) %number%[ degree[s]]"); + Skript.registerExpression(ExprLocationRotXYZ.class, Location.class, ExpressionType.SIMPLE, "%locations% rotated around (1¦x|2¦y|3¦z)(-| )axis at %location% (with angle|by) %number%[ degree[s]]"); + Skript.registerExpression(ExprLocationScale.class, Location.class, ExpressionType.SIMPLE, "%locations% scaled at %location% by %number%[ (in|with|and) direction %-vector%]"); + Skript.registerExpression(ExprLocationSphere.class, Location.class, ExpressionType.SIMPLE, "sphere[s] at %locations% with radius %number%(,| and) density %number%"); + Skript.registerExpression(ExprLocationSphereLoc.class, Location.class, ExpressionType.SIMPLE, "spher(e|ic[al]) (coordinate[s]|position[s]|location[s]) at %locations% with radius %number%(,| and) yaw %number%(,| and) pitch %number%"); + Skript.registerExpression(ExprLocationSphereRand.class, Location.class, ExpressionType.SIMPLE, "random sphere[s] at %locations% with radius %number%(,| and) density %number%"); + + Skript.registerExpression(ExprVectorCircle.class, Vector.class, ExpressionType.SIMPLE, "[vector ]circle with radius %number%(,| and) density %number%"); + Skript.registerExpression(ExprVectorCube.class, Vector.class, ExpressionType.SIMPLE, "[vector ]cube with radius %number%"); + Skript.registerExpression(ExprVectorCubeOutline.class, Vector.class, ExpressionType.SIMPLE, "[vector ]cube outline with radius %number%(,| and) density %number%"); + Skript.registerExpression(ExprVectorHelix.class, Vector.class, ExpressionType.SIMPLE, "[vector ]helix with radius %number%(,| and) height %number%(,| and) step[(height|size)] %number%(,| and) density %number%"); + Skript.registerExpression(ExprVectorInFrame.class, Vector.class, ExpressionType.SIMPLE, "[vector ]%vectors% in %frame%"); + Skript.registerExpression(ExprVectorLine.class, Vector.class, ExpressionType.SIMPLE, "[vector ]line with length %number%(,| and) density %number%"); + Skript.registerExpression(ExprVectorLineBetweenVectors.class, Vector.class, ExpressionType.SIMPLE, "vector line between %vector% and %vector% with density %number%"); + Skript.registerExpression(ExprVectorLinkAll.class, Vector.class, ExpressionType.SIMPLE, "vector[s] %vectors% (linked|connected) with density %number%"); + Skript.registerExpression(ExprVectorMidpoint.class, Vector.class, ExpressionType.SIMPLE, "vector midpoint of %vectors%"); + Skript.registerExpression(ExprVectorMove.class, Vector.class, ExpressionType.SIMPLE, "[vector[s] ]%vectors% moved by %vector%"); + Skript.registerExpression(ExprVectorPolygon.class, Vector.class, ExpressionType.SIMPLE, "[vector ]polygon with %integer% (vertex|vertices|vertexes|points)(,| and) radius %number%"); + Skript.registerExpression(ExprVectorPolygonOutline.class, Vector.class, ExpressionType.SIMPLE, "[vector ]polygon outline with %integer% (vertex|vertices|vertexes|points)(,| and) radius %number%(,|and) density %number%"); + Skript.registerExpression(ExprVectorReflection.class, Vector.class, ExpressionType.SIMPLE, "vector[s] %vectors% (mirrored|reflected) at %vector%[ (in|with) direction [of ]%-vector%]"); + Skript.registerExpression(ExprVectorRotate.class, Vector.class, ExpressionType.SIMPLE, "[vector[s] ]%vectors% rotated around %vector% (with angle|by) %number%[ degree[s]]"); + Skript.registerExpression(ExprVectorRotateXYZ.class, Vector.class, ExpressionType.SIMPLE, "[vector[s] ]%vectors% rotated around (1¦x|2¦y|3¦z)(-| )axis (with angle|by) %number%[ degree[s]]"); + Skript.registerExpression(ExprVectorScale.class, Vector.class, ExpressionType.SIMPLE, "[vector[s] ]%vectors% scaled by %number%[ (in|with|and) direction %-vector%]"); + Skript.registerExpression(ExprVectorSphere.class, Vector.class, ExpressionType.SIMPLE, "[vector ]sphere with radius %number%(,| and) density %number%"); + Skript.registerExpression(ExprVectorSphereRand.class, Vector.class, ExpressionType.SIMPLE, "[vector ]random sphere with radius %number%(,| and) density %number%"); + } + + public static Biosphere getPlugin() { + return plugin; + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprDegToRad.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprDegToRad.java new file mode 100755 index 0000000..6fd6e86 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprDegToRad.java @@ -0,0 +1,49 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import org.bukkit.event.Event; +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; + +@Name("Degree to Radian") +@Description("Converts degree to radian") +@Examples({"#returns PI", "set {_num} to 180 in radian" }) +public class ExprDegToRad extends SimpleExpression{ + + private Expression degree; + + @Override + public Class getReturnType() { + return Number.class; + } + + @Override + public boolean isSingle() { + return true; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult arg3) { + degree = (Expression) expr[0]; + return true; + } + + @Override + public String toString(@Nullable Event e, boolean arg1) { + return degree.toString(e, arg1) + " in radian"; + } + + @Override + @Nullable + protected Number[] get(Event e) { + return new Double[]{ degree.getSingle(e).doubleValue() * Math.PI / 180.0}; + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprFrameFromEntity.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprFrameFromEntity.java new file mode 100644 index 0000000..e82a7a5 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprFrameFromEntity.java @@ -0,0 +1,57 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import io.github.bi0qaw.biosphere.util.VectorMath; +import org.bukkit.entity.Entity; +import org.bukkit.event.Event; +import io.github.bi0qaw.biosphere.util.Frame; + +@Name("Frame of Entity") +@Description("Gives the reference frame of an entity. The reference frame is used to get locations with respect to the entities orientation.") +@Examples({"set {_frame} to frame of player", + "show happy villager at player's head offset by vector 1, 2, 3 in {_frame}", + "#shows a happy villager particle at the location 1 in front, 2 above and 3 blocks to the right of the player's head", + "set {_circle::*} to circle with radius 1 and density 5", + "show happy villager at player's head offset by {_circle::*} in {_frame}", + "#shows a halo around the player's head that follows the movement of the head"}) +public class ExprFrameFromEntity extends SimpleExpression { + + private Expression entity; + + @Override + protected Frame[] get(Event event) { + Entity e = entity.getSingle(event); + float yaw = e.getLocation().getYaw(); + float pitch = e.getLocation().getPitch(); + yaw = VectorMath.fromNotchYaw(yaw); + pitch = VectorMath.fromNotchPitch(pitch); + return new Frame[] {new Frame().setFromYawPitch(yaw, pitch)}; + } + + @Override + public boolean isSingle() { + return true; + } + + @Override + public Class getReturnType() { + return Frame.class; + } + + @Override + public String toString(Event event, boolean b) { + return "frame of " + entity.toString(event, b); + } + + @Override + public boolean init(Expression[] expressions, int i, Kleenean kleenean, SkriptParser.ParseResult parseResult) { + entity = (Expression) expressions[0]; + return true; + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprFrameFromYawPitch.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprFrameFromYawPitch.java new file mode 100644 index 0000000..08fc283 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprFrameFromYawPitch.java @@ -0,0 +1,57 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import io.github.bi0qaw.biosphere.util.Frame; +import io.github.bi0qaw.biosphere.util.VectorMath; + +@Name("Frame from Yaw and Pitch") +@Description("Creates a reference frame from a yaw and a pitch.") +@Examples({"set {_frame} to frame with yaw 90 and pitch -45", + "set {_circle::*} to circle with radius 1 and density 5 in {_frame}", + "show happy villager at player's head offset by {_circle::*}", + "#shows a particle circle at the player's head with a yaw of 90 and pitch of -45"}) +public class ExprFrameFromYawPitch extends SimpleExpression { + + Expression yaw; + Expression pitch; + + @Override + protected Frame[] get(Event event) { + float y = yaw.getSingle(event).floatValue(); + float p = pitch.getSingle(event).floatValue(); + y = VectorMath.fromSkriptYaw(y); + p = VectorMath.fromSkriptPitch(p); + y = VectorMath.wrapYaw(y); + p = VectorMath.wrapPitch(p); + return new Frame[] {new Frame().setFromYawPitch(y, p)}; + } + + @Override + public boolean isSingle() { + return true; + } + + @Override + public Class getReturnType() { + return Frame.class; + } + + @Override + public String toString(Event event, boolean b) { + return "frame with yaw " + yaw.toString(event, b) + " and pitch " + pitch.toString(event, b); + } + + @Override + public boolean init(Expression[] expressions, int i, Kleenean kleenean, SkriptParser.ParseResult parseResult) { + yaw = (Expression) expressions[0]; + pitch = (Expression) expressions[1]; + return true; + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprFrameYawPitch.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprFrameYawPitch.java new file mode 100644 index 0000000..e5e221c --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprFrameYawPitch.java @@ -0,0 +1,109 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.classes.Changer; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.expressions.base.SimplePropertyExpression; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import io.github.bi0qaw.biosphere.util.Frame; +import io.github.bi0qaw.biosphere.util.VectorMath; + +@Name("Frame Yaw and Pitch") +@Description("Yaw and pitch of a frame. Mainly used to rotate or incline reference frames") +@Examples({"set {_frame} to frame from yaw 90 and pitch -45", + "set {_yaw} to frame yaw of {_frame} #{_yaw} is now equal to 90", + "add 90 to frame yaw of {_frame}", + "set {_yaw} to frame yaw of {_frame} #{_yaw} is now equal to 180"}) +public class ExprFrameYawPitch extends SimplePropertyExpression{ + + private int mark; + private final static String[] type = new String[] {"yaw", "pitch"}; + + + @Override + public boolean init(Expression[] expressions, int i, Kleenean kleenean, SkriptParser.ParseResult parseResult) { + super.init(expressions, i, kleenean, parseResult); + mark = parseResult.mark; + return true; + } + + + + @Override + protected String getPropertyName() { + return "frame " + type[mark]; + } + + + @Override + public Number convert(Frame frame) { + if (frame != null) { + switch (mark) { + case 0: + return VectorMath.skriptYaw(frame.getYaw()); + case 1: + return VectorMath.skriptPitch(frame.getPitch()); + default: + break; + } + } + return null; + } + + @Override + public Class getReturnType() { + return Number.class; + } + + @Override + public Class[] acceptChange(Changer.ChangeMode mode) { + if ((mode == Changer.ChangeMode.SET || mode == Changer.ChangeMode.ADD || mode == Changer.ChangeMode.REMOVE) && getExpr().isSingle() && Changer.ChangerUtils.acceptsChange(getExpr(), Changer.ChangeMode.SET, Frame.class)) + return new Class[] { Number.class }; + return null; + } + + @Override + public void change(Event e, Object[] delta, Changer.ChangeMode mode) { + Frame f = getExpr().getSingle(e); + if (f == null){ + return; + } + float v = ((Number) delta[0]).floatValue(); + float yaw = f.getYaw(); + float pitch = f.getPitch(); + switch (mode) { + case REMOVE: + v = -v; + case ADD: + if (mark == 0){ + yaw += v; + } else if (mark == 1){ + pitch -= v; //negative because skript pitch is upside down and we are adding the value to the non-skript pitch + } + yaw = VectorMath.wrapYaw(yaw); + pitch = VectorMath.wrapPitch(pitch); + f = f.setFromYawPitch(yaw, pitch); + getExpr().change(e, new Frame[]{f}, Changer.ChangeMode.SET); + break; + case SET: + if (mark == 0){ + yaw = VectorMath.wrapYaw(v); + yaw = VectorMath.fromSkriptYaw(yaw); + } else if (mark == 1){ + pitch = VectorMath.wrapPitch(v); + pitch = VectorMath.fromSkriptPitch(pitch); + } + f = f.setFromYawPitch(yaw, pitch); + getExpr().change(e, new Frame[]{f}, Changer.ChangeMode.SET); + break; + case REMOVE_ALL: + case DELETE: + case RESET: + assert false; + } + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationCircle.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationCircle.java new file mode 100755 index 0000000..4e5f6fa --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationCircle.java @@ -0,0 +1,59 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import io.github.bi0qaw.biosphere.util.VectorLib; +import org.bukkit.Location; +import org.bukkit.event.Event; + +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.util.Vector; + +@Name("Location Circle") +@Description("Creates a list of locations in the shape of a circle. The density value controls the amount of points. A higher number increases the amount of points. A density of 1 corresponds to 1 point per block.") +@Examples("show happy villager at circle at player's head with radius 1 and density 5") +public class ExprLocationCircle extends SimpleExpression { + private Expression locations; + private Expression radius; + private Expression density; + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult arg3) { + locations = (Expression) expr[0]; + radius = (Expression) expr[1]; + density = (Expression) expr[2]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "circle at " + locations.toString(arg0, arg1) + " with radius " + radius.toString(arg0, arg1) + " and density " + density.toString(arg0, arg1); + } + + @Override + @Nullable + protected Location[] get(Event e) { + double r = radius.getSingle(e).doubleValue(); + double d = density.getSingle(e).doubleValue(); + return LocationLib.getCircle(locations.getArray(e), r, d); + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationCube.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationCube.java new file mode 100755 index 0000000..2520743 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationCube.java @@ -0,0 +1,55 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import io.github.bi0qaw.biosphere.util.VectorLib; +import org.bukkit.Location; +import org.bukkit.event.Event; + +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.util.Vector; + +@Name("Location Cube") +@Description("Creates a list of locations in the shape of a cube (only the corners!). To get a list of locations including the cube edges use cube outline.") +@Examples("show happy villager at cube at player's head with radius 1") +public class ExprLocationCube extends SimpleExpression{ + private Expression locations; + private Expression radius; + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult arg3) { + locations = (Expression) expr[0]; + radius = (Expression) expr[1]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "cube at " + locations.toString(arg0, arg1) + " with radius " + radius.toString(arg0, arg1); + } + + @Override + @Nullable + protected Location[] get(Event e) { + double r = radius.getSingle(e).doubleValue(); + return LocationLib.getCube(locations.getArray(e), r); + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationCubeOutline.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationCubeOutline.java new file mode 100755 index 0000000..eb20855 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationCubeOutline.java @@ -0,0 +1,60 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import io.github.bi0qaw.biosphere.util.VectorLib; +import org.bukkit.Location; +import org.bukkit.event.Event; + +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.util.Vector; + +@Name("Location Cube Outline") +@Description("Creates a list of locations in the shape of a cube, including the corners and vertices of the cube. The density value controls the amount of points. A higher number increases the amount of points. A density of 1 corresponds to 1 point per block.") +@Examples("show happy villager at cube outline at player's head with radius 1 and density 5") +public class ExprLocationCubeOutline extends SimpleExpression{ + + private Expression locations; + private Expression radius; + private Expression density; + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult arg3) { + locations = (Expression) expr[0]; + radius = (Expression) expr[1]; + density = (Expression) expr[2]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "cube outline at " + locations.toString(arg0, arg1) + " with radius " + radius.toString(arg0, arg1) + " and density " + density.toString(arg0, arg1); + } + + @Override + @Nullable + protected Location[] get(Event e) { + double r = radius.getSingle(e).doubleValue(); + double d = density.getSingle(e).doubleValue(); + return LocationLib.getCubeOutline(locations.getArray(e), r, d); + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationCylinderLoc.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationCylinderLoc.java new file mode 100755 index 0000000..8f91d74 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationCylinderLoc.java @@ -0,0 +1,62 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import io.github.bi0qaw.biosphere.util.VectorMath; +import org.bukkit.Location; +import org.bukkit.event.Event; +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; + +@Name("Location Cylinder Coordinate") +@Description("Gives locations in a cylindrical coordinate system.") +@Examples({"set {_loc} to cylinder coordinate at player with radius 5, yaw 90 and height 5", + "#gives the location 5 blocks in the negative x-direction and 5 blocks above the player"}) +public class ExprLocationCylinderLoc extends SimpleExpression{ + private Expression locations; + private Expression radius; + private Expression yaw; + private Expression height; + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult arg3) { + locations = (Expression) expr[0]; + radius = (Expression) expr[1]; + yaw = (Expression) expr[2]; + height = (Expression) expr[3]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "cylinder coordinate at " + locations.toString(arg0, arg1) + " with radius " + radius.toString(arg0, arg1) + ", yaw " + yaw.toString(arg0, arg1) + " and height " + height.toString(arg0, arg1); + } + + @Override + @Nullable + protected Location[] get(Event e) { + double r = radius.getSingle(e).doubleValue(); + float y = yaw.getSingle(e).floatValue(); + double h = height.getSingle(e).doubleValue(); + y = VectorMath.fromSkriptYaw(y); + y = VectorMath.wrapYaw((y)); + return LocationLib.getCylindricalCoordinate(locations.getArray(e), r, y, h); + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationHelix.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationHelix.java new file mode 100755 index 0000000..7822dab --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationHelix.java @@ -0,0 +1,65 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import io.github.bi0qaw.biosphere.util.VectorLib; +import org.bukkit.Location; +import org.bukkit.event.Event; +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.util.Vector; + +@Name("Location Helix") +@Description("Creates a list of locations in the shape of a helix. The step parameter determines how many blocks the helix will go upwards in one rotation. The density value controls the amount of points. A higher number increases the amount of points. A density of 1 corresponds to 1 point per block.") +@Examples({"show happy villager at helix at player's head with radius 2, height 3, step 1 and density 5", + "#shows a helix at the player's head with a height of 3 blocks that makes a total of 3 rotations"}) +public class ExprLocationHelix extends SimpleExpression{ + + private Expression locations; + private Expression radius; + private Expression height; + private Expression step; + private Expression density; + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult arg3) { + locations = (Expression) expr[0]; + radius = (Expression) expr[1]; + height = (Expression) expr[2]; + step = (Expression) expr[3]; + density = (Expression) expr[4]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "helix at " + locations.toString(arg0, arg1) + " with radius " + radius.toString(arg0, arg1) + ", height " + height.toString(arg0, arg1) + ", step " + step.toString(arg0, arg1) + " and density " + density.toString(arg0, arg1); + } + + @Override + @Nullable + protected Location[] get(Event e) { + double r = radius.getSingle(e).doubleValue(); + double h = height.getSingle(e).doubleValue(); + double s = step.getSingle(e).doubleValue(); + double d = density.getSingle(e).doubleValue(); + return LocationLib.getHelix(locations.getArray(e), r, h, s, d); + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationLine.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationLine.java new file mode 100755 index 0000000..d271a36 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationLine.java @@ -0,0 +1,61 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import io.github.bi0qaw.biosphere.util.VectorLib; +import org.bukkit.Location; +import org.bukkit.event.Event; + +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.util.Vector; + +import java.util.ArrayList; +import java.util.Arrays; + +@Name("Location Line") +@Description("Creates a list of locations in the shape of a line. The density value controls the amount of points. A higher number increases the amount of points. A density of 1 corresponds to 1 point per block.") +@Examples("show happy villager at line between player's head and location 5 in front of player's head with density 5") +public class ExprLocationLine extends SimpleExpression{ + private Expression location1; + private Expression location2; + private Expression density; + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult arg3) { + location1 = (Expression) expr[0]; + location2 = (Expression) expr[1]; + density = (Expression) expr[2]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "line from " + location1.toString(arg0, arg1) + " to " + location2.toString(arg0, arg1); + } + + @Override + @Nullable + protected Location[] get(Event e) { + double d = density.getSingle(e).doubleValue(); + return LocationLib.getLine(location1.getArray(e), location2.getSingle(e), d); + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationLineLoc.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationLineLoc.java new file mode 100755 index 0000000..48581a6 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationLineLoc.java @@ -0,0 +1,61 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import io.github.bi0qaw.biosphere.util.TrigLib; +import io.github.bi0qaw.biosphere.util.VectorMath; +import org.bukkit.Location; +import org.bukkit.event.Event; + +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.util.Vector; + +@Name("Location Line Coordinate") +@Description("Gives a location on the line between two locations. The number parameter describes how far the point is from the first location. A value of 0 corresponds the the start (first location). A value of 1 corresponds to the end (location 2). 0.5 is the midpoint between the two locations.") +@Examples({"show happy villager at line coordinate 0.2 between player and location 10 in front of player", + "#shows a particle 2 blocks in front of the player"}) + +public class ExprLocationLineLoc extends SimpleExpression { + + private Expression point; + private Expression location1; + private Expression location2; + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return location1.isSingle(); + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult arg3) { + point = (Expression) expr[0]; + location1 = (Expression) expr[1]; + location2 = (Expression) expr[2]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "line coordinate " + point.toString(arg0, arg1) + " from " + location1.toString(arg0, arg1) + " to " + location2.toString(arg0, arg1); + } + + @Override + @Nullable + protected Location[] get(Event e) { + double p = point.getSingle(e).doubleValue(); + return LocationLib.getLineCoordinate(location1.getArray(e), location2.getSingle(e), p); + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationLinkAll.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationLinkAll.java new file mode 100755 index 0000000..3d76bbc --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationLinkAll.java @@ -0,0 +1,60 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import io.github.bi0qaw.biosphere.util.TrigLib; +import org.bukkit.Location; +import org.bukkit.event.Event; + +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; + +@Name("Location Link All") +@Description("Creates a list of locations linking multiple locations with lines. The density value controls the amount of points. A higher number increases the amount of points. A density of 1 corresponds to 1 point per block.") +@Examples({"set {_circle::*} to circle at player with radius 5 and density 0.2", + "#gets the points in the shape of a circle around the player", + "set {_linked::*} to {_circle::*} linked with density 2", + "#connects all points on the circle with lines", + "show happy villager at {_linked::*}"}) +public class ExprLocationLinkAll extends SimpleExpression{ + + private Expression locations; + private Expression density; + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult arg3) { + locations = (Expression) expr[0]; + density = (Expression) expr[1]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return locations.toString(arg0, arg1) + " linked with density " + density.toString(arg0, arg1); + } + + @Override + @Nullable + protected Location[] get(Event e) { + double d = density.getSingle(e).doubleValue(); + return LocationLib.linkAll(locations.getArray(e), d); + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationMidpoint.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationMidpoint.java new file mode 100755 index 0000000..21c4874 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationMidpoint.java @@ -0,0 +1,54 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import io.github.bi0qaw.biosphere.util.TrigLib; +import org.bukkit.Location; +import org.bukkit.event.Event; + +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; + +@Name("Location Midpoint") +@Description("Gives the midpoint between locations.") +@Examples({"show happy villager at midpoint between player and location 5 in front of player", + "#shows a particle at the location 2.5 in front of the player"}) +public class ExprLocationMidpoint extends SimpleExpression{ + + private Expression locations; + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return true; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult arg3) { + locations = (Expression) expr[0]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "midpoint of " + locations.toString(arg0, arg1); + } + + @Override + @Nullable + protected Location[] get(Event e) { + return new Location[] {LocationLib.midpoint(locations.getArray(e))}; + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationMove.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationMove.java new file mode 100755 index 0000000..a943e2d --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationMove.java @@ -0,0 +1,59 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import org.bukkit.Location; +import org.bukkit.event.Event; +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.util.Vector; + +@Name("Location Move") +@Description("Moves a list of locations from one location to another.") +@Examples({"set {_circle::*} to circle at player with radius 1 and density 5", + "set {_moved::*} to {_circle::*} with center player moved to location 5 in front of player", + "show happy villager at {_moved::*}"}) +public class ExprLocationMove extends SimpleExpression{ + + private Expression locations; + private Expression center; + private Expression point; + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return locations.isSingle(); + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult arg3) { + locations = (Expression) expr[0]; + center = (Expression) expr[1]; + point = (Expression) expr[2]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "move " + locations.toString(arg0, arg1) + " with center " + center.toString(arg0, arg1) + " to " + locations.toString(arg0, arg1); + } + + @Override + @Nullable + protected Location[] get(Event e) { + Vector delta = point.getSingle(e).toVector().subtract(center.getSingle(e).toVector()); + return LocationLib.offset(locations.getArray(e), delta); + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationOffset.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationOffset.java new file mode 100644 index 0000000..8c352eb --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationOffset.java @@ -0,0 +1,53 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import io.github.bi0qaw.biosphere.util.LocationLib; +import org.bukkit.Location; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; + +@Name("Location Offset") +@Description("Calculates the offset of locations from vectors.") +@Examples({"set {_circle::*} to circle with radius 1 and density 5", + "#creates a list of vectors in the shape of a circle", + "set {_offset::*} to player's head offset by {_circle::*}", + "#gives a list of locations around the player's head in the shape of a circle", + "show happy villager at {_offset::*}"}) +public class ExprLocationOffset extends SimpleExpression { + + private Expression locations; + private Expression vectors; + + @Override + public boolean isSingle() { + return locations.isSingle() && vectors.isSingle(); + } + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public String toString(Event event, boolean b) { + return locations.toString() + " offset by " + vectors.toString(); + } + + @Override + public boolean init(Expression[] expressions, int i, Kleenean kleenean, SkriptParser.ParseResult parseResult) { + locations = (Expression) expressions[0]; + vectors = (Expression) expressions[1]; + return true; + } + + @Override + protected Location[] get(Event event) { + return LocationLib.offset(locations.getArray(event), vectors.getAll(event)); + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationPolygon.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationPolygon.java new file mode 100755 index 0000000..7adf973 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationPolygon.java @@ -0,0 +1,60 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import io.github.bi0qaw.biosphere.util.TrigLib; +import org.bukkit.Location; +import org.bukkit.event.Event; + +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; + +@Name("Location Polygon") +@Description("Creates a list of locations in the shape of a polygon (only the corners!). If you want a polygon with edges use polygon outline.") +@Examples({"set {_polygon::*} to polygon at player with 3 points and radius 3", + "show happy villager at {_polygon::*}", + "#shows particles in the shape of a triangle at the player"}) +public class ExprLocationPolygon extends SimpleExpression{ + + private Expression locations; + private Expression points; + private Expression radius; + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult arg3) { + locations = (Expression) expr[0]; + points = (Expression) expr[1]; + radius = (Expression) expr[2]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "polygon at " + locations.toString(arg0, arg1) + " with " + points.toString(arg0, arg1) + " points and radius " + radius.toString(arg0, arg1); + } + + @Override + @Nullable + protected Location[] get(Event e) { + int p = points.getSingle(e).intValue(); + double r = radius.getSingle(e).doubleValue(); + return LocationLib.getPolygon(locations.getArray(e), p, r); + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationPolygonOutline.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationPolygonOutline.java new file mode 100755 index 0000000..35c8dcf --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationPolygonOutline.java @@ -0,0 +1,62 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import io.github.bi0qaw.biosphere.util.TrigLib; +import org.bukkit.Location; +import org.bukkit.event.Event; + +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; + +@Name("Location Polygon Outline") +@Description("Creates a list of locations in the shape of a polygon including corners and edges. The density value controls the amount of points. A higher number increases the amount of points. A density of 1 corresponds to 1 point per block.") +@Examples("show happy villager at polygon outline at player with 6 points, radius 3 and density 5") +public class ExprLocationPolygonOutline extends SimpleExpression{ + + private Expression locations; + private Expression points; + private Expression radius; + private Expression density; + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult arg3) { + locations = (Expression) expr[0]; + points = (Expression) expr[1]; + radius = (Expression) expr[2]; + density = (Expression) expr[3]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return null; + } + + @Override + @Nullable + protected Location[] get(Event e) { + int p = points.getSingle(e).intValue(); + double r = radius.getSingle(e).doubleValue(); + double d = density.getSingle(e).doubleValue(); + return LocationLib.getPolygonOutline(locations.getArray(e), p, r, d); + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationReflection.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationReflection.java new file mode 100755 index 0000000..c187b85 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationReflection.java @@ -0,0 +1,69 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import io.github.bi0qaw.biosphere.util.TrigLib; +import org.bukkit.Location; +import org.bukkit.event.Event; + +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.util.Vector; + +@Name("Location Reflection") +@Description("Mirrors locations in another location. The direction parameter applies an additional scaling for each axis.") +@Examples({"set {_point} to location 5 in front of player mirrored at player", + "show happy villager at {_point}", + "#shows a particle 5 blocks behind the player"}) +public class ExprLocationReflection extends SimpleExpression{ + private Expression locations; + private Expression point; + private Expression direction; + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return locations.isSingle(); + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult parseResult) { + locations = (Expression) expr[0]; + point = (Expression) expr[1]; + direction = (Expression) expr[2]; + return true; + } + + @Override + public String toString(@Nullable Event e, boolean arg1) { + if (direction == null) { + return locations.toString(e, arg1) + " reflected at " + point.toString(e, arg1); + } + else { + return locations.toString(e, arg1) + " reflected at " + point.toString(e, arg1) + " in direction " + direction.toString(e, arg1); + } + } + + @Override + @Nullable + protected Location[] get(Event e) { + if(direction == null) { + return LocationLib.pointReflection(locations.getArray(e), point.getSingle(e)); + } + else { + return LocationLib.reflection(locations.getArray(e), point.getSingle(e), direction.getSingle(e)); + } + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationRotXYZ.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationRotXYZ.java new file mode 100755 index 0000000..dc2c06c --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationRotXYZ.java @@ -0,0 +1,70 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import org.bukkit.Location; +import org.bukkit.event.Event; + +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; + +@Name("Location Rotate XYZ") +@Description("Rotates locations around a rotation center and rotation axis by an angle.") +@Examples({"set {_point} to location 5 in front of player", + "set {_point} to {_point} rotated around y-axis at player with angle 180"}) +public class ExprLocationRotXYZ extends SimpleExpression{ + + private Expression locations; + private Expression center; + private Expression angle; + private int axis; + private String[] axisStr = new String[] {"x", "y", "z"}; + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return locations.isSingle(); + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, SkriptParser.ParseResult parseResult) { + locations = (Expression) expr[0]; + center = (Expression) expr[1]; + angle = (Expression) expr[2]; + axis = parseResult.mark; + return true; + } + + @Override + public String toString(@Nullable Event e, boolean arg1) { + return locations.toString(e, arg1) + " rotated around " + axisStr[axis] + "-axis at " + center.toString(e, arg1) + " with angle " + angle.toString(e, arg1); + } + + @Override + @Nullable + protected Location[] get(Event e) { + float a = angle.getSingle(e).floatValue(); + a = -a; //Skript uses clockwise and LocationLib anti-clockwise rotation + if (axis == 1) { + return LocationLib.rotateX(locations.getArray(e), center.getSingle(e), a); + } + else if (axis == 2) { + return LocationLib.rotateY(locations.getArray(e), center.getSingle(e), a); + } + else { + return LocationLib.rotateZ(locations.getArray(e), center.getSingle(e), a); + } + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationRotate.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationRotate.java new file mode 100755 index 0000000..29a222f --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationRotate.java @@ -0,0 +1,66 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import org.bukkit.Location; +import org.bukkit.event.Event; + +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.util.Vector; + +@Name("Location Rotate") +@Description("Rotates locations around a rotation center and a rotation axis by an angle.") +@Examples({"set {_point} to location 5 in front of player", + "set {_point} to {_point} rotated around vector 0, 1, 0 at player with angle 180", + "show happy villager at {_point}", + "#shows a particle at the location 5 behind the player", + "#vector 0, 1, 0 creates a vector along the y-axis, which is used as the rotation axis"}) +public class ExprLocationRotate extends SimpleExpression{ + + private Expression locations; + private Expression axis; + private Expression center; + private Expression angle; + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return locations.isSingle(); + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int matchedPattern, Kleenean arg2, SkriptParser.ParseResult parseResult) { + locations = (Expression) expr[0]; + axis = (Expression) expr[1]; + center = (Expression) expr[2]; + angle = (Expression) expr[3]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return locations.toString(arg0, arg1) + " rotated around " + axis.toString(arg0, arg1) + " at " + center.toString(arg0, arg1) + " with angle " + angle.toString(arg0, arg1); + } + + @Override + @Nullable + protected Location[] get(Event e) { + float a = angle.getSingle(e).floatValue(); + a = -a; //Skript uses clockwise and LocationLib anti-clockwise rotation + Vector ax = axis.getSingle(e).clone().normalize(); + return LocationLib.rotate(locations.getArray(e), center.getSingle(e), ax, a); + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationScale.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationScale.java new file mode 100755 index 0000000..734b067 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationScale.java @@ -0,0 +1,78 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import io.github.bi0qaw.biosphere.util.TrigLib; +import org.bukkit.Location; +import org.bukkit.event.Event; + +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.util.Vector; + +@Name("Location Scale") +@Description("Scales locations from a center by a scaling factor. The direction parameter adds an additional scaling along the x-, y- and z-axis.") +@Examples({"set {_circle::*} to circle at player with radius 1 and density 5", + "set {_bigcircle::*} to {_circle::*} scaled at player by 2", + "#Makes the circle 2 times bigger.", + "set {_ellipse::*} to {_circle::*} scaled at player by 1 in direction vector 1, 1, 2", + "#Makes the circle 2 times bigger in the z-direction but does not change the x- and y-coordinates. This creates an ellipse.", + "show happy villager at {_circle::*}", + "show happy villager at {_bigcircle::*}", + "show happy villager at {_ellipse::*}"}) +public class ExprLocationScale extends SimpleExpression{ + + private Expression locations; + private Expression center; + private Expression factor; + private Expression direction; + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return locations.isSingle(); + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult parseResult) { + locations = (Expression) expr[0]; + center = (Expression) expr[1]; + factor = (Expression) expr[2]; + direction = (Expression) expr[3]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + if (direction == null) { + return locations.toString(arg0, arg1) + " scaled with center " + center.toString(arg0, arg1) + " by " + factor.toString(arg0, arg1); + } + else { + return locations.toString(arg0, arg1) + " scaled with center " + center.toString(arg0, arg1) + " by " + factor.toString(arg0, arg1) + " in direction " + direction.toString(arg0, arg1); + } + } + + @Override + @Nullable + protected Location[] get(Event e) { + double f = factor.getSingle(e).doubleValue(); + if (direction == null) { + return LocationLib.scale(locations.getArray(e), center.getSingle(e), f); + } + else { + return LocationLib.scaleDirectional(locations.getArray(e), center.getSingle(e), direction.getSingle(e), f); + } + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationSphere.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationSphere.java new file mode 100755 index 0000000..d3cb0aa --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationSphere.java @@ -0,0 +1,59 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import io.github.bi0qaw.biosphere.util.TrigLib; +import org.bukkit.Location; +import org.bukkit.event.Event; + +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; + +@Name("Location Sphere") +@Description("Creates a list of locations in the shape of a sphere. The density value controls the amount of points. A higher number increases the amount of points. A density of 1 corresponds to 1 point per block.") +@Examples("show happy villager at sphere at player with radius 1 and density 5") +public class ExprLocationSphere extends SimpleExpression{ + + private Expression locations; + private Expression radius; + private Expression density; + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult arg3) { + locations = (Expression) expr[0]; + radius = (Expression) expr[1]; + density = (Expression) expr[2]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "sphere at " + locations.toString(arg0, arg1) + " with radius " + radius.toString(arg0, arg1) + " and density " + density.toString(arg0, arg1); + } + + @Override + @Nullable + protected Location[] get(Event e) { + double r = radius.getSingle(e).doubleValue(); + double d = density.getSingle(e).doubleValue(); + return LocationLib.getSphere(locations.getArray(e), r, d); + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationSphereLoc.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationSphereLoc.java new file mode 100755 index 0000000..0a491d0 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationSphereLoc.java @@ -0,0 +1,69 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import io.github.bi0qaw.biosphere.util.TrigLib; +import io.github.bi0qaw.biosphere.util.VectorMath; +import org.bukkit.Location; +import org.bukkit.event.Event; + +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; + +@Name("Location Sphere Coordinate") +@Description("Gives locations in a spherical coordinate system.") +@Examples({"set {_loc} to spherical coordinate at player with radius 3, yaw 90 and pitch -45", + "show happy villager at {_loc}", + "#shows a particle at the location 3 in the negative x direction (because of yaw = 90) and a little bit above the player (because of pitch = -45)"}) +public class ExprLocationSphereLoc extends SimpleExpression{ + + private Expression locations; + private Expression radius; + private Expression yaw; + private Expression pitch; + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return locations.isSingle(); + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult arg3) { + locations = (Expression) expr[0]; + radius = (Expression) expr[1]; + yaw = (Expression) expr[2]; + pitch = (Expression) expr[3]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "sphere coordinate at " + locations.toString(arg0, arg1) + " with radius " + radius.toString(arg0, arg1) + ", yaw " + yaw.toString(arg0, arg1) + " and pitch " + pitch.toString(arg0, arg1); + } + + @Override + @Nullable + protected Location[] get(Event e) { + double r = radius.getSingle(e).doubleValue(); + float y = yaw.getSingle(e).floatValue(); + float p = pitch.getSingle(e).floatValue(); + y = VectorMath.fromSkriptYaw(y); + y = VectorMath.wrapYaw(y + 180); + p = VectorMath.fromSkriptPitch(p); + p = VectorMath.wrapPitch(p); + p = p - 90; + return LocationLib.getSphericalCoordinates(locations.getArray(e), r, y, p); + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationSphereRand.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationSphereRand.java new file mode 100755 index 0000000..64cac2e --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprLocationSphereRand.java @@ -0,0 +1,58 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import io.github.bi0qaw.biosphere.util.LocationLib; +import io.github.bi0qaw.biosphere.util.TrigLib; +import org.bukkit.Location; +import org.bukkit.event.Event; + +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; + +@Name("Location Random Sphere") +@Description("Creates a list of locations in the shape of a sphere. The locations are randomly distributed on the sphere surface.") +@Examples("show happy villager at random sphere at player with radius 1 and density 5") +public class ExprLocationSphereRand extends SimpleExpression { + + private Expression locations; + private Expression radius; + private Expression density; + + @Override + public Class getReturnType() { + return Location.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult arg3) { + locations = (Expression) expr[0]; + radius = (Expression) expr[1]; + density = (Expression) expr[2]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "random sphere at " + locations.toString(arg0, arg1) + " with density " + density.toString(arg0, arg1); + } + + @Override + @Nullable + protected Location[] get(Event e) { + double r = radius.getSingle(e).doubleValue(); + double d = density.getSingle(e).doubleValue(); + return LocationLib.getRandomSphere(locations.getArray(e), r, d); + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprRadToDeg.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprRadToDeg.java new file mode 100755 index 0000000..34df6bf --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprRadToDeg.java @@ -0,0 +1,50 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import org.bukkit.event.Event; +import javax.annotation.Nullable; + +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; + +@Name("Radian to Degree") +@Description("Converts radian to degree") +@Examples({"set {_num} to 3.14159 in degree", + "#{_num} is now equal to 180"}) +public class ExprRadToDeg extends SimpleExpression{ + + private Expression radian; + + @Override + public Class getReturnType() { + return Number.class; + } + + @Override + public boolean isSingle() { + return true; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, ParseResult arg3) { + radian = (Expression) expr[0]; + return true; + } + + @Override + public String toString(@Nullable Event e, boolean arg1) { + return radian.toString(e, arg1) + " in degree"; + } + + @Override + @Nullable + protected Number[] get(Event e) { + return new Double[]{ radian.getSingle(e).doubleValue() * 180 / Math.PI}; + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorCircle.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorCircle.java new file mode 100644 index 0000000..dde7407 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorCircle.java @@ -0,0 +1,55 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.util.VectorLib; + +import javax.annotation.Nullable; + +@Name("Vector Circle") +@Description("Creates a list of vectors in the shape of a circle") +@Examples({"set {_circle::*} to circle with radius 1 and density 5", + "show happy villager at player's head offset by {_circle::*}"}) +public class ExprVectorCircle extends SimpleExpression { + private Expression radius; + private Expression density; + + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, SkriptParser.ParseResult arg3) { + radius = (Expression) expr[0]; + density = (Expression) expr[1]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "circle with radius " + radius.toString(arg0, arg1) + " and density " + density.toString(arg0, arg1); + } + + @Override + @Nullable + protected Vector[] get(Event e) { + int n = (int) (radius.getSingle(e).doubleValue() * 2 * Math.PI * density.getSingle(e).doubleValue()); + double r = radius.getSingle(e).doubleValue(); + return VectorLib.getPolygon(n, r); + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorCube.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorCube.java new file mode 100644 index 0000000..57b1514 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorCube.java @@ -0,0 +1,52 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.util.VectorLib; + +import javax.annotation.Nullable; + +@Name("Vector Cube") +@Description("Creates a list of vectors in the shape of a cube (only the corners!). To get a list of locations including the cube edges use cube outline.") +@Examples({"set {_cube::*} to cube with radius 1", + "show happy villager at player's head offset by {_cube::*}"}) +public class ExprVectorCube extends SimpleExpression{ + + private Expression radius; + + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, SkriptParser.ParseResult arg3) { + radius = (Expression) expr[0]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "cube with radius " + radius.toString(arg0, arg1); + } + + @Override + @Nullable + protected Vector[] get(Event e) { + double r = radius.getSingle(e).doubleValue(); + return VectorLib.getCube(r); + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorCubeOutline.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorCubeOutline.java new file mode 100644 index 0000000..18369a1 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorCubeOutline.java @@ -0,0 +1,53 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.util.VectorLib; + +import javax.annotation.Nullable; + +@Name("Vector Cube Outline") +@Description("Creates a list of vectors in the shape of a polygon including corners and edges. The density value controls the amount of points. A higher number increases the amount of points. A density of 1 corresponds to 1 point per block.") +@Examples({"set {_cubeoutline::*} to cube outline with radius 1 and density 5", + "show happy villager at player's head offset by {_cubeoutline::*}"}) +public class ExprVectorCubeOutline extends SimpleExpression { + private Expression radius; + private Expression density; + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, SkriptParser.ParseResult arg3) { + radius = (Expression) expr[0]; + density = (Expression) expr[1]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "cube outline with radius " + radius.toString(arg0, arg1) + " and density " + density.toString(arg0, arg1); + } + + @Override + @Nullable + protected Vector[] get(Event e) { + double r = radius.getSingle(e).doubleValue(); + double d = density.getSingle(e).doubleValue(); + return VectorLib.getCubeOutline(r, d); + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorHelix.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorHelix.java new file mode 100644 index 0000000..4792a35 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorHelix.java @@ -0,0 +1,60 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.util.VectorLib; + +import javax.annotation.Nullable; + +@Name("Vector Helix") +@Description("Creates a list of vectors in the shape of a helix. The step parameter determines how many blocks the helix will go upwards in one rotation. The density value controls the amount of points. A higher number increases the amount of points. A density of 1 corresponds to 1 point per block.") +@Examples({"set {_helix::*} to helix with radius 2, height 3, step 1 and density 5", + "show happy villager at player's head offset by {_helix::*}"}) +public class ExprVectorHelix extends SimpleExpression{ + private Expression radius; + private Expression height; + private Expression step; + private Expression density; + + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, SkriptParser.ParseResult arg3) { + radius = (Expression) expr[0]; + height = (Expression) expr[1]; + step = (Expression) expr[2]; + density = (Expression) expr[3]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "helix with radius " + radius.toString(arg0, arg1) + ", height " + height.toString(arg0, arg1) + ", step " + step.toString(arg0, arg1) + " and density " + density.toString(arg0, arg1); + } + + @Override + @Nullable + protected Vector[] get(Event e) { + double r = radius.getSingle(e).doubleValue(); + double h = height.getSingle(e).doubleValue(); + double s = step.getSingle(e).doubleValue(); + double d = density.getSingle(e).doubleValue(); + return VectorLib.getHelix(r, h, s, d); + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorInFrame.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorInFrame.java new file mode 100644 index 0000000..0e74360 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorInFrame.java @@ -0,0 +1,59 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.util.Frame; + +@Name("Vector in Frame") +@Description("Converts a vector in a reference frame to the standard Minecraft coordinates.") +@Examples({"set {_vector} to vector 5, 0, 0 in frame of player", + "#Gives a vector that points 5 blocks in the player's direction", + "set {_circle::*} to circle with radius 1 and density 5 in frame of player", + "#Gives a list of vectors in the shape of a circle around the player's head with the correct yaw and pitch"}) +public class ExprVectorInFrame extends SimpleExpression { + + private Expression vectors; + private Expression frame; + + @Override + protected Vector[] get(Event event) { + Frame f = frame.getSingle(event); + Vector[] v = vectors.getArray(event); + Vector[] transformed = new Vector[v.length]; + int i = 0; + for (Vector vec: v) { + transformed[i] = f.transform(vec.clone()); + i++; + } + return transformed; + } + + @Override + public boolean isSingle() { + return vectors.isSingle(); + } + + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public String toString(Event event, boolean b) { + return vectors.toString(event, b) + " in " + frame.toString(event, b); + } + + @Override + public boolean init(Expression[] expressions, int i, Kleenean kleenean, SkriptParser.ParseResult parseResult) { + vectors = (Expression) expressions[0]; + frame = (Expression) expressions[1]; + return true; + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorLine.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorLine.java new file mode 100644 index 0000000..0cb656e --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorLine.java @@ -0,0 +1,56 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.util.VectorLib; + +import javax.annotation.Nullable; + +@Name("Vector Line") +@Description("Creates a list of vectors in the shape of a line. The density value controls the amount of points. A higher number increases the amount of points. A density of 1 corresponds to 1 point per block.") +@Examples({"set {_line::*} to line with length 1 and density 5", + "show happy villager at player offset by {_line::*}", + "#shows a particle line that goes from the player to the location 1 block in the x-direction from the player"}) +public class ExprVectorLine extends SimpleExpression { + + private Expression length; + private Expression density; + + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, SkriptParser.ParseResult arg3) { + length = (Expression) expr[0]; + density = (Expression) expr[1]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "line with length " + length.toString(arg0, arg1) + " and density " + density.toString(arg0, arg1); + } + + @Override + @Nullable + protected Vector[] get(Event e) { + double l = length.getSingle(e).doubleValue(); + double d = density.getSingle(e).doubleValue(); + return VectorLib.getLine(l, d); + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorLineBetweenVectors.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorLineBetweenVectors.java new file mode 100644 index 0000000..9ac8f7a --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorLineBetweenVectors.java @@ -0,0 +1,53 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import io.github.bi0qaw.biosphere.util.VectorLib; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; + +@Name("Vector Line Between Vectors") +@Description("Creates a list of vectors in the shape of a line between the two given vectors. The density value controls the amount of points. A higher number increases the amount of points. A density of 1 corresponds to 1 point per block.") +@Examples({"set {_line::*} to vector line between vector 1, 2, 3 and vector 3, 2, 1", + "show happy villager at player offset by {_line::*}"}) +public class ExprVectorLineBetweenVectors extends SimpleExpression { + + private Expression vector1; + private Expression vector2; + private Expression density; + + @Override + public boolean isSingle() { + return false; + } + + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public String toString(Event event, boolean b) { + return "vector line between " + vector1.toString(event, b) + " and " + vector2.toString(event, b) + " with density " + density.toString(event, b); + } + + @Override + public boolean init(Expression[] expressions, int i, Kleenean kleenean, SkriptParser.ParseResult parseResult) { + vector1 = (Expression) expressions[0]; + vector2 = (Expression) expressions[1]; + density = (Expression) expressions[2]; + return true; + } + + @Override + protected Vector[] get(Event event) { + double d = density.getSingle(event).doubleValue(); + return VectorLib.getLine(vector1.getSingle(event), vector2.getSingle(event), d); + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorLinkAll.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorLinkAll.java new file mode 100644 index 0000000..fe6fdb4 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorLinkAll.java @@ -0,0 +1,55 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.util.VectorLib; + +import javax.annotation.Nullable; + +@Name("Vector Link All") +@Description("Creates a list of vectors linking multiple vectors with lines. The density value controls the amount of points. A higher number increases the amount of points. A density of 1 corresponds to 1 point per block.") +@Examples({"set {_circle::*} to circle with radius 5 and density 0.2", + "set {_linked::*} to {_circle::*} linked with density 2", + "#connects all points on the circle with lines", + "show happy villager at player offset by {_linked::*}"}) +public class ExprVectorLinkAll extends SimpleExpression{ + + private Expression vectors; + private Expression density; + + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, SkriptParser.ParseResult arg3) { + vectors = (Expression) expr[0]; + density = (Expression) expr[1]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "all vectors " + vectors.toString() + " linked with density " + density.toString(); + } + + @Override + @Nullable + protected Vector[] get(Event e) { + return VectorLib.linkAll(vectors.getArray(e), density.getSingle(e).doubleValue()); + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorMidpoint.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorMidpoint.java new file mode 100644 index 0000000..90f02fe --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorMidpoint.java @@ -0,0 +1,52 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.util.VectorLib; + +import javax.annotation.Nullable; + +@Name("Vector Midpoint") +@Description("Gives the midpoint between vectors.") +@Examples({"set {_circle::*} to circle with radius 1 and density 5", + "set {_midpoint} to vector midpoint of {_circle::*}", + "#{_midpoint} is a vector pointing to the center of the circle"}) +public class ExprVectorMidpoint extends SimpleExpression { + + private Expression vectors; + + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public boolean isSingle() { + return true; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, SkriptParser.ParseResult arg3) { + vectors = (Expression) expr[0]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "vector midpoint of " + vectors.toString(arg0, arg1); + } + + @Override + @Nullable + protected Vector[] get(Event e) { + return new Vector[] {VectorLib.midpoint(vectors.getArray(e))}; + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorMove.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorMove.java new file mode 100644 index 0000000..ea73afe --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorMove.java @@ -0,0 +1,59 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.util.VectorLib; + +import javax.annotation.Nullable; + +@Name("Vector Offset") +@Description("Offset vectors by other vectors.") +@Examples({"set {_circle::*} to circle with radius 1 and density 5", + "set {_offset::*} to {_circle::*} offset by vector 1, 2, 3", + "#Moves the circle from the origin (vector 0, 0, 0) to the position at 1, 2, 3"}) +public class ExprVectorMove extends SimpleExpression { + + private Expression vectors; + private Expression offset; + + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public boolean isSingle() { + return vectors.isSingle(); + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, SkriptParser.ParseResult arg3) { + vectors = (Expression) expr[0]; + offset = (Expression) expr[1]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return vectors.toString() + " moved by " + offset.toString(); + } + + @Override + @Nullable + protected Vector[] get(Event e) { + Vector[] o = offset.getAll(e); + Vector totalOffset = new Vector(); + for (Vector v: o) { + totalOffset.add(v); + } + return VectorLib.offset(vectors.getArray(e).clone(), totalOffset); + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorPolygon.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorPolygon.java new file mode 100644 index 0000000..09e80bd --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorPolygon.java @@ -0,0 +1,56 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.util.VectorLib; + +import javax.annotation.Nullable; + +@Name("Vector Polygon") +@Description("Creates a list of vectors in the shape of a polygon (only the corners!). If you want a polygon with edges use polygon outline.") +@Examples({"set {_polygon::*} to polygon with 3 points and radius 3", + "show happy villager at player offset by {_polygon::*}", + "#shows particles in the shape of a triangle at the player"}) +public class ExprVectorPolygon extends SimpleExpression{ + + private Expression vertices; + private Expression radius; + + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, SkriptParser.ParseResult arg3) { + vertices = (Expression) expr[0]; + radius = (Expression) expr[1]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "vector polygon with " + vertices.toString() + " vertices and radius " + radius.toString(); + } + + @Override + @Nullable + protected Vector[] get(Event e) { + int p = vertices.getSingle(e).intValue(); + double r = radius.getSingle(e).doubleValue(); + return VectorLib.getPolygon(p, r); + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorPolygonOutline.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorPolygonOutline.java new file mode 100644 index 0000000..7a23364 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorPolygonOutline.java @@ -0,0 +1,60 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.util.VectorLib; + +import javax.annotation.Nullable; + +@Name("Vector Polygon Outline") +@Description("Creates a list of vectors in the shape of a polygon including corners and edges. The density value controls the amount of points. A higher number increases the amount of points. A density of 1 corresponds to 1 point per block.") +@Examples({"set {_polyoutline::*} to polygon outline with 6 points, radius 3 and density 5", + "show happy villager at player offset by {_polygonoutline::*}", + "#shows particles in the shape of a hexagon (6 points) at the player"}) +public class ExprVectorPolygonOutline extends SimpleExpression { + + private Expression vertices; + private Expression radius; + private Expression density; + + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, SkriptParser.ParseResult arg3) { + vertices = (Expression) expr[0]; + radius = (Expression) expr[1]; + density = (Expression) expr[2]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "polygon outline with " + vertices.toString(arg0, arg1) + " vertices, radius " + radius.toString(arg0, arg1) + " and density " + density.toString(arg0, arg1); + } + + @Override + @Nullable + protected Vector[] get(Event e) { + int v = vertices.getSingle(e).intValue(); + double r = radius.getSingle(e).doubleValue(); + double d = density.getSingle(e).doubleValue(); + return VectorLib.getPolygonOutline(v, r, d); + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorReflection.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorReflection.java new file mode 100644 index 0000000..b2c960c --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorReflection.java @@ -0,0 +1,60 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.util.VectorLib; + +import javax.annotation.Nullable; + +@Name("Vector Reflection") +@Description("Mirrors vectors in another vector. The direction parameter applies an additional scaling for each axis.") +@Examples({"set {_point} to vector 1, 2, 3 mirrored at vector 0, 0, 0", + "#{_point} is now equal to the vector -1, -2, -3"}) +public class ExprVectorReflection extends SimpleExpression { + + private Expression vectors; + private Expression center; + private Expression direction; + + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public boolean isSingle() { + return vectors.isSingle(); + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, SkriptParser.ParseResult parseResult) { + vectors = (Expression) expr[0]; + center = (Expression) expr[1]; + direction = (Expression) expr[2]; + return true; + } + + @Override + public String toString(@Nullable Event e, boolean arg1) { + return vectors.toString() + " mirrored at " + center.toString() + " in direction " + direction.toString(); + } + + @Override + @Nullable + protected Vector[] get(Event e) { + if (direction == null) { + return VectorLib.pointReflection(vectors.getArray(e).clone(), center.getSingle(e)); + } + else { + return VectorLib.reflection(vectors.getArray(e).clone(), center.getSingle(e), direction.getSingle(e)); + } + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorRotate.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorRotate.java new file mode 100644 index 0000000..c0f4775 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorRotate.java @@ -0,0 +1,62 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.Bukkit; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.util.VectorLib; + +import javax.annotation.Nullable; + +@Name("Vector Rotate") +@Description("Rotates vectors around a rotation center and a rotation axis by an angle.") +@Examples({"set {_point} to vector 1, 2, 3", + "set {_point} to {_point} rotated around vector 0, 1, 0 at player with angle 180", + "#{_point} is now equal to the vector -1, 2, -3", + "#vector 0, 1, 0 creates a vector along the y-axis, which is used as the rotation axis"}) +public class ExprVectorRotate extends SimpleExpression { + + private Expression vectors; + private Expression axis; + private Expression angle; + + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public boolean isSingle() { + return vectors.isSingle(); + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int matchedPattern, Kleenean arg2, SkriptParser.ParseResult parseResult) { + vectors = (Expression) expr[0]; + axis = (Expression) expr[1]; + angle = (Expression) expr[2]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return vectors.toString(arg0, arg1) + " rotated around " + axis.toString(arg0, arg1) + " with angle " + angle.toString(arg0, arg1); + } + + @Override + @Nullable + protected Vector[] get(Event e) { + float a = angle.getSingle(e).floatValue(); + a = -a; //Skript uses clockwise and VectorMath anti-clockwise rotation + Vector ax = axis.getSingle(e).clone().normalize(); + return VectorLib.rotate(vectors.getArray(e).clone(), ax, a); + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorRotateXYZ.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorRotateXYZ.java new file mode 100644 index 0000000..3d750cf --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorRotateXYZ.java @@ -0,0 +1,68 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.util.VectorLib; + +import javax.annotation.Nullable; + +@Name("Vector Rotate XYZ") +@Description("Rotates vectors around a rotation center and rotation axis by an angle.") +@Examples({"set {_point} to vector 1, 2, 3", + "set {_point} to {_point} rotated around y-axis at player with angle 180", + "#{_point} is now equal to the vector -1, 2, -3"}) +public class ExprVectorRotateXYZ extends SimpleExpression { + + private Expression vectors; + private Expression angle; + private int axis; + private static final String[] axisStr = new String[] {"x", "y", "z"}; + + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public boolean isSingle() { + return vectors.isSingle(); + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, SkriptParser.ParseResult parseResult) { + vectors = (Expression) expr[0]; + angle = (Expression) expr[1]; + axis = parseResult.mark; + return true; + } + + @Override + public String toString(@Nullable Event e, boolean arg1) { + return vectors.toString(e, arg1) + " rotated around " + axisStr[axis] + "-axis with angle " + angle.toString(e, arg1); + } + + @Override + @Nullable + protected Vector[] get(Event e) { + float a = angle.getSingle(e).floatValue(); + a = -a; //Skript uses clockwise and VectorMath anti-clockwise rotation + if (axis == 1) { + return VectorLib.rotateX(vectors.getArray(e).clone(), a); + } + else if (axis == 2) { + return VectorLib.rotateY(vectors.getArray(e).clone(), a); + } + else { + return VectorLib.rotateZ(vectors.getArray(e).clone(), a); + } + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorScale.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorScale.java new file mode 100644 index 0000000..61b4baa --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorScale.java @@ -0,0 +1,68 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.util.VectorLib; + +import javax.annotation.Nullable; + +@Name("Vector Scale") +@Description("Scales vectors by a scaling factor and an optional direction. The direction parameter adds an additional scaling along the x-, y- and z-axis.") +@Examples({"set {_circle::*} to circle with radius 1 and density 5", + "set {_bigcircle::*} to {_circle::*} scaled by 2", + "#Makes the circle 2 times bigger.", + "set {_ellipse::*} to {_circle::*} scaled by 1 in direction vector 1, 1, 2", + "#Makes the circle 2 times bigger in the z-direction but does not change the x- and y-coordinates. This creates an ellipse.", + "show happy villager at player offset by {_circle::*}", + "show happy villager at player offset by {_bigcircle::*}", + "show happy villager at player offset by {_ellipse::*}"}) + +public class ExprVectorScale extends SimpleExpression { + + private Expression vectors; + private Expression factor; + private Expression direction; + + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public boolean isSingle() { + return vectors.isSingle(); + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, SkriptParser.ParseResult parseResult) { + vectors = (Expression) expr[0]; + factor = (Expression) expr[1]; + direction = (Expression) expr[2]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return vectors.toString() + " scaled by " + factor.toString() + " in direction " + direction.toString(); + } + + @Override + @Nullable + protected Vector[] get(Event e) { + double f = factor.getSingle(e).doubleValue(); + if (direction == null) { + return VectorLib.scale(vectors.getArray(e).clone(), f); + } + else { + return VectorLib.scaleDirectional(vectors.getArray(e).clone(), direction.getSingle(e), f); + } + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorSphere.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorSphere.java new file mode 100644 index 0000000..0d694cb --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorSphere.java @@ -0,0 +1,55 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.util.VectorLib; + +import javax.annotation.Nullable; + +@Name("Vector Sphere") +@Description("Creates a list of vectors in the shape of a sphere. The density value controls the amount of points. A higher number increases the amount of points. A density of 1 corresponds to 1 point per block.") +@Examples({"set {_sphere::*} to sphere with radius 1 and density 5", + "show happy villager at player offset by {_sphere::*}"}) +public class ExprVectorSphere extends SimpleExpression { + + private Expression radius; + private Expression density; + + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, SkriptParser.ParseResult arg3) { + radius = (Expression) expr[0]; + density = (Expression) expr[1]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "sphere with radius " + radius.toString() + " and density " + density.toString(); + } + + @Override + @Nullable + protected Vector[] get(Event e) { + double r = radius.getSingle(e).doubleValue(); + double d = density.getSingle(e).doubleValue(); + return VectorLib.getSphere(r, d); + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorSphereRand.java b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorSphereRand.java new file mode 100644 index 0000000..2274d21 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/expression/ExprVectorSphereRand.java @@ -0,0 +1,55 @@ +package io.github.bi0qaw.biosphere.expression; + +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.bukkit.util.Vector; +import io.github.bi0qaw.biosphere.util.VectorLib; + +import javax.annotation.Nullable; + +@Name("Vector Random Sphere") +@Description("Creates a list of vectors in the shape of a sphere. The locations are randomly distributed on the sphere surface.") +@Examples({"set {_sphere::*} to random sphere with radius 1 and density 5", + "show happy villager at player offset by {_sphere::*}"}) +public class ExprVectorSphereRand extends SimpleExpression { + + private Expression radius; + private Expression density; + + @Override + public Class getReturnType() { + return Vector.class; + } + + @Override + public boolean isSingle() { + return false; + } + + @SuppressWarnings("unchecked") + @Override + public boolean init(Expression[] expr, int arg1, Kleenean arg2, SkriptParser.ParseResult arg3) { + radius = (Expression) expr[0]; + density = (Expression) expr[1]; + return true; + } + + @Override + public String toString(@Nullable Event arg0, boolean arg1) { + return "random sphere with radius " + radius.toString() + " and density " + density.toString(); + } + + @Override + @Nullable + protected Vector[] get(Event e) { + double r = radius.getSingle(e).doubleValue(); + double d = density.getSingle(e).doubleValue(); + return VectorLib.getRandomSphere(r, d); + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/type/FrameType.java b/src/main/java/io/github/bi0qaw/biosphere/type/FrameType.java new file mode 100644 index 0000000..c71f3b8 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/type/FrameType.java @@ -0,0 +1,114 @@ +package io.github.bi0qaw.biosphere.type; + +import ch.njol.skript.Skript; +import ch.njol.skript.classes.ClassInfo; +import ch.njol.skript.classes.Parser; +import ch.njol.skript.classes.Serializer; +import ch.njol.skript.expressions.base.EventValueExpression; +import ch.njol.skript.lang.ParseContext; +import ch.njol.skript.registrations.Classes; +import ch.njol.yggdrasil.Fields; +import io.github.bi0qaw.biosphere.util.Frame; +import io.github.bi0qaw.biosphere.util.VectorMath; + +import java.io.NotSerializableException; +import java.io.StreamCorruptedException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class FrameType { + public FrameType(){} + + static { + Classes.registerClass(new ClassInfo(Frame.class, "frame") + .user("frames?") + .name("Frame") + .description("Reference frame that can be used to get vectors relative to some direction.") + .usage(new String[]{"frame of %entity%", "frame with yaw %number% and pitch %number%"}) + .examples(new String[]{"set {_frame} to frame of player", "set {_circle::*} to circle with radius 1 and density 5", + "show happy villager at player's head offset by {_circle::*} in frame of player", + "#shows a particle circle around the player's head with the yaw and pitch of the player"}) + .defaultExpression(new EventValueExpression(Frame.class)) + .parser(new Parser() { + + private final Pattern parsePattern = Pattern.compile("frame:(-?\\d+(\\.\\d+)?),(-?\\d+(\\.\\d+)?)" + , Pattern.CASE_INSENSITIVE); + + @Override + public Frame parse(String s, ParseContext parseContext) { + final Matcher m = parsePattern.matcher(s); + if (m.matches()) { + float yaw = VectorMath.fromSkriptYaw(Float.parseFloat(m.group(1))); + float pitch = VectorMath.fromSkriptPitch(Float.parseFloat(m.group(3))); + return new Frame().setFromYawPitch(yaw, pitch); + } + return null; + } + + @Override + public boolean canParse(ParseContext context) { + return (context == ParseContext.COMMAND || context == ParseContext.EVENT || context == ParseContext.SCRIPT); + } + + @Override + public String toString(Frame f, int i) { + float yaw = VectorMath.skriptYaw(f.getYaw()); + float pitch = VectorMath.skriptPitch(f.getPitch()); + return "frame:yaw: " + Skript.toString(yaw) + ", pitch: " + Skript.toString(pitch); + } + + @Override + public String toVariableNameString(Frame f) { + float yaw = VectorMath.skriptYaw(f.getYaw()); + float pitch = VectorMath.skriptPitch(f.getPitch()); + return "frame:" + Skript.toString(yaw) + "," + Skript.toString(pitch); + } + + @Override + public String getVariableNamePattern() { + return "frame:(-?\\d+(\\.\\d+)?),(-?\\d+(\\.\\d+)?)"; + } + + @Override + public String getDebugMessage(Frame f) { + float yaw = VectorMath.skriptYaw(f.getYaw()); + float pitch = VectorMath.skriptPitch(f.getPitch()); + return "frame:yaw=" + Skript.toString(yaw) + ",pitch=" + Skript.toString(pitch); + } + }) + .serializer(new Serializer() { + @Override + public Fields serialize(Frame f) throws NotSerializableException { + final Fields fields = new Fields(); + float yaw = VectorMath.skriptYaw(f.getYaw()); + float pitch = VectorMath.skriptPitch(f.getPitch()); + fields.putPrimitive("yaw", yaw); + fields.putPrimitive("pitch", pitch); + return fields; + } + + @Override + public void deserialize(Frame f, Fields fields) throws StreamCorruptedException, NotSerializableException { + assert false; + } + + @Override + protected Frame deserialize(Fields f) throws StreamCorruptedException, NotSerializableException { + float yaw = VectorMath.fromSkriptYaw(f.getPrimitive("yaw", float.class)); + float pitch = VectorMath.fromSkriptPitch(f.getPrimitive("pitch", float.class)); + return new Frame().setFromYawPitch(yaw, pitch); + } + + @Override + public boolean mustSyncDeserialization() { + return false; + } + + @Override + protected boolean canBeInstantiated() { + return false; + } + }) + ); + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/util/Frame.java b/src/main/java/io/github/bi0qaw/biosphere/util/Frame.java new file mode 100644 index 0000000..5cd1b87 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/util/Frame.java @@ -0,0 +1,75 @@ +package io.github.bi0qaw.biosphere.util; + +import org.bukkit.util.Vector; + +public class Frame implements Cloneable { + + private static Vector tempv1 = new Vector(); + + private Vector u; + private Vector v; + private Vector w; + + public Frame() { + this.u = new Vector(1, 0, 0); + this.v = new Vector(0, 1, 0); + this.w = new Vector(0, 0, 1); + } + + public Frame setFromFrame(Frame f) { + this.u = f.getU().clone(); + this.v = f.getV().clone(); + this.w = f.getW().clone(); + return this; + } + + public Frame setFromYawPitch(float yaw, float pitch ) { + this.u = VectorMath.fromYawPitch(yaw, pitch); + this.w = VectorMath.fromYawPitch(yaw + 90, 0); + this.v = w.clone().crossProduct(u); + return this; + } + + public Vector getU() { + return u; + } + + public Vector getV() { + return v; + } + + public Vector getW() { + return w; + } + + public float getYaw() { + return VectorMath.wrapAngleDeg(VectorMath.getYaw(this.getW()) - 90); + } + + public Frame setYaw(float yaw) { + this.setFromYawPitch(yaw, this.getPitch()); + return this; + } + + public float getPitch() { + return VectorMath.getPitch(this.getU()); + } + + public Frame setPitch(float pitch) { + this.setFromYawPitch(this.getYaw(), pitch); + return this; + } + + public Vector transform(Vector v) { + tempv1.setX(v.getX()).setY(v.getY()).setZ(v.getZ()); + v.zero(); + VectorMath.addMul(v, this.getU(), tempv1.getX()); + VectorMath.addMul(v, this.getV(), tempv1.getY()); + VectorMath.addMul(v, this.getW(), tempv1.getZ()); + return v; + } + + public Frame clone() { + return new Frame().setFromFrame(this); + } +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/util/LocationLib.java b/src/main/java/io/github/bi0qaw/biosphere/util/LocationLib.java new file mode 100644 index 0000000..8626a70 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/util/LocationLib.java @@ -0,0 +1,227 @@ +package io.github.bi0qaw.biosphere.util; + +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.util.Vector; + +import java.util.ArrayList; +import java.util.Arrays; + +public class LocationLib { + + public static Location[] getCircle(Location[] locations, double radius, double density) { + Vector[] circle = VectorLib.getCircle(radius, density); + return offset(locations, circle); + } + + public static Location[] getCube(Location[] locations, double radius) { + Vector[] cube = VectorLib.getCube(radius); + return offset(locations, cube); + } + + public static Location[] getCubeOutline(Location[] locations, double radius, double density) { + Vector[] cubeoutline = VectorLib.getCubeOutline(radius, density); + return offset(locations, cubeoutline); + } + + public static Location[] getCylindricalCoordinate(Location[] locations, double radius, double yaw, double height) { + return offset(locations, VectorMath.fromCylindricalCoordinates(radius, yaw, height)); + } + + public static Location[] getHelix(Location[] locations, double radius, double height, double step, double density) { + Vector[] helix = VectorLib.getHelix(radius, height, step, density); + return offset(locations, helix); + } + + public static Location[] getLine(Location[] locations, Location end, double density) { + Vector origin = new Vector(0, 0, 0); + Vector vector; + ArrayList line = new ArrayList(); + for (Location l: locations) { + vector = end.toVector().subtract(l.toVector()); + line.addAll(Arrays.asList(LocationLib.offset(new Location[] {l}, VectorLib.getLine(origin, vector, density)))); + } + return line.toArray(new Location[line.size()]); + } + + public static Location[] getLineCoordinate(Location[] locations, Location end, double position) { + Location[] coordinates = new Location[locations.length]; + Vector delta; + int i = 0; + for (Location l: locations) { + delta = end.toVector().subtract(l.toVector()).multiply(position); + locations[i] = l.clone().add(delta); + i++; + } + return locations; + } + + public static Location[] getPolygon(Location[] locations, int points, double radius) { + Vector[] polygon = VectorLib.getPolygon(points, radius); + return offset(locations, polygon); + } + + public static Location[] getPolygonOutline(Location[] locations, int points, double radius, double density) { + Vector[] polygonoutline = VectorLib.getPolygonOutline(points, radius, density); + return offset(locations, polygonoutline); + } + + public static Location[] getRandomSphere(Location[] locations, double radius, double density) { + Vector[] sphere = VectorLib.getRandomSphere(radius, density); + return offset(locations, sphere); + } + + public static Location[] getSphere(Location[] locations, double radius, double density) { + Vector[] sphere = VectorLib.getSphere(radius, density); + return offset(locations, sphere); + } + + public static Location[] getSphericalCoordinates(Location[] locations, double radius, double yaw, double pitch) { + Vector spherical = VectorMath.fromSphericalCoordinates(radius, yaw, pitch); + return offset(locations, spherical); + } + + public static Location[] linkAll(Location[] locations, double density) { + if (locations.length > 0) { + Vector[] vectors = VectorLib.toVector(locations); + Vector[] linked = VectorLib.linkAll(vectors, density); + Location[] linkedLocations = new Location[linked.length]; + World world = locations[0].getWorld(); + int i = 0; + for (Vector v: linked) { + linkedLocations[i] = v.toLocation(world); + i++; + } + return linkedLocations; + } + else { + return new Location[] {}; + } + } + + public static Location midpoint(Location[] locations) { + if (locations.length > 0) { + Vector[] vectors = VectorLib.toVector(locations); + Vector midpoint = VectorLib.midpoint(vectors); + return midpoint.toLocation(locations[0].getWorld()); + } + else { + return null; + } + } + + public static Location[] offset(Location[] locations, Vector vector) { + return offset(locations, new Vector[]{vector}); + } + + public static Location[] offset(Location[] locations, Vector[] vectors) { + Location[] offset = new Location[locations.length * vectors.length]; + int i = 0; + for (Location l: locations) { + for (Vector v: vectors) { + offset[i] = l.clone().add(v); + i++; + } + } + return offset; + } + + public static Location[] pointReflection(Location[] locations, Location point) { + if (locations.length > 0) { + Vector[] vectors = VectorLib.toVector(locations); + Vector[] reflection = VectorLib.pointReflection(vectors, point.toVector()); + return toLocation(reflection, locations[0].getWorld()); + } + else { + return new Location[]{}; + } + } + + public static Location[] reflection(Location[] locations, Location center, Vector direction) { + if (locations.length > 0) { + Vector[] vectors = VectorLib.toVector(locations); + Vector[] reflection = VectorLib.reflection(vectors, center.toVector(), direction); + return toLocation(reflection, locations[0].getWorld()); + } + else { + return new Location[]{}; + } + } + + public static Location[] rotate(Location[] locations, Location center, Vector axis, double angle) { + if (locations.length > 0) { + Vector[] vectors = VectorLib.toVector(offset(locations, center.toVector().multiply(-1))); //move locations to origin & convert to vectors + vectors = VectorLib.rotate(vectors, axis, angle); + return offset(toLocation(vectors, locations[0].getWorld()), center.toVector()); + } + else { + return new Location[] {}; + } + } + + public static Location[] rotateX(Location[] locations, Location center, double angle) { + if (locations.length > 0) { + Vector[] vectors = VectorLib.toVector(offset(locations, center.toVector().multiply(-1))); + VectorLib.rotateX(vectors, angle); + return offset(toLocation(vectors, locations[0].getWorld()), center.toVector()); + } + else { + return new Location[]{}; + } + } + + public static Location[] rotateY(Location[] locations, Location center, double angle) { + if (locations.length > 0) { + Vector[] vectors = VectorLib.toVector(offset(locations, center.toVector().multiply(-1))); + VectorLib.rotateY(vectors, angle); + return offset(toLocation(vectors, locations[0].getWorld()), center.toVector()); + } + else { + return new Location[]{}; + } + } + + public static Location[] rotateZ(Location[] locations, Location center, double angle) { + if (locations.length > 0) { + Vector[] vectors = VectorLib.toVector(offset(locations, center.toVector().multiply(-1))); + VectorLib.rotateZ(vectors, angle); + return offset(toLocation(vectors, locations[0].getWorld()), center.toVector()); + } + else { + return new Location[]{}; + } + } + + public static Location[] scale(Location[] locations, Location center, double factor) { + if (locations.length > 0) { + Vector[] vectors = VectorLib.toVector(offset(locations, center.toVector().multiply(-1))); + VectorLib.scale(vectors, factor); + return offset(toLocation(vectors, locations[0].getWorld()), center.toVector()); + } + else { + return new Location[]{}; + } + } + + public static Location[] scaleDirectional(Location[] locations, Location center, Vector direction, double factor) { + if (locations.length > 0) { + Vector[] vectors = VectorLib.toVector(offset(locations, center.toVector().multiply(-1))); + VectorLib.scaleDirectional(vectors, direction, factor); + return offset(toLocation(vectors, locations[0].getWorld()), center.toVector()); + } + else { + return new Location[]{}; + } + } + + public static Location[] toLocation(Vector[] vectors, World world) { + Location[] locations = new Location[vectors.length]; + int i = 0; + for (Vector v: vectors) { + locations[i] = v.toLocation(world); + i++; + } + return locations; + } + +} diff --git a/src/main/java/io/github/bi0qaw/biosphere/util/TrigLib.java b/src/main/java/io/github/bi0qaw/biosphere/util/TrigLib.java new file mode 100755 index 0000000..b738fd3 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/util/TrigLib.java @@ -0,0 +1,476 @@ +package io.github.bi0qaw.biosphere.util; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Random; + +import org.bukkit.Location; +import org.bukkit.util.Vector; + +public class TrigLib { + + public static Number[] llen(Location[] l){ + Number[] nums = new Number[Array.getLength(l)]; + int i = 0; + for(Location tempLoc : l){ + nums[i] = tempLoc.clone().toVector().length(); + i++; + } + return nums; + } + + public static Location[] lneg(Location[] l){ + Location[] locs = new Location[Array.getLength(l)]; + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().multiply(-1); + i++; + } + return locs; + } + + public static Location[] offset(Location[] l, Location l2){ + Location[] locs = new Location[Array.getLength(l)]; + Vector vect = l2.clone().toVector(); + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().add(vect); + i++; + } + return locs; + } + + public static Location[] coffset(Location[] l, double x, double y, double z){ + Location[] locs = new Location[Array.getLength(l)]; + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().add(x, y, z); + i++; + } + return locs; + } + + public static Location[] vectorOffset(Location[] locations, Vector[] vectors) { + Location[] offset = new Location[locations.length * vectors.length]; + int i = 0; + for (Location l: locations) { + for (Vector v: vectors) { + offset[i] = l.clone().add(v); + i++; + } + } + return offset; + + } + + public static Location[] mult(Location[] l, double p){ + Location[] locs = new Location[Array.getLength(l)]; + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().multiply(p); + i++; + } + return locs; + } + public static Location[] lmult(Location[] l, Location l2){ + Location[] locs = new Location[Array.getLength(l)]; + Vector vect = l2.clone().toVector(); + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().toVector().multiply(vect).toLocation(tempLoc.getWorld()); + i++; + } + return locs; + } + + public static Location[] lnorm(Location[] l){ + Location[] locs = new Location[Array.getLength(l)]; + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().toVector().normalize().toLocation(tempLoc.getWorld()); + i++; + } + return locs; + } + + public static Location[] relloc(Location[] l, Location l2){ + Location[] locs = new Location[Array.getLength(l)]; + Vector vect = l2.clone().toVector(); + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().subtract(vect); + i++; + } + return locs; + } + + public static Number[] dotp(Location[] l, Location l2){ + Number[] nums = new Number[Array.getLength(l)]; + Vector vect = l2.clone().toVector(); + int i = 0; + for(Location tempLoc : l){ + nums[i] = tempLoc.clone().toVector().dot(vect); + i++; + } + return nums; + } + + public static Number[] cdotp(Location[] l, double x, double y, double z){ + Number[] nums = new Number[Array.getLength(l)]; + Vector vect = new Vector(x, y, z); + int i = 0; + for(Location tempLoc : l){ + nums[i] = tempLoc.clone().toVector().dot(vect); + i++; + } + return nums; + } + + public static Location[] crossp(Location[] l, Location l2){ + Location[] locs = new Location[Array.getLength(l)]; + Vector vect = l2.clone().toVector(); + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().toVector().crossProduct(vect).toLocation(tempLoc.getWorld()); + i++; + } + return locs; + } + + public static Location[] ccrossp(Location[] l, double x, double y, double z){ + Location[] locs = new Location[Array.getLength(l)]; + Vector vect = new Vector(x, y, z); + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().toVector().crossProduct(vect).toLocation(tempLoc.getWorld()); + i++; + } + return locs; + } + + public static Location[] rot(Location[] l, Location center, Vector axis, double phi){ + Vector centerVect = center.clone().toVector(); + Location[] locs = new Location[Array.getLength(l)]; + double n1 = axis.getX(); + double n2 = axis.getY(); + double n3 = axis.getZ(); + double x; + double y; + double z; + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().subtract(centerVect); + x = locs[i].getX(); + y = locs[i].getY(); + z = locs[i].getZ(); + locs[i].setX(x * (Math.pow(n1, 2) * ( 1 - Math.cos(phi)) + Math.cos(phi)) + y * (n2 * n1 * ( 1 - Math.cos(phi)) - n3 * Math.sin(phi)) + z * (n1 * n3 * ( 1-Math.cos(phi)) + n2 * (Math.sin(phi))) ); + locs[i].setY(x * (n1 * n2 * ( 1 - Math.cos(phi)) + n3 * Math.sin(phi)) + y * (Math.pow(n2, 2) * (1-Math.cos(phi))+Math.cos(phi)+z*(n2*n3*(1-Math.cos(phi))-n1*Math.sin(phi)))); + locs[i].setZ(x * (n3*n1* (1-Math.cos(phi))-n2*Math.sin(phi))+y*(n3*n2* ( 1 - Math.cos(phi))+n1*Math.sin(phi))+ z* (Math.pow(n3,2) * (1-Math.cos(phi)) + Math.cos(phi))); + i++; + } + return offset(locs,center); + } + + public static Location[] xrot(Location[] l, Location center, double phi){ + Vector centerVect = center.clone().toVector(); + Location[] locs = new Location[Array.getLength(l)]; + double y; + double z; + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().subtract(centerVect); + y = locs[i].getY(); + z = locs[i].getZ(); + locs[i].setY(y * Math.cos(phi) + z * Math.sin(phi)); + locs[i].setZ(-y * Math.sin(phi) + z * Math.cos(phi)); + i++; + } + return offset(locs,center); + } + + public static Location[] yrot(Location[] l, Location center, double phi){ + Vector centerVect = center.clone().toVector(); + Location[] locs = new Location[Array.getLength(l)]; + double x; + double z; + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().subtract(centerVect); + x = locs[i].getX(); + z = locs[i].getZ(); + locs[i].setX(x * Math.cos(phi) - z * Math.sin(phi)); + locs[i].setZ(x * Math.sin(phi) + z * Math.cos(phi)); + locs[i].add(center); + i++; + } + return locs; + } + + public static Location[] zrot(Location[] l, Location center, double phi){ + Vector centerVect = center.clone().toVector(); + Location[] locs = new Location[Array.getLength(l)]; + double x; + double y; + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().subtract(centerVect); + x = locs[i].getX(); + y = locs[i].getY(); + locs[i].setX(x * Math.cos(phi) + y * Math.sin(phi)); + locs[i].setY(-x * Math.sin(phi) + y * Math.cos(phi)); + i++; + } + return offset(locs,center); + } + + public static Location[] ptrefl(Location[] l, Location l2){ + Vector vect = l2.clone().toVector(); + Location[] locs = new Location[Array.getLength(l)]; + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().subtract(vect).multiply(-1).add(vect); + i++; + } + return locs; + } + + public static Location[] refl(Location[] l, Location point, Vector dir){ + Location[] locs = new Location[Array.getLength(l)]; + Vector vect = point.clone().toVector(); + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.toVector().subtract(vect).multiply(-1).multiply(dir).add(vect).toLocation(tempLoc.getWorld()); + i++; + } + return locs; + } + + public static Location[] scale(Location[] l, Location center, double d){ + Vector centerVect = center.clone().toVector(); + Location[] locs = new Location[Array.getLength(l)]; + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().subtract(centerVect).multiply(d).add(centerVect); + i++; + } + return locs; + } + + public static Location[] dirscale(Location[] l, Location center, Vector dir, double d){ + Location[] locs = new Location[Array.getLength(l)]; + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().subtract(center).toVector().multiply(d).multiply(dir).toLocation(tempLoc.getWorld()).add(center); + i++; + } + return locs; + } + + public static Location[] cdirscale(Location[] l, Location center, double x, double y, double z){ + Vector centerVect = center.clone().toVector(); + Location[] locs = new Location[Array.getLength(l)]; + Vector vect = new Vector(x, y ,z); + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().subtract(centerVect).toVector().multiply(vect).toLocation(tempLoc.getWorld()).add(centerVect); + i++; + } + return locs; + } + + public static Location[] midpt(Location[] l){ + Location loc = new Location(l[0].getWorld(), 0, 0, 0); + for(Location tempLoc : l){ + loc.add(tempLoc); + } + return new Location[]{loc.multiply(1D / Array.getLength(l))}; + } + + public static Location[] sphloc(Location[] l, double phi, double the, double r){ + Location[] locs = new Location[Array.getLength(l)]; + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone(); + locs[i].setX(tempLoc.getX() + r * Math.sin(the) * Math.cos(phi)); + locs[i].setY(tempLoc.getY() + r * Math.cos(the)); + locs[i].setZ(tempLoc.getZ() + r * Math.sin(the) * Math.sin(phi)); + i++; + } + return locs; + } + + public static Location[] cylloc(Location[] l, double phi, double r, double h){ + Location[] locs = new Location[Array.getLength(l)]; + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone(); + locs[i].setX(tempLoc.getX() + r * Math.cos(phi)); + locs[i].setY(tempLoc.getY() + h); + locs[i].setZ(tempLoc.getZ() + r * Math.sin(phi)); + i++; + } + return locs; + } + + public static Location[] cubeloc(Location[] l, Location l2, double r){ + Location[] locs = new Location[Array.getLength(l)]; + double d = r / Math.pow(2, 0.5); + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone(); + locs[i].setX(tempLoc.getX() + d * (2 * l2.getX() - 1)); + locs[i].setY(tempLoc.getY() + d * (2 * l2.getY() - 1)); + locs[i].setZ(tempLoc.getZ() + d * (2 * l2.getZ() - 1)); + i++; + } + return locs; + } + + public static Location[] ccubeloc(Location[] l, double x, double y, double z, double r){ + Location[] locs = new Location[Array.getLength(l)]; + double d = r / Math.pow(2, 0.5); + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone(); + locs[i].setX(tempLoc.getX() + d * (2 * x - 1)); + locs[i].setY(tempLoc.getY() + d * (2 * y - 1)); + locs[i].setZ(tempLoc.getZ() + d * (2 * z - 1)); + i++; + } + return locs; + } + + public static Location[] lineloc(Location[] l, Location l2, double p){ + Vector vect = l2.clone().toVector(); + Location[] locs = new Location[Array.getLength(l)]; + int i = 0; + for(Location tempLoc : l){ + locs[i] = tempLoc.clone().add(vect.clone().subtract(tempLoc.clone().toVector()).multiply(p)); + i++; + } + return locs; + } + + public static Location[] getLine(Location[] l, Location l2, double d){ + Vector vect = l2.clone().toVector(); + List locs = new ArrayList(); + Vector v; + Vector tempVect; + + int k; + int i; + for(Location tempLoc : l){ + tempVect = tempLoc.toVector(); + k = (int) (tempVect.distance(vect) * d); + v = vect.clone().subtract(tempVect).multiply(1D/k); + for(i = 0; i < k ; i++){ + locs.add(tempLoc.clone().add(v.clone().multiply(i))); + } + } + return (Location[]) locs.toArray(new Location[locs.size()]); + } + + public static Location[] linkAll(Location[] loc, double d){ + List locs = new ArrayList(); + for(Location l1 : loc){ + for(Location l2 : loc){ + if(!l1.equals(l2)){ + locs.addAll(Arrays.asList(getLine(new Location[]{l1}, l2, d))); + } + } + } + return locs.toArray(new Location[locs.size()]); + } + + public static Location[] getPoly(Location[] l, int n, double r){ + double phi = Math.PI * 2 / (double) n; + Location[] locs = new Location[n*Array.getLength(l)]; + Location loc; + int i = 0; + int j = 0; + for(Location tempLoc : l){ + loc = tempLoc.clone().add(r, 0D, 0D); + for(i = 0; i < n; i++){ + locs[j] = yrot(new Location[]{loc}, tempLoc, i * phi)[0]; + j++; + } + } + return locs; + } + + public static Location[] getPolyOutline(Location[] l, Integer n, double r, double d){ + Location[] loc = new Location[n]; + List locs = new ArrayList(); + int i = 0; + for(Location tempLoc : l){ + loc = getPoly(new Location[]{tempLoc}, n , r); + for(i = 0; i < n - 1; i++){ + locs.addAll(Arrays.asList(getLine(new Location[]{loc[i]},loc[i+1],d))); + } + locs.addAll(Arrays.asList(getLine(new Location[]{loc[i]},loc[0],d))); + } + return (Location[]) locs.toArray(new Location[locs.size()]); + } + + public static Location[] getHelix(Location[] l, double r, double h, double k, double d){ + int n = (int) ( h * 2 * Math.PI * r * d); + double dphi = 1D / (k * r * d); + Location[] locs = new Location[n * Array.getLength(l)]; + int i = 0; + int j = 0; + for(Location tempLoc : l){ + for(j = 0; j < n; j++){ + locs[i] = cylloc(new Location[]{ tempLoc }, j * dphi, r, j * h / n)[0]; + i++; + } + } + return locs; + + } + public static Location[] getCube(Location[] l, double r){ + return vectorOffset(l, VectorLib.getCube(r)); + } + + public static Location[] getCubeOutline(Location[] l, double r, double d){ + return vectorOffset(l, VectorLib.getCubeOutline(r, d)); + } + + public static Location[] getSphereRand(Location[] l, double r, double d){ + List locs = new ArrayList(); + int n = (int) (4 * Math.PI * r * r * d); + double phi = 0D; + double the = 0D; + Random randGen = new Random(); + for(Location tempLoc : l){ + for(int i = 0; i < n; i++){ + phi = randGen.nextDouble() * 2 * Math.PI; + the = randGen.nextDouble() * Math.PI; + locs.add(sphloc(new Location[]{tempLoc}, phi, the, r)[0]); + } + } + return (Location[]) locs.toArray(new Location[locs.size()]); + } + + public static Location[] getSphere(Location[] l, double r, double d){ + List locs = new ArrayList(); + double the = 0D; + double phi = 0D; + int n = (int) (Math.PI * r * d); + for(Location tempLoc : l){ + the = 0; + for(int i = 0; i < n; i++){ + phi = 0; + for(int j = 0; j < n * 2; j++){ + locs.add(sphloc(new Location[]{tempLoc}, phi, the, r)[0]); + phi += Math.PI / n; + } + the += Math.PI / n; + } + } + return (Location[]) locs.toArray(new Location[locs.size()]); + } + + +} \ No newline at end of file diff --git a/src/main/java/io/github/bi0qaw/biosphere/util/VectorLib.java b/src/main/java/io/github/bi0qaw/biosphere/util/VectorLib.java new file mode 100644 index 0000000..c4b8320 --- /dev/null +++ b/src/main/java/io/github/bi0qaw/biosphere/util/VectorLib.java @@ -0,0 +1,244 @@ +package io.github.bi0qaw.biosphere.util; + +import org.bukkit.Location; +import org.bukkit.util.Vector; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Random; + + +public class VectorLib { + + public static final double SQRT2 = Math.sqrt(2); + + public static Vector[] getLine(double length, double density) { + int points = (int) (length * density) + 1; + double delta = length / (double) points; + Vector[] vectors = new Vector[points]; + for (int i = 0; i < points; i++) { + vectors[i] = new Vector(i*delta, 0, 0); + } + return vectors; + } + + public static Vector[] getLine(Vector vector1, Vector vector2, double density) { + double length = vector1.distance(vector2); + int points = (int) (length * density) + 1; + Vector delta = vector2.clone().subtract(vector1).multiply(1.0 / (double) points); + Vector[] vectors = new Vector[points]; + for (int i=0; i < points; i++) { + vectors[i] = delta.clone().multiply(i).add(vector1); + } + return vectors; + } + + public static Vector[] linkLine(Vector v1, Vector v2, double density) { + int points = (int) (v1.distance(v2) * density) + 1; + Vector delta = v2.clone().subtract(v1).multiply(1D / (double) points); + Vector[] vectors = new Vector[points]; + vectors[0] = v1.clone(); + for (int i=1; i vectors = new ArrayList(); + for (int i=0; i < points - 1; i++) { + vectors.addAll(Arrays.asList(linkLine(polygon[i], polygon[i+1], density))); + } + vectors.addAll(Arrays.asList(linkLine(polygon[polygon.length - 1], polygon[0], density))); + return vectors.toArray(new Vector[vectors.size()]); + } + + public static Vector[] getCube(double radius) { + Vector[] vectors = new Vector[8]; + double d = radius / SQRT2; + vectors[0] = new Vector(-d, -d, -d); + vectors[1] = new Vector(-d, -d, d); + vectors[2] = new Vector(-d, d, -d); + vectors[3] = new Vector(-d, d, d); + vectors[4] = new Vector(d, -d, -d); + vectors[5] = new Vector(d, -d, d); + vectors[6] = new Vector(d, d, -d); + vectors[7] = new Vector(d, d, d); + return vectors; + } + + public static Vector[] getCubeOutline(double radius, double density) { + double off = radius/SQRT2; + int points = (int) (radius * density) + 1; + double d = 2*off/points; + Vector[] vectors = new Vector[points * 12]; + for (int i=0; i < points; i++) { + vectors[i + 0*points] = new Vector(i*d-off, -off, -off); + vectors[i + 1*points] = new Vector(i*d-off, off, -off); + vectors[i + 2*points] = new Vector(i*d-off, -off, off); + vectors[i + 3*points] = new Vector(i*d-off, off, off); + vectors[i + 4*points] = new Vector(-off, i*d-off, -off); + vectors[i + 5*points] = new Vector(off, i*d-off, -off); + vectors[i + 6*points] = new Vector(-off, i*d-off, off); + vectors[i + 7*points] = new Vector(off, i*d-off, off); + vectors[i + 8*points] = new Vector(-off, -off, i*d-off); + vectors[i + 9*points] = new Vector(off, -off, i*d-off); + vectors[i + 10*points] = new Vector(-off, off, i*d-off); + vectors[i + 11*points] = new Vector(off, off, i*d-off); + } + return vectors; + } + + public static Vector[] getHelix(double radius, double height, double step, double density) { + int points = (int) ( height * 2 * Math.PI * radius * density); + double deltaAngle = 360 * height / (step * points); + double deltaHeight = height / points; + Vector[] vectors = new Vector[points]; + for(int i=0; i < points; i++){ + vectors[i] = VectorMath.fromCylindricalCoordinates(radius, deltaAngle * i, deltaHeight * i); + } + return vectors; + } + + public static Vector[] linkAll(Vector[] vectors, double density) { + List vecs = new ArrayList(); + for (Vector v1 : vectors) { + for (Vector v2 : vectors) { + if (!v1.equals(v2)) { + vecs.addAll(Arrays.asList(linkLine(v1, v2, density))); + } + } + } + return vecs.toArray(new Vector[vecs.size()]); + } + + public static Vector midpoint(Vector[] vectors) { + Vector midpoint = new Vector(); + double factor = 1D/vectors.length; + for (Vector vec: vectors) { + VectorMath.addMul(midpoint, vec, factor); + } + return midpoint; + } + + public static Vector[] offset(Vector[] vectors, Vector offset) { + for (Vector v: vectors) { + v.add(offset); + } + return vectors; + } + + public static Vector[] pointReflection(Vector[] vectors, Vector center) { + for (Vector v: vectors) { + v.subtract(center).multiply(-1).add(center); + } + return vectors; + } + + public static Vector[] reflection(Vector[] vectors, Vector center, Vector direction) { + for(Vector v: vectors){ + v.subtract(center).multiply(-1).multiply(direction).add(center); + } + return vectors; + } + + public static Vector[] rotate(Vector[] vectors, Vector axis, double angle) { + for (Vector v: vectors) { + v = VectorMath.rot(v, axis, angle); + } + return vectors; + } + + public static Vector[] rotateX(Vector[] vectors, double angle) { + for (Vector v: vectors) { + v = VectorMath.rotX(v, angle); + } + return vectors; + } + + public static Vector[] rotateY(Vector[] vectors, double angle) { + for (Vector v: vectors) { + v = VectorMath.rotY(v, angle); + } + return vectors; + } + + public static Vector[] rotateZ(Vector[] vectors, double angle) { + for (Vector v: vectors) { + v = VectorMath.rotZ(v, angle); + } + return vectors; + } + + public static Vector[] scale(Vector[] vectors, double factor) { + for (Vector v: vectors) { + v.multiply(factor); + } + return vectors; + } + + public static Vector[] scaleDirectional(Vector[] vectors, Vector direction, double factor) { + for (Vector v: vectors) { + v.multiply(direction); + v.multiply(factor); + } + return vectors; + } + + public static Vector[] getSphere(double radius, double density){ + int points = (int) (Math.PI * radius * density); + Vector[] vectors = new Vector[points * points]; + double deltaYaw = 360D / points; + double deltaPitch = 180D / points; + double yaw = 0; + double pitch = 0; + for (int i=0; i 180){ + y -= 360; + } + return y; + } + + public static float fromNotchPitch(float notchPitch){ + return -notchPitch; + } + + public static float skriptYaw(float yaw){ + float y = yaw - 90; + if (y < 0){ + y += 360; + } + return y; + } + + public static float skriptPitch(float pitch){ + return -pitch; + } + + public static float fromSkriptYaw(float yaw){ + float y = yaw + 90; + if (y > 360){ + y -= 360; + } + return y; + } + + public static float fromSkriptPitch(float pitch){ + return -pitch; + } + + public static float wrapAngleDeg(float angle) { + angle %= 360f; + if (angle <= -180) { + return angle + 360; + } else if (angle > 180) { + return angle - 360; + } else { + return angle; + } + } + + public static float wrap(float a, float min, float max) { + if (a < min || a > max) { + return (a - min) - (float) Math.floor((a-min)/(max-min)); + } + else { + return a; + } + } + + public static float clamp(float a, float min, float max) { + if (a < min) { + return min; + } + else if (a > max) { + return max; + } + else { + return a; + } + } + + public static float wrapYaw(float yaw) { + return wrap(yaw, 0, 360); + } + + public static float wrapPitch(float pitch) { + return clamp(pitch, -90, 90); + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..98b2f73 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,4 @@ +name: Biosphere2 +main: io.github.bi0qaw.biosphere.Biosphere +author: bi0qaw +version: 1.0.0 \ No newline at end of file