Initalized Git Repo

Behold the power of Version Control!
This commit is contained in:
david 2021-06-25 02:14:57 -04:00
parent 34be102ebc
commit 86de82b45f
9 changed files with 475 additions and 0 deletions

98
API.md Normal file
View File

@ -0,0 +1,98 @@
# Item Clone > API
This mod comes with an Application Programming Interface.
## Add
A rather simple thing, inserts or updates based on if itemstring exists already.
> Does not return anything.
I.E.
```lua
-- Adds 2 default:dirt_with_grass every 15 seconds
item_clone.add("default:dirt_with_grass", 2, 15)
```
## Is
Does the itemstring exist?
> Returns true if the itemstring does, else false.
I.E.
```lua
-- Tests if default:dirt exists
if item_clone.is("default:dirt") then
-- Do something
end
-- Tests if default:tree does not exist
if not item_clone.is("default:tree") then
-- Do something
end
```
## Get Amount
If the itemstring exists it will return the amount per process.
> Returns settings "unknown_item_amount" if the itemstring does not exist
I.E.
```lua
-- Tests if defualt:dirt_with_grass produces 2 per process
if item_clone.get_amount("default:dirt_with_grass") == 2 then
-- Do something
end
-- Tests if default:dirt produces 1 per process
if item_clone.get_ammount("defualt:dirt") == 1 then
-- Do something
end
```
## Get Time
If the itemstring exists it will return the number of seconds per process.
> Returns settings "unknown_item_time" if the itemstring does not exist
I.E.
```lua
-- Tests if default:dirt_with_grass processes at 15 seconds
if item_clone.get_time("default:dirt_with_grass") == 15 then
-- Do something
end
-- Tests if default:dirt processes at 60 seconds (Default settings set unknown items to 60 seconds, adjust if wanted)
if item_clone.get_time("default:dirt") == 60 then
-- Do something
end
```
## Remove
If the itemstring exists it will remove the item.
> Returns nothing
I.E.
```lua
-- Removes the item
item_clone.remove("defualt:dirt_with_grass")
-- Then checks to ensure it's gone
if item_clone.is("default:dirt_with_grass") then
-- error("In this case it failed to remove")
-- Because errors are frequently used in my mod in the testing (test.lua) I built my own error function
item_clone_internal.throw_error("In this case it failed to remove")
-- It simply adds the error message and follows it with my mod version, attempts to identify what game you used,
-- And promotes making an issue on my repo so I know something is broken.
end
```

24
INTERNALS.md Normal file
View File

@ -0,0 +1,24 @@
# Item Clone > Internal Assitant Functions
Inside my mod I use a variety of little or big functions which help me perform some test or task with ease.
Some are used only for testing, others are used directly by the mod's main functions. (Like it's API or even else where)
## Version
This will simply be used to obtain a string of the version you are using.
It's also used by me to make automatic update reminders for the older versions. (Just to encourage folks to use the latest)
## Game Mode
This simply attempts to identify what game you are running.
E.G. MTG, MCL2.
## Throw Error
This is a function typically seen in the testing (test.lua) file.
Simply put it logs to minetest logs an error message, the current version of my mod, attemt's to get what game your running,
and encourages you to make an issue with your logs.

18
README.md Normal file
View File

@ -0,0 +1,18 @@
# Item Clone
Clone items with ease.
## What's in the box
* [Unique API](API.md)
* [Internal Assitant Functions](INTERNALS.md)
* Multi-player support
* Security
* ALL item support
## Quick-Start
1. Get this repo!
2. Drop this directory into your Minetest mods folder.
3. Add `load_mod_item_clone = true` to your worlds world.mt file.
4. Enjoy the mod.

95
api.lua Normal file
View File

@ -0,0 +1,95 @@
-- The API
function item_clone.add(itemstring, amount, time)
if not item_clone.is(itemstring) then
table.insert(item_clone_items, {itemstring, amount, time})
if item_clone_settings["log_api"] then
minetest.log("action", "[item_clone] API Add (Insert) {'"..itemstring.."', "..amount..", "..time.."}")
end
else
local index = 0
for ind, val in pairs(item_clone_items) do
if val[1] == itemstring then
break
end
index = index + 1
end
item_clone_items[index] = {itemstring, amount, time}
if item_clone_settings["log_api"] then
minetest.log("action", "[item_clone] API Add (Update) {'"..itemstring.."', "..amount..", "..time.."}")
end
end
end
function item_clone.is(itemstring)
local result = false
for ind, val in pairs(item_clone_items) do
if val[1] == itemstring then
result = true
break
end
end
if result then
if item_clone_settings["log_api"] then
minetest.log("action", "[item_clone] API Is '"..itemstring.."' == true")
end
return true
end
if item_clone_settings["log_api"] then
minetest.log("action", "[item_clone] API Is '"..itemstring.."' == false")
end
return false
end
function item_clone.get_amount(itemstring)
if item_clone.is(itemstring) then
for ind, val in pairs(item_clone_items) do
if val[1] == itemstring then
if item_clone_settings["log_api"] then
minetest.log("action", "[item_clone] API get_amount '"..itemstring.."' x "..val[2])
end
return val[2]
end
end
end
if item_clone_settings["log_api"] then
minetest.log("action", "[item_clone] API get_amount '"..itemstring.."' was not found, return 1")
end
return 1
end
function item_clone.get_time(itemstring)
if item_clone.is(itemstring) then
for ind, val in pairs(item_clone_items) do
if val[1] == itemstring then
if item_clone_settings["log_api"] then
minetest.log("action", "[item_clone] API get_time '"..itemstring.."' @ "..val[3].." second(s)")
end
return val[3]
end
end
end
if item_clone_settings["log_api"] then
minetest.log("action", "[item_clone] API get_time '"..itemstring.."' was not found, returning "..item_clone_settings["unknown_item_time"].." second(s)")
end
return item_clone_settings["unknown_item_time"]
end
function item_clone.remove(itemstring)
if item_clone.is(itemstring) then
local index = 0
for ind, val in pairs(item_clone_items) do
if val[1] == itemstring then
break
end
index = index + 1
end
table.remove(item_clone_items, index)
if item_clone_settings["log_api"] then
minetest.log("action", "[item_clone] API remove '"..itemstring.."' was removed")
end
end
if item_clone_settings["log_api"] then
minetest.log("action", "[item_clone] API remove '"..itemstring.."' was not found, ignoring")
end
end

51
init.lua Normal file
View File

@ -0,0 +1,51 @@
local mod_path = minetest.get_modpath("item_clone")
-- The API
item_clone = {}
-- The Internal stuff
item_clone_internal = {}
-- Items known to this mod
item_clone_items = {}
-- My version
function item_clone.version()
-- DO NOT TOUCH THIS, This lets me know what version of the code your really running
return "0.1 Inital"
end
-- Attempt to detect what gamemode/game these folks are running on
function item_clone.game_mode()
local reported = false -- Have we send a report
local game_mode = "???" -- let's even return that
if (minetest.get_modpath("default") or false) and not reported then
reported = true
game_mode = "MTG"
end
if (minetest.get_modpath("mcl_clone") or false) and not reported then
reported = true
game_mode = "MCL2"
end
return game_mode
end
-- Just a helper to report issues in the test suite
function item_clone_internal.throw_error(msg)
minetest.log("action", "[item_clone] Error: "..msg)
minetest.log("action", "[item_clone] Version: "..item_clone.version())
minetest.log("action", "[item_clone] GameMode: "..item_clone.game_mode())
error("[item_clone] Please make an issue on my repo with the logs from your debug.txt")
end
-- Initalize Settings
dofile(mod_path.."/settings.lua")
-- Initalize API
dofile(mod_path.."/api.lua")
-- Execute an API check
if item_clone_settings["run_test"] then
dofile(mod_path.."/test.lua")
end

2
mod.conf Normal file
View File

@ -0,0 +1,2 @@
name = item_clone
description = Clone items with ease.

124
register.lua Normal file
View File

@ -0,0 +1,124 @@
item_clone_internal.update = function (pos, elapsed)
local meta=minetest.get_meta(pos)
local inv=meta:get_inventory()
local gen=inv:get_stack("gen",1):get_name() -- The item to be produced
local process=meta:get_int("proc")
if inv:is_empty("gen") or inv:room_for_item("done",gen)==false then
minetest.get_node_timer(pos):stop()
meta:set_int("proc", 0)
if inv:room_for_item("done",gen)==false then
meta:set_string("infotext", "Item Clone [Full] (" .. meta:get_string("owner") .. ")")
end
if inv:is_empty("gen") then
meta:set_string("infotext", "Item Clone [No Product] (" .. meta:get_string("owner") .. ")")
end
minetest.swap_node(pos, {name ="item_clone:clone"})
return false
end
process=process+1
if process>=item_clone.get_time(gen) then
process=0
for i=1,item_clone.get_amount(gen),1 do
if inv:room_for_item("done",gen)==true then -- Only works if there is room
inv:add_item("done",gen)
end
end
if item_clone_settings["log_production"] then
minetest.log("action", "[item_clone] item_clone:clone_active at ("..pos.x..", "..pos.y..", "..pos.z..") by '"..meta:get_string("owner").."' has produced '"..gen.."' x "..item_clone.get_amount(gen))
end
end
meta:set_int("proc",process)
-- Let's really use a percent rather than some made up stuff.
meta:set_string("infotext", "Item Clone " .. ((process/item_clone.get_time(gen)) * 100) .."% (" .. meta:get_string("owner") .. ")")
return true
end
item_clone_internal.inv = function (placer, pos)
local meta=minetest.get_meta(pos)
local names=meta:get_string("names")
local op=meta:get_int("open")
local open=""
if op==0 then
open="Locked"
elseif op==1 then
open="Members"
else
open="Public"
end
meta:set_string("formspec",
"size[8,11]" ..
"list[context;gen;0,0;1,1;]" ..
"button[0,1; 1.5,1;save;Save]" ..
"button[0,2; 1.5,1;open;" .. open .."]" ..
"textarea[2.2,1.3;6,1.8;names;Members list (allow members to take and add);" .. names .."]"..
"list[context;done;0,2.9;8,4;]" ..
"list[current_player;main;0,7;8,4;]" ..
"listring[current_player;main]" ..
"listring[current_name;done]"
)
meta:set_string("infotext", "Item Clone (" .. placer:get_player_name() .. ")")
end
-- Now we use all this to make our clone machine
minetest.register_node("item_clone:clone", {
description = "Item Clone",
tiles = {},
groups = {dig_immediate = 3},
paramtype2 = "facedir",
light_source = 1,
after_place_node = function(pos, placer, itemstack)
local meta = minetest.get_meta(pos)
meta:set_string("owner", placer:get_player_name())
meta:set_int("open", 0)
meta:set_string("names", "")
meta:set_int("proc", 0)
meta:set_int("state", 0)
local inv = meta:get_inventory()
inv:set_size("done", 32)
inv:set_size("gen", 1)
item_clone_internal.inv(placer,pos)
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
local meta=minetest.get_meta(pos)
local open=meta:get_int("open")
local name=player:get_player_name()
local owner=meta:get_string("owner")
local count=0
if name==owner and listname~="done" then count=stack:get_count() end
if open>0 and listname=="done" then count=stack:get_count() end
if count>0 and meta:get_int("type")==0 then
minetest.get_node_timer(pos):start(1)
minetest.swap_node(pos, {name ="item_clone:clone_active"})
end
return count
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
local meta=minetest.get_meta(pos)
local open=meta:get_int("open")
local name=player:get_player_name()
local owner=meta:get_string("owner")
if name==owner then return stack:get_count() end
if open==2 and listname=="done" then return stack:get_count() end
if open==1 and listname=="done" then
local names=meta:get_string("names")
local txt=names.split(names,"\n")
for i in pairs(txt) do
if name==txt[i] then
return stack:get_count()
end
end
end
return 0
end,
can_dig = function(pos, player)
local meta=minetest.get_meta(pos)
local owner=meta:get_string("owner")
local inv=meta:get_inventory()
--
return (player:get_player_name()==owner and
owner~="")
end,
})

17
settings.lua Normal file
View File

@ -0,0 +1,17 @@
-- Settings
item_clone_settings = {}
-- Do we log the api calls? (Great for debugging issues but not so good on a long running production server)
item_clone_settings["log_api"] = true
-- Do we log that items were produced? (Great for debugging issues but not so good on a long running production server)
item_clone_settings["log_production"] = true
-- Testing the API (Turn these false when not testing)
item_clone_settings["run_test"] = true
item_clone_settings["exit_after_test"] = true
-- If an item is not in the known items list it will equal this value
item_clone_settings["unknown_item_time"] = 30
item_clone_settings["unknown_item_amount"] = 1

46
test.lua Normal file
View File

@ -0,0 +1,46 @@
-- Test file for the API
minetest.log("action", "[item_clone] Test: ....")
-- Test adding an item (Insert and Update check
item_clone.add("default:dirt", 2, 15)
item_clone.add("default:dirt", 4, 10)
item_clone.add("default:dirt", 2, 15)
-- Test validity
if item_clone.is("default:dirt_with_grass") then
item_clone_internal.throw_error("item_clone.is('default:dirt_with_grass') ~= false")
end
if not item_clone.is("default:dirt") then
item_clone_internal.throw_error("item_clone.is('default:dirt') ~= true")
end
-- Test amounts
if item_clone.get_amount("default:dirt_with_grass") ~= 1 then
item_clone_internal.throw_error("item_clone.get_amount('default:dirt_with_grass') ~= 1")
end
if item_clone.get_amount("default:dirt") ~= 2 then
item_clone_internal.throw_error("item_clone.get_amount('default:dirt') ~= 2")
end
-- Test times
if item_clone.get_time("default:dirt_with_grass") ~= item_clone_settings["unknown_item_time"] then
item_clone_internal.throw_error("item_clone.get_time('default:dirt_with_grass') ~= "..item_clone_settings["unknown_item_time"])
end
if item_clone.get_time("default:dirt") ~= 15 then
item_clone_internal.throw_error("item_clone.get_time('default:dirt') ~= 15")
end
-- Test Cleanup (Also a test against removals)
item_clone.remove("default:dirt")
if item_clone.is("default:dirt") then
item_clone_internal.throw_error("item_clone.remove('defualt:dirt') did not remove!")
end
-- If the user wants to... just have them exit
if item_clone_settings["exit_after_test"] then
minetest.log("action", "[item_clone] Test: OKAY")
error("[item_clone] Test: OKAY (Your seeing this because exit_after_test is true)")
end
-- The user chosen to keep running... so let's just let them know the tests passed
minetest.log("action", "[item_clone] Test: OKAY")