2021-09-18 21:51:29 +02:00
|
|
|
package net.knarcraft.stargate.container;
|
2021-02-07 03:37:25 +01:00
|
|
|
|
|
|
|
/**
|
2021-02-16 21:58:31 +01:00
|
|
|
* This stores a block location as a vector relative to a position
|
2021-02-12 00:26:47 +01:00
|
|
|
*
|
2021-02-16 21:58:31 +01:00
|
|
|
* <p>A relative block vector stores a vector relative to some origin. The origin in this plugin is usually the
|
2021-02-22 17:01:47 +01:00
|
|
|
* top-left block of a gate (top-left when looking at the side with the sign). The right is therefore the distance
|
2021-10-24 22:48:13 +02:00
|
|
|
* from the top-left corner towards the top-right corner. Down is the distance from the top-left corner towards the
|
|
|
|
* bottom-left corner. Out is the distance outward from the gate.</p>
|
2021-02-07 03:37:25 +01:00
|
|
|
*/
|
|
|
|
public class RelativeBlockVector {
|
|
|
|
|
2021-09-09 15:25:08 +02:00
|
|
|
private final int right;
|
2021-10-24 22:48:13 +02:00
|
|
|
private final int down;
|
|
|
|
private final int out;
|
2021-02-07 03:37:25 +01:00
|
|
|
|
2021-10-29 18:43:22 +02:00
|
|
|
/**
|
|
|
|
* A specifier for one of the relative block vector's three properties
|
|
|
|
*/
|
|
|
|
public enum Property {
|
|
|
|
/**
|
|
|
|
* Specifies the relative block vector's right property
|
|
|
|
*/
|
|
|
|
RIGHT,
|
|
|
|
/**
|
|
|
|
* Specifies the relative block vector's down property
|
|
|
|
*/
|
|
|
|
DOWN,
|
|
|
|
/**
|
|
|
|
* Specifies the relative block vector's out property
|
|
|
|
*/
|
|
|
|
OUT
|
|
|
|
}
|
2021-10-08 18:23:42 +02:00
|
|
|
|
2021-02-12 00:26:47 +01:00
|
|
|
/**
|
|
|
|
* Instantiates a new relative block vector
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-10-24 22:48:13 +02:00
|
|
|
* <p>Relative block vectors start from a top-left corner. A yaw is used to orient a relative block vector in the
|
|
|
|
* "real world".
|
|
|
|
* In terms of a gate layout, the origin is 0,0. Right is towards the end of the line. Down is to the
|
|
|
|
* next line. Out is towards the observer.</p>
|
|
|
|
*
|
|
|
|
* @param right <p>The distance rightward relative to the origin</p>
|
|
|
|
* @param down <p>The distance downward relative to the origin</p>
|
|
|
|
* @param out <p>The distance outward relative to the origin</p>
|
2021-02-12 00:26:47 +01:00
|
|
|
*/
|
2021-10-24 22:48:13 +02:00
|
|
|
public RelativeBlockVector(int right, int down, int out) {
|
2021-02-07 03:37:25 +01:00
|
|
|
this.right = right;
|
2021-10-24 22:48:13 +02:00
|
|
|
this.down = down;
|
|
|
|
this.out = out;
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
2021-10-08 18:23:42 +02:00
|
|
|
/**
|
|
|
|
* Adds a value to one of the properties of this relative block vector
|
|
|
|
*
|
2021-10-13 14:08:38 +02:00
|
|
|
* @param propertyToAddTo <p>The property to add to</p>
|
|
|
|
* @param valueToAdd <p>The value to add to the property (negative to move in the opposite direction)</p>
|
2021-10-08 18:23:42 +02:00
|
|
|
* @return <p>A new relative block vector with the property altered</p>
|
|
|
|
*/
|
|
|
|
public RelativeBlockVector addToVector(Property propertyToAddTo, int valueToAdd) {
|
|
|
|
switch (propertyToAddTo) {
|
|
|
|
case RIGHT:
|
2021-10-24 22:48:13 +02:00
|
|
|
return new RelativeBlockVector(this.right + valueToAdd, this.down, this.out);
|
|
|
|
case DOWN:
|
|
|
|
return new RelativeBlockVector(this.right, this.down + valueToAdd, this.out);
|
|
|
|
case OUT:
|
|
|
|
return new RelativeBlockVector(this.right, this.down, this.out + valueToAdd);
|
2021-10-08 18:23:42 +02:00
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Invalid relative block vector property given");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a relative block vector which is this inverted (pointing in the opposite direction)
|
|
|
|
*
|
|
|
|
* @return <p>This vector, but inverted</p>
|
|
|
|
*/
|
|
|
|
public RelativeBlockVector invert() {
|
2021-10-24 22:48:13 +02:00
|
|
|
return new RelativeBlockVector(-this.right, -this.down, -this.out);
|
2021-10-08 18:23:42 +02:00
|
|
|
}
|
|
|
|
|
2021-02-16 21:58:31 +01:00
|
|
|
/**
|
|
|
|
* Gets the distance to the right relative to the origin
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-10-13 14:08:38 +02:00
|
|
|
* @return <p>The distance to the right relative to the origin</p>
|
2021-02-16 21:58:31 +01:00
|
|
|
*/
|
2021-02-07 03:37:25 +01:00
|
|
|
public int getRight() {
|
|
|
|
return right;
|
|
|
|
}
|
|
|
|
|
2021-02-16 21:58:31 +01:00
|
|
|
/**
|
|
|
|
* Gets the distance downward relative to the origin
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-10-13 14:08:38 +02:00
|
|
|
* @return <p>The distance downward relative to the origin</p>
|
2021-02-16 21:58:31 +01:00
|
|
|
*/
|
2021-10-24 22:48:13 +02:00
|
|
|
public int getDown() {
|
|
|
|
return down;
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
2021-02-16 21:58:31 +01:00
|
|
|
/**
|
|
|
|
* Gets the distance outward relative to the origin
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-10-13 14:08:38 +02:00
|
|
|
* @return <p>The distance outward relative to the origin</p>
|
2021-02-16 21:58:31 +01:00
|
|
|
*/
|
2021-10-24 22:48:13 +02:00
|
|
|
public int getOut() {
|
|
|
|
return out;
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 15:49:44 +01:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
2021-10-24 22:48:13 +02:00
|
|
|
return String.format("(right = %d, down = %d, out = %d)", right, down, out);
|
2021-02-22 15:49:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object other) {
|
2021-02-22 17:01:47 +01:00
|
|
|
if (other == this) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (other == null || this.getClass() != other.getClass()) {
|
2021-02-22 15:49:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
RelativeBlockVector otherVector = (RelativeBlockVector) other;
|
2021-10-24 22:48:13 +02:00
|
|
|
return this.right == otherVector.right && this.down == otherVector.down &&
|
|
|
|
this.out == otherVector.out;
|
2021-02-22 15:49:44 +01:00
|
|
|
}
|
|
|
|
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|