Skip to content

Commit

Permalink
Added 'Redstone Lamp' alternative using states. WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jul 10, 2020
1 parent 5b77f61 commit d68e17a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/lua-api-block.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ To get the current state of a block:
local block_param = world:get_data(pos.x, pos.y, pos.z)
local current_state = block:param():get_param(BlockParamType.State, block_param)
-- if you don't
local current_state = world:get_block_state(pos.x, pos.y, pos.z)
local current_state = world:get_block_state(pos.x, pos.y, pos.z):id()
```

To set the current state of a block:
Expand Down
6 changes: 6 additions & 0 deletions docs/lua-api-cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
- `BlockParamType.State`
- `BlockParamType.Count`

## BlockState

- `u16 id()`
- `std::string label()`
- `ItemStack get_item_drop()`

## Chunk

- `u16 get_block(int x, int y, int z)`
Expand Down
15 changes: 15 additions & 0 deletions mods/default/blocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,21 @@ mod:block {
end,
}

mod:block {
id = "redstone_lamp",
name = "Redstone Lamp (with states)",
tiles = "redstone_lamp_off.png",

states = {
{ is_light_source = true, tiles = "redstone_lamp_on.png" }
},

on_block_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
local current_state = math.abs(world:get_block_state(pos.x, pos.y, pos.z):id() - 1)
world:set_block_state(pos.x, pos.y, pos.z, current_state)
end
}

mod:block {
id = "redstone_lamp_off",
name = "Redstone Lamp",
Expand Down
1 change: 1 addition & 0 deletions source/common/world/BlockState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ bool BlockState::isOpaque() const {
// Please update 'docs/lua-api-cpp.md' if you change this
void BlockState::initUsertype(sol::state &lua) {
lua.new_usertype<BlockState>("BlockState",
"id", &BlockState::id,
"label", (const std::string &(BlockState::*)() const)&BlockState::label,
"get_item_drop", &BlockState::getItemDrop
);
Expand Down
15 changes: 15 additions & 0 deletions source/common/world/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ void Chunk::setBlockState(int x, int y, int z, u16 stateID) {
u16 blockID = getBlock(x, y, z);
u16 blockParam = getData(x, y, z);
const Block &block = Registry::getInstance().getBlock(blockID);
const BlockState &blockState = block.getState(block.param().hasParam(BlockParam::State)
? block.param().getParam(BlockParam::State, blockParam) : 0);
const BlockState &newBlockState = block.getState(block.param().hasParam(BlockParam::State)
? stateID : 0);

if (!blockState.isLightSource() && newBlockState.isLightSource()) {
m_lightmap.addTorchlight(x, y, z, 14);
}
else if (blockState.isLightSource() && !newBlockState.isLightSource()) {
gkDebug() << block.stringID();

m_lightmap.removeTorchlight(x, y, z);
m_lightmap.removeSunlight(x, y, z);
}

if (block.param().hasParam(BlockParam::State))
setData(x, y, z, block.param().setParam(BlockParam::State, blockParam, stateID));
}
Expand Down

0 comments on commit d68e17a

Please sign in to comment.