Skip to content

Commit

Permalink
refactor: use double instead of float (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd authored Feb 7, 2025
1 parent 4378d95 commit 0bce082
Show file tree
Hide file tree
Showing 105 changed files with 1,363 additions and 3,114 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Unless otherwise specified, any version comparison below is the comparison of se

### Changed

- (API) We now used `double` instead of `float` in entity location, motion, aabb and some other classes which require high precision.
This should improve the accuracy of entity movement and collision detection.
- (API) Renamed `FullContainerTypeBuilder` to `Builder`.
- (API) Moved method `Chunk#isLoaded` to `UnsafeChunk#isLoaded`.
- (API) Made method `Dimension#createUpdateBlockPacket` private, consider using `Dimension#sendBlockUpdateTo` method instead.
Expand All @@ -55,6 +57,7 @@ Unless otherwise specified, any version comparison below is the comparison of se
- (API) Replaced `Chunk#batchProcess` method with new `Chunk#applyOperation` and `Chunk#applyOperationInSection` methods.
- (API) Moved inner class `ItemArmorBaseComponent#ArmorType` to package `org.allaymc.api.item.data`.
- (API) Changed the default value of `ServerSettings#GenericSettings#defaultPermission` to `PlayerPermission.MEMBER`.
- (API) `VoxelShape` have being refactored. Now it doesn't allow using `vacancy`, this change is required by physics engine to fix some bugs.
- Main thread will sleep a short time if gui is enabled when the server exits abnormally. This gives user time to see what goes wrong.
- Server won't crash if failed to load the descriptor of a plugin now. An error message will be print to the console instead.
- Server won't crash if failed to create world generator. Void world generator will be used instead.
Expand All @@ -75,6 +78,7 @@ Unless otherwise specified, any version comparison below is the comparison of se
- Fixed the bug that damage that smaller than 1 will never kill an entity even if the entity has only 1 health.
- Fixed the bug caused by incorrect initial value of runtime id counter. The initial value should be 1 instead of 0.
- Fixed the bug that `/alwaysday` command actually do the opposite thing.
- Several bugs in physics engine, including wrong collision detection and wrong movement calculation are fixed.

### Removed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import lombok.ToString;
import lombok.experimental.Accessors;
import org.allaymc.api.math.voxelshape.VoxelShape;
import org.joml.Vector3fc;
import org.joml.Vector3dc;
import org.joml.Vector3ic;
import org.joml.primitives.AABBf;
import org.joml.primitives.AABBd;

import java.awt.*;

Expand All @@ -36,18 +36,16 @@ public class BlockStateData {
protected static Gson SERIALIZER = new GsonBuilder()
.registerTypeAdapter(VoxelShape.class, (JsonDeserializer<Object>) (json, typeOfT, context) -> {
var array = json.getAsJsonArray();
var minX = array.get(0).getAsFloat();
var minY = array.get(1).getAsFloat();
var minZ = array.get(2).getAsFloat();
var maxX = array.get(3).getAsFloat();
var maxY = array.get(4).getAsFloat();
var maxZ = array.get(5).getAsFloat();
var minX = array.get(0).getAsDouble();
var minY = array.get(1).getAsDouble();
var minZ = array.get(2).getAsDouble();
var maxX = array.get(3).getAsDouble();
var maxY = array.get(4).getAsDouble();
var maxZ = array.get(5).getAsDouble();
if (minX == 0 && minY == 0 && minZ == 0 && maxX == 0 && maxY == 0 && maxZ == 0) {
return VoxelShape.EMPTY;
}
return VoxelShape.builder().solid(
new AABBf(minX, minY, minZ, maxX, maxY, maxZ)
).build();
return VoxelShape.builder().solid(new AABBd(minX, minY, minZ, maxX, maxY, maxZ)).build();
})
.registerTypeAdapter(Color.class, (JsonDeserializer<Object>) (json, typeOfT, context) -> {
// Example: #4c4c4cff
Expand Down Expand Up @@ -167,23 +165,23 @@ public boolean hasCollision() {
return !collisionShape.getSolids().isEmpty();
}

public VoxelShape computeOffsetCollisionShape(float x, float y, float z) {
public VoxelShape computeOffsetCollisionShape(double x, double y, double z) {
return collisionShape.translate(x, y, z);
}

public VoxelShape computeOffsetCollisionShape(Vector3fc vector) {
public VoxelShape computeOffsetCollisionShape(Vector3dc vector) {
return computeOffsetCollisionShape(vector.x(), vector.y(), vector.z());
}

public VoxelShape computeOffsetCollisionShape(Vector3ic vector) {
return computeOffsetCollisionShape(vector.x(), vector.y(), vector.z());
}

public VoxelShape computeOffsetShape(float x, float y, float z) {
public VoxelShape computeOffsetShape(double x, double y, double z) {
return shape.translate(x, y, z);
}

public VoxelShape computeOffsetShape(Vector3fc vector) {
public VoxelShape computeOffsetShape(Vector3dc vector) {
return computeOffsetShape(vector.x(), vector.y(), vector.z());
}

Expand Down
26 changes: 13 additions & 13 deletions api/src/main/java/org/allaymc/api/block/data/BlockFace.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import lombok.Getter;
import org.allaymc.api.block.property.enums.MinecraftCardinalDirection;
import org.jetbrains.annotations.ApiStatus;
import org.joml.Vector3f;
import org.joml.Vector3fc;
import org.joml.Vector3d;
import org.joml.Vector3dc;
import org.joml.Vector3i;
import org.joml.Vector3ic;
import org.joml.primitives.AABBf;
import org.joml.primitives.AABBfc;
import org.joml.primitives.AABBd;
import org.joml.primitives.AABBdc;

import static java.lang.Math.max;
import static java.lang.Math.min;
Expand Down Expand Up @@ -103,12 +103,12 @@ public Vector3ic offsetPos(Vector3ic pos) {
*
* @return the rotated AABB.
*/
public AABBf rotateAABB(AABBfc aabb) {
var c1 = new Vector3f(aabb.minX(), aabb.minY(), aabb.minZ());
var c2 = new Vector3f(aabb.maxX(), aabb.maxY(), aabb.maxZ());
public AABBd rotateAABB(AABBdc aabb) {
var c1 = new Vector3d(aabb.minX(), aabb.minY(), aabb.minZ());
var c2 = new Vector3d(aabb.maxX(), aabb.maxY(), aabb.maxZ());
var nc1 = rotateVector(c1);
var nc2 = rotateVector(c2);
return new AABBf(
return new AABBd(
min(nc1.x, nc2.x),
min(nc1.y, nc2.y),
min(nc1.z, nc2.z),
Expand All @@ -126,12 +126,12 @@ public AABBf rotateAABB(AABBfc aabb) {
* @return the rotated vector.
*/
@SuppressWarnings("SuspiciousNameCombination")
public Vector3f rotateVector(Vector3fc vec) {
Vector3f result = new Vector3f(vec);
public Vector3d rotateVector(Vector3dc vec) {
Vector3d result = new Vector3d(vec);
// Translate to rotation point (0.5, 0.5, 0.5)
result.sub(0.5f, 0.5f, 0.5f);
result.sub(0.5, 0.5, 0.5);

float temp;
double temp;
switch (this) {
case EAST -> {
// No rotation needed as EAST is the default orientation
Expand Down Expand Up @@ -163,7 +163,7 @@ public Vector3f rotateVector(Vector3fc vec) {
}

// Translate back to original point
result.add(0.5f, 0.5f, 0.5f);
result.add(0.5, 0.5, 0.5);

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static PlayerData createEmpty() {
var server = Server.getInstance();
var globalSpawnPoint = server.getWorldPool().getGlobalSpawnPoint();
var builder = NbtMap.builder();
writeVector3f(builder, "Pos", server.getWorldPool().getGlobalSpawnPointVec3f());
writeVector3f(builder, "Pos", globalSpawnPoint.x(), globalSpawnPoint.y(), globalSpawnPoint.z());
var worldName = globalSpawnPoint.dimension().getWorld().getWorldData().getDisplayName();
var dimId = globalSpawnPoint.dimension().getDimensionInfo().dimensionId();
return builder()
Expand Down
4 changes: 2 additions & 2 deletions api/src/main/java/org/allaymc/api/command/CommandSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.i18n.TextReceiver;
import org.allaymc.api.i18n.TrContainer;
import org.allaymc.api.math.location.Location3fc;
import org.allaymc.api.math.location.Location3dc;
import org.allaymc.api.permission.Permissible;
import org.allaymc.api.server.Server;
import org.allaymc.api.world.gamerule.GameRule;
Expand Down Expand Up @@ -35,7 +35,7 @@ public interface CommandSender extends TextReceiver, Permissible {
*
* @return The location where the command was executed.
*/
Location3fc getCmdExecuteLocation();
Location3dc getCmdExecuteLocation();

/**
* Handle the result of the command execution.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.allaymc.api.entity.interfaces.EntityNpc;
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.i18n.TrContainer;
import org.allaymc.api.math.location.Location3fc;
import org.allaymc.api.math.location.Location3dc;
import org.allaymc.api.permission.DefaultPermissions;
import org.allaymc.api.permission.tree.PermissionTree;
import org.cloudburstmc.protocol.bedrock.data.command.CommandOriginData;
Expand Down Expand Up @@ -35,7 +35,7 @@ public CommandOriginData getCommandOriginData() {
}

@Override
public Location3fc getCmdExecuteLocation() {
public Location3dc getCmdExecuteLocation() {
return npc.getLocation();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.allaymc.api.command.selector.SelectorSyntaxException;
import org.allaymc.api.command.selector.SelectorType;
import org.allaymc.api.entity.Entity;
import org.allaymc.api.math.location.Location3fc;
import org.allaymc.api.math.location.Location3dc;

import java.util.List;
import java.util.Set;
Expand All @@ -27,7 +27,7 @@ public CachedFilterSelectorArgument() {
}

@Override
public Function<List<Entity>, List<Entity>> getFilter(SelectorType selectorType, CommandSender sender, Location3fc basePos, String... arguments) throws SelectorSyntaxException {
public Function<List<Entity>, List<Entity>> getFilter(SelectorType selectorType, CommandSender sender, Location3dc basePos, String... arguments) throws SelectorSyntaxException {
var value = cache.getIfPresent(Sets.newHashSet(arguments));
if (value == null) {
value = cache(selectorType, sender, basePos, arguments);
Expand Down Expand Up @@ -56,7 +56,7 @@ public boolean isFilter() {
*
* @throws SelectorSyntaxException if there is an error parsing the arguments.
*/
protected abstract Function<List<Entity>, List<Entity>> cache(SelectorType selectorType, CommandSender sender, Location3fc basePos, String... arguments) throws SelectorSyntaxException;
protected abstract Function<List<Entity>, List<Entity>> cache(SelectorType selectorType, CommandSender sender, Location3dc basePos, String... arguments) throws SelectorSyntaxException;

/**
* Provides the cache service used to store filter functions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import org.allaymc.api.command.selector.SelectorSyntaxException;
import org.allaymc.api.command.selector.SelectorType;
import org.allaymc.api.entity.Entity;
import org.allaymc.api.math.location.Location3f;
import org.allaymc.api.math.location.Location3fc;
import org.allaymc.api.math.location.Location3d;
import org.allaymc.api.math.location.Location3dc;

import java.util.Set;
import java.util.concurrent.TimeUnit;
Expand All @@ -27,7 +27,7 @@ public CachedSimpleSelectorArgument() {
}

@Override
public Predicate<Entity> getPredicate(SelectorType selectorType, CommandSender sender, Location3f basePos, String... arguments) throws SelectorSyntaxException {
public Predicate<Entity> getPredicate(SelectorType selectorType, CommandSender sender, Location3d basePos, String... arguments) throws SelectorSyntaxException {
var value = cache.getIfPresent(Sets.newHashSet(arguments));
if (value == null) {
value = cache(selectorType, sender, basePos, arguments);
Expand All @@ -50,7 +50,7 @@ public Predicate<Entity> getPredicate(SelectorType selectorType, CommandSender s
*
* @throws SelectorSyntaxException if the arguments cannot be parsed.
*/
protected abstract Predicate<Entity> cache(SelectorType selectorType, CommandSender sender, Location3fc basePos, String... arguments) throws SelectorSyntaxException;
protected abstract Predicate<Entity> cache(SelectorType selectorType, CommandSender sender, Location3dc basePos, String... arguments) throws SelectorSyntaxException;

/**
* Provides the cache service used to store predicates.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import org.allaymc.api.command.selector.SelectorSyntaxException;
import org.allaymc.api.command.selector.SelectorType;
import org.allaymc.api.entity.Entity;
import org.allaymc.api.math.location.Location3f;
import org.allaymc.api.math.location.Location3fc;
import org.allaymc.api.math.location.Location3d;
import org.allaymc.api.math.location.Location3dc;
import org.jetbrains.annotations.NotNull;

import java.util.List;
Expand Down Expand Up @@ -33,7 +33,7 @@ public interface SelectorArgument extends Comparable<SelectorArgument> {
*
* @throws SelectorSyntaxException if an error occurs while parsing the arguments.
*/
default Predicate<Entity> getPredicate(SelectorType selectorType, CommandSender sender, Location3f basePos, String... arguments) throws SelectorSyntaxException {
default Predicate<Entity> getPredicate(SelectorType selectorType, CommandSender sender, Location3d basePos, String... arguments) throws SelectorSyntaxException {
return null;
}

Expand All @@ -52,7 +52,7 @@ default Predicate<Entity> getPredicate(SelectorType selectorType, CommandSender
*
* @throws SelectorSyntaxException if an error occurs while parsing the arguments.
*/
default Function<List<Entity>, List<Entity>> getFilter(SelectorType selectorType, CommandSender sender, Location3fc basePos, String... arguments) throws SelectorSyntaxException {
default Function<List<Entity>, List<Entity>> getFilter(SelectorType selectorType, CommandSender sender, Location3dc basePos, String... arguments) throws SelectorSyntaxException {
return null;
}

Expand Down
Loading

0 comments on commit 0bce082

Please sign in to comment.