]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/lua_api/l_util.cpp
Fix number of tool uses being off by 1..32767 (#11110)
[dragonfireclient.git] / src / script / lua_api / l_util.cpp
index 9152b5f7f054063333371432947109a165482ca3..53319ccfd2a21f569446181be0ba80a63f5a0096 100644 (file)
@@ -160,28 +160,33 @@ int ModApiUtil::l_write_json(lua_State *L)
        return 1;
 }
 
-// get_dig_params(groups, tool_capabilities)
+// get_dig_params(groups, tool_capabilities[, wear])
 int ModApiUtil::l_get_dig_params(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
        ItemGroupList groups;
        read_groups(L, 1, groups);
        ToolCapabilities tp = read_tool_capabilities(L, 2);
-       push_dig_params(L, getDigParams(groups, &tp));
+       if (lua_isnoneornil(L, 3)) {
+               push_dig_params(L, getDigParams(groups, &tp));
+       } else {
+               u16 wear = readParam<int>(L, 3);
+               push_dig_params(L, getDigParams(groups, &tp, wear));
+       }
        return 1;
 }
 
-// get_hit_params(groups, tool_capabilities[, time_from_last_punch])
+// get_hit_params(groups, tool_capabilities[, time_from_last_punch, [, wear]])
 int ModApiUtil::l_get_hit_params(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
        std::unordered_map<std::string, int> groups;
        read_groups(L, 1, groups);
        ToolCapabilities tp = read_tool_capabilities(L, 2);
-       if(lua_isnoneornil(L, 3))
-               push_hit_params(L, getHitParams(groups, &tp));
-       else
-               push_hit_params(L, getHitParams(groups, &tp, readParam<float>(L, 3)));
+       float time_from_last_punch = readParam<float>(L, 3, 1000000);
+       int wear = readParam<int>(L, 4, 0);
+       push_hit_params(L, getHitParams(groups, &tp,
+               time_from_last_punch, wear));
        return 1;
 }
 
@@ -272,11 +277,11 @@ int ModApiUtil::l_compress(lua_State *L)
        const char *data = luaL_checklstring(L, 1, &size);
 
        int level = -1;
-       if (!lua_isnone(L, 3) && !lua_isnil(L, 3))
-               level = readParam<float>(L, 3);
+       if (!lua_isnoneornil(L, 3))
+               level = readParam<int>(L, 3);
 
-       std::ostringstream os;
-       compressZlib(std::string(data, size), os, level);
+       std::ostringstream os(std::ios_base::binary);
+       compressZlib(reinterpret_cast<const u8 *>(data), size, os, level);
 
        std::string out = os.str();
 
@@ -292,8 +297,8 @@ int ModApiUtil::l_decompress(lua_State *L)
        size_t size;
        const char *data = luaL_checklstring(L, 1, &size);
 
-       std::istringstream is(std::string(data, size));
-       std::ostringstream os;
+       std::istringstream is(std::string(data, size), std::ios_base::binary);
+       std::ostringstream os(std::ios_base::binary);
        decompressZlib(is, os);
 
        std::string out = os.str();