diff --git a/res/layouts/inventory.xml b/res/layouts/inventory.xml
index 90279abd..c33d57d6 100644
--- a/res/layouts/inventory.xml
+++ b/res/layouts/inventory.xml
@@ -1,4 +1,3 @@
-
-
+
diff --git a/res/layouts/inventory.xml.lua b/res/layouts/inventory.xml.lua
index 8669b0a7..532b9ee5 100644
--- a/res/layouts/inventory.xml.lua
+++ b/res/layouts/inventory.xml.lua
@@ -5,3 +5,7 @@ end
function on_close(inv)
print("CLOSE", inv)
end
+
+function inventory_share_func(invid, slotid)
+ inventory.set(invid, slotid, 0, 0)
+end
diff --git a/src/frontend/InventoryView.cpp b/src/frontend/InventoryView.cpp
index 5ec6d986..c25bce9a 100644
--- a/src/frontend/InventoryView.cpp
+++ b/src/frontend/InventoryView.cpp
@@ -23,6 +23,7 @@
#include "../frontend/gui/controls.h"
#include "../util/stringutil.h"
#include "../world/Level.h"
+#include "../logic/scripting/scripting.h"
SlotLayout::SlotLayout(
int index,
@@ -204,7 +205,7 @@ void SlotView::clicked(gui::GUI* gui, int button) {
if (button == mousecode::BUTTON_1) {
if (Events::pressed(keycode::LEFT_SHIFT)) {
if (layout.shareFunc) {
- layout.shareFunc(stack);
+ layout.shareFunc(layout.index, stack);
}
return;
}
@@ -344,6 +345,17 @@ void InventoryView::setInventory(std::shared_ptr inventory) {
#include "../coders/xml.h"
#include "gui/gui_xml.h"
+static itemsharefunc readShareFunc(InventoryView* view, gui::UiXmlReader& reader, xml::xmlelement& element) {
+ auto consumer = scripting::create_int_array_consumer(
+ reader.getEnvironment().getId(),
+ element->attr("sharefunc").getText()
+ );
+ return [=](uint slot, ItemStack& stack) {
+ int args[] {int(view->getInventory()->getId()), int(slot)};
+ consumer(args, 2);
+ };
+}
+
static void readSlot(InventoryView* view, gui::UiXmlReader& reader, xml::xmlelement element) {
int index = element->attr("index", "0").asInt();
bool itemSource = element->attr("item-source", "false").asBool();
@@ -351,6 +363,9 @@ static void readSlot(InventoryView* view, gui::UiXmlReader& reader, xml::xmlelem
if (element->has("coord")) {
layout.position = element->attr("coord").asVec2();
}
+ if (element->has("sharefunc")) {
+ layout.shareFunc = readShareFunc(view, reader, element);
+ }
auto slot = view->addSlot(layout);
reader.readUINode(reader, element, *slot);
view->add(slot);
@@ -382,6 +397,9 @@ static void readSlotsGrid(InventoryView* view, gui::UiXmlReader& reader, xml::xm
if (element->has("coord")) {
layout.position = element->attr("coord").asVec2();
}
+ if (element->has("sharefunc")) {
+ layout.shareFunc = readShareFunc(view, reader, element);
+ }
layout.padding = padding;
glm::vec2 size (
diff --git a/src/frontend/InventoryView.h b/src/frontend/InventoryView.h
index 0d593d8b..1267942c 100644
--- a/src/frontend/InventoryView.h
+++ b/src/frontend/InventoryView.h
@@ -26,7 +26,7 @@ namespace scripting {
class Environment;
}
-using itemsharefunc = std::function;
+using itemsharefunc = std::function;
using slotcallback = std::function;
class InventoryInteraction {
diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp
index a0d4e235..f2597a76 100644
--- a/src/frontend/hud.cpp
+++ b/src/frontend/hud.cpp
@@ -182,7 +182,7 @@ std::shared_ptr HudRenderer::createContentAccess() {
}
SlotLayout slotLayout(-1, glm::vec2(), false, true,
- [=](ItemStack& item) {
+ [=](uint, ItemStack& item) {
auto copy = ItemStack(item);
inventory->move(copy, indices);
},
diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp
index 4baea4b3..d40ef210 100644
--- a/src/logic/scripting/scripting.cpp
+++ b/src/logic/scripting/scripting.cpp
@@ -74,20 +74,37 @@ wstringconsumer scripting::create_wstring_consumer(
const std::string& src,
const std::string& file
) {
- try {
- if (state->eval(env, src, file) == 0)
- return [](const std::wstring& _) {};
- } catch (const lua::luaerror& err) {
- std::cerr << err.what() << std::endl;
- return [](const std::wstring& _) {};
- }
-
auto funcName = state->storeAnonymous();
return [=](const std::wstring& x){
- if (state->getglobal(funcName)) {
- state->pushstring(util::wstr2str_utf8(x));
- state->callNoThrow(1);
+ try {
+ if (state->eval(env, src, file) == 0)
+ return;
+ } catch (lua::luaerror err) {
+ std::cerr << err.what() << std::endl;
+ return;
}
+ state->pushstring(util::wstr2str_utf8(x));
+ state->callNoThrow(1);
+ };
+}
+
+int_array_consumer scripting::create_int_array_consumer(
+ int env,
+ const std::string& src,
+ const std::string& file
+) {
+ return [=](const int arr[], size_t len){
+ try {
+ if (state->eval(env, src, file) == 0)
+ return;
+ } catch (lua::luaerror err) {
+ std::cerr << err.what() << std::endl;
+ return;
+ }
+ for (uint i = 0; i < len; i++) {
+ state->pushinteger(arr[i]);
+ }
+ state->callNoThrow(len);
};
}
diff --git a/src/logic/scripting/scripting.h b/src/logic/scripting/scripting.h
index 717ad88a..12e2372d 100644
--- a/src/logic/scripting/scripting.h
+++ b/src/logic/scripting/scripting.h
@@ -23,6 +23,8 @@ struct uidocscript;
class BlocksController;
namespace scripting {
+ using int_array_consumer = std::function;
+
extern Engine* engine;
extern const Content* content;
extern const ContentIndices* indices;
@@ -53,6 +55,12 @@ namespace scripting {
const std::string& file=""
);
+ int_array_consumer create_int_array_consumer(
+ int env,
+ const std::string& src,
+ const std::string& file=""
+ );
+
std::unique_ptr create_environment(int parent=0);
std::unique_ptr create_pack_environment(const ContentPack& pack);