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

modernize, fix addressee bug #3

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
25 changes: 25 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -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",
},
}
19 changes: 19 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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,.]
File renamed without changes.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 0 additions & 1 deletion depends.txt

This file was deleted.

95 changes: 63 additions & 32 deletions init.lua
Original file line number Diff line number Diff line change
@@ -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]" ..
Expand All @@ -19,24 +23,34 @@ 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"))
desc = desc .. "\nAttn: " .. meta:get_string("attn")
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
})

Expand All @@ -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
Expand All @@ -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"}
}
})
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
7 changes: 7 additions & 0 deletions mod.conf
Original file line number Diff line number Diff line change
@@ -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