Skip to content

Commit

Permalink
Added 'Farmland' block, which is possible to create with any hoe.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jul 5, 2020
1 parent 3a325c7 commit 7497b6b
Show file tree
Hide file tree
Showing 31 changed files with 307 additions and 67 deletions.
1 change: 1 addition & 0 deletions docs/lua-api-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Possible events:
- `BlockPlaced`: `funcion(pos, block, player, world, client, server)`
- `BlockDigged`: `funcion(pos, block, player, world, client, server)`
- `BlockActivated`: `function(pos, block, player, world, client, server)`
- `ItemActivated`: `function(pos, block, player, world, client, server)`
- `PlayerConnected`: `function(pos, player, client, server)`

### `openminer:get_config(name)`
Expand Down
2 changes: 2 additions & 0 deletions docs/lua-api-cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,6 @@
- `void set_data(int x, int y, int z, u16 data)`
- `BlockData *add_block_data(int x, int y, int z, int inventoryWidth, int inventoryHeight)`
- `BlockData *get_block_data(int x, int y, int z)`
- `const Block &get_block_def(int x, int y, int z)`
- `void set_block_from_str(int x, int y, int z, string block_id)`

16 changes: 16 additions & 0 deletions docs/lua-api-item.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,19 @@ The textures will be loaded from `mods/<your-mod>/textures/items`

**Note:** Currently, you can only use textures of the exact same size (16x16, 32x32) than the other block/item textures in the game.

## Functions

### `on_item_activated`

Parameters:

- `pos` (`ivec3`): position of the targeted block
- `block` (`Block`): definition of the targeted block
- `player` (`Player`): player that activated the item
- `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

11 changes: 11 additions & 0 deletions docs/network-protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@ _This packet has no field._
| Block Z | s32 | Block Z coordinate |
| Inventory | Inventory | Block inventory |

#### ItemActivated

| Field name | Field type | Notes |
| ------------- | ----------- | ---------------------------------------------------- |
| Block X | s32 | Selected block X coordinate |
| Block Y | s32 | Selected block Y coordinate |
| Block Z | s32 | Selected block Z coordinate |
| Screen width | u16 | Client screen width |
| Screen height | u16 | Client screen height |
| GUI scale | u8 | Client GUI scale |

#### ChatMessage (serverbound)

| Field name | Field type | Notes |
Expand Down
10 changes: 10 additions & 0 deletions mods/default/blocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@ mod:block {
end
}

mod:block {
id = "farmland",
name = "Farmland",
tiles = {"farmland_dry.png", "dirt.png", "dirt.png"},
alt_tiles = {"farmland_wet.png", "dirt.png", "dirt.png"},

draw_type = "boundingbox",
bounding_box = {0, 0, 0, 1, 1, 15 / 16},
}

dofile("blocks/workbench.lua")
dofile("blocks/furnace.lua")
dofile("blocks/door.lua")
Expand Down
38 changes: 34 additions & 4 deletions mods/default/items.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ mod:item {
mod:item {
id = "stone_hoe",
name = "Stone Hoe",
tiles = "stone_hoe.png"
tiles = "stone_hoe.png",

on_item_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
if block:string_id() == "default:grass" then
world:set_block_from_str(pos.x, pos.y, pos.z, "default:farmland")
end
end
}

mod:item {
Expand Down Expand Up @@ -105,6 +111,12 @@ mod:item {
id = "wooden_hoe",
name = "Wooden Hoe",
tiles = "wooden_hoe.png",

on_item_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
if block:string_id() == "default:grass" then
world:set_block_from_str(pos.x, pos.y, pos.z, "default:farmland")
end
end
}

mod:item {
Expand Down Expand Up @@ -178,7 +190,13 @@ mod:item {
mod:item {
id = "iron_hoe",
name = "Iron Hoe",
tiles = "iron_hoe.png"
tiles = "iron_hoe.png",

on_item_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
if block:string_id() == "default:grass" then
world:set_block_from_str(pos.x, pos.y, pos.z, "default:farmland")
end
end
}

mod:item {
Expand Down Expand Up @@ -214,7 +232,13 @@ mod:item {
mod:item {
id = "diamond_hoe",
name = "Diamond Hoe",
tiles = "diamond_hoe.png"
tiles = "diamond_hoe.png",

on_item_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
if block:string_id() == "default:grass" then
world:set_block_from_str(pos.x, pos.y, pos.z, "default:farmland")
end
end
}

mod:item {
Expand Down Expand Up @@ -250,7 +274,13 @@ mod:item {
mod:item {
id = "golden_hoe",
name = "Golden Hoe",
tiles = "golden_hoe.png"
tiles = "golden_hoe.png",

on_item_activated = function(pos, block, player, world, client, server, screen_width, screen_height, gui_scale)
if block:string_id() == "default:grass" then
world:set_block_from_str(pos.x, pos.y, pos.z, "default:farmland")
end
end
}

mod:item {
Expand Down
4 changes: 2 additions & 2 deletions mods/default/listeners.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ openminer:add_listener(Event.PlayerConnected, function(pos, player, client, serv
local starting_items = {
{"default:workbench", 1},
{"default:dirt", 64},
{"default:grass", 64},
{"default:stone", 64},
{"default:glass", 64},
{"default:glowstone", 64},
{"default:furnace", 1},
{"default:stone_pickaxe", 1},
{"default:stone_axe", 1},
{"default:stone_hoe", 1},

{"default:oak_wood", 64},
{"default:oak_planks", 64},
{"default:grass", 64},
{"default:cobblestone", 64},
{"default:stick", 64},
{"default:stone_hoe", 1},
{"default:stone_shovel", 1},
{"default:iron_ore", 64},
{"default:coal", 64},
Expand Down
Binary file added mods/default/textures/blocks/farmland_dry.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mods/default/textures/blocks/farmland_wet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions source/client/graphics/TextureAtlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ void TextureAtlas::loadFromRegistry(const std::string &texturePack) {
}

for (auto &item : Registry::getInstance().items()) {
if (!item.isBlock() || !item.tiles().textureFilenames().empty()) {
if (!item->isBlock() || !item->tiles().textureFilenames().empty()) {
std::string path;
if (texturePack.empty())
path = "mods/" + item.modName() + "/textures/items/";
path = "mods/" + item->modName() + "/textures/items/";
else
path = "texturepacks/" + texturePack + "/items/";

const TilesDef &tiles = item.tiles();
const TilesDef &tiles = item->tiles();
for (auto &textureFilename : tiles.textureFilenames())
addFile(path, textureFilename);
for (auto &textureFilename : tiles.altTextureFilenames())
Expand Down
16 changes: 14 additions & 2 deletions source/client/hud/BlockCursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,25 @@ void BlockCursor::onEvent(const sf::Event &event, const Hotbar &hotbar) {
const Block &block = Registry::getInstance().getBlock(blockId);
const Item &item = hotbar.currentItem();

bool itemActivationSent = false;
bool sneakedItemActivation = false;
if (item.id() && item.canBeActivated()) {
if (!gk::GamePad::isKeyPressed(GameKey::Sneak)) {
m_client.sendItemActivated(m_selectedBlock);
itemActivationSent = true;
}
else
sneakedItemActivation = true;
}

bool blockActivationSent = false;
if (block.id() && block.canBeActivated() && !gk::GamePad::isKeyPressed(GameKey::Sneak)) {
if (!itemActivationSent && block.id() && block.canBeActivated()
&& (!gk::GamePad::isKeyPressed(GameKey::Sneak) || sneakedItemActivation)) {
m_client.sendBlockActivated(m_selectedBlock);
blockActivationSent = true;
}

if (block.id() && !blockActivationSent && hotbar.currentItem().id() && item.isBlock()) {
if (block.id() && !itemActivationSent && !blockActivationSent && hotbar.currentItem().id() && item.isBlock()) {
s8 face = m_selectedBlock.w;

s32 x = m_selectedBlock.x;
Expand Down
10 changes: 10 additions & 0 deletions source/client/network/ClientCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ void ClientCommandHandler::sendBlockInvUpdate(Inventory &inventory) {
m_client.send(packet);
}

void ClientCommandHandler::sendItemActivated(const glm::ivec4 &selectedBlock) {
Network::Packet packet;
packet << Network::Command::ItemActivated
<< s32(selectedBlock.x)
<< s32(selectedBlock.y)
<< s32(selectedBlock.z)
<< u16(Config::screenWidth) << u16(Config::screenHeight) << u8(Config::guiScale);
m_client.send(packet);
}

void ClientCommandHandler::sendChunkRequest(s32 chunkX, s32 chunkY, s32 chunkZ) {
Network::Packet packet;
packet << Network::Command::ChunkRequest;
Expand Down
1 change: 1 addition & 0 deletions source/client/network/ClientCommandHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class ClientCommandHandler {
void sendPlayerHeldItemChanged(u8 hotbarSlot, u16 itemID);
void sendBlockActivated(const glm::ivec4 &selectedBlock);
void sendBlockInvUpdate(Inventory &inventory);
void sendItemActivated(const glm::ivec4 &selectedBlock);
void sendChunkRequest(s32 chunkX, s32 chunkY, s32 chunkZ);
void sendChatMessage(const std::string &message);
void sendKeyPressed(u16 keyID);
Expand Down
27 changes: 5 additions & 22 deletions source/common/core/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,6 @@

Registry *Registry::s_instance = nullptr;

Item &Registry::registerItem(const TilesDef &tiles, const std::string &stringID, const std::string &label) {
u32 id = m_items.size();
m_itemsID.emplace(stringID, id);
m_items.emplace_back(id, tiles, stringID, label);
return m_items.back();
}

Item &Registry::registerSerializedItem(sf::Packet &packet) {
m_items.emplace_back();
m_items.back().deserialize(packet);

u32 id = m_items.size() - 1;
m_itemsID.emplace(m_items.back().stringID(), id);

return m_items.back();
}

Sky &Registry::registerSky(const std::string &stringID) {
size_t id = m_skies.size();
m_skiesID.emplace(stringID, id);
Expand Down Expand Up @@ -233,14 +216,14 @@ EntityCallbackContainer &Registry::getEntityCallbackContainer(const std::string
}

void Registry::serialize(sf::Packet &packet) const {
for (auto &it : m_items) {
packet << u8(DataType::Item) << it;
}

for (auto &it : m_blocks) {
packet << u8(DataType::Block) << *it;
}

for (auto &it : m_items) {
packet << u8(DataType::Item) << *it;
}

for (auto &it : m_recipes) {
packet << u8((it->type() == "craft") ? DataType::CraftingRecipe : DataType::SmeltingRecipe)
<< *it;
Expand Down Expand Up @@ -275,7 +258,7 @@ void Registry::deserialize(sf::Packet &packet) {
registerSerializedBlock<Block>(packet);
}
else if (type == u8(DataType::Item)) {
registerSerializedItem(packet);
registerSerializedItem<Item>(packet);
}
else if (type == u8(DataType::CraftingRecipe)) {
registerRecipe<CraftingRecipe>()->deserialize(packet);
Expand Down
26 changes: 21 additions & 5 deletions source/common/core/Registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,24 @@ class Registry : public gk::ISerializable {
return *static_cast<T*>(m_blocks.back().get());
}

Item &registerItem(const TilesDef &tiles, const std::string &stringID, const std::string &name);
Item &registerSerializedItem(sf::Packet &packet);
template<typename T>
auto registerItem(const TilesDef &tiles, const std::string &stringID, const std::string &label) -> typename std::enable_if<std::is_base_of<Item, T>::value, T&>::type {
u32 id = m_items.size();
m_itemsID.emplace(stringID, id);
m_items.emplace_back(std::make_unique<T>(id, tiles, stringID, label));
return *static_cast<T*>(m_items.back().get());
}

template<typename T>
auto registerSerializedItem(sf::Packet &packet) -> typename std::enable_if<std::is_base_of<Item, T>::value, T&>::type {
m_items.emplace_back(std::make_unique<T>());
m_items.back()->deserialize(packet);

u32 id = m_items.size() - 1;
m_itemsID.emplace(m_items.back()->stringID(), id);

return *static_cast<T*>(m_items.back().get());
}

template<typename T, typename... Args>
auto registerRecipe(Args &&...args) -> typename std::enable_if<std::is_base_of<Recipe, T>::value, Recipe*>::type {
Expand All @@ -93,7 +109,7 @@ class Registry : public gk::ISerializable {
entt::entity registerEntity(const std::string &stringID);

const Block &getBlock(u16 id) const { return *m_blocks.at(id).get(); }
const Item &getItem(u16 id) const { return m_items.at(id); }
const Item &getItem(u16 id) const { return *m_items.at(id).get(); }
const Sky &getSky(u16 id) const { return m_skies.at(id); }
const Tree &getTree(u16 id) const { return m_trees.at(id); }
const Biome &getBiome(u16 id) const { return m_biomes.at(id); }
Expand All @@ -120,7 +136,7 @@ class Registry : public gk::ISerializable {
static void initUsertype(sol::state &lua);

const std::vector<std::unique_ptr<Block>> &blocks() const { return m_blocks; }
const std::vector<Item> &items() const { return m_items; }
const std::vector<std::unique_ptr<Item>> &items() const { return m_items; }
const std::vector<Tree> &trees() const { return m_trees; }
const std::vector<Biome> &biomes() const { return m_biomes; }
const std::vector<Dimension> &dimensions() const { return m_dimensions; }
Expand All @@ -133,7 +149,7 @@ class Registry : public gk::ISerializable {
static Registry *s_instance;

std::vector<std::unique_ptr<Block>> m_blocks;
std::vector<Item> m_items;
std::vector<std::unique_ptr<Item>> m_items;
std::vector<std::unique_ptr<Recipe>> m_recipes;
std::vector<Sky> m_skies;
std::vector<Tree> m_trees;
Expand Down
4 changes: 2 additions & 2 deletions source/common/inventory/Item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ Item::Item(u32 id, const TilesDef &tiles, const std::string &stringID, const std

void Item::serialize(sf::Packet &packet) const {
packet << m_id << m_tiles << m_stringID << m_label << m_isBlock
<< m_miningSpeed << m_harvestCapability << m_groups;
<< m_miningSpeed << m_harvestCapability << m_groups << m_canBeActivated;
}

void Item::deserialize(sf::Packet &packet) {
packet >> m_id >> m_tiles >> m_stringID >> m_label >> m_isBlock
>> m_miningSpeed >> m_harvestCapability >> m_groups;
>> m_miningSpeed >> m_harvestCapability >> m_groups >> m_canBeActivated;
}

// Please update 'docs/lua-api-cpp.md' if you change this
Expand Down
4 changes: 4 additions & 0 deletions source/common/inventory/Item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ class Item : public gk::ISerializable {
return it->second;
}

bool canBeActivated() const { return m_canBeActivated; }

static void initUsertype(sol::state &lua);

protected:
bool m_isBlock = false;

bool m_canBeActivated = false;

private:
u32 m_id = 0;
TilesDef m_tiles;
Expand Down
2 changes: 2 additions & 0 deletions source/common/network/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ std::string Network::commandToString(Network::Command command) {
{Network::Command::BlockInvUpdate, "BlockInvUpdate"},
{Network::Command::BlockDataUpdate, "BlockDataUpdate"},

{Network::Command::ItemActivated, "ItemActivated"},

{Network::Command::RegistryData, "RegistryData"},

{Network::Command::ChatMessage, "ChatMessage"},
Expand Down
Loading

0 comments on commit 7497b6b

Please sign in to comment.