From 9d06e4560d6381f80b50328fa678ab42dc4378a1 Mon Sep 17 00:00:00 2001 From: Code-Sploit Date: Fri, 14 May 2021 14:16:56 +0000 Subject: [PATCH] Add code --- api.lua | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ commands.lua | 15 ++++++++++ init.lua | 14 +++++++++ kits.lua | 30 +++++++++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 api.lua create mode 100644 commands.lua create mode 100644 init.lua create mode 100644 kits.lua diff --git a/api.lua b/api.lua new file mode 100644 index 0000000..67981d6 --- /dev/null +++ b/api.lua @@ -0,0 +1,83 @@ +kitpvp = { + api = {}, + kits = {} +} + +function kitpvp.api.give_to_player(player, item) + -- Values nil? If so, close this function + if not player or not item then return end + + -- Add it + minetest.add_item(player:get_pos(), item) +end + +function kitpvp.api.kit_exists(kitname) + return kitpvp.kits[kitname] ~= nil +end + +function kitpvp.api.get_kit_definition(kitname) + -- Just to be sure + if not kitpvp.api.kit_exists(kitname) then return end + + return kitpvp.kits[kitname] +end + +function kitpvp.api.register_kit(def) + local name = def.name + local items = def.items + local armor = def.armor + --local effects = def.effects + + -- Is $def completly filled in? + if not name or not items or not armor then return end + + -- Register the kit + kitpvp.kits[name] = def +end + +function kitpvp.api.get_kit_names() + local kits = "" + + for kit in pairs(kitpvp.kits) do + kits = kits .. kit .. "," + end + + kits = string.sub(kits, 0, string.len(kits) - 1) + + return kits +end + +function kitpvp.api.give_kit(player, kitname) + -- Check if the kit exists + if not kitpvp.api.kit_exists(kitname) then return end + + -- Make sure that $player != NULL + if not player then return end + + -- Get the kit's definition + local def = kitpvp.api.get_kit_definition(kitname) + + -- Add the things + -- DEFAULT items + if def.default_items then + for i=1,64 do + kitpvp.api.give_to_player(player, "mcl_mobitems:cooked_beef") + end + end + + -- ARMOR + local components = {"helmet", "chestplate", "leggings", "boots"} + + for i, component in pairs(components) do + i = i + 1 + + local item_name = "mcl_armor:" .. component .. "_" .. def.armor + + kitpvp.api.give_to_player(player, item_name) + end + + -- ITEMS + for i, item in pairs(def.items) do + kitpvp.api.give_to_player(player, item) + end +end diff --git a/commands.lua b/commands.lua new file mode 100644 index 0000000..22a6efd --- /dev/null +++ b/commands.lua @@ -0,0 +1,15 @@ +minetest.register_chatcommand("kit", { + description = "Gives you a kit to PvP", + + func = function(name, param) + local player = minetest.get_player_by_name(name) + + if not player then return end + + if param == "list" then + return false, "Available kits: " .. kitpvp.api.get_kit_names() + else + kitpvp.api.give_kit(player, param) + end + end +}) diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..bb1ea81 --- /dev/null +++ b/init.lua @@ -0,0 +1,14 @@ +local modname = minetest.get_current_modname() +local modpath = minetest.get_modpath(modname) + +if modpath then modpath = modpath .. "/" end + +local launch = { + "api.lua", + "kits.lua", + "commands.lua" +} + +for _, item in pairs(launch) do + dofile(modpath .. item) +end diff --git a/kits.lua b/kits.lua new file mode 100644 index 0000000..1b185ab --- /dev/null +++ b/kits.lua @@ -0,0 +1,30 @@ +--[[ +HOW TO ADD KITS: + +kitpvp.api.register_kit({ + name = agreatkitname, + items = {itemstr1, itemstr2, itemstr3}, + armor = leather/chain/iron/gold/diamond/netherite +}) +]] + +kitpvp.api.register_kit({ + name = "tank", + items = {"mcl_tools:sword_iron"}, + default_items = true, + armor = "iron" +}) + +kitpvp.api.register_kit({ + name = "archer", + items = {"mcl_tools:sword_stone", "mcl_bows:bow", "mcl_bows:arrow 10000"}, + default_items = true, + armor = "gold" +}) + +kitpvp.api.register_kit({ + name = "speedrunner", + items = {"mcl_tools:sword_stone", "mcl_potions:swiftness_2"}, + default_items = true, + armor = "gold" +})