Skip to content

Commit

Permalink
Added an option for chairs to set the player and armour stand to face…
Browse files Browse the repository at this point in the history
… the correct direction.
  • Loading branch information
me4502 committed Jan 5, 2018
1 parent 4000ce2 commit 93ac5e4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
Expand Up @@ -18,6 +18,7 @@

import static com.sk89q.craftbook.sponge.util.locale.TranslationsManager.USE_PERMISSIONS;

import com.flowpowered.math.vector.Vector3d;
import com.google.common.collect.Lists;
import com.google.common.reflect.TypeToken;
import com.google.inject.Inject;
Expand All @@ -31,6 +32,7 @@
import com.sk89q.craftbook.sponge.mechanics.types.SpongeBlockMechanic;
import com.sk89q.craftbook.sponge.util.BlockFilter;
import com.sk89q.craftbook.sponge.util.BlockUtil;
import com.sk89q.craftbook.sponge.util.LocationUtil;
import com.sk89q.craftbook.sponge.util.SignUtil;
import com.sk89q.craftbook.sponge.util.SpongePermissionNode;
import com.sk89q.craftbook.sponge.util.type.TypeTokens;
Expand Down Expand Up @@ -75,6 +77,7 @@ public class Chairs extends SpongeBlockMechanic implements DocumentationProvider
private ConfigValue<Boolean> exitAtEntry = new ConfigValue<>("exit-at-last-position", "Moves player's to their entry position when they exit the chair.", false);
private ConfigValue<Boolean> requireSigns = new ConfigValue<>("require-sign", "Require signs on the chairs.", false);
private ConfigValue<Integer> maxSignDistance = new ConfigValue<>("max-sign-distance", "The distance the sign can be from the clicked chair.", 3);
private ConfigValue<Boolean> faceCorrectDirection = new ConfigValue<>("face-correct-direction", "When the player sits, automatically face them the direction of the chair. (If possible)", true);
private ConfigValue<Boolean> healPassenger = new ConfigValue<>("heal-passenger", "Heal the player when they're sitting in the chair.", false);
private ConfigValue<Double> healAmount = new ConfigValue<>("heal-amount", "Amount to heal the player by.", 1.0d, TypeToken.of(Double.class));

Expand All @@ -92,6 +95,7 @@ public void onInitialize() throws CraftBookException {
exitAtEntry.load(config);
requireSigns.load(config);
maxSignDistance.load(config);
faceCorrectDirection.load(config);
healPassenger.load(config);
healAmount.load(config);

Expand Down Expand Up @@ -163,6 +167,13 @@ private void addChair(Player player, Location<World> location) {
entity.offer(Keys.INVISIBLE, true);
entity.offer(Keys.HAS_GRAVITY, false);

if (faceCorrectDirection.getValue() && location.supports(Keys.DIRECTION)) {
System.out.println(location.get(Keys.DIRECTION).orElse(Direction.NONE).getOpposite().asOffset());
Vector3d euler = LocationUtil.cartesianToEuler(location.get(Keys.DIRECTION).orElse(Direction.NONE).getOpposite().asOffset());
entity.setRotation(euler);
player.setRotation(euler);
}

Sponge.getCauseStackManager().pushCause(player);
location.getExtent().spawnEntity(entity);
Sponge.getCauseStackManager().popCause();
Expand Down Expand Up @@ -285,6 +296,7 @@ public ConfigValue<?>[] getConfigurationNodes() {
exitAtEntry,
requireSigns,
maxSignDistance,
faceCorrectDirection,
healPassenger,
healAmount
};
Expand Down
Expand Up @@ -252,4 +252,26 @@ private static Location<World> getRelativeBlock(Location<World> block, Direction
public static String stringFromLocation(Location<World> location) {
return location.getExtent().getName() + ',' + location.getPosition().getX() + ',' + location.getPosition().getY() + ',' + location.getPosition().getZ();
}

/**
* Takes a cartesian {@link Vector3d} and returns a euler {@link Vector3d}
*
* @return A euler vector3d
*/
public static Vector3d cartesianToEuler(Vector3d cartesian) {
double x = cartesian.getX();
double y = cartesian.getY();
double z = cartesian.getZ();

if (x == 0 && z == 0) {
return Vector3d.ZERO;
}

double theta = Math.atan2(-x, z);
float yaw = (float) Math.toDegrees((theta + (2 * Math.PI)) % (2 * Math.PI));
double xz = Math.sqrt(x*x + z*z);
float pitch = (float) Math.toDegrees(Math.atan(-y / xz));

return new Vector3d(pitch, yaw, 0);
}
}
@@ -0,0 +1,28 @@
package com.sk89q.craftbook.sponge.util;

import static org.junit.Assert.assertEquals;

import com.flowpowered.math.vector.Vector3d;
import org.junit.Test;

public class LocationUtilTest {

@Test
public void testCartesianToEuler() {
Vector3d cartesian = new Vector3d(1.0, 0.0, 0.0);
Vector3d euler = LocationUtil.cartesianToEuler(cartesian);
assertEquals(euler, new Vector3d(-0.0, 270.0, 0.0));

cartesian = new Vector3d(1.0, 0.0, 1.0);
euler = LocationUtil.cartesianToEuler(cartesian);
assertEquals(euler, new Vector3d(-0.0, 315.0, 0.0));

cartesian = new Vector3d(1.0, 0.0, -1.0);
euler = LocationUtil.cartesianToEuler(cartesian);
assertEquals(euler, new Vector3d(-0.0, 225.0, 0.0));

cartesian = new Vector3d(1.0, -1.0, 0.0);
euler = LocationUtil.cartesianToEuler(cartesian);
assertEquals(euler, new Vector3d(45.0, 270.0, 0.0));
}
}
2 changes: 1 addition & 1 deletion docs
Submodule docs updated 1 files
+11 −10 source/mechanics/chairs.rst

0 comments on commit 93ac5e4

Please sign in to comment.