Point interface improvement

This commit is contained in:
TheMode 2021-07-05 11:38:33 +02:00
parent 796b6820ce
commit 799dbf4a61
13 changed files with 494 additions and 46 deletions

View File

@ -238,9 +238,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
playerConnection.sendPacket(serverDifficultyPacket);
SpawnPositionPacket spawnPositionPacket = new SpawnPositionPacket();
spawnPositionPacket.x = (int) respawnPoint.getX();
spawnPositionPacket.y = (int) respawnPoint.getY();
spawnPositionPacket.z = (int) respawnPoint.getZ();
spawnPositionPacket.position = respawnPoint;
playerConnection.sendPacket(spawnPositionPacket);
// Add player to list with spawning skin

View File

@ -182,7 +182,7 @@ public class InstanceContainer extends Instance {
final Chunk chunk = getChunkAt(blockPosition);
if (!ChunkUtils.isLoaded(chunk))
return false;
UNSAFE_setBlock(chunk, (int) blockPosition.x(), (int) blockPosition.y(), (int) blockPosition.z(), block,
UNSAFE_setBlock(chunk, blockPosition.blockX(), blockPosition.blockY(), blockPosition.blockZ(), block,
new BlockHandler.PlayerPlacement(block, this, blockPosition, player, blockFace, cursorX, cursorY, cursorZ), null);
return true;
}
@ -200,10 +200,9 @@ public class InstanceContainer extends Instance {
return false;
final Block block = getBlock(blockPosition);
final int x = (int) blockPosition.x();
final int y = (int) blockPosition.y();
final int z = (int) blockPosition.z();
final int x = blockPosition.blockX();
final int y = blockPosition.blockY();
final int z = blockPosition.blockZ();
// The player probably have a wrong version of this chunk section, send it
if (block.isAir()) {
chunk.sendChunk(player);

View File

@ -7,7 +7,6 @@ import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.utils.block.BlockUtils;
import net.minestom.server.utils.coordinate.Point;
import net.minestom.server.utils.coordinate.Vec;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
@ -101,7 +100,7 @@ public class RedstonePlacementRule extends BlockPlacementRule {
public Block blockPlace(@NotNull Instance instance,
@NotNull Block block, @NotNull BlockFace blockFace, @NotNull Point blockPosition,
@NotNull Player pl) {
final Block belowBlock = instance.getBlock(new Vec(0, -1, 0).add(blockPosition));
final Block belowBlock = instance.getBlock(blockPosition.sub(0, 1, 0));
return belowBlock.isSolid() ? block : null;
}
}

View File

@ -18,9 +18,9 @@ public class WallPlacementRule extends BlockPlacementRule {
@Override
public @NotNull Block blockUpdate(@NotNull Instance instance, @NotNull Point blockPosition, @NotNull Block block) {
final int x = (int) blockPosition.x();
final int y = (int) blockPosition.y();
final int z = (int) blockPosition.z();
final int x = blockPosition.blockX();
final int y = blockPosition.blockY();
final int z = blockPosition.blockZ();
String east = "none";
String north = "none";

View File

@ -5,11 +5,12 @@ import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.coordinate.Point;
import net.minestom.server.utils.coordinate.Vec;
import org.jetbrains.annotations.NotNull;
public class SpawnPositionPacket implements ServerPacket {
public int x, y, z;
public Point position = Vec.ZERO;
public float angle;
public SpawnPositionPacket() {
@ -17,16 +18,13 @@ public class SpawnPositionPacket implements ServerPacket {
@Override
public void write(@NotNull BinaryWriter writer) {
writer.writeBlockPosition(x, y, z);
writer.writeBlockPosition(position);
writer.writeFloat(angle);
}
@Override
public void read(@NotNull BinaryReader reader) {
Point pos = reader.readBlockPosition();
this.x = (int) pos.x();
this.y = (int) pos.y();
this.z = (int) pos.z();
this.position = reader.readBlockPosition();
this.angle = reader.readFloat();
}

View File

@ -6,6 +6,7 @@ import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.function.DoubleUnaryOperator;
// TODO: pool block positions?
@ -230,6 +231,96 @@ public class BlockPosition implements Point {
return z;
}
@Override
public @NotNull Point withX(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withX(double x) {
return null;
}
@Override
public @NotNull Point withY(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withY(double y) {
return null;
}
@Override
public @NotNull Point withZ(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withZ(double z) {
return null;
}
@Override
public @NotNull Point add(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point add(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point add(double value) {
return null;
}
@Override
public @NotNull Point sub(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point sub(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point sub(double value) {
return null;
}
@Override
public @NotNull Point mul(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point mul(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point mul(double value) {
return null;
}
@Override
public @NotNull Point div(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point div(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point div(double value) {
return null;
}
@Override
public @NotNull BlockPosition clone() {
return new BlockPosition(x, y, z);

View File

@ -5,6 +5,7 @@ import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.function.DoubleUnaryOperator;
/**
* Represents a position.
@ -50,6 +51,61 @@ public class Position implements Point {
return this;
}
@Override
public @NotNull Point add(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point add(double value) {
return null;
}
@Override
public @NotNull Point sub(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point sub(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point sub(double value) {
return null;
}
@Override
public @NotNull Point mul(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point mul(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point mul(double value) {
return null;
}
@Override
public @NotNull Point div(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point div(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point div(double value) {
return null;
}
/**
* Adds a position to the current position.
*
@ -218,6 +274,36 @@ public class Position implements Point {
return z;
}
@Override
public @NotNull Point withX(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withX(double x) {
return null;
}
@Override
public @NotNull Point withY(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withY(double y) {
return null;
}
@Override
public @NotNull Point withZ(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withZ(double z) {
return null;
}
@Override
public @NotNull Position clone() {
return new Position(x, y, z, yaw, pitch);

View File

@ -5,6 +5,8 @@ import net.minestom.server.utils.clone.PublicCloneable;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
import java.util.function.DoubleUnaryOperator;
/**
* @deprecated use {@link net.minestom.server.utils.coordinate.Vec} instead
*/
@ -43,6 +45,61 @@ public class Vector implements Point {
return this;
}
@Override
public @NotNull Point add(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point add(double value) {
return null;
}
@Override
public @NotNull Point sub(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point sub(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point sub(double value) {
return null;
}
@Override
public @NotNull Point mul(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point mul(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point mul(double value) {
return null;
}
@Override
public @NotNull Point div(double x, double y, double z) {
return null;
}
@Override
public @NotNull Point div(@NotNull Point point) {
return null;
}
@Override
public @NotNull Point div(double value) {
return null;
}
/**
* Subtracts a vector from this one.
*
@ -504,6 +561,36 @@ public class Vector implements Point {
return z;
}
@Override
public @NotNull Point withX(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withX(double x) {
return null;
}
@Override
public @NotNull Point withY(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withY(double y) {
return null;
}
@Override
public @NotNull Point withZ(@NotNull DoubleUnaryOperator operator) {
return null;
}
@Override
public @NotNull Point withZ(double z) {
return null;
}
@Override
public @NotNull Vector clone() {
return new Vector(x,y,z);

View File

@ -268,7 +268,7 @@ public class BinaryWriter extends OutputStream {
}
public void writeBlockPosition(@NotNull Point point) {
writeBlockPosition((int) point.x(), (int) point.y(), (int) point.z());
writeBlockPosition(point.blockX(), point.blockY(), point.blockZ());
}
public void writeBlockPosition(int x, int y, int z) {

View File

@ -4,7 +4,6 @@ import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.utils.StringUtils;
import net.minestom.server.utils.coordinate.Point;
import net.minestom.server.utils.coordinate.Vec;
import java.util.Collections;
import java.util.HashMap;
@ -21,7 +20,7 @@ public class BlockUtils {
}
public BlockUtils getRelativeTo(int x, int y, int z) {
return new BlockUtils(instance, new Vec(x, y, z).add(position));
return new BlockUtils(instance, position.add(x, y, z));
}
public BlockUtils above() {

View File

@ -5,6 +5,8 @@ import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.function.DoubleUnaryOperator;
/**
* Represents a 3D point.
* <p>
@ -53,6 +55,60 @@ public interface Point {
return MathUtils.floor(z());
}
@Contract(pure = true)
@NotNull Point withX(@NotNull DoubleUnaryOperator operator);
@Contract(pure = true)
@NotNull Point withX(double x);
@Contract(pure = true)
@NotNull Point withY(@NotNull DoubleUnaryOperator operator);
@Contract(pure = true)
@NotNull Point withY(double y);
@Contract(pure = true)
@NotNull Point withZ(@NotNull DoubleUnaryOperator operator);
@Contract(pure = true)
@NotNull Point withZ(double z);
@Contract(pure = true)
@NotNull Point add(double x, double y, double z);
@Contract(pure = true)
@NotNull Point add(@NotNull Point point);
@Contract(pure = true)
@NotNull Point add(double value);
@Contract(pure = true)
@NotNull Point sub(double x, double y, double z);
@Contract(pure = true)
@NotNull Point sub(@NotNull Point point);
@Contract(pure = true)
@NotNull Point sub(double value);
@Contract(pure = true)
@NotNull Point mul(double x, double y, double z);
@Contract(pure = true)
@NotNull Point mul(@NotNull Point point);
@Contract(pure = true)
@NotNull Point mul(double value);
@Contract(pure = true)
@NotNull Point div(double x, double y, double z);
@Contract(pure = true)
@NotNull Point div(@NotNull Point point);
@Contract(pure = true)
@NotNull Point div(double value);
/**
* Gets the distance between this point and another. The value of this
* method is not cached and uses a costly square-root function, so do not

View File

@ -70,20 +70,124 @@ public final class Pos implements Point {
}
@Override
@Contract(pure = true)
public double x() {
return x;
}
@Override
@Contract(pure = true)
public double y() {
return y;
}
@Override
@Contract(pure = true)
public double z() {
return z;
}
@Contract(pure = true)
public @NotNull Pos with(@NotNull Operator operator) {
return operator.apply(x, y, z);
}
@Override
@Contract(pure = true)
public @NotNull Pos withX(@NotNull DoubleUnaryOperator operator) {
return new Pos(operator.applyAsDouble(x), y, z);
}
@Override
@Contract(pure = true)
public @NotNull Pos withX(double x) {
return new Pos(x, y, z, yaw, pitch);
}
@Override
@Contract(pure = true)
public @NotNull Pos withY(@NotNull DoubleUnaryOperator operator) {
return new Pos(x, operator.applyAsDouble(y), z);
}
@Override
@Contract(pure = true)
public @NotNull Pos withY(double y) {
return new Pos(x, y, z, yaw, pitch);
}
@Override
@Contract(pure = true)
public @NotNull Point withZ(@NotNull DoubleUnaryOperator operator) {
return new Pos(x, y, operator.applyAsDouble(z));
}
@Override
@Contract(pure = true)
public @NotNull Point withZ(double z) {
return new Pos(x, y, z, yaw, pitch);
}
@Override
public @NotNull Pos add(double x, double y, double z) {
return new Pos(this.x + x, this.y + y, this.z + z, yaw, pitch);
}
@Override
public @NotNull Pos add(@NotNull Point point) {
return add(point.x(), point.y(), point.z());
}
@Override
public @NotNull Pos add(double value) {
return add(value, value, value);
}
@Override
public @NotNull Pos sub(double x, double y, double z) {
return new Pos(this.x - x, this.y - y, this.z - z, yaw, pitch);
}
@Override
public @NotNull Pos sub(@NotNull Point point) {
return sub(point.x(), point.y(), point.z());
}
@Override
public @NotNull Pos sub(double value) {
return sub(value, value, value);
}
@Override
public @NotNull Pos mul(double x, double y, double z) {
return new Pos(this.x * x, this.y * y, this.z * z, yaw, pitch);
}
@Override
public @NotNull Pos mul(@NotNull Point point) {
return mul(point.x(), point.y(), point.z());
}
@Override
public @NotNull Pos mul(double value) {
return mul(value, value, value);
}
@Override
public @NotNull Pos div(double x, double y, double z) {
return new Pos(this.x / x, this.y / y, this.z / z, yaw, pitch);
}
@Override
public @NotNull Pos div(@NotNull Point point) {
return div(point.x(), point.y(), point.z());
}
@Override
public @NotNull Pos div(double value) {
return div(value, value, value);
}
@Contract(pure = true)
public float yaw() {
return yaw;
@ -98,4 +202,9 @@ public final class Pos implements Point {
public @NotNull Vec asVec() {
return new Vec(x, y, z);
}
@FunctionalInterface
interface Operator {
@NotNull Pos apply(double x, double y, double z);
}
}

View File

@ -52,84 +52,110 @@ public final class Vec implements Point {
}
/**
* Creates a new vec with coordinated depending on {@code this}.
* Creates a new point with coordinated depending on {@code this}.
*
* @param operator the operator
* @return the created vec
* @return the created point
*/
@Contract(pure = true)
public @NotNull Vec with(@NotNull Operator operator) {
return operator.apply(x, y, z);
}
@Override
@Contract(pure = true)
public @NotNull Vec withX(@NotNull DoubleUnaryOperator operator) {
return new Vec(operator.applyAsDouble(x), y, z);
}
@Override
@Contract(pure = true)
public @NotNull Vec withX(double x) {
return new Vec(x, y, z);
}
@Override
@Contract(pure = true)
public @NotNull Vec withY(@NotNull DoubleUnaryOperator operator) {
return new Vec(x, operator.applyAsDouble(y), z);
}
@Override
@Contract(pure = true)
public @NotNull Vec withY(double y) {
return new Vec(x, y, z);
}
@Override
@Contract(pure = true)
public @NotNull Vec withZ(@NotNull DoubleUnaryOperator operator) {
return new Vec(x, y, operator.applyAsDouble(z));
}
@Override
@Contract(pure = true)
public @NotNull Vec withZ(double z) {
return new Vec(x, y, z);
}
@Contract(pure = true)
@Override
public @NotNull Vec add(double x, double y, double z) {
return new Vec(this.x + x, this.y + y, this.z + z);
}
@Override
public @NotNull Vec add(@NotNull Point point) {
return new Vec(x + point.x(), y + point.y(), z + point.z());
return add(point.x(), point.y(), point.z());
}
@Contract(pure = true)
@Override
public @NotNull Vec add(double value) {
return new Vec(x + value, y + value, z + value);
return add(value, value, value);
}
@Contract(pure = true)
@Override
public @NotNull Vec sub(double x, double y, double z) {
return new Vec(this.x - x, this.y - y, this.z - z);
}
@Override
public @NotNull Vec sub(@NotNull Point point) {
return new Vec(x - point.x(), y - point.y(), z - point.z());
return sub(point.x(), point.y(), point.z());
}
@Contract(pure = true)
@Override
public @NotNull Vec sub(double value) {
return new Vec(x - value, y - value, z - value);
return sub(value, value, value);
}
@Contract(pure = true)
@Override
public @NotNull Vec mul(double x, double y, double z) {
return new Vec(this.x * x, this.y * y, this.z * z);
}
@Override
public @NotNull Vec mul(@NotNull Point point) {
return new Vec(x * point.x(), y * point.y(), z * point.z());
return mul(point.x(), point.y(), point.z());
}
@Contract(pure = true)
@Override
public @NotNull Vec mul(double value) {
return new Vec(x * value, y * value, z * value);
return mul(value, value, value);
}
@Contract(pure = true)
@Override
public @NotNull Vec div(double x, double y, double z) {
return new Vec(this.x / x, this.y / y, this.z / z);
}
@Override
public @NotNull Vec div(@NotNull Point point) {
return new Vec(x / point.x(), y / point.y(), z / point.z());
return div(point.x(), point.y(), point.z());
}
@Contract(pure = true)
@Override
public @NotNull Vec div(double value) {
return new Vec(x / value, y / value, z / value);
return div(value, value, value);
}
@Contract(pure = true)
@ -249,7 +275,7 @@ public final class Vec implements Point {
z * o.x - o.z * x,
x * o.y - o.x * y);
}
/**
* Rotates the vector around the x axis.
* <p>
@ -266,7 +292,7 @@ public final class Vec implements Point {
public @NotNull Vec rotateAroundX(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double newY = angleCos * y - angleSin * z;
double newZ = angleSin * y + angleCos * z;
return new Vec(x, newY, newZ);
@ -288,8 +314,8 @@ public final class Vec implements Point {
public @NotNull Vec rotateAroundY(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double newX = angleCos * x + angleSin * z;
double newX = angleCos * x + angleSin * z;
double newZ = -angleSin * x + angleCos * z;
return new Vec(newX, y, newZ);
}
@ -310,7 +336,7 @@ public final class Vec implements Point {
public @NotNull Vec rotateAroundZ(double angle) {
double angleCos = Math.cos(angle);
double angleSin = Math.sin(angle);
double newX = angleCos * x - angleSin * y;
double newY = angleSin * x + angleCos * y;
return new Vec(newX, newY, z);
@ -365,7 +391,7 @@ public final class Vec implements Point {
}
@FunctionalInterface
public interface Operator {
interface Operator {
@NotNull Vec apply(double x, double y, double z);
}