Skip to content

Commit

Permalink
Furnace output inventory is now read-only.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Mar 30, 2020
1 parent 228e7a3 commit 218eddb
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 10 deletions.
9 changes: 9 additions & 0 deletions docs/lua-api-gui-inventory.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ Possible values:
- `block`: Set a specific block as the source
- `temp`: Set a temporary inventory as the source

### `is_read_only`

Defines if the inventory is read-only or not.

Example:
```lua
is_read_only = false -- this is the default value
```

### `name`

Name of the widget. **Mandatory field.**
Expand Down
2 changes: 2 additions & 0 deletions mods/default/blocks/furnace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ mod:block {
count = 1,
},

is_read_only = true,

size = {x = 1, y = 1},

shift_destination = "inv_main,inv_hotbar",
Expand Down
7 changes: 6 additions & 1 deletion source/client/gui/AbstractInventoryWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@

class AbstractInventoryWidget : public Widget {
public:
AbstractInventoryWidget(Widget *parent) : Widget(parent) {}
AbstractInventoryWidget(Widget *parent, bool isReadOnly = false)
: Widget(parent), m_isReadOnly(isReadOnly) {}

virtual bool sendItemStackToDest(const ItemWidget *itemStack, AbstractInventoryWidget *dest) = 0;
virtual bool receiveItemStack(const ItemWidget *itemStack, AbstractInventoryWidget *src) = 0;
Expand All @@ -44,10 +45,14 @@ class AbstractInventoryWidget : public Widget {
void setFilter(const std::string &filter) { m_filter = filter; }
bool doItemMatchFilter(const Item &item);

bool isReadOnly() const { return m_isReadOnly; }

protected:
std::vector<std::string> m_shiftDestination;

std::string m_filter;

bool m_isReadOnly = false;
};

#endif // ABSTRACTINVENTORYWIDGET_HPP_
6 changes: 1 addition & 5 deletions source/client/gui/InventoryWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ClientCommandHandler;
class InventoryWidget : public AbstractInventoryWidget {
public:
InventoryWidget(ClientCommandHandler &client, bool isReadOnly = false, Widget *parent = nullptr)
: AbstractInventoryWidget(parent), m_client(client), m_isReadOnly(isReadOnly) {}
: AbstractInventoryWidget(parent, isReadOnly), m_client(client) {}

void init(Inventory &inventory, u16 offset = 0, u16 size = 0);

Expand All @@ -54,8 +54,6 @@ class InventoryWidget : public AbstractInventoryWidget {

Inventory *inventory() const { return m_inventory; }

bool isReadOnly() const { return m_isReadOnly; }

ItemWidget *currentItemWidget() const { return m_currentItemWidget; }

private:
Expand All @@ -73,8 +71,6 @@ class InventoryWidget : public AbstractInventoryWidget {
u16 m_offset = 0;
u16 m_size = 0;

bool m_isReadOnly = false;

std::vector<ItemWidget> m_itemWidgets;
ItemWidget *m_currentItemWidget = nullptr;

Expand Down
2 changes: 1 addition & 1 deletion source/client/gui/MouseItemWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void MouseItemWidget::draggingBehaviour(ItemWidget *newItemWidget) {

void MouseItemWidget::updateCurrentItem(ItemWidget *currentItemWidget) {
if (currentItemWidget) {
bool doItemMatchFilter = !m_currentInventoryWidget || m_currentInventoryWidget->doItemMatchFilter(m_draggedStack.item());
bool doItemMatchFilter = !m_currentInventoryWidget || (m_currentInventoryWidget->doItemMatchFilter(m_draggedStack.item()) && !m_currentInventoryWidget->isReadOnly());
if (m_isDragging && currentItemWidget != m_currentItemWidget && doItemMatchFilter)
draggingBehaviour(currentItemWidget);

Expand Down
6 changes: 4 additions & 2 deletions source/client/states/LuaGUIState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ void LuaGUIState::loadInventoryWidget(const std::string &name, s32 x, s32 y, sf:
std::string inventory, shiftDestination, filter;
u16 width, height;
u16 offset, count;
packet >> width >> height >> shiftDestination >> offset >> count >> inventory >> filter;
bool isReadOnly;
packet >> width >> height >> shiftDestination >> offset >> count >> inventory
>> filter >> isReadOnly;

Inventory *widgetInventory = nullptr;
if (inventory == "player") {
Expand Down Expand Up @@ -264,7 +266,7 @@ void LuaGUIState::loadInventoryWidget(const std::string &name, s32 x, s32 y, sf:
}

if (widgetInventory) {
m_inventoryWidgets.emplace(name, InventoryWidget{m_client, false, &m_mainWidget});
m_inventoryWidgets.emplace(name, InventoryWidget{m_client, isReadOnly, &m_mainWidget});

auto &inventoryWidget = m_inventoryWidgets.at(name);
inventoryWidget.setPosition(x, y);
Expand Down
3 changes: 2 additions & 1 deletion source/server/gui/InventoryWidgetDef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void InventoryWidgetDef::serialize(sf::Packet &packet) const {
WidgetDef::serialize(packet);

packet << m_width << m_height << m_shiftDestination << m_offset << m_count
<< m_inventory << m_filter;
<< m_inventory << m_filter << m_isReadOnly;

if (m_inventory == "player")
packet << m_player << m_inventoryName;
Expand All @@ -49,6 +49,7 @@ void InventoryWidgetDef::loadFromLuaTable(const sol::table &table) {

m_shiftDestination = table["shift_destination"].get_or<std::string>("");
m_filter = table["filter"].get_or<std::string>("");
m_isReadOnly = table["is_read_only"].get_or(false);

sol::optional<sol::table> size = table["size"];
if (size != sol::nullopt) {
Expand Down
1 change: 1 addition & 0 deletions source/server/gui/InventoryWidgetDef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class InventoryWidgetDef : public WidgetDef {
u16 m_count = 0;

std::string m_filter;
bool m_isReadOnly = false;
};

#endif // INVENTORYWIDGETDEF_HPP_

0 comments on commit 218eddb

Please sign in to comment.