Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/block/component/CollisionBoxComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ class CollisionBoxComponent implements BlockComponent {
private Vector3 $size;

/**
* Defines the area of the block that collides with entities. If set to true, default values are used. If set to false, the block's collision with entities is disabled. If this component is omitted, default values are used.
* @param Vector3 $origin Minimal position of the bounds of the collision box. "origin" is specified as [x, y, z] and must be in the range (-8, 0, -8) to (8, 16, 8), inclusive.
* @param Vector3 $size Size of each side of the collision box. Size is specified as [x, y, z]. "origin" + "size" must be in the range (-8, 0, -8) to (8, 16, 8), inclusive.
* Defines the area of the block that collides with entities. If set to true, default values are used. If set to
* false, the block's collision with entities is disabled. If this component is omitted, default values are used.
* @param Vector3 $origin Minimal position of the bounds of the collision box. "origin" is specified as [x, y, z]
* and must be in the range (-8, 0, -8) to (8, 16, 8), inclusive.
* @param Vector3 $size Size of each side of the collision box. Size is specified as [x, y, z]. "origin" + "size"
* must be in the range (-8, 0, -8) to (8, 16, 8), inclusive.
* @param bool $useCollisionBox If collision should be enabled, default is set to `true`.
*/
public function __construct(bool $useCollisionBox = true, Vector3 $origin = new Vector3(-8.0, 0.0, -8.0), Vector3 $size = new Vector3(16.0, 16.0, 16.0)) {
Expand All @@ -30,6 +33,23 @@ public function getName(): string {
}

public function getValue(): CompoundTag {

$minX = max(0, $this->origin->getX() + 8);
$minY = max(0, $this->origin->getY());
$minZ = max(0, $this->origin->getZ() + 8);

$maxX = min(16, $minX + $this->size->getX());
$maxY = min(16, $minY + $this->size->getY());
$maxZ = min(16, $minZ + $this->size->getZ());

$box = CompoundTag::create()
->setFloat("minX", $minX)
->setFloat("minY", $minY)
->setFloat("minZ", $minZ)
->setFloat("maxX", $maxX)
->setFloat("maxY", $maxY)
->setFloat("maxZ", $maxZ);
$boxes = new ListTag([$box]);
return CompoundTag::create()
->setByte("enabled", $this->useCollisionBox ? 1 : 0)
->setTag("origin", new ListTag([
Expand All @@ -41,6 +61,7 @@ public function getValue(): CompoundTag {
new FloatTag($this->size->getX()),
new FloatTag($this->size->getY()),
new FloatTag($this->size->getZ())
]));
]))
->setTag("boxes", $boxes);
}
}