[Lua API] Screen size and gui scale are now available from ClientInfo.

This commit is contained in:
Quentin Bazin 2020-07-13 12:50:16 +02:00
parent 1485c15808
commit 79e0794389
16 changed files with 37 additions and 38 deletions

View File

@ -339,9 +339,6 @@ Parameters:
- `world` (`ServerWorld`): instance of the world
- `client` (`Client`): client that activated the block
- `server` (`Server`): current server
- `screen_width` (`u16`): width of the screen
- `screen_height` (`u16`): height of the screen
- `gui_scale` (`u8`): current scaling setting
### `on_block_destroyed`

View File

@ -55,7 +55,10 @@
## ClientInfo
- `u16 id()`
- `u16 id`
- `u16 screen_width`
- `u16 screen_height`
- `u8 gui_scale`
## Dimension

View File

@ -106,7 +106,4 @@ Parameters:
- `world` (`World`): instance of the `ServerWorld`
- `client` (`Client`): client that activated the item
- `server` (`Server`): current server
- `screen_width` (`u16`): width of the screen
- `screen_height` (`u16`): height of the screen
- `gui_scale` (`u8`): current scaling setting

View File

@ -8,8 +8,8 @@ mod:key {
name = "Inventory",
default_key = "E",
callback = function(keyID, client, screen_width, screen_height, gui_scale)
show_inventory(keyID, client, screen_width, screen_height, gui_scale)
callback = function(keyID, client)
show_inventory(keyID, client)
end
}
```
@ -22,8 +22,8 @@ Function called when the key is pressed.
Example:
```lua
callback = function(keyID, client, screen_width, screen_height, gui_scale)
show_inventory(keyID, client, screen_width, screen_height, gui_scale)
callback = function(keyID, client)
show_inventory(keyID, client)
end
```

View File

@ -229,7 +229,7 @@ mod:block {
draw_type = "glass",
is_opaque = false,
on_block_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
on_block_activated = function(pos, block, player, world, client, server)
local dim = (player:dimension() + 1) % 2
local pos = {
x = math.floor(player:x()),
@ -343,7 +343,7 @@ mod:block {
{ 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)
on_block_activated = function(pos, block, player, world, client, server)
local next_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, next_state)
end
@ -411,7 +411,7 @@ mod:block {
end
end,
on_block_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
on_block_activated = function(pos, block, player, world, client, server)
local block_param = world:get_data(pos.x, pos.y, pos.z)
local current_state = block:param():get_param(BlockParamType.State, block_param)
if current_state >= 7 then

View File

@ -69,7 +69,7 @@ mod:block {
end
end,
on_block_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
on_block_activated = function(pos, block, player, world, client, server)
open_door(pos, world)
open_door({x = pos.x, y = pos.y, z = pos.z - 1}, world)
end

View File

@ -46,7 +46,7 @@ mod:block {
world:add_block_data(pos.x, pos.y, pos.z, 3, 1)
end,
on_block_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
on_block_activated = function(pos, block, player, world, client, server)
local gui = LuaGUI.new()
gui:set_size(176, 166)

View File

@ -41,7 +41,7 @@ mod:block {
world:add_block_data(pos.x, pos.y, pos.z, 3, 3)
end,
on_block_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
on_block_activated = function(pos, block, player, world, client, server)
local gui = LuaGUI.new()
gui:set_size(176, 166)

View File

@ -41,7 +41,7 @@ function register_tool(name, material, mining_speed, harvest_capability)
end
if name == "hoe" then
tool_def.on_item_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
tool_def.on_item_activated = function(pos, block, player, world, client, server)
if block:string_id() == "default:grass" then
world:set_block_from_str(pos.x, pos.y, pos.z, "default:farmland")
end
@ -52,7 +52,7 @@ function register_tool(name, material, mining_speed, harvest_capability)
"group:om_material:sand"
}
tool_def.on_item_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
tool_def.on_item_activated = function(pos, block, player, world, client, server)
if block:string_id() == "default:grass" then
world:set_block_from_str(pos.x, pos.y, pos.z, "default:grass_path")
end

View File

@ -89,7 +89,10 @@ void ScriptEngine::initUsertypes() {
);
m_lua.new_usertype<ClientInfo>("ClientInfo",
"id", &ClientInfo::id
"id", &ClientInfo::id,
"screen_width", &ClientInfo::screenWidth,
"screen_height", &ClientInfo::screenHeight,
"gui_scale", &ClientInfo::guiScale
);
LuaCore::initUsertype(m_lua);

View File

@ -48,6 +48,10 @@ class ClientInfo {
NetworkInputHandler inputHandler;
std::string playerName;
u16 screenWidth = 1;
u16 screenHeight = 1;
u8 guiScale = 1;
};
#endif // CLIENTINFO_HPP_

View File

@ -325,15 +325,13 @@ void ServerCommandHandler::setupCallbacks() {
ServerPlayer *player = m_players.getPlayerFromClientID(client.id);
if (player) {
s32 x, y, z;
u16 screenWidth, screenHeight;
u8 guiScale;
packet >> x >> y >> z >> screenWidth >> screenHeight >> guiScale;
packet >> x >> y >> z >> client.screenWidth >> client.screenHeight >> client.guiScale;
ServerWorld &world = getWorldForClient(client.id);
u16 id = world.getBlock(x, y, z);
ServerBlock &block = (ServerBlock &)(m_registry.getBlock(id));
bool hasBeenActivated = block.onBlockActivated({x, y, z}, *player, world, client, *this, screenWidth, screenHeight, guiScale);
bool hasBeenActivated = block.onBlockActivated({x, y, z}, *player, world, client, *this);
if (hasBeenActivated)
m_scriptEngine.luaCore().onEvent(LuaEventType::BlockActivated, glm::ivec3{x, y, z}, block, *player, world, client, *this);
@ -369,9 +367,7 @@ void ServerCommandHandler::setupCallbacks() {
ServerPlayer *player = m_players.getPlayerFromClientID(client.id);
if (player) {
s32 x, y, z;
u16 screenWidth, screenHeight;
u8 guiScale;
packet >> x >> y >> z >> screenWidth >> screenHeight >> guiScale;
packet >> x >> y >> z >> client.screenWidth >> client.screenHeight >> client.guiScale;
ServerWorld &world = getWorldForClient(client.id);
@ -380,7 +376,7 @@ void ServerCommandHandler::setupCallbacks() {
ServerItem &item = (ServerItem &)player->heldItemStack().item();
if (item.canBeActivated()) {
bool hasBeenActivated = item.onItemActivated({x, y, z}, block, *player, world, client, *this, screenWidth, screenHeight, guiScale);
bool hasBeenActivated = item.onItemActivated({x, y, z}, block, *player, world, client, *this);
if (hasBeenActivated)
m_scriptEngine.luaCore().onEvent(LuaEventType::ItemActivated, glm::ivec3{x, y, z}, block, *player, world, client, *this);
@ -406,11 +402,10 @@ void ServerCommandHandler::setupCallbacks() {
});
m_server.setCommandCallback(Network::Command::KeyPressed, [this](ClientInfo &client, Network::Packet &packet) {
u16 keyID, screenWidth, screenHeight;
u8 guiScale;
packet >> keyID >> screenWidth >> screenHeight >> guiScale;
u16 keyID;
packet >> keyID >> client.screenWidth >> client.screenHeight >> client.guiScale;
m_registry.getKey(keyID).callback()(keyID, client, screenWidth, screenHeight, guiScale);
m_registry.getKey(keyID).callback()(keyID, client);
});
}

View File

@ -56,10 +56,10 @@ void ServerBlock::onTick(const glm::ivec3 &pos, ServerChunk &chunk, ServerWorld
}
}
bool ServerBlock::onBlockActivated(const glm::ivec3 &pos, ServerPlayer &player, ServerWorld &world, ClientInfo &client, ServerCommandHandler &server, u16 screenWidth, u16 screenHeight, u8 guiScale) const {
bool ServerBlock::onBlockActivated(const glm::ivec3 &pos, ServerPlayer &player, ServerWorld &world, ClientInfo &client, ServerCommandHandler &server) const {
try {
if (m_onBlockActivated) {
m_onBlockActivated(pos, *this, player, world, client, server, screenWidth, screenHeight, guiScale);
m_onBlockActivated(pos, *this, player, world, client, server);
// FIXME: Check if data changed before sending
BlockData *blockData = world.getBlockData(pos.x, pos.y, pos.z);

View File

@ -41,7 +41,7 @@ class ServerBlock : public Block {
: Block(id, name) {}
void onTick(const glm::ivec3 &pos, ServerChunk &chunk, ServerWorld &world, ServerCommandHandler &server) const;
bool onBlockActivated(const glm::ivec3 &pos, ServerPlayer &player, ServerWorld &world, ClientInfo &client, ServerCommandHandler &server, u16 screenWidth, u16 screenHeight, u8 guiScale) const;
bool onBlockActivated(const glm::ivec3 &pos, ServerPlayer &player, ServerWorld &world, ClientInfo &client, ServerCommandHandler &server) const;
void onBlockPlaced(const glm::ivec3 &pos, ServerWorld &world) const;
void onBlockDestroyed(const glm::ivec3 &pos, ServerWorld &world) const;

View File

@ -33,10 +33,10 @@
#include "ServerPlayer.hpp"
#include "World.hpp"
bool ServerItem::onItemActivated(const glm::ivec3 &pos, Block &block, Player &player, World &world, ClientInfo &client, ServerCommandHandler &server, u16 screenWidth, u16 screenHeight, u8 guiScale) const {
bool ServerItem::onItemActivated(const glm::ivec3 &pos, Block &block, Player &player, World &world, ClientInfo &client, ServerCommandHandler &server) const {
try {
if (m_onItemActivated) {
m_onItemActivated(pos, block, player, world, client, server, screenWidth, screenHeight, guiScale);
m_onItemActivated(pos, block, player, world, client, server);
return true;
}
}

View File

@ -42,7 +42,7 @@ class ServerItem : public Item {
ServerItem(u32 id, const TilesDef &tiles, const std::string &stringID, const std::string &label)
: Item(id, tiles, stringID, label) {}
bool onItemActivated(const glm::ivec3 &pos, Block &block, Player &player, World &world, ClientInfo &client, ServerCommandHandler &server, u16 screenWidth, u16 screenHeight, u8 guiScale) const;
bool onItemActivated(const glm::ivec3 &pos, Block &block, Player &player, World &world, ClientInfo &client, ServerCommandHandler &server) const;
void setOnItemActivated(const sol::protected_function &function);