Merge branch 'master' into double-position

This commit is contained in:
themode 2021-01-25 19:24:19 +01:00
commit 9ee25ee8ce
2 changed files with 241 additions and 20 deletions

View File

@ -1,6 +1,5 @@
package net.minestom.server.scoreboard;
import com.google.common.collect.Queues;
import it.unimi.dsi.fastutil.ints.IntLinkedOpenHashSet;
import net.minestom.server.chat.ChatParser;
import net.minestom.server.chat.ColoredText;
@ -16,8 +15,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.Iterator;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
@ -37,7 +34,7 @@ public class Sidebar implements Scoreboard {
private static final AtomicInteger COUNTER = new AtomicInteger();
/**
* <b>WARNING:</b> You shouldn't create scoreboards/teams with the same prefixes as those
* <b>WARNING:</b> You should NOT create any scoreboards/teams with the same prefixes as those
*/
private static final String SCOREBOARD_PREFIX = "sb-";
private static final String TEAM_PREFIX = "sbt-";
@ -48,9 +45,8 @@ public class Sidebar implements Scoreboard {
private static final int MAX_LINES_COUNT = 15;
private final Set<Player> viewers = new CopyOnWriteArraySet<>();
private final Set<Player> unmodifiableViewers = Collections.unmodifiableSet(viewers);
private final Queue<ScoreboardLine> lines = Queues.newConcurrentLinkedQueue();
private final Set<ScoreboardLine> lines = new CopyOnWriteArraySet<>();
private final IntLinkedOpenHashSet availableColors = new IntLinkedOpenHashSet();
private final String objectiveName;
@ -164,26 +160,33 @@ public class Sidebar implements Scoreboard {
return null;
}
/**
* Gets a {@link Set} containing all the registered lines.
*
* @return an unmodifiable set containing the sidebar's lines
*/
@NotNull
public Set<ScoreboardLine> getLines() {
return Collections.unmodifiableSet(lines);
}
/**
* Removes a {@link ScoreboardLine} through the given identifier
*
* @param id the identifier of the {@link ScoreboardLine}
*/
public void removeLine(@NotNull String id) {
synchronized (lines) {
Iterator<ScoreboardLine> iterator = lines.iterator();
while (iterator.hasNext()) {
final ScoreboardLine line = iterator.next();
if (line.id.equals(id)) {
this.lines.removeIf(line -> {
if (line.id.equals(id)) {
// Remove the line for current viewers
sendPacketsToViewers(line.getScoreCreationPacket(objectiveName), line.sidebarTeam.getDestructionPacket());
// Remove the line for current viewers
sendPacketsToViewers(line.getScoreCreationPacket(objectiveName), line.sidebarTeam.getDestructionPacket());
line.returnName(availableColors);
iterator.remove();
}
line.returnName(availableColors);
return true;
}
}
return false;
});
}
@Override
@ -221,7 +224,7 @@ public class Sidebar implements Scoreboard {
@NotNull
@Override
public Set<Player> getViewers() {
return unmodifiableViewers;
return Collections.unmodifiableSet(viewers);
}
@Override
@ -258,7 +261,7 @@ public class Sidebar implements Scoreboard {
*/
private SidebarTeam sidebarTeam;
public ScoreboardLine(String id, JsonMessage content, int line) {
public ScoreboardLine(@NotNull String id, @NotNull JsonMessage content, int line) {
this.id = id;
this.content = content;
this.line = line;
@ -271,6 +274,7 @@ public class Sidebar implements Scoreboard {
*
* @return the line identifier
*/
@NotNull
public String getId() {
return id;
}
@ -280,6 +284,7 @@ public class Sidebar implements Scoreboard {
*
* @return The line content
*/
@NotNull
public JsonMessage getContent() {
return sidebarTeam == null ? content : sidebarTeam.getPrefix();
}
@ -473,7 +478,7 @@ public class Sidebar implements Scoreboard {
*
* @param prefix The refreshed prefix
*/
private void refreshPrefix(JsonMessage prefix) {
private void refreshPrefix(@NotNull JsonMessage prefix) {
this.prefix = prefix;
}
}

View File

@ -1,5 +1,6 @@
package net.minestom.server.utils;
import com.google.common.primitives.Doubles;
import net.minestom.server.MinecraftServer;
import net.minestom.server.utils.clone.PublicCloneable;
import org.jetbrains.annotations.NotNull;
@ -126,6 +127,15 @@ public class Vector implements PublicCloneable<Vector> {
return Math.sqrt(MathUtils.square(x) + MathUtils.square(y) + MathUtils.square(z));
}
/**
* Gets the magnitude of the vector squared.
*
* @return the magnitude
*/
public double lengthSquared() {
return MathUtils.square(x) + MathUtils.square(y) + MathUtils.square(z);
}
/**
* Gets the distance between this vector and another. The value of this
* method is not cached and uses a costly square-root function, so do not
@ -150,6 +160,18 @@ public class Vector implements PublicCloneable<Vector> {
return MathUtils.square(x - o.x) + MathUtils.square(y - o.y) + MathUtils.square(z - o.z);
}
/**
* Gets the angle between this vector and another in radians.
*
* @param other The other vector
* @return angle in radians
*/
public float angle(@NotNull Vector other) {
double dot = Doubles.constrainToRange(dot(other) / (length() * other.length()), -1.0, 1.0);
return (float) Math.acos(dot);
}
/**
* Performs scalar multiplication, multiplying all components with a
* scalar.
@ -195,6 +217,61 @@ public class Vector implements PublicCloneable<Vector> {
return this;
}
/**
* Calculates the dot product of this vector with another. The dot product
* is defined as x1*x2+y1*y2+z1*z2. The returned value is a scalar.
*
* @param other The other vector
* @return dot product
*/
public double dot(@NotNull Vector other) {
return x * other.x + y * other.y + z * other.z;
}
/**
* Calculates the cross product of this vector with another. The cross
* product is defined as:
* <ul>
* <li>x = y1 * z2 - y2 * z1
* <li>y = z1 * x2 - z2 * x1
* <li>z = x1 * y2 - x2 * y1
* </ul>
*
* @param o The other vector
* @return the same vector
*/
@NotNull
public Vector crossProduct(@NotNull Vector o) {
float newX = y * o.z - o.y * z;
float newY = z * o.x - o.z * x;
float newZ = x * o.y - o.x * y;
x = newX;
y = newY;
z = newZ;
return this;
}
/**
* Calculates the cross product of this vector with another without mutating
* the original. The cross product is defined as:
* <ul>
* <li>x = y1 * z2 - y2 * z1
* <li>y = z1 * x2 - z2 * x1
* <li>z = x1 * y2 - x2 * y1
* </ul>
*
* @param o The other vector
* @return a new vector
*/
@NotNull
public Vector getCrossProduct(@NotNull Vector o) {
float x = this.y * o.z - o.y * this.z;
float y = this.z * o.x - o.z * this.x;
float z = this.x * o.y - o.x * this.y;
return new Vector(x, y, z);
}
/**
* Converts this vector to a unit vector (a vector with length of 1).
*
@ -223,6 +300,145 @@ public class Vector implements PublicCloneable<Vector> {
return this;
}
/**
* Returns if a vector is normalized
*
* @return whether the vector is normalised
*/
public boolean isNormalized() {
return Math.abs(this.lengthSquared() - 1) < getEpsilon();
}
/**
* Rotates the vector around the x axis.
* <p>
* This piece of math is based on the standard rotation matrix for vectors
* in three dimensional space. This matrix can be found here:
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
* Matrix</a>.
*
* @param angle the angle to rotate the vector about. This angle is passed
* in radians
* @return the same vector
*/
@NotNull
public Vector rotateAroundX(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
this.y = (float) (angleCos * getY() - angleSin * getZ());
this.z = (float) (angleSin * getY() + angleCos * getZ());
return this;
}
/**
* Rotates the vector around the y axis.
* <p>
* This piece of math is based on the standard rotation matrix for vectors
* in three dimensional space. This matrix can be found here:
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
* Matrix</a>.
*
* @param angle the angle to rotate the vector about. This angle is passed
* in radians
* @return the same vector
*/
@NotNull
public Vector rotateAroundY(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
this.x = (float) (angleCos * getX() + angleSin * getZ());
this.z = (float) (-angleSin * getX() + angleCos * getZ());
return this;
}
/**
* Rotates the vector around the z axis
* <p>
* This piece of math is based on the standard rotation matrix for vectors
* in three dimensional space. This matrix can be found here:
* <a href="https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations">Rotation
* Matrix</a>.
*
* @param angle the angle to rotate the vector about. This angle is passed
* in radians
* @return the same vector
*/
@NotNull
public Vector rotateAroundZ(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
this.x = (float) (angleCos * getX() - angleSin * getY());
this.y = (float) (angleSin * getX() + angleCos * getY());
return this;
}
/**
* Rotates the vector around a given arbitrary axis in 3 dimensional space.
*
* <p>
* Rotation will follow the general Right-Hand-Rule, which means rotation
* will be counterclockwise when the axis is pointing towards the observer.
* <p>
* This method will always make sure the provided axis is a unit vector, to
* not modify the length of the vector when rotating. If you are experienced
* with the scaling of a non-unit axis vector, you can use
* {@link Vector#rotateAroundNonUnitAxis(Vector, double)}.
*
* @param axis the axis to rotate the vector around. If the passed vector is
* not of length 1, it gets copied and normalized before using it for the
* rotation. Please use {@link Vector#normalize()} on the instance before
* passing it to this method
* @param angle the angle to rotate the vector around the axis
* @return the same vector
*/
@NotNull
public Vector rotateAroundAxis(@NotNull Vector axis, double angle) throws IllegalArgumentException {
return rotateAroundNonUnitAxis(axis.isNormalized() ? axis : axis.clone().normalize(), angle);
}
/**
* Rotates the vector around a given arbitrary axis in 3 dimensional space.
*
* <p>
* Rotation will follow the general Right-Hand-Rule, which means rotation
* will be counterclockwise when the axis is pointing towards the observer.
* <p>
* Note that the vector length will change accordingly to the axis vector
* length. If the provided axis is not a unit vector, the rotated vector
* will not have its previous length. The scaled length of the resulting
* vector will be related to the axis vector. If you are not perfectly sure
* about the scaling of the vector, use
* {@link Vector#rotateAroundAxis(Vector, double)}
*
* @param axis the axis to rotate the vector around.
* @param angle the angle to rotate the vector around the axis
* @return the same vector
*/
@NotNull
public Vector rotateAroundNonUnitAxis(@NotNull Vector axis, double angle) throws IllegalArgumentException {
double x = getX(), y = getY(), z = getZ();
double x2 = axis.getX(), y2 = axis.getY(), z2 = axis.getZ();
double cosTheta = Math.cos(angle);
double sinTheta = Math.sin(angle);
double dotProduct = this.dot(axis);
this.x = (float) (x2 * dotProduct * (1d - cosTheta)
+ x * cosTheta
+ (-z2 * y + y2 * z) * sinTheta);
this.y = (float) (y2 * dotProduct * (1d - cosTheta)
+ y * cosTheta
+ (z2 * x - x2 * z) * sinTheta);
this.z = (float) (z2 * dotProduct * (1d - cosTheta)
+ z * cosTheta
+ (-y2 * x + x2 * y) * sinTheta);
return this;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Vector)) {