diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 0000000..9d5b459 --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,25 @@ +name: pre-commit +on: [push, pull_request, workflow_dispatch] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@master + - uses: actions/setup-python@master + + - name: install luarocks + run: sudo apt-get install -y luarocks + + - name: luacheck install + run: luarocks install --local luacheck + + - name: add luacheck path + run: echo "$HOME/.luarocks/bin" >> $GITHUB_PATH + + - name: Install pre-commit + run: pip3 install pre-commit + + - name: Run pre-commit + run: pre-commit run --all-files diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 0000000..644efe9 --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,31 @@ +std = "lua51+luajit+minetest+envelopes" +unused_args = false +max_line_length = 120 + +stds.minetest = { + read_globals = { + "DIR_DELIM", + "minetest", + "core", + "dump", + "vector", + "nodeupdate", + "VoxelManip", + "VoxelArea", + "PseudoRandom", + "ItemStack", + "default", + "table", + "math", + "string", + } +} + +stds.envelopes = { + globals = { + "envelopes", + }, + read_globals = { + "canonical_name", + }, +} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..1b81d73 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,19 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.3.0 + hooks: + - id: fix-byte-order-marker + - id: end-of-file-fixer + - id: trailing-whitespace + + - id: mixed-line-ending + args: [ --fix=lf ] + + - repo: local + hooks: + - id: luacheck + name: luacheck + language: system + entry: luacheck + pass_filenames: false + args: [-q,.] diff --git a/LICENSE b/LICENSE.txt similarity index 100% rename from LICENSE rename to LICENSE.txt diff --git a/README.md b/README.md index bd1611f..93f2046 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,10 @@ Envelope mod for Minetest. https://minetest.net/ # How to use -Blank envelopes are crafted with 6 paper. Punch with a blank envelope to open the letter-wrting formspec, which allows you to specify an addressee, letter text, and an optional "Attn." field which will be displayed in the envelope description. Once writing is complete, the envelope is sealed by pressing the "Seal" button. The blank envelope is then replaced by a sealed envelope which shows the sender, addressee, and optionally the Attn. field in the description. If the addressee punches with the sealed envelope, it will become an opened envelope, which, once punched, shows a formspec with the text. While a sealed envelope is only openable by the addressee, an opened envelope can be read by anyone - thus, secret correspondence should be pulverized. +Blank envelopes are crafted with 6 paper. Punch with a blank envelope to open the letter-wrting formspec, which +allows you to specify an addressee, letter text, and an optional "Attn." field which will be displayed in the +envelope description. Once writing is complete, the envelope is sealed by pressing the "Seal" button. The blank +envelope is then replaced by a sealed envelope which shows the sender, addressee, and optionally the Attn. field in +the description. If the addressee punches with the sealed envelope, it will become an opened envelope, which, once +punched, shows a formspec with the text. While a sealed envelope is only openable by the addressee, an opened +envelope can be read by anyone - thus, secret correspondence should be pulverized. diff --git a/depends.txt b/depends.txt deleted file mode 100644 index 5e78c21..0000000 --- a/depends.txt +++ /dev/null @@ -1 +0,0 @@ -default? diff --git a/init.lua b/init.lua index ad7882f..25fbc8b 100644 --- a/init.lua +++ b/init.lua @@ -1,8 +1,12 @@ +local f = string.format + +local has_canonical_name = minetest.get_modpath("canonical_name") + minetest.register_craftitem("envelopes:envelope_blank", { description = "Blank Envelope", inventory_image = "envelopes_envelope_blank.png", on_use = function(itemstack, user, pointed_thing) - minetest.show_formspec(user:get_player_name(), "envelopes:input", + minetest.show_formspec(user:get_player_name(), "envelopes:input", "size[5.5,5.5]" .. "field[2,0.5;3.5,1;addressee;Addressee;]" .. "label[0,0;Write a letter]" .. @@ -19,14 +23,22 @@ minetest.register_craftitem("envelopes:envelope_sealed", { stack_max = 1, groups = {not_in_creative_inventory = 1}, on_use = function(itemstack, user, pointed_thing) - meta = itemstack:get_meta() - if user:get_player_name() == meta:get_string("receiver") then - open_env = ItemStack("envelopes:envelope_opened") - open_meta = open_env:get_meta() + local user_name = user:get_player_name() + local meta = itemstack:get_meta() + local addressee = meta:get_string("receiver") + + if has_canonical_name then + addressee = canonical_name.get(addressee) + end + + if user_name == addressee then + local open_env = ItemStack("envelopes:envelope_opened") + local open_meta = open_env:get_meta() open_meta:set_string("sender", meta:get_string("sender")) open_meta:set_string("receiver", meta:get_string("receiver")) open_meta:set_string("text", meta:get_string("text")) - local desc = ("Opened Envelope\nTo: " .. meta:get_string("receiver") .. "\nFrom: " .. meta:get_string("sender")) + local desc = ("Opened Envelope\nTo: " .. meta:get_string("receiver") .. "\nFrom: " + .. meta:get_string("sender")) open_meta:set_string("description", desc) if meta:get_string("attn") ~= "" then open_meta:set_string("attn", meta:get_string("attn")) @@ -34,9 +46,11 @@ minetest.register_craftitem("envelopes:envelope_sealed", { open_meta:set_string("description", desc) end return open_env + + else + minetest.chat_send_player(user_name, f("The seal can only be opened by %s!", addressee)) + return itemstack end - minetest.chat_send_player(user:get_player_name(), "The seal can only be opened by the addressee!") - return itemstack end }) @@ -51,7 +65,7 @@ minetest.register_craftitem("envelopes:envelope_opened", { local receiver = meta:get_string("receiver") local text = meta:get_string("text") local attn = meta:get_string("attn") or "" - local form = + local form = "size[5,5]" .. "label[0,0;A letter from " .. sender .. " to " .. receiver if attn ~= "" then @@ -67,45 +81,62 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) return false end - if fields.addressee == "" or fields.addressee == nil or fields.text == "" or fields.text == nil then - minetest.chat_send_player(player:get_player_name(), "Please fill out all required fields.") + local sender_name = player:get_player_name() + + local addressee = (fields.addressee or ""):trim() + local text = (fields.text or ""):trim() + local attn = (fields.attn or ""):trim() + + if addressee == "" or text == "" then + minetest.chat_send_player(sender_name, "Please fill out all required fields.") + return true + end + + if has_canonical_name then + addressee = canonical_name.get(addressee) or addressee + end + + if not minetest.player_exists(addressee) then + minetest.chat_send_player(sender_name, f("unknown addressee %q", addressee)) return true end local inv = player:get_inventory() - local letter = ItemStack('envelopes:envelope_sealed') - local blank = ItemStack('envelopes:envelope_blank') + local letter = ItemStack("envelopes:envelope_sealed") + local blank = ItemStack("envelopes:envelope_blank") local meta = letter:get_meta() - meta:set_string("sender", player:get_player_name()) - meta:set_string("receiver", fields.addressee) - meta:set_string("text", fields.text) + meta:set_string("sender", sender_name) + meta:set_string("receiver", addressee) + meta:set_string("text", text) - local desc = ("Sealed Envelope\nTo: " .. fields.addressee .. "\nFrom: " .. player:get_player_name()) - meta:set_string("description", desc) + local desc = ("Sealed Envelope\nTo: " .. addressee .. "\nFrom: " .. sender_name) - if fields.attn ~= "" then - meta:set_string("attn", fields.attn) - desc = desc .. "\nAttn: " .. fields.attn - meta:set_string("description", desc) + if attn ~= "" then + meta:set_string("attn", attn) + desc = desc .. "\nAttn: " .. attn end + meta:set_string("description", desc) + if inv:room_for_item("main", letter) and inv:contains_item("main", blank) then inv:add_item("main", letter) inv:remove_item("main", blank) else - minetest.chat_send_player(player:get_player_name(), "Unable to create letter! Check your inventory space.") + minetest.chat_send_player(sender_name, "Unable to create letter! Check your inventory space.") end return true end) -minetest.register_craft({ - type = "shaped", - output = "envelopes:envelope_blank 1", - recipe = { - {"", "", ""}, - {"default:paper", "default:paper", "default:paper"}, - {"default:paper", "default:paper", "default:paper"} - } -}) \ No newline at end of file +if minetest.get_modpath("default") then + minetest.register_craft({ + type = "shaped", + output = "envelopes:envelope_blank 1", + recipe = { + {"", "", ""}, + {"default:paper", "default:paper", "default:paper"}, + {"default:paper", "default:paper", "default:paper"} + } + }) +end diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..297483f --- /dev/null +++ b/mod.conf @@ -0,0 +1,7 @@ +name = envelopes +title = Envelopes +description = Provides envelopes and sealed letters, which can only be opened by the addressee. +author = archfan7411 +license = MIT +version = 2022-10-25 +optional_depends = default, canonical_name