master
Lars Mueller 2020-04-25 13:09:57 +02:00
commit 5f0df049a2
5 changed files with 131 additions and 0 deletions

52
Readme.md Normal file
View File

@ -0,0 +1,52 @@
# Cycle Limit (`cycle_limit`)
Makes switching between inventory slots take time.
## About
Cycle Limit only depends on [`modlib`](https://github.com/appgurueu/cellestial/modlib). Code by Lars Mueller aka LMD or appguru(eu) and licensed under the MIT license.
Part of the Limit Series: [`item_limit`](https://github.com/appgurueu/item_limit), [`place_limit`](https://github.com/appgurueu/place_limit) and [`cycle_limit`](https://github.com/appgurueu/cycle_limit)
## Features
* When switching between inventory slots, the item you are switching to will be "hidden"
* A bar appears showing you the time left, and after it's over (or if you switch again) the hidden items reappear
* During switching you can only use your hand
* Hidden items are not lost if the server crashes
Known issues:
* The item is temporarily removed from the inventory
* Can't be circumvented because else `get_wield_item` would return item that is being switched to
* Accordingly, it can't be seen
*
## Screenshot
![Screenshot](screenshot.png)
### Links
* [GitHub](https://github.com/appgurueu/cycle_limit)
* [Discord](https://discordapp.com/invite/ysP74by)
* [ContentDB](https://content.minetest.net/packages/LMD/cycle_limit)
* [Minetest Forum](https://example.com/to_be_created)
## Configuration
Configuration can be found under `<worldpath>/conf/cycle_limit.json`.
Default configuration:
```json
{
"name": "Switching",
"duration": 2,
"color": "545AA7"
}
```
* `name` is the timer name
* `duration` is the time it takes to switch in seconds
* `color` is a hex color (but without `#`)

5
default_config.json Normal file
View File

@ -0,0 +1,5 @@
{
"name": "Switching",
"duration": 2,
"color": "545AA7"
}

1
init.lua Normal file
View File

@ -0,0 +1 @@
modlib.mod.init("cycle_limit")

69
main.lua Normal file
View File

@ -0,0 +1,69 @@
config = modlib.conf.import("cycle_limit", {
type = "table",
children = {
name = {type = "string"},
duration = {type = "number", range = {0}},
color = {type = "string", func = function(num)
if not tonumber(num, 16) then
return "Expected hex color"
end
end}
}
})
if config.duration == 0 then
return
end
local players = {}
minetest.register_on_joinplayer(function(player)
players[player:get_player_name()] = {
index = player:get_wield_index()
}
local meta = player:get_meta()
local taken = meta:get("cycle_limit_taken")
if taken then
taken = minetest.parse_json(taken)
player:get_inventory():set_stack(player:get_wield_list(), taken[1], ItemStack(taken[2]))
meta:set_string("cycle_limit_taken", "")
end
end)
minetest.register_globalstep(function()
for _, player in pairs(minetest.get_connected_players()) do
local switching = players[player:get_player_name()]
local index = player:get_wield_index()
if index ~= switching.index and index ~= switching.target_index then
if switching.item then
local inv = player:get_inventory()
inv:set_stack(player:get_wield_list(), switching.target_index, switching.item)
end
switching.item = player:get_wielded_item()
if switching.timer then
hud_timers.remove_timer_by_reference(player:get_player_name(), switching.timer)
end
switching.timer = hud_timers.add_timer(
player:get_player_name(),
{
name = config.name,
duration = config.duration,
color = config.color,
on_complete = function()
player:get_inventory():set_stack(player:get_wield_list(), switching.target_index, switching.item)
switching.item = nil
switching.timer = nil
switching.index = switching.target_index
switching.target_index = nil
player:get_meta():set_string("cycle_limit_taken", "")
player:hud_set_flags{wielditem = true}
end
}
)
player:get_meta():set_string("cycle_limit_taken", minetest.write_json{index, switching.item:to_string()})
player:set_wielded_item("")
player:hud_set_flags{wielditem = false}
switching.target_index = index
end
end
end)

4
mod.conf Normal file
View File

@ -0,0 +1,4 @@
name=cycle_limit
title=Cycle Limit
description=Limits hotbar slot switching
depends=modlib