place_limit: Use HUD notifications & quota instead of time between placements (#844)
* place_limit: Use HUD notifications * Adjust warning color * Quota instead of time between placements * Bump maps submodule
This commit is contained in:
parent
fd5b8a484e
commit
e40d1e666a
2 changed files with 27 additions and 28 deletions
|
@ -1,53 +1,51 @@
|
||||||
-- Licensed under the MIT license, written by appgurueu.
|
-- Licensed under the MIT license, written by appgurueu.
|
||||||
local players = {}
|
local players = {}
|
||||||
local blocks_per_second = 5
|
local blocks_per_second = 5
|
||||||
local resend_chat_message_seconds = 10
|
local resend_notification_seconds = 10
|
||||||
|
-- Bootstrap 4 "warning" color
|
||||||
|
local warning_color = 0xFFC107
|
||||||
|
|
||||||
minetest.register_on_joinplayer(function(player)
|
minetest.register_on_joinplayer(function(player)
|
||||||
-- player has to wait after join before they can place a node
|
-- player has to wait after join before they can place a node
|
||||||
players[player:get_player_name()] = {
|
players[player:get_player_name()] = {
|
||||||
last_placement = minetest.get_us_time(),
|
last_notification_sent = -math.huge
|
||||||
last_chat_message_sent = -math.huge
|
|
||||||
}
|
}
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local chat_send_player = minetest.chat_send_player
|
|
||||||
function minetest.chat_send_player(name, message)
|
|
||||||
if players[name] then
|
|
||||||
players[name].last_chat_message_sent = -math.huge
|
|
||||||
end
|
|
||||||
return chat_send_player(name, message)
|
|
||||||
end
|
|
||||||
|
|
||||||
local chat_send_all = minetest.chat_send_all
|
|
||||||
function minetest.chat_send_all(message)
|
|
||||||
for _, playerdata in pairs(players) do
|
|
||||||
playerdata.last_chat_message_sent = -math.huge
|
|
||||||
end
|
|
||||||
return chat_send_all(message)
|
|
||||||
end
|
|
||||||
|
|
||||||
minetest.register_on_leaveplayer(function(player)
|
minetest.register_on_leaveplayer(function(player)
|
||||||
players[player:get_player_name()] = nil
|
players[player:get_player_name()] = nil
|
||||||
end)
|
end)
|
||||||
|
|
||||||
minetest.register_on_placenode(function(pos, _newnode, placer, oldnode, _itemstack, pointed_thing)
|
minetest.register_on_placenode(function(pos, _newnode, placer, oldnode, _itemstack, pointed_thing)
|
||||||
if not ItemStack(minetest.get_node(pointed_thing.under).name):get_definition().pointable then
|
|
||||||
-- this should happen rarely
|
|
||||||
minetest.chat_send_player(placer:get_player_name(), "The block you have been building to has been dug/replaced!")
|
|
||||||
minetest.set_node(pos, oldnode)
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
local name = placer:get_player_name()
|
local name = placer:get_player_name()
|
||||||
local playerdata = players[name]
|
if not ItemStack(minetest.get_node(pointed_thing.under).name):get_definition().pointable then
|
||||||
|
-- This should happen rarely
|
||||||
|
hud_event.new(name, {
|
||||||
|
name = "place_limit:unpointable",
|
||||||
|
color = warning_color,
|
||||||
|
value = "Block not pointable (dug/replaced)!",
|
||||||
|
})
|
||||||
|
minetest.set_node(pos, oldnode)
|
||||||
|
return true
|
||||||
|
end
|
||||||
local time = minetest.get_us_time()
|
local time = minetest.get_us_time()
|
||||||
if (time - playerdata.last_placement) / 1e6 < 1 / blocks_per_second then
|
local placements = players[name]
|
||||||
if (time - playerdata.last_chat_message_sent) / 1e6 >= resend_chat_message_seconds then
|
for i = #placements, 1, -1 do
|
||||||
chat_send_player(placer:get_player_name(), "You are placing blocks too fast (more than " .. blocks_per_second .. " blocks per second) !")
|
if time - placements[i] > 1e6 then
|
||||||
playerdata.last_chat_message_sent = time
|
placements[i] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if #placements >= blocks_per_second then
|
||||||
|
if (time - placements.last_notification_sent) / 1e6 >= resend_notification_seconds then
|
||||||
|
hud_event.new(name, {
|
||||||
|
name = "place_limit:speed",
|
||||||
|
color = warning_color,
|
||||||
|
value = "Placing too fast!",
|
||||||
|
})
|
||||||
|
placements.last_notification_sent = time
|
||||||
end
|
end
|
||||||
minetest.set_node(pos, oldnode)
|
minetest.set_node(pos, oldnode)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
playerdata.last_placement = time
|
table.insert(placements, 1, time)
|
||||||
end)
|
end)
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
name = place_limit
|
name = place_limit
|
||||||
description = Limits block placement
|
description = Limits block placement
|
||||||
|
depends = hud_events
|
||||||
|
|
Loading…
Reference in a new issue