Start implementing the new coordinate API

This commit is contained in:
TheMode 2021-07-05 09:10:03 +02:00
parent aa0868f02a
commit 796b6820ce
43 changed files with 309 additions and 305 deletions

View File

@ -2,8 +2,8 @@ package net.minestom.server.collision;
import net.minestom.server.entity.Entity;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.Vector;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
/**
@ -57,17 +57,16 @@ public class BoundingBox {
* @param blockPosition the position to check
* @return true if the bounding box intersects with the position, false otherwise
*/
public boolean intersect(@NotNull BlockPosition blockPosition) {
public boolean intersectWithBlock(@NotNull Point blockPosition) {
final double offsetX = 1;
final double x = blockPosition.getX();
final double x = blockPosition.x();
final double maxX = x + offsetX;
final boolean checkX = getMinX() < maxX && getMaxX() > x;
if (!checkX)
return false;
final double y = blockPosition.getY();
final double y = blockPosition.y();
final double maxY = y + 0.99999;
final boolean checkY = getMinY() < maxY && getMaxY() > y;
@ -75,7 +74,7 @@ public class BoundingBox {
return false;
final double offsetZ = 1;
final double z = blockPosition.getZ();
final double z = blockPosition.z();
final double maxZ = z + offsetZ;
// Z check
@ -88,8 +87,8 @@ public class BoundingBox {
(z >= getMinZ() && z <= getMaxZ());
}
public boolean intersect(@NotNull Position position) {
return intersect(position.getX(), position.getY(), position.getZ());
public boolean intersect(@NotNull Point point) {
return intersect(point.x(), point.y(), point.z());
}
/**

View File

@ -37,12 +37,12 @@ import net.minestom.server.potion.TimedPotion;
import net.minestom.server.tag.Tag;
import net.minestom.server.tag.TagHandler;
import net.minestom.server.thread.ThreadProvider;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.Vector;
import net.minestom.server.utils.callback.OptionalCallback;
import net.minestom.server.utils.chunk.ChunkCallback;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.coordinate.Vec;
import net.minestom.server.utils.entity.EntityUtils;
import net.minestom.server.utils.player.PlayerUtils;
import net.minestom.server.utils.time.Cooldown;
@ -588,7 +588,7 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
this.velocity.setX(velocity.getX() * drag);
this.velocity.setZ(velocity.getZ() * drag);
if (!hasNoGravity())
this.velocity.setY(velocity.getY() * (1-gravityDragPerTick));
this.velocity.setY(velocity.getY() * (1 - gravityDragPerTick));
if (velocity.equals(new Vector())) {
this.velocity.zero();
@ -613,7 +613,6 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
final int maxY = (int) Math.ceil(boundingBox.getMaxY());
final int minZ = (int) Math.floor(boundingBox.getMinZ());
final int maxZ = (int) Math.ceil(boundingBox.getMaxZ());
final BlockPosition tmpPosition = new BlockPosition(0, 0, 0); // allow reuse
for (int y = minY; y <= maxY; y++) {
for (int x = minX; x <= maxX; x++) {
for (int z = minZ; z <= maxZ; z++) {
@ -624,13 +623,11 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
final Block block = chunk.getBlock(x, y, z);
final BlockHandler handler = block.handler();
if (handler != null) {
tmpPosition.setX(x);
tmpPosition.setY(y);
tmpPosition.setZ(z);
final var blockPosition = new Vec(x, y, z);
// checks that we are actually in the block, and not just here because of a rounding error
if (boundingBox.intersect(tmpPosition)) {
if (boundingBox.intersectWithBlock(blockPosition)) {
// TODO: replace with check with custom block bounding box
handler.onTouch(new BlockHandler.Touch(block, instance, tmpPosition, this));
handler.onTouch(new BlockHandler.Touch(block, instance, blockPosition, this));
}
}
}
@ -990,8 +987,8 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
/**
* Changes the gravity of the entity.
*
* @param gravityDragPerTick the gravity drag per tick in block
* @param gravityAcceleration the gravity acceleration in block
* @param gravityDragPerTick the gravity drag per tick in block
* @param gravityAcceleration the gravity acceleration in block
* @see <a href="https://minecraft.gamepedia.com/Entity#Motion_of_entities">Entities motion</a>
*/
public void setGravity(double gravityDragPerTick, double gravityAcceleration) {
@ -1508,8 +1505,8 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
/**
* Triggers {@link #remove()} after the specified time.
*
* @param delay the time before removing the entity,
* 0 to cancel the removing
* @param delay the time before removing the entity,
* 0 to cancel the removing
* @param temporalUnit the unit of the delay
*/
public void scheduleRemove(long delay, @NotNull TemporalUnit temporalUnit) {
@ -1519,8 +1516,8 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
/**
* Triggers {@link #remove()} after the specified time.
*
* @param delay the time before removing the entity,
* 0 to cancel the removing
* @param delay the time before removing the entity,
* 0 to cancel the removing
*/
public void scheduleRemove(Duration delay) {
if (delay.isZero()) { // Cancel the scheduled remove
@ -1735,8 +1732,8 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
* Applies knockback to the entity
*
* @param strength the strength of the knockback, 0.4 is the vanilla value for a bare hand hit
* @param x knockback on x axle, for default knockback use the following formula <pre>sin(attacker.yaw * (pi/180))</pre>
* @param z knockback on z axle, for default knockback use the following formula <pre>-cos(attacker.yaw * (pi/180))</pre>
* @param x knockback on x axle, for default knockback use the following formula <pre>sin(attacker.yaw * (pi/180))</pre>
* @param z knockback on z axle, for default knockback use the following formula <pre>-cos(attacker.yaw * (pi/180))</pre>
*/
public void takeKnockback(final float strength, final double x, final double z) {
if (strength > 0) {

View File

@ -1,7 +1,6 @@
package net.minestom.server.entity;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minestom.server.MinecraftServer;
import net.minestom.server.chat.ColoredText;
import net.minestom.server.chat.JsonMessage;
@ -14,6 +13,7 @@ import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.binary.Readable;
import net.minestom.server.utils.binary.Writeable;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBT;
@ -106,11 +106,11 @@ public class Metadata {
}, reader -> new Vector(reader.readFloat(), reader.readFloat(), reader.readFloat()));
}
public static Value<BlockPosition> Position(@NotNull BlockPosition value) {
public static Value<Point> Position(@NotNull Point value) {
return new Value<>(TYPE_POSITION, value, writer -> writer.writeBlockPosition(value), BinaryReader::readBlockPosition);
}
public static Value<BlockPosition> OptPosition(@Nullable BlockPosition value) {
public static Value<Point> OptPosition(@Nullable Point value) {
return new Value<>(TYPE_OPTPOSITION, value, writer -> {
final boolean present = value != null;
writer.writeBoolean(present);

View File

@ -5,7 +5,7 @@ import net.minestom.server.event.trait.BlockEvent;
import net.minestom.server.event.trait.CancellableEvent;
import net.minestom.server.event.trait.PlayerEvent;
import net.minestom.server.instance.block.Block;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
public class PlayerBlockBreakEvent implements PlayerEvent, BlockEvent, CancellableEvent {
@ -13,12 +13,12 @@ public class PlayerBlockBreakEvent implements PlayerEvent, BlockEvent, Cancellab
private final Player player;
private final Block block;
private Block resultBlock;
private final BlockPosition blockPosition;
private final Point blockPosition;
private boolean cancelled;
public PlayerBlockBreakEvent(@NotNull Player player,
@NotNull Block block, @NotNull Block resultBlock, @NotNull BlockPosition blockPosition) {
@NotNull Block block, @NotNull Block resultBlock, @NotNull Point blockPosition) {
this.player = player;
this.block = block;
@ -59,7 +59,7 @@ public class PlayerBlockBreakEvent implements PlayerEvent, BlockEvent, Cancellab
*
* @return the block position
*/
public @NotNull BlockPosition getBlockPosition() {
public @NotNull Point getBlockPosition() {
return blockPosition;
}

View File

@ -7,6 +7,7 @@ import net.minestom.server.event.trait.PlayerEvent;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
/**
@ -18,7 +19,7 @@ public class PlayerBlockInteractEvent implements PlayerEvent, BlockEvent, Cancel
private final Player player;
private final Player.Hand hand;
private final Block block;
private final BlockPosition blockPosition;
private final Point blockPosition;
private final BlockFace blockFace;
/**
@ -30,7 +31,7 @@ public class PlayerBlockInteractEvent implements PlayerEvent, BlockEvent, Cancel
private boolean cancelled;
public PlayerBlockInteractEvent(@NotNull Player player, @NotNull Player.Hand hand,
@NotNull Block block, @NotNull BlockPosition blockPosition,
@NotNull Block block, @NotNull Point blockPosition,
@NotNull BlockFace blockFace) {
this.player = player;
this.hand = hand;
@ -62,8 +63,7 @@ public class PlayerBlockInteractEvent implements PlayerEvent, BlockEvent, Cancel
*
* @return the block position
*/
@NotNull
public BlockPosition getBlockPosition() {
public @NotNull Point getBlockPosition() {
return blockPosition;
}
@ -72,8 +72,7 @@ public class PlayerBlockInteractEvent implements PlayerEvent, BlockEvent, Cancel
*
* @return the hand used
*/
@NotNull
public Player.Hand getHand() {
public @NotNull Player.Hand getHand() {
return hand;
}
@ -82,8 +81,7 @@ public class PlayerBlockInteractEvent implements PlayerEvent, BlockEvent, Cancel
*
* @return the block face
*/
@NotNull
public BlockFace getBlockFace() {
public @NotNull BlockFace getBlockFace() {
return blockFace;
}

View File

@ -6,6 +6,7 @@ import net.minestom.server.event.trait.CancellableEvent;
import net.minestom.server.event.trait.PlayerEvent;
import net.minestom.server.instance.block.Block;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
/**
@ -15,7 +16,7 @@ public class PlayerBlockPlaceEvent implements PlayerEvent, BlockEvent, Cancellab
private final Player player;
private Block block;
private final BlockPosition blockPosition;
private final Point blockPosition;
private final Player.Hand hand;
private boolean consumeBlock;
@ -23,7 +24,7 @@ public class PlayerBlockPlaceEvent implements PlayerEvent, BlockEvent, Cancellab
private boolean cancelled;
public PlayerBlockPlaceEvent(@NotNull Player player, @NotNull Block block,
@NotNull BlockPosition blockPosition, @NotNull Player.Hand hand) {
@NotNull Point blockPosition, @NotNull Player.Hand hand) {
this.player = player;
this.block = block;
this.blockPosition = blockPosition;
@ -55,8 +56,7 @@ public class PlayerBlockPlaceEvent implements PlayerEvent, BlockEvent, Cancellab
*
* @return the block position
*/
@NotNull
public BlockPosition getBlockPosition() {
public @NotNull Point getBlockPosition() {
return blockPosition;
}
@ -65,8 +65,7 @@ public class PlayerBlockPlaceEvent implements PlayerEvent, BlockEvent, Cancellab
*
* @return the hand used
*/
@NotNull
public Player.Hand getHand() {
public @NotNull Player.Hand getHand() {
return hand;
}

View File

@ -5,7 +5,7 @@ import net.minestom.server.event.trait.BlockEvent;
import net.minestom.server.event.trait.CancellableEvent;
import net.minestom.server.event.trait.PlayerEvent;
import net.minestom.server.instance.block.Block;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
/**
@ -20,11 +20,11 @@ public class PlayerStartDiggingEvent implements PlayerEvent, BlockEvent, Cancell
private final Player player;
private final Block block;
private final BlockPosition blockPosition;
private final Point blockPosition;
private boolean cancelled;
public PlayerStartDiggingEvent(@NotNull Player player, @NotNull Block block, @NotNull BlockPosition blockPosition) {
public PlayerStartDiggingEvent(@NotNull Player player, @NotNull Block block, @NotNull Point blockPosition) {
this.player = player;
this.block = block;
this.blockPosition = blockPosition;
@ -41,11 +41,11 @@ public class PlayerStartDiggingEvent implements PlayerEvent, BlockEvent, Cancell
}
/**
* Gets the {@link BlockPosition}.
* Gets the block position.
*
* @return the {@link BlockPosition}
* @return the block position
*/
public @NotNull BlockPosition getBlockPosition() {
public @NotNull Point getBlockPosition() {
return blockPosition;
}

View File

@ -4,8 +4,8 @@ import net.minestom.server.entity.Player;
import net.minestom.server.event.trait.ItemEvent;
import net.minestom.server.event.trait.PlayerEvent;
import net.minestom.server.item.ItemStack;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.Direction;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
/**
@ -16,12 +16,12 @@ public class PlayerUseItemOnBlockEvent implements PlayerEvent, ItemEvent {
private final Player player;
private final Player.Hand hand;
private final ItemStack itemStack;
private final BlockPosition position;
private final Point position;
private final Direction blockFace;
public PlayerUseItemOnBlockEvent(@NotNull Player player, @NotNull Player.Hand hand,
@NotNull ItemStack itemStack,
@NotNull BlockPosition position, @NotNull Direction blockFace) {
@NotNull Point position, @NotNull Direction blockFace) {
this.player = player;
this.hand = hand;
this.itemStack = itemStack;
@ -34,8 +34,7 @@ public class PlayerUseItemOnBlockEvent implements PlayerEvent, ItemEvent {
*
* @return the block position
*/
@NotNull
public BlockPosition getPosition() {
public @NotNull Point getPosition() {
return position;
}
@ -44,8 +43,7 @@ public class PlayerUseItemOnBlockEvent implements PlayerEvent, ItemEvent {
*
* @return the block face
*/
@NotNull
public Direction getBlockFace() {
public @NotNull Direction getBlockFace() {
return blockFace;
}
@ -54,8 +52,7 @@ public class PlayerUseItemOnBlockEvent implements PlayerEvent, ItemEvent {
*
* @return the hand
*/
@NotNull
public Player.Hand getHand() {
public @NotNull Player.Hand getHand() {
return hand;
}
@ -64,8 +61,8 @@ public class PlayerUseItemOnBlockEvent implements PlayerEvent, ItemEvent {
*
* @return the item
*/
@NotNull
public ItemStack getItemStack() {
@Override
public @NotNull ItemStack getItemStack() {
return itemStack;
}

View File

@ -34,6 +34,7 @@ import net.minestom.server.utils.PacketUtils;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.chunk.ChunkCallback;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.coordinate.Point;
import net.minestom.server.utils.entity.EntityUtils;
import net.minestom.server.utils.time.Cooldown;
import net.minestom.server.utils.time.TimeUnit;
@ -45,7 +46,6 @@ import org.jetbrains.annotations.Nullable;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.time.Duration;
import java.time.temporal.TemporalUnit;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
@ -62,7 +62,7 @@ import java.util.function.Consumer;
* you need to be sure to signal the {@link UpdateManager} of the changes using
* {@link UpdateManager#signalChunkLoad(Chunk)} and {@link UpdateManager#signalChunkUnload(Chunk)}.
*/
public abstract class Instance implements BlockGetter, BlockSetter, Tickable,TagHandler, EventHandler<InstanceEvent>, DataContainer, PacketGroupingAudience {
public abstract class Instance implements BlockGetter, BlockSetter, Tickable, TagHandler, EventHandler<InstanceEvent>, DataContainer, PacketGroupingAudience {
protected static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
protected static final UpdateManager UPDATE_MANAGER = MinecraftServer.getUpdateManager();
@ -149,7 +149,7 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable,Tag
}
@ApiStatus.Internal
public abstract boolean placeBlock(@NotNull Player player, @NotNull Block block, @NotNull BlockPosition blockPosition,
public abstract boolean placeBlock(@NotNull Player player, @NotNull Block block, @NotNull Point blockPosition,
@NotNull BlockFace blockFace, float cursorX, float cursorY, float cursorZ);
/**
@ -157,11 +157,11 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable,Tag
* and send particle packets
*
* @param player the {@link Player} who break the block
* @param blockPosition the {@link BlockPosition} of the broken block
* @param blockPosition the position of the broken block
* @return true if the block has been broken, false if it has been cancelled
*/
@ApiStatus.Internal
public abstract boolean breakBlock(@NotNull Player player, @NotNull BlockPosition blockPosition);
public abstract boolean breakBlock(@NotNull Player player, @NotNull Point blockPosition);
/**
* Forces the generation of a {@link Chunk}, even if no file and {@link ChunkGenerator} are defined.
@ -614,6 +614,17 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable,Tag
return getChunk(chunkX, chunkZ);
}
/**
* Gets the {@link Chunk} at the given {@link Point}, null if not loaded.
*
* @param point the chunk position
* @return the chunk at the given position, null if not loaded
*/
@Nullable
public Chunk getChunkAt(@NotNull Point point) {
return getChunkAt(point.x(), point.z());
}
/**
* Checks if the {@link Chunk} at the position is loaded.
*
@ -625,28 +636,6 @@ public abstract class Instance implements BlockGetter, BlockSetter, Tickable,Tag
return getChunk(chunkX, chunkZ) != null;
}
/**
* Gets the {@link Chunk} at the given {@link BlockPosition}, null if not loaded.
*
* @param blockPosition the chunk position
* @return the chunk at the given position, null if not loaded
*/
@Nullable
public Chunk getChunkAt(@NotNull BlockPosition blockPosition) {
return getChunkAt(blockPosition.getX(), blockPosition.getZ());
}
/**
* Gets the {@link Chunk} at the given {@link Position}, null if not loaded.
*
* @param position the chunk position
* @return the chunk at the given position, null if not loaded
*/
@Nullable
public Chunk getChunkAt(@NotNull Position position) {
return getChunkAt(position.getX(), position.getZ());
}
/**
* Saves a {@link Chunk} without any callback.
*

View File

@ -24,13 +24,13 @@ import net.minestom.server.utils.callback.OptionalCallback;
import net.minestom.server.utils.chunk.ChunkCallback;
import net.minestom.server.utils.chunk.ChunkSupplier;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.coordinate.Point;
import net.minestom.server.utils.validate.Check;
import net.minestom.server.world.DimensionType;
import net.minestom.server.world.biomes.Biome;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.time.temporal.TemporalUnit;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
@ -177,18 +177,18 @@ public class InstanceContainer extends Instance {
}
@Override
public boolean placeBlock(@NotNull Player player, @NotNull Block block, @NotNull BlockPosition blockPosition,
public boolean placeBlock(@NotNull Player player, @NotNull Block block, @NotNull Point blockPosition,
@NotNull BlockFace blockFace, float cursorX, float cursorY, float cursorZ) {
final Chunk chunk = getChunkAt(blockPosition);
if (!ChunkUtils.isLoaded(chunk))
return false;
UNSAFE_setBlock(chunk, blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), block,
UNSAFE_setBlock(chunk, (int) blockPosition.x(), (int) blockPosition.y(), (int) blockPosition.z(), block,
new BlockHandler.PlayerPlacement(block, this, blockPosition, player, blockFace, cursorX, cursorY, cursorZ), null);
return true;
}
@Override
public boolean breakBlock(@NotNull Player player, @NotNull BlockPosition blockPosition) {
public boolean breakBlock(@NotNull Player player, @NotNull Point blockPosition) {
final Chunk chunk = getChunkAt(blockPosition);
Check.notNull(chunk, "You cannot break blocks in a null chunk!");
// Cancel if the chunk is read-only
@ -200,9 +200,9 @@ public class InstanceContainer extends Instance {
return false;
final Block block = getBlock(blockPosition);
final int x = blockPosition.getX();
final int y = blockPosition.getY();
final int z = blockPosition.getZ();
final int x = (int) blockPosition.x();
final int y = (int) blockPosition.y();
final int z = (int) blockPosition.z();
// The player probably have a wrong version of this chunk section, send it
if (block.isAir()) {

View File

@ -4,13 +4,12 @@ import net.minestom.server.entity.Player;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.storage.StorageLocation;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.Position;
import net.minestom.server.utils.chunk.ChunkCallback;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.time.temporal.TemporalUnit;
import java.util.Collection;
import java.util.UUID;
@ -33,13 +32,13 @@ public class SharedInstance extends Instance {
}
@Override
public boolean placeBlock(@NotNull Player player, @NotNull Block block, @NotNull BlockPosition blockPosition,
public boolean placeBlock(@NotNull Player player, @NotNull Block block, @NotNull Point blockPosition,
@NotNull BlockFace blockFace, float cursorX, float cursorY, float cursorZ) {
return instanceContainer.placeBlock(player, block, blockPosition, blockFace, cursorX, cursorY, cursorZ);
}
@Override
public boolean breakBlock(@NotNull Player player, @NotNull BlockPosition blockPosition) {
public boolean breakBlock(@NotNull Player player, @NotNull Point blockPosition) {
return instanceContainer.breakBlock(player, blockPosition);
}

View File

@ -1,6 +1,6 @@
package net.minestom.server.instance.block;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
public interface BlockGetter {
@ -9,10 +9,10 @@ public interface BlockGetter {
/**
* Gets block from given position.
*
* @param blockPosition position to get the block from
* @param point position to get the block from
* @return Block at given position.
*/
default @NotNull Block getBlock(@NotNull BlockPosition blockPosition) {
return getBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
default @NotNull Block getBlock(@NotNull Point point) {
return getBlock(point.blockX(), point.blockY(), point.blockZ());
}
}

View File

@ -4,8 +4,8 @@ import net.minestom.server.entity.Entity;
import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.tag.Tag;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.NamespaceID;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
@ -90,10 +90,10 @@ public interface BlockHandler {
class Placement {
private final Block block;
private final Instance instance;
private final BlockPosition blockPosition;
private final Point blockPosition;
@ApiStatus.Internal
public Placement(Block block, Instance instance, BlockPosition blockPosition) {
public Placement(Block block, Instance instance, Point blockPosition) {
this.block = block;
this.instance = instance;
this.blockPosition = blockPosition;
@ -107,7 +107,7 @@ public interface BlockHandler {
return instance;
}
public @NotNull BlockPosition getBlockPosition() {
public @NotNull Point getBlockPosition() {
return blockPosition;
}
}
@ -118,7 +118,7 @@ public interface BlockHandler {
private final float cursorX, cursorY, cursorZ;
@ApiStatus.Internal
public PlayerPlacement(Block block, Instance instance, BlockPosition blockPosition,
public PlayerPlacement(Block block, Instance instance, Point blockPosition,
Player player, BlockFace blockFace, float cursorX, float cursorY, float cursorZ) {
super(block, instance, blockPosition);
this.player = player;
@ -153,10 +153,10 @@ public interface BlockHandler {
class Destroy {
private final Block block;
private final Instance instance;
private final BlockPosition blockPosition;
private final Point blockPosition;
@ApiStatus.Internal
public Destroy(Block block, Instance instance, BlockPosition blockPosition) {
public Destroy(Block block, Instance instance, Point blockPosition) {
this.block = block;
this.instance = instance;
this.blockPosition = blockPosition;
@ -170,7 +170,7 @@ public interface BlockHandler {
return instance;
}
public @NotNull BlockPosition getBlockPosition() {
public @NotNull Point getBlockPosition() {
return blockPosition;
}
}
@ -179,7 +179,7 @@ public interface BlockHandler {
private final Player player;
@ApiStatus.Internal
public PlayerDestroy(Block block, Instance instance, BlockPosition blockPosition, Player player) {
public PlayerDestroy(Block block, Instance instance, Point blockPosition, Player player) {
super(block, instance, blockPosition);
this.player = player;
}
@ -193,12 +193,12 @@ public interface BlockHandler {
class Interaction {
private final Block block;
private final Instance instance;
private final BlockPosition blockPosition;
private final Point blockPosition;
private final Player player;
private final Player.Hand hand;
@ApiStatus.Internal
public Interaction(Block block, Instance instance, BlockPosition blockPosition, Player player, Player.Hand hand) {
public Interaction(Block block, Instance instance, Point blockPosition, Player player, Player.Hand hand) {
this.block = block;
this.instance = instance;
this.blockPosition = blockPosition;
@ -214,7 +214,7 @@ public interface BlockHandler {
return instance;
}
public @NotNull BlockPosition getBlockPosition() {
public @NotNull Point getBlockPosition() {
return blockPosition;
}
@ -231,11 +231,11 @@ public interface BlockHandler {
class Touch {
private final Block block;
private final Instance instance;
private final BlockPosition blockPosition;
private final Point blockPosition;
private final Entity touching;
@ApiStatus.Internal
public Touch(Block block, Instance instance, BlockPosition blockPosition, Entity touching) {
public Touch(Block block, Instance instance, Point blockPosition, Entity touching) {
this.block = block;
this.instance = instance;
this.blockPosition = blockPosition;
@ -250,7 +250,7 @@ public interface BlockHandler {
return instance;
}
public @NotNull BlockPosition getBlockPosition() {
public @NotNull Point getBlockPosition() {
return blockPosition;
}
@ -263,10 +263,10 @@ public interface BlockHandler {
class Tick {
private final Block block;
private final Instance instance;
private final BlockPosition blockPosition;
private final Point blockPosition;
@ApiStatus.Internal
public Tick(Block block, Instance instance, BlockPosition blockPosition) {
public Tick(Block block, Instance instance, Point blockPosition) {
this.block = block;
this.instance = instance;
this.blockPosition = blockPosition;
@ -280,7 +280,7 @@ public interface BlockHandler {
return instance;
}
public @NotNull BlockPosition getBlockPosition() {
public @NotNull Point getBlockPosition() {
return blockPosition;
}
}

View File

@ -4,14 +4,11 @@ import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public abstract class BlockPlacementRule {
public static final int CANCEL_CODE = -1;
private final Block block;
public BlockPlacementRule(@NotNull Block block) {
@ -26,7 +23,7 @@ public abstract class BlockPlacementRule {
* @param currentBlock the current block
* @return the updated block
*/
public abstract @NotNull Block blockUpdate(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @NotNull Block currentBlock);
public abstract @NotNull Block blockUpdate(@NotNull Instance instance, @NotNull Point blockPosition, @NotNull Block currentBlock);
/**
* Called when the block is placed.
@ -39,7 +36,7 @@ public abstract class BlockPlacementRule {
* @return the block to place, {@code null} to cancel
*/
public abstract @Nullable Block blockPlace(@NotNull Instance instance,
@NotNull Block block, @NotNull BlockFace blockFace, @NotNull BlockPosition blockPosition,
@NotNull Block block, @NotNull BlockFace blockFace, @NotNull Point blockPosition,
@NotNull Player pl);
public @NotNull Block getBlock() {

View File

@ -5,7 +5,7 @@ import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
public class AxisPlacementRule extends BlockPlacementRule {
@ -15,13 +15,13 @@ public class AxisPlacementRule extends BlockPlacementRule {
}
@Override
public @NotNull Block blockUpdate(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @NotNull Block block) {
public @NotNull Block blockUpdate(@NotNull Instance instance, @NotNull Point blockPosition, @NotNull Block block) {
return block;
}
@Override
public Block blockPlace(@NotNull Instance instance,
@NotNull Block block, @NotNull BlockFace blockFace, @NotNull BlockPosition blockPosition,
@NotNull Block block, @NotNull BlockFace blockFace, @NotNull Point blockPosition,
@NotNull Player pl) {
String axis = "y";
if (blockFace == BlockFace.WEST || blockFace == BlockFace.EAST) {

View File

@ -5,8 +5,9 @@ import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.utils.BlockPosition;
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;
@ -18,7 +19,7 @@ public class RedstonePlacementRule extends BlockPlacementRule {
}
@Override
public @NotNull Block blockUpdate(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @NotNull Block block) {
public @NotNull Block blockUpdate(@NotNull Instance instance, @NotNull Point blockPosition, @NotNull Block block) {
BlockUtils blockUtils = new BlockUtils(instance, blockPosition);
String east = "none";
@ -98,9 +99,9 @@ public class RedstonePlacementRule extends BlockPlacementRule {
@Override
public Block blockPlace(@NotNull Instance instance,
@NotNull Block block, @NotNull BlockFace blockFace, @NotNull BlockPosition blockPosition,
@NotNull Block block, @NotNull BlockFace blockFace, @NotNull Point blockPosition,
@NotNull Player pl) {
final Block belowBlock = instance.getBlock(blockPosition.getX(), blockPosition.getY() - 1, blockPosition.getZ());
final Block belowBlock = instance.getBlock(new Vec(0, -1, 0).add(blockPosition));
return belowBlock.isSolid() ? block : null;
}
}

View File

@ -7,6 +7,7 @@ import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -19,14 +20,14 @@ public class StairsPlacementRule extends BlockPlacementRule {
}
@Override
public @NotNull Block blockUpdate(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @NotNull Block block) {
public @NotNull Block blockUpdate(@NotNull Instance instance, @NotNull Point blockPosition, @NotNull Block block) {
return block;
}
@Override
public Block blockPlace(@NotNull Instance instance,
@NotNull Block block, @NotNull BlockFace blockFace,
@NotNull BlockPosition blockPosition, @NotNull Player player) {
@NotNull Point blockPosition, @NotNull Player player) {
Facing facing = this.getFacing(player);
Shape shape = this.getShape(instance, blockPosition, facing);
BlockFace half = BlockFace.BOTTOM; // waiting for new block faces to be implemented
@ -75,8 +76,10 @@ public class StairsPlacementRule extends BlockPlacementRule {
}
@NotNull
public Pair<@Nullable Shape, @Nullable Facing> getFront(@NotNull Instance instance, @NotNull BlockPosition blockPosition) {
return this.getProperties(instance, blockPosition.clone().add(this.front));
public Pair<@Nullable Shape, @Nullable Facing> getFront(@NotNull Instance instance, @NotNull Point blockPosition) {
// TODO FIX
return null;
//return this.getProperties(instance, blockPosition.clone().add(this.front));
}
@NotNull
@ -87,7 +90,7 @@ public class StairsPlacementRule extends BlockPlacementRule {
@NotNull
private Pair<@Nullable Shape, @Nullable Facing> getProperties(@NotNull Instance instance, @NotNull BlockPosition blockPosition) {
Block block = instance.getBlock(blockPosition);
if (block == null) {
if (block.isAir()) {
return Pair.of(null, null);
}
Block state = instance.getBlock(blockPosition);
@ -104,14 +107,16 @@ public class StairsPlacementRule extends BlockPlacementRule {
}
@NotNull
private Shape getShape(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @NotNull Facing facing) {
Pair<Shape, Facing> front = facing.getFront(instance, blockPosition);
private Shape getShape(@NotNull Instance instance, @NotNull Point blockPosition, @NotNull Facing facing) {
// TODO FIX
return null;
/*Pair<Shape, Facing> front = facing.getFront(instance, blockPosition);
Pair<Shape, Facing> back = facing.getBack(instance, blockPosition);
Shape shape = this.getShapeFromSide(front, facing, Shape.INNER_RIGHT, Shape.INNER_LEFT);
if (shape == null) {
shape = this.getShapeFromSide(back, facing, Shape.OUTER_RIGHT, Shape.OUTER_LEFT);
}
return shape == null ? Shape.STRAIGHT : shape;
return shape == null ? Shape.STRAIGHT : shape;*/
}
@Nullable

View File

@ -5,7 +5,7 @@ import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.instance.block.rule.BlockPlacementRule;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
@ -17,10 +17,10 @@ public class WallPlacementRule extends BlockPlacementRule {
}
@Override
public @NotNull Block blockUpdate(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @NotNull Block block) {
final int x = blockPosition.getX();
final int y = blockPosition.getY();
final int z = blockPosition.getZ();
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();
String east = "none";
String north = "none";
@ -56,7 +56,7 @@ public class WallPlacementRule extends BlockPlacementRule {
@Override
public Block blockPlace(@NotNull Instance instance,
@NotNull Block block, @NotNull BlockFace blockFace, @NotNull BlockPosition blockPosition,
@NotNull Block block, @NotNull BlockFace blockFace, @NotNull Point blockPosition,
@NotNull Player pl) {
return block;
}

View File

@ -22,9 +22,10 @@ import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
import net.minestom.server.network.packet.client.play.ClientPlayerBlockPlacementPacket;
import net.minestom.server.network.packet.server.play.BlockChangePacket;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.Direction;
import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.coordinate.Point;
import net.minestom.server.utils.coordinate.Vec;
import net.minestom.server.utils.validate.Check;
import java.util.Set;
@ -37,7 +38,7 @@ public class BlockPlacementListener {
final PlayerInventory playerInventory = player.getInventory();
final Player.Hand hand = packet.hand;
final BlockFace blockFace = packet.blockFace;
final BlockPosition blockPosition = packet.blockPosition;
final Point blockPosition = packet.blockPosition;
final Direction direction = blockFace.toDirection();
final Instance instance = player.getInstance();
@ -90,22 +91,21 @@ public class BlockPlacementListener {
final int offsetY = blockFace == BlockFace.BOTTOM ? -1 : blockFace == BlockFace.TOP ? 1 : 0;
final int offsetZ = blockFace == BlockFace.NORTH ? -1 : blockFace == BlockFace.SOUTH ? 1 : 0;
blockPosition.add(offsetX, offsetY, offsetZ);
final Point placementPosition = new Vec(offsetX, offsetY, offsetZ).add(blockPosition);
if (!canPlaceBlock) {
if (useMaterial.isBlock()) {
//Send a block change with AIR as block to keep the client in sync,
//using refreshChunk results in the client not being in sync
//after rapid invalid block placements
player.getPlayerConnection().sendPacket(new BlockChangePacket(blockPosition, Block.AIR.stateId()));
player.getPlayerConnection().sendPacket(new BlockChangePacket(placementPosition, Block.AIR.stateId()));
}
return;
}
final Chunk chunk = instance.getChunkAt(blockPosition);
final Chunk chunk = instance.getChunkAt(placementPosition);
Check.stateCondition(!ChunkUtils.isLoaded(chunk),
"A player tried to place a block in the border of a loaded chunk " + blockPosition);
"A player tried to place a block in the border of a loaded chunk {0}", placementPosition);
// The concerned chunk will be send to the player if an error occur
// This will ensure that the player has the correct version of the chunk
@ -116,7 +116,7 @@ public class BlockPlacementListener {
final Block placedBlock = useMaterial.getBlock();
final Set<Entity> entities = instance.getChunkEntities(chunk);
// Check if the player is trying to place a block in an entity
boolean intersect = player.getBoundingBox().intersect(blockPosition);
boolean intersect = player.getBoundingBox().intersectWithBlock(placementPosition);
if (!intersect && placedBlock.isSolid()) {
// TODO push entities too close to the position
for (Entity entity : entities) {
@ -124,25 +124,22 @@ public class BlockPlacementListener {
if (entity == player ||
entity.getEntityType() == EntityType.ITEM)
continue;
// Marker Armor Stands should not prevent block placement
if(entity.getEntityMeta() instanceof ArmorStandMeta) {
if (entity.getEntityMeta() instanceof ArmorStandMeta) {
ArmorStandMeta armorStandMeta = (ArmorStandMeta) entity.getEntityMeta();
if(armorStandMeta.isMarker()) {
if (armorStandMeta.isMarker()) {
continue;
}
}
intersect = entity.getBoundingBox().intersect(blockPosition);
intersect = entity.getBoundingBox().intersectWithBlock(placementPosition);
if (intersect)
break;
}
}
if (!intersect) {
// BlockPlaceEvent check
PlayerBlockPlaceEvent playerBlockPlaceEvent = new PlayerBlockPlaceEvent(player, placedBlock, blockPosition, packet.hand);
PlayerBlockPlaceEvent playerBlockPlaceEvent = new PlayerBlockPlaceEvent(player, placedBlock, placementPosition, packet.hand);
playerBlockPlaceEvent.consumeBlock(player.getGameMode() != GameMode.CREATIVE);
EventDispatcher.call(playerBlockPlaceEvent);
@ -158,7 +155,7 @@ public class BlockPlacementListener {
final boolean placementRuleCheck = resultBlock != null;
if (placementRuleCheck) {
// Place the block
instance.placeBlock(player, resultBlock, blockPosition,
instance.placeBlock(player, resultBlock, placementPosition,
blockFace, packet.cursorPositionX, packet.cursorPositionY, packet.cursorPositionZ);
// Block consuming
if (playerBlockPlaceEvent.doesConsumeBlock()) {
@ -180,8 +177,7 @@ public class BlockPlacementListener {
}
} else {
// Player didn't try to place a block but interacted with one
final BlockPosition usePosition = blockPosition.clone().subtract(offsetX, offsetY, offsetZ);
PlayerUseItemOnBlockEvent event = new PlayerUseItemOnBlockEvent(player, hand, usedItem, usePosition, direction);
PlayerUseItemOnBlockEvent event = new PlayerUseItemOnBlockEvent(player, hand, usedItem, blockPosition, direction);
EventDispatcher.call(event);
refreshChunk = true;
}
@ -191,5 +187,4 @@ public class BlockPlacementListener {
chunk.sendChunk(player);
}
}
}

View File

@ -14,13 +14,14 @@ import net.minestom.server.item.StackingRule;
import net.minestom.server.network.packet.client.play.ClientPlayerDiggingPacket;
import net.minestom.server.network.packet.server.play.AcknowledgePlayerDiggingPacket;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
public class PlayerDiggingListener {
public static void playerDiggingListener(ClientPlayerDiggingPacket packet, Player player) {
final ClientPlayerDiggingPacket.Status status = packet.status;
final BlockPosition blockPosition = packet.blockPosition;
final Point blockPosition = packet.blockPosition;
final Instance instance = player.getInstance();
@ -137,7 +138,7 @@ public class PlayerDiggingListener {
private static void breakBlock(Instance instance,
Player player,
BlockPosition blockPosition, Block block,
Point blockPosition, Block block,
ClientPlayerDiggingPacket.Status status) {
// Unverified block break, client is fully responsible
final boolean result = instance.breakBlock(player, blockPosition);
@ -177,7 +178,7 @@ public class PlayerDiggingListener {
* @param status the status of the digging
* @param success true to notify of a success, false otherwise
*/
private static void sendAcknowledgePacket(@NotNull Player player, @NotNull BlockPosition blockPosition, Block block,
private static void sendAcknowledgePacket(@NotNull Player player, @NotNull Point blockPosition, Block block,
@NotNull ClientPlayerDiggingPacket.Status status, boolean success) {
player.getPlayerConnection().sendPacket(new AcknowledgePlayerDiggingPacket(blockPosition, block.stateId(), status, success));
}

View File

@ -1,14 +1,15 @@
package net.minestom.server.network.packet.client.play;
import net.minestom.server.network.packet.client.ClientPlayPacket;
import net.minestom.server.utils.BlockPosition;
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 ClientGenerateStructurePacket extends ClientPlayPacket {
public BlockPosition blockPosition = new BlockPosition(0,0,0);
public Point blockPosition = Vec.ZERO;
public int level;
public boolean keepJigsaws;

View File

@ -6,12 +6,14 @@ import net.minestom.server.network.packet.client.ClientPlayPacket;
import net.minestom.server.utils.BlockPosition;
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 ClientPlayerBlockPlacementPacket extends ClientPlayPacket {
public Player.Hand hand = Player.Hand.MAIN;
public BlockPosition blockPosition = new BlockPosition(0,0,0);
public Point blockPosition = Vec.ZERO;
public BlockFace blockFace = BlockFace.TOP;
public float cursorPositionX, cursorPositionY, cursorPositionZ;
public boolean insideBlock;

View File

@ -2,15 +2,16 @@ package net.minestom.server.network.packet.client.play;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.network.packet.client.ClientPlayPacket;
import net.minestom.server.utils.BlockPosition;
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 ClientPlayerDiggingPacket extends ClientPlayPacket {
public Status status = Status.SWAP_ITEM_HAND;
public BlockPosition blockPosition = new BlockPosition(0,0,0);
public Point blockPosition = Vec.ZERO;
public BlockFace blockFace = BlockFace.TOP;
@Override

View File

@ -1,15 +1,16 @@
package net.minestom.server.network.packet.client.play;
import net.minestom.server.network.packet.client.ClientPlayPacket;
import net.minestom.server.utils.BlockPosition;
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 ClientQueryBlockNbtPacket extends ClientPlayPacket {
public int transactionId;
public BlockPosition blockPosition = new BlockPosition(0,0,0);
public Point blockPosition = Vec.ZERO;
@Override
public void read(@NotNull BinaryReader reader) {

View File

@ -1,20 +1,21 @@
package net.minestom.server.network.packet.client.play;
import net.minestom.server.network.packet.client.ClientPlayPacket;
import net.minestom.server.utils.BlockPosition;
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 ClientUpdateCommandBlockPacket extends ClientPlayPacket {
public BlockPosition blockPosition;
public Point blockPosition;
public String command;
public Mode mode;
public byte flags;
public ClientUpdateCommandBlockPacket() {
blockPosition = new BlockPosition(0,0,0);
blockPosition = Vec.ZERO;
command = "";
mode = Mode.REDSTONE;
}

View File

@ -1,14 +1,15 @@
package net.minestom.server.network.packet.client.play;
import net.minestom.server.network.packet.client.ClientPlayPacket;
import net.minestom.server.utils.BlockPosition;
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 ClientUpdateSignPacket extends ClientPlayPacket {
public BlockPosition blockPosition = new BlockPosition(0,0,0);
public Point blockPosition = Vec.ZERO;
public String line1 = "";
public String line2 = "";
public String line3 = "";
@ -26,13 +27,13 @@ public class ClientUpdateSignPacket extends ClientPlayPacket {
@Override
public void write(@NotNull BinaryWriter writer) {
writer.writeBlockPosition(blockPosition);
if(line1.length() > 384)
if (line1.length() > 384)
throw new IllegalArgumentException("line1 is too long! Signs allow a maximum of 384 characters per line.");
if(line2.length() > 384)
if (line2.length() > 384)
throw new IllegalArgumentException("line2 is too long! Signs allow a maximum of 384 characters per line.");
if(line3.length() > 384)
if (line3.length() > 384)
throw new IllegalArgumentException("line3 is too long! Signs allow a maximum of 384 characters per line.");
if(line4.length() > 384)
if (line4.length() > 384)
throw new IllegalArgumentException("line4 is too long! Signs allow a maximum of 384 characters per line.");
writer.writeSizedString(line1);
writer.writeSizedString(line2);

View File

@ -1,10 +1,11 @@
package net.minestom.server.network.packet.client.play;
import net.minestom.server.network.packet.client.ClientPlayPacket;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.Rotation;
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 ClientUpdateStructureBlockPacket extends ClientPlayPacket {
@ -16,12 +17,12 @@ public class ClientUpdateStructureBlockPacket extends ClientPlayPacket {
*/
public static final byte SHOW_BOUNDING_BOX = 0x4;
public BlockPosition location = new BlockPosition(0, 0, 0);
public Point location = Vec.ZERO;
public Action action = Action.UPDATE_DATA;
public Mode mode = Mode.DATA;
public String name = "";
public BlockPosition offset = new BlockPosition(0, 1, 0);
public BlockPosition size = new BlockPosition(1, 1, 1);
public Point offset = new Vec(0, 1, 0);
public Point size = Vec.ONE;
public Mirror mirror = Mirror.NONE;
public Rotation rotation = Rotation.NONE;
public String metadata = "";
@ -35,12 +36,12 @@ public class ClientUpdateStructureBlockPacket extends ClientPlayPacket {
writer.writeVarInt(action.ordinal());
writer.writeVarInt(mode.ordinal());
writer.writeSizedString(name);
writer.writeByte((byte) offset.getX());
writer.writeByte((byte) offset.getY());
writer.writeByte((byte) offset.getZ());
writer.writeByte((byte) size.getX());
writer.writeByte((byte) size.getY());
writer.writeByte((byte) size.getZ());
writer.writeByte((byte) offset.x());
writer.writeByte((byte) offset.y());
writer.writeByte((byte) offset.z());
writer.writeByte((byte) size.x());
writer.writeByte((byte) size.y());
writer.writeByte((byte) size.z());
writer.writeVarInt(mirror.ordinal());
writer.writeVarInt(toRestrictedRotation(rotation));
writer.writeSizedString(metadata);
@ -55,12 +56,12 @@ public class ClientUpdateStructureBlockPacket extends ClientPlayPacket {
action = Action.values()[reader.readVarInt()];
mode = Mode.values()[reader.readVarInt()];
name = reader.readSizedString(Short.MAX_VALUE);
offset = new BlockPosition(
offset = new Vec(
reader.readByte(),
reader.readByte(),
reader.readByte()
);
size = new BlockPosition(
size = new Vec(
reader.readByte(),
reader.readByte(),
reader.readByte()
@ -90,22 +91,31 @@ public class ClientUpdateStructureBlockPacket extends ClientPlayPacket {
private int toRestrictedRotation(Rotation rotation) {
switch (rotation) {
case NONE: return 0;
case CLOCKWISE: return 1;
case FLIPPED: return 2;
case COUNTER_CLOCKWISE: return 3;
default: throw new IllegalArgumentException("ClientUpdateStructurePacket#rotation must be a valid 90-degree rotation.");
case NONE:
return 0;
case CLOCKWISE:
return 1;
case FLIPPED:
return 2;
case COUNTER_CLOCKWISE:
return 3;
default:
throw new IllegalArgumentException("ClientUpdateStructurePacket#rotation must be a valid 90-degree rotation.");
}
}
private Rotation fromRestrictedRotation(int rotation) {
switch (rotation) {
case 0: return Rotation.NONE;
case 1: return Rotation.CLOCKWISE;
case 2: return Rotation.FLIPPED;
case 3: return Rotation.COUNTER_CLOCKWISE;
default: throw new IllegalArgumentException("ClientUpdateStructurePacket#rotation must be a valid 90-degree rotation.");
case 0:
return Rotation.NONE;
case 1:
return Rotation.CLOCKWISE;
case 2:
return Rotation.FLIPPED;
case 3:
return Rotation.COUNTER_CLOCKWISE;
default:
throw new IllegalArgumentException("ClientUpdateStructurePacket#rotation must be a valid 90-degree rotation.");
}
}
}

View File

@ -3,19 +3,20 @@ package net.minestom.server.network.packet.server.play;
import net.minestom.server.network.packet.client.play.ClientPlayerDiggingPacket;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.BlockPosition;
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 AcknowledgePlayerDiggingPacket implements ServerPacket {
public BlockPosition blockPosition;
public Point blockPosition;
public int blockStateId;
public ClientPlayerDiggingPacket.Status status;
public boolean successful;
public AcknowledgePlayerDiggingPacket(@NotNull BlockPosition blockPosition, int blockStateId,
public AcknowledgePlayerDiggingPacket(@NotNull Point blockPosition, int blockStateId,
@NotNull ClientPlayerDiggingPacket.Status status, boolean success) {
this.blockPosition = blockPosition;
this.blockStateId = blockStateId;
@ -24,7 +25,7 @@ public class AcknowledgePlayerDiggingPacket implements ServerPacket {
}
public AcknowledgePlayerDiggingPacket() {
this(new BlockPosition(0, 0, 0), 0, ClientPlayerDiggingPacket.Status.STARTED_DIGGING, false);
this(Vec.ZERO, 0, ClientPlayerDiggingPacket.Status.STARTED_DIGGING, false);
}
@Override
@ -37,10 +38,10 @@ public class AcknowledgePlayerDiggingPacket implements ServerPacket {
@Override
public void read(@NotNull BinaryReader reader) {
blockPosition = reader.readBlockPosition();
blockStateId = reader.readVarInt();
status = ClientPlayerDiggingPacket.Status.values()[reader.readVarInt()];
successful = reader.readBoolean();
this.blockPosition = reader.readBlockPosition();
this.blockStateId = reader.readVarInt();
this.status = ClientPlayerDiggingPacket.Status.values()[reader.readVarInt()];
this.successful = reader.readBoolean();
}
@Override

View File

@ -2,20 +2,21 @@ package net.minestom.server.network.packet.server.play;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.BlockPosition;
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 BlockActionPacket implements ServerPacket {
public BlockPosition blockPosition;
public Point blockPosition;
public byte actionId;
public byte actionParam;
public int blockId;
public BlockActionPacket() {
blockPosition = new BlockPosition(0,0,0);
blockPosition = Vec.ZERO;
}
@Override
@ -28,10 +29,10 @@ public class BlockActionPacket implements ServerPacket {
@Override
public void read(@NotNull BinaryReader reader) {
blockPosition = reader.readBlockPosition();
actionId = reader.readByte();
actionParam = reader.readByte();
blockId = reader.readVarInt();
this.blockPosition = reader.readBlockPosition();
this.actionId = reader.readByte();
this.actionParam = reader.readByte();
this.blockId = reader.readVarInt();
}
@Override

View File

@ -2,22 +2,23 @@ package net.minestom.server.network.packet.server.play;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.BlockPosition;
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 BlockBreakAnimationPacket implements ServerPacket {
public int entityId;
public BlockPosition blockPosition;
public Point blockPosition;
public byte destroyStage;
public BlockBreakAnimationPacket() {
blockPosition = new BlockPosition(0,0,0);
blockPosition = Vec.ZERO;
}
public BlockBreakAnimationPacket(int entityId, BlockPosition blockPosition, byte destroyStage) {
public BlockBreakAnimationPacket(int entityId, Point blockPosition, byte destroyStage) {
this.entityId = entityId;
this.blockPosition = blockPosition;
this.destroyStage = destroyStage;
@ -32,9 +33,9 @@ public class BlockBreakAnimationPacket implements ServerPacket {
@Override
public void read(@NotNull BinaryReader reader) {
entityId = reader.readVarInt();
blockPosition = reader.readBlockPosition();
destroyStage = reader.readByte();
this.entityId = reader.readVarInt();
this.blockPosition = reader.readBlockPosition();
this.destroyStage = reader.readByte();
}
@Override

View File

@ -2,23 +2,24 @@ package net.minestom.server.network.packet.server.play;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.BlockPosition;
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 BlockChangePacket implements ServerPacket {
public BlockPosition blockPosition;
public Point blockPosition;
public int blockStateId;
public BlockChangePacket(BlockPosition blockPosition, int blockStateId) {
public BlockChangePacket(Point blockPosition, int blockStateId) {
this.blockPosition = blockPosition;
this.blockStateId = blockStateId;
}
public BlockChangePacket() {
this(new BlockPosition(0, 0, 0), 0);
this(Vec.ZERO, 0);
}
@Override
@ -29,8 +30,8 @@ public class BlockChangePacket implements ServerPacket {
@Override
public void read(@NotNull BinaryReader reader) {
blockPosition = reader.readBlockPosition();
blockStateId = reader.readVarInt();
this.blockPosition = reader.readBlockPosition();
this.blockStateId = reader.readVarInt();
}
@Override

View File

@ -3,9 +3,10 @@ package net.minestom.server.network.packet.server.play;
import net.minestom.server.MinecraftServer;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.BlockPosition;
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;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
@ -15,12 +16,12 @@ import java.io.IOException;
public class BlockEntityDataPacket implements ServerPacket {
public BlockPosition blockPosition;
public Point blockPosition;
public byte action;
public NBTCompound nbtCompound;
public BlockEntityDataPacket() {
blockPosition = new BlockPosition(0,0,0);
blockPosition = Vec.ZERO;
}
@Override
@ -41,7 +42,7 @@ public class BlockEntityDataPacket implements ServerPacket {
action = reader.readByte();
try {
NBT tag = reader.readTag();
if(tag instanceof NBTCompound) {
if (tag instanceof NBTCompound) {
nbtCompound = (NBTCompound) tag;
}
} catch (IOException | NBTException e) {

View File

@ -2,20 +2,21 @@ package net.minestom.server.network.packet.server.play;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.BlockPosition;
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 EffectPacket implements ServerPacket {
public int effectId;
public BlockPosition position;
public Point position;
public int data;
public boolean disableRelativeVolume;
public EffectPacket() {
position = new BlockPosition(0,0,0);
position = Vec.ZERO;
}
@Override
@ -28,10 +29,10 @@ public class EffectPacket implements ServerPacket {
@Override
public void read(@NotNull BinaryReader reader) {
effectId = reader.readInt();
position = reader.readBlockPosition();
data = reader.readInt();
disableRelativeVolume = reader.readBoolean();
this.effectId = reader.readInt();
this.position = reader.readBlockPosition();
this.data = reader.readInt();
this.disableRelativeVolume = reader.readBoolean();
}
@Override

View File

@ -2,20 +2,21 @@ package net.minestom.server.network.packet.server.play;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.BlockPosition;
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 OpenSignEditorPacket implements ServerPacket {
/**
* WARNING: There must be a sign in this location (you can send a BlockChangePacket beforehand)
*/
public BlockPosition signPosition;
*/
public Point signPosition;
public OpenSignEditorPacket() {
signPosition = new BlockPosition(0,0,0);
signPosition = Vec.ZERO;
}
@Override
@ -25,7 +26,7 @@ public class OpenSignEditorPacket implements ServerPacket {
@Override
public void read(@NotNull BinaryReader reader) {
signPosition = reader.readBlockPosition();
this.signPosition = reader.readBlockPosition();
}
@Override

View File

@ -2,9 +2,10 @@ package net.minestom.server.network.packet.server.play;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.BlockPosition;
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;
import java.util.UUID;
@ -14,12 +15,12 @@ public class SpawnPaintingPacket implements ServerPacket {
public int entityId;
public UUID entityUuid;
public int motive;
public BlockPosition position;
public Point position;
public byte direction;
public SpawnPaintingPacket() {
entityUuid = new UUID(0, 0);
position = new BlockPosition(0, 0, 0);
position = Vec.ZERO;
}
@Override
@ -33,11 +34,11 @@ public class SpawnPaintingPacket implements ServerPacket {
@Override
public void read(@NotNull BinaryReader reader) {
entityId = reader.readVarInt();
entityUuid = reader.readUuid();
motive = reader.readVarInt();
position = reader.readBlockPosition();
direction = reader.readByte();
this.entityId = reader.readVarInt();
this.entityUuid = reader.readUuid();
this.motive = reader.readVarInt();
this.position = reader.readBlockPosition();
this.direction = reader.readByte();
}
@Override

View File

@ -2,9 +2,9 @@ package net.minestom.server.network.packet.server.play;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.ServerPacketIdentifier;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.binary.BinaryReader;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
public class SpawnPositionPacket implements ServerPacket {
@ -12,7 +12,8 @@ public class SpawnPositionPacket implements ServerPacket {
public int x, y, z;
public float angle;
public SpawnPositionPacket() {}
public SpawnPositionPacket() {
}
@Override
public void write(@NotNull BinaryWriter writer) {
@ -22,11 +23,11 @@ public class SpawnPositionPacket implements ServerPacket {
@Override
public void read(@NotNull BinaryReader reader) {
BlockPosition pos = reader.readBlockPosition();
x = pos.getX();
y = pos.getY();
z = pos.getZ();
angle = reader.readFloat();
Point pos = reader.readBlockPosition();
this.x = (int) pos.x();
this.y = (int) pos.y();
this.z = (int) pos.z();
this.angle = reader.readFloat();
}
@Override

View File

@ -1,6 +1,7 @@
package net.minestom.server.utils;
import net.minestom.server.instance.block.BlockFace;
import net.minestom.server.utils.clone.PublicCloneable;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
@ -230,7 +231,7 @@ public class BlockPosition implements Point {
}
@Override
public @NotNull Point clone() {
public @NotNull BlockPosition clone() {
return new BlockPosition(x, y, z);
}

View File

@ -91,6 +91,11 @@ public final class MathUtils {
return Math.min(Math.max(value, min), max);
}
public static int floor(double num) {
final int floor = (int) num;
return floor == num ? floor : floor - (int) (Double.doubleToRawLongBits(num) >>> 63);
}
public static double mod(final double a, final double b) {
return (a % b + b) % b;
}

View File

@ -1,5 +1,8 @@
package net.minestom.server.utils;
import net.minestom.server.utils.coordinate.Point;
import net.minestom.server.utils.coordinate.Vec;
public final class SerializerUtils {
private SerializerUtils() {
@ -10,11 +13,10 @@ public final class SerializerUtils {
return (((long) x & 0x3FFFFFF) << 38) | (((long) z & 0x3FFFFFF) << 12) | ((long) y & 0xFFF);
}
public static BlockPosition longToBlockPosition(long value) {
public static Point longToBlockPosition(long value) {
final int x = (int) (value >> 38);
final int y = (int) (value & 0xFFF);
final int z = (int) (value << 26 >> 38);
return new BlockPosition(x, y, z);
return new Vec(x, y, z);
}
}

View File

@ -10,6 +10,7 @@ import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.NBTUtils;
import net.minestom.server.utils.SerializerUtils;
import net.minestom.server.utils.Utils;
import net.minestom.server.utils.coordinate.Point;
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBT;
@ -164,7 +165,7 @@ public class BinaryReader extends InputStream {
return readBytes(available());
}
public BlockPosition readBlockPosition() {
public Point readBlockPosition() {
final long value = buffer.readLong();
return SerializerUtils.longToBlockPosition(value);
}

View File

@ -6,12 +6,11 @@ import io.netty.buffer.Unpooled;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minestom.server.MinecraftServer;
import net.minestom.server.adventure.AdventureSerializer;
import net.minestom.server.chat.JsonMessage;
import net.minestom.server.item.ItemStack;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.SerializerUtils;
import net.minestom.server.utils.Utils;
import net.minestom.server.utils.coordinate.Point;
import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTWriter;
@ -268,8 +267,8 @@ public class BinaryWriter extends OutputStream {
writeLong(uuid.getLeastSignificantBits());
}
public void writeBlockPosition(@NotNull BlockPosition blockPosition) {
writeBlockPosition(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
public void writeBlockPosition(@NotNull Point point) {
writeBlockPosition((int) point.x(), (int) point.y(), (int) point.z());
}
public void writeBlockPosition(int x, int y, int z) {

View File

@ -2,8 +2,9 @@ package net.minestom.server.utils.block;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.utils.BlockPosition;
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;
@ -12,16 +13,15 @@ import java.util.Map;
public class BlockUtils {
private final Instance instance;
private final BlockPosition position;
private final Point position;
public BlockUtils(Instance instance, BlockPosition position) {
public BlockUtils(Instance instance, Point position) {
this.instance = instance;
this.position = position;
}
public BlockUtils getRelativeTo(int x, int y, int z) {
BlockPosition position = this.position.clone().add(x, y, z);
return new BlockUtils(instance, position);
return new BlockUtils(instance, new Vec(x, y, z).add(position));
}
public BlockUtils above() {

View File

@ -38,6 +38,21 @@ public interface Point {
@Contract(pure = true)
double z();
@Contract(pure = true)
default int blockX() {
return MathUtils.floor(x());
}
@Contract(pure = true)
default int blockY() {
return MathUtils.floor(y());
}
@Contract(pure = true)
default int blockZ() {
return MathUtils.floor(z());
}
/**
* 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
@ -67,26 +82,4 @@ public interface Point {
MathUtils.square(y() - point.y()) +
MathUtils.square(z() - point.z());
}
/**
* Converts all coordinates to integers.
*
* @return a new point representing a block position
*/
@Contract(pure = true)
default @NotNull Point asBlockPosition() {
final int castedY = (int) y();
return new Vec((int) Math.floor(x()),
(y() == castedY) ? castedY : castedY + 1,
(int) Math.floor(z()));
}
/**
* @deprecated present for backward compatibility
*/
@Deprecated
@Contract(pure = true)
default @NotNull Point clone() {
return this;
}
}