Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update unified_inventory #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions mods/unified_inventory/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Unified Inventory for Minetest
Copyright (C) 2012-2014 Maciej Kasatkin (RealBadAngel)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

Contact information:
Examine a git patch to get the contributor's email address.
49 changes: 42 additions & 7 deletions mods/unified_inventory/README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
Unified inventory
=================
# Unified Inventory

Unified Inventory replaces the default survival and creative inventory.
It adds a nicer interface and a number of features, such as a crafting guide.

License
=======

## Features

* Node, item and tool browser
* Crafting guide
* Can copy the recipe to the crafting grid
* Recipe search function by ingredients
* Up to four bags with up to 24 slots each
* Home function to teleport
* Trash slot
* Lite mode: reduces the item browser width
* Mod API for modders: see [mod_api.txt](doc/mod_api.txt)
* Setting-determinated features: see [settingtypes.txt](settingtypes.txt)


## Requirements

* Minetest 5.0.0+ since commit 4403b69
* Minetest 0.4.16+ prior commit 4403b69


# Licenses

Copyright (C) 2012-2014 Maciej Kasatkin (RealBadAngel)

Unified inventory code is licensed under the GNU LGPLv2+.
Copyright (C) 2012-? Various minetest-mods contributors


## Code

GNU LGPLv2+, see [license notice](LICENSE.txt)


Licenses for textures:
## Textures

VanessaE: (CC-BY-4.0)

* `ui_group.png`

Tango Project: (Public Domain, CC-BY-4.0)

* [`ui_reset_icon.png`](https://commons.wikimedia.org/wiki/File:Edit-clear.svg)
* [`ui_doubleleft_icon.png`](http://commons.wikimedia.org/wiki/File:Media-seek-backward.svg)
* [`ui_doubleright_icon.png`](http://commons.wikimedia.org/wiki/File:Media-seek-forward.svg)
Expand All @@ -25,6 +51,7 @@ Tango Project: (Public Domain, CC-BY-4.0)
* [`ui_skip_forward_icon.png`](http://commons.wikimedia.org/wiki/File:Media-skip-forward.svg)

From http://www.clker.com (Public Domain, CC-BY-4.0):

* [`bags_small.png`](http://www.clker.com/clipart-moneybag-empty.html)
* [`bags_medium.png`](http://www.clker.com/clipart-backpack-1.html)
* [`bags_large.png` / `ui_bags_icon.png`](http://www.clker.com/clipart-backpack-green-brown.html)
Expand All @@ -37,25 +64,33 @@ From http://www.clker.com (Public Domain, CC-BY-4.0):
* [`ui_waypoint_set_icon.png`](http://www.clker.com/clipart-larger-flag.html)

Everaldo Coelho (YellowIcon) (LGPL v2.1+):

* [`ui_craftguide_icon.png` / `ui_craft_icon.png`](http://commons.wikimedia.org/wiki/File:Advancedsettings.png)

Gregory H. Revera: (CC-BY-SA 3.0)

* [`ui_moon_icon.png`](http://commons.wikimedia.org/wiki/File:FullMoon2010.jpg)

Thomas Bresson: (CC-BY 3.0)

* [`ui_sun_icon.png`](http://commons.wikimedia.org/wiki/File:2012-10-13_15-29-35-sun.jpg)

Fibonacci: (Public domain, CC-BY 4.0)

* [`ui_xyz_off_icon.png`](http://commons.wikimedia.org/wiki/File:No_sign.svg)

Gregory Maxwell: (Public domain, CC-BY 4.0)

* [`ui_ok_icon.png`](http://commons.wikimedia.org/wiki/File:Yes_check.svg)

Adrien Facélina: (LGPL v2.1+)

* [`inventory_plus_worldedit_gui.png`](http://commons.wikimedia.org/wiki/File:Erioll_world_2.svg)

Other files from Wikimedia Commons:

* [`ui_gohome_icon.png` / `ui_home_icon.png` / `ui_sethome_icon.png`](http://commons.wikimedia.org/wiki/File:Home_256x256.png) (GPL v2+)

RealBadAngel: (CC-BY-4.0)

* Everything else.
5 changes: 2 additions & 3 deletions mods/unified_inventory/api.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local S = unified_inventory.gettext
local S = minetest.get_translator("unified_inventory")
local F = minetest.formspec_escape

-- Create detached creative inventory after loading all mods
Expand Down Expand Up @@ -187,7 +187,7 @@ end
function unified_inventory.go_home(player)
local pos = unified_inventory.home_pos[player:get_player_name()]
if pos then
player:setpos(pos)
player:set_pos(pos)
end
end

Expand Down Expand Up @@ -305,4 +305,3 @@ function unified_inventory.is_creative(playername)
return minetest.check_player_privs(playername, {creative=true})
or minetest.settings:get_bool("creative_mode")
end

117 changes: 61 additions & 56 deletions mods/unified_inventory/bags.lua
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
-- Bags for Minetest
--[[
Bags for Minetest

-- Copyright (c) 2012 cornernote, Brett O'Donnell <[email protected]>
-- License: GPLv3
Copyright (c) 2012 cornernote, Brett O'Donnell <[email protected]>
License: GPLv3
--]]

local S = unified_inventory.gettext
local S = minetest.get_translator("unified_inventory")
local F = minetest.formspec_escape

unified_inventory.register_page("bags", {
get_formspec = function(player)
local player_name = player:get_player_name()
local formspec = "background[0.06,0.99;7.92,7.52;ui_bags_main_form.png]"
formspec = formspec.."label[0,0;"..F(S("Bags")).."]"
formspec = formspec.."button[0,2;2,0.5;bag1;"..F(S("Bag @1", 1)).."]"
formspec = formspec.."button[2,2;2,0.5;bag2;"..F(S("Bag @1", 2)).."]"
formspec = formspec.."button[4,2;2,0.5;bag3;"..F(S("Bag @1", 3)).."]"
formspec = formspec.."button[6,2;2,0.5;bag4;"..F(S("Bag @1", 4)).."]"
formspec = formspec.."listcolors[#00000000;#00000000]"
formspec = formspec.."list[detached:"..F(player_name).."_bags;bag1;0.5,1;1,1;]"
formspec = formspec.."list[detached:"..F(player_name).."_bags;bag2;2.5,1;1,1;]"
formspec = formspec.."list[detached:"..F(player_name).."_bags;bag3;4.5,1;1,1;]"
formspec = formspec.."list[detached:"..F(player_name).."_bags;bag4;6.5,1;1,1;]"
return {formspec=formspec}
return { formspec = table.concat({
"background[0.06,0.99;7.92,7.52;ui_bags_main_form.png]",
"label[0,0;" .. F(S("Bags")) .. "]",
"button[0,2;2,0.5;bag1;" .. F(S("Bag @1", 1)) .. "]",
"button[2,2;2,0.5;bag2;" .. F(S("Bag @1", 2)) .. "]",
"button[4,2;2,0.5;bag3;" .. F(S("Bag @1", 3)) .. "]",
"button[6,2;2,0.5;bag4;" .. F(S("Bag @1", 4)) .. "]",
"listcolors[#00000000;#00000000]",
"list[detached:" .. F(player_name) .. "_bags;bag1;0.5,1;1,1;]",
"list[detached:" .. F(player_name) .. "_bags;bag2;2.5,1;1,1;]",
"list[detached:" .. F(player_name) .. "_bags;bag3;4.5,1;1,1;]",
"list[detached:" .. F(player_name) .. "_bags;bag4;6.5,1;1,1;]"
}) }
end,
})

Expand All @@ -38,37 +41,39 @@ local function get_player_bag_stack(player, i)
}):get_stack("bag" .. i, 1)
end

for i = 1, 4 do
local bi = i
unified_inventory.register_page("bag"..bi, {
for bag_i = 1, 4 do
unified_inventory.register_page("bag" .. bag_i, {
get_formspec = function(player)
local stack = get_player_bag_stack(player, bi)
local stack = get_player_bag_stack(player, bag_i)
local image = stack:get_definition().inventory_image
local formspec = ("image[7,0;1,1;"..image.."]"
.."label[0,0;"..F(S("Bag @1", bi)).."]"
.."listcolors[#00000000;#00000000]"
.."list[current_player;bag"..bi.."contents;0,1;8,3;]"
.."listring[current_name;bag"..bi.."contents]"
.."listring[current_player;main]")
local fs = {
"image[7,0;1,1;" .. image .. "]",
"label[0,0;" .. F(S("Bag @1", bag_i)) .. "]",
"listcolors[#00000000;#00000000]",
"list[current_player;bag" .. bag_i .. "contents;0,1;8,3;]",
"listring[current_name;bag" .. bag_i .. "contents]",
"listring[current_player;main]"
}
local slots = stack:get_definition().groups.bagslots
if slots == 8 then
formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_sm_form.png]"
fs[#fs + 1] = "background[0.06,0.99;7.92,7.52;ui_bags_sm_form.png]"
elseif slots == 16 then
formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_med_form.png]"
fs[#fs + 1] = "background[0.06,0.99;7.92,7.52;ui_bags_med_form.png]"
elseif slots == 24 then
formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_lg_form.png]"
fs[#fs + 1] = "background[0.06,0.99;7.92,7.52;ui_bags_lg_form.png]"
end
local player_name = player:get_player_name() -- For if statement.
if unified_inventory.trash_enabled or unified_inventory.is_creative(player_name) or minetest.get_player_privs(player_name).give then
formspec = (formspec.."background[6.06,0;0.92,0.92;ui_bags_trash.png]"
.."list[detached:trash;main;6,0.1;1,1;]")
if unified_inventory.trash_enabled
or unified_inventory.is_creative(player_name)
or minetest.get_player_privs(player_name).give then
fs[#fs + 1] = "background[6.06,0;0.92,0.92;ui_bags_trash.png]"
.. "list[detached:trash;main;6,0.1;1,1;]"
end
local inv = player:get_inventory()
for i = 1, 4 do
local def = get_player_bag_stack(player, i):get_definition()
local button
if def.groups.bagslots then
local list_name = "bag"..i.."contents"
local list_name = "bag" .. i .. "contents"
local size = inv:get_size(list_name)
local used = 0
for si = 1, size do
Expand All @@ -78,14 +83,12 @@ for i = 1, 4 do
end
end
local img = def.inventory_image
local label = F(S("Bag @1", i)).."\n"..used.."/"..size
button = "image_button["..(i+1)..",0;1,1;"..img..";bag"..i..";"..label.."]"
else
button = ""
local label = F(S("Bag @1", i)) .. "\n" .. used .. "/" .. size
fs[#fs + 1] = string.format("image_button[%i,0;1,1;%s;bag%i;%s]",
i + 1, img, i, label)
end
formspec = formspec..button
end
return {formspec=formspec}
return { formspec = table.concat(fs) }
end,
})
end
Expand All @@ -95,12 +98,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
return
end
for i = 1, 4 do
if fields["bag"..i] then
if fields["bag" .. i] then
local stack = get_player_bag_stack(player, i)
if not stack:get_definition().groups.bagslots then
return
end
unified_inventory.set_inventory_formspec(player, "bag"..i)
unified_inventory.set_inventory_formspec(player, "bag" .. i)
return
end
end
Expand All @@ -110,30 +113,32 @@ local function save_bags_metadata(player, bags_inv)
local is_empty = true
local bags = {}
for i = 1, 4 do
local bag = "bag"..i
local bag = "bag" .. i
if not bags_inv:is_empty(bag) then
-- Stack limit is 1, otherwise use stack:to_string()
bags[i] = bags_inv:get_stack(bag, 1):get_name()
is_empty = false
end
end
local meta = player:get_meta()
if is_empty then
player:set_attribute("unified_inventory:bags", nil)
meta:set_string("unified_inventory:bags", nil)
else
player:set_attribute("unified_inventory:bags",
meta:set_string("unified_inventory:bags",
minetest.serialize(bags))
end
end

local function load_bags_metadata(player, bags_inv)
local player_inv = player:get_inventory()
local bags_meta = player:get_attribute("unified_inventory:bags")
local meta = player:get_meta()
local bags_meta = meta:get_string("unified_inventory:bags")
local bags = bags_meta and minetest.deserialize(bags_meta) or {}
local dirty_meta = false
if not bags_meta then
-- Backwards compatiblity
for i = 1, 4 do
local bag = "bag"..i
local bag = "bag" .. i
if not player_inv:is_empty(bag) then
-- Stack limit is 1, otherwise use stack:to_string()
bags[i] = player_inv:get_stack(bag, 1):get_name()
Expand All @@ -143,7 +148,7 @@ local function load_bags_metadata(player, bags_inv)
end
-- Fill detached slots
for i = 1, 4 do
local bag = "bag"..i
local bag = "bag" .. i
bags_inv:set_size(bag, 1)
bags_inv:set_stack(bag, 1, bags[i] or "")
end
Expand All @@ -155,17 +160,17 @@ local function load_bags_metadata(player, bags_inv)

-- Clean up deprecated garbage after saving
for i = 1, 4 do
local bag = "bag"..i
local bag = "bag" .. i
player_inv:set_size(bag, 0)
end
end

minetest.register_on_joinplayer(function(player)
local player_inv = player:get_inventory()
local player_name = player:get_player_name()
local bags_inv = minetest.create_detached_inventory(player_name.."_bags",{
local bags_inv = minetest.create_detached_inventory(player_name .. "_bags",{
on_put = function(inv, listname, index, stack, player)
player:get_inventory():set_size(listname.."contents",
player:get_inventory():set_size(listname .. "contents",
stack:get_definition().groups.bagslots)
save_bags_metadata(player, inv)
end,
Expand All @@ -175,14 +180,14 @@ minetest.register_on_joinplayer(function(player)
return 0
end
local player_inv = player:get_inventory()
local old_slots = player_inv:get_size(listname.."contents")
local old_slots = player_inv:get_size(listname .. "contents")

if new_slots >= old_slots then
return 1
end

-- using a smaller bag, make sure it fits
local old_list = player_inv:get_list(listname.."contents")
local old_list = player_inv:get_list(listname .. "contents")
local new_list = {}
local slots_used = 0
local use_new_list = false
Expand All @@ -196,21 +201,21 @@ minetest.register_on_joinplayer(function(player)
end
if new_slots >= slots_used then
if use_new_list then
player_inv:set_list(listname.."contents", new_list)
player_inv:set_list(listname .. "contents", new_list)
end
return 1
end
-- New bag is smaller: Disallow inserting
return 0
end,
allow_take = function(inv, listname, index, stack, player)
if player:get_inventory():is_empty(listname.."contents") then
if player:get_inventory():is_empty(listname .. "contents") then
return stack:get_count()
end
return 0
end,
on_take = function(inv, listname, index, stack, player)
player:get_inventory():set_size(listname.."contents", 0)
player:get_inventory():set_size(listname .. "contents", 0)
save_bags_metadata(player, inv)
end,
allow_move = function()
Expand Down
Loading