mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-22 02:19:44 +00:00
Merge pull request #1026 from ostermine/feat/can-be-placed
add can_be_placed block event
This commit is contained in:
commit
bccc6e154f
6 changed files with 68 additions and 0 deletions
|
|
@ -14,6 +14,26 @@ function on_placed(x, y, z, playerid)
|
||||||
|
|
||||||
Called on block placed by player
|
Called on block placed by player
|
||||||
|
|
||||||
|
```lua
|
||||||
|
function can_be_placed(x, y, z, playerid, rotation) -> bool
|
||||||
|
```
|
||||||
|
|
||||||
|
Called before a player places the block, after the built-in checks
|
||||||
|
(replaceability, `grounded`, intersection with entities).
|
||||||
|
The block is placed only if the function returns `true`.
|
||||||
|
`rotation` is the rotation index the block will be placed with
|
||||||
|
(see `block.get_rotation`).
|
||||||
|
|
||||||
|
Example: a sapling that can only be planted on dirt.
|
||||||
|
|
||||||
|
```lua
|
||||||
|
function can_be_placed(x, y, z, playerid, rotation)
|
||||||
|
return block.name(block.get(x, y - 1, z)) == "base:dirt"
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
The event is not called when the block is placed with `block.place` or `block.set`.
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
function on_broken(x, y, z, playerid)
|
function on_broken(x, y, z, playerid)
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,26 @@ function on_placed(x, y, z, playerid)
|
||||||
|
|
||||||
Вызывается после установки блока игроком
|
Вызывается после установки блока игроком
|
||||||
|
|
||||||
|
```lua
|
||||||
|
function can_be_placed(x, y, z, playerid, rotation) -> bool
|
||||||
|
```
|
||||||
|
|
||||||
|
Вызывается перед установкой блока игроком, после встроенных проверок
|
||||||
|
(заменяемость, `grounded`, пересечение с сущностями).
|
||||||
|
Блок устанавливается, только если функция возвращает `true`.
|
||||||
|
`rotation` - индекс поворота, с которым будет установлен блок
|
||||||
|
(см. `block.get_rotation`).
|
||||||
|
|
||||||
|
Пример: саженец, который можно посадить только на землю.
|
||||||
|
|
||||||
|
```lua
|
||||||
|
function can_be_placed(x, y, z, playerid, rotation)
|
||||||
|
return block.name(block.get(x, y - 1, z)) == "base:dirt"
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
Событие не вызывается при установке блока через `block.place` и `block.set`.
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
function on_broken(x, y, z, playerid)
|
function on_broken(x, y, z, playerid)
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -480,6 +480,11 @@ void PlayerController::processRightClick(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (chosenBlock != vox->id && chosenBlock) {
|
if (chosenBlock != vox->id && chosenBlock) {
|
||||||
|
if (!scripting::can_block_be_placed(
|
||||||
|
&player, def, coord, state.rotation
|
||||||
|
)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!player.isInfiniteItems()) {
|
if (!player.isInfiniteItems()) {
|
||||||
auto& slot = player.getInventory()->getSlot(player.getChosenSlot());
|
auto& slot = player.getInventory()->getSlot(player.getChosenSlot());
|
||||||
slot.setCount(slot.getCount() - 1);
|
slot.setCount(slot.getCount() - 1);
|
||||||
|
|
|
||||||
|
|
@ -451,6 +451,23 @@ static bool on_block_common(
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool scripting::can_block_be_placed(
|
||||||
|
Player* player, const Block& block, const glm::ivec3& pos, int rotation
|
||||||
|
) {
|
||||||
|
if (!block.rt.funcsset.canbeplaced) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
std::string name = block.name + ".canbeplaced";
|
||||||
|
return lua::emit_event(
|
||||||
|
lua::get_main_state(), name, [pos, player, rotation](auto L) {
|
||||||
|
lua::pushivec_stack(L, pos);
|
||||||
|
lua::pushinteger(L, player ? player->getId() : -1);
|
||||||
|
lua::pushinteger(L, rotation);
|
||||||
|
return 5;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
void scripting::on_block_placed(
|
void scripting::on_block_placed(
|
||||||
Player* player, const Block& block, const glm::ivec3& pos
|
Player* player, const Block& block, const glm::ivec3& pos
|
||||||
) {
|
) {
|
||||||
|
|
@ -717,6 +734,8 @@ void scripting::load_content_script(
|
||||||
register_event(env, "on_breaking", prefix + ".breaking");
|
register_event(env, "on_breaking", prefix + ".breaking");
|
||||||
funcsset.onbroken = register_event(env, "on_broken", prefix + ".broken");
|
funcsset.onbroken = register_event(env, "on_broken", prefix + ".broken");
|
||||||
funcsset.onplaced = register_event(env, "on_placed", prefix + ".placed");
|
funcsset.onplaced = register_event(env, "on_placed", prefix + ".placed");
|
||||||
|
funcsset.canbeplaced =
|
||||||
|
register_event(env, "can_be_placed", prefix + ".canbeplaced");
|
||||||
funcsset.onreplaced =
|
funcsset.onreplaced =
|
||||||
register_event(env, "on_replaced", prefix + ".replaced");
|
register_event(env, "on_replaced", prefix + ".replaced");
|
||||||
funcsset.oninteract =
|
funcsset.oninteract =
|
||||||
|
|
|
||||||
|
|
@ -92,6 +92,9 @@ namespace scripting {
|
||||||
void on_blocks_tick(const Block& block, int tps);
|
void on_blocks_tick(const Block& block, int tps);
|
||||||
void update_block(const Block& block, const glm::ivec3& pos);
|
void update_block(const Block& block, const glm::ivec3& pos);
|
||||||
void random_update_block(const Block& block, const glm::ivec3& pos);
|
void random_update_block(const Block& block, const glm::ivec3& pos);
|
||||||
|
bool can_block_be_placed(
|
||||||
|
Player* player, const Block& block, const glm::ivec3& pos, int rotation
|
||||||
|
);
|
||||||
void on_block_placed(
|
void on_block_placed(
|
||||||
Player* player, const Block& block, const glm::ivec3& pos
|
Player* player, const Block& block, const glm::ivec3& pos
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@ struct BlockFuncsSet {
|
||||||
bool init : 1;
|
bool init : 1;
|
||||||
bool update : 1;
|
bool update : 1;
|
||||||
bool onplaced : 1;
|
bool onplaced : 1;
|
||||||
|
bool canbeplaced : 1;
|
||||||
bool onbreaking : 1;
|
bool onbreaking : 1;
|
||||||
bool onbroken : 1;
|
bool onbroken : 1;
|
||||||
bool onreplaced : 1;
|
bool onreplaced : 1;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue