From 71853506ef6fc9b47dc39bd252423a5f24cb4187 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sat, 2 Jul 2022 16:49:27 +0200 Subject: [PATCH] Cycle flags backwards when punching --- README.txt | 5 +++-- init.lua | 46 +++++++++++++++++++++++++++++++++------------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/README.txt b/README.txt index a3915c7..e86ed25 100644 --- a/README.txt +++ b/README.txt @@ -9,8 +9,9 @@ conspicuous location, such as atop a building or along a promenade. /giveme pride_flags:mast_lower /giveme pride_flags:mast_upper -By default, a rainbow pride flag will appear when the upper mast is placed. Right-click -the node to select a different flag (this requires the "server" privilege). +Initially, the rainbow pride flag will appear when the upper mast is placed. +Right-click or punch the node to select a different flag +(this requires the "server" privilege). Repository diff --git a/init.lua b/init.lua index d7dfb0e..3a91747 100644 --- a/init.lua +++ b/init.lua @@ -114,8 +114,16 @@ minetest.register_entity( "pride_flags:wavingflag", { reset_texture = function ( self, flag_idx ) if not flag_idx then + -- next flag self.flag_idx = self.flag_idx % #flag_list + 1 -- this automatically increments + elseif flag_idx == -1 then + -- previous flag + self.flag_idx = self.flag_idx - 1 + if self.flag_idx < 1 then + self.flag_idx = #flag_list + end else + -- set flag directly self.flag_idx = flag_idx end @@ -201,6 +209,27 @@ local function spawn_flag( pos ) return obj end +local function cycle_flag( pos, player, cycle_backwards ) + local node_idx = minetest.hash_node_position( pos ) + + if minetest.check_player_privs( player:get_player_name( ), "server" ) then + local aflag = active_flags[ node_idx ] + local flag + if aflag then + flag = aflag:get_luaentity( ) + end + if flag then + if cycle_backwards then + flag:reset_texture( -1 ) + else + flag:reset_texture( ) + end + else + spawn_flag( pos ) + end + end +end + minetest.register_node( "pride_flags:upper_mast", { description = S("Flag Pole with Flag"), drawtype = "mesh", @@ -223,20 +252,11 @@ minetest.register_node( "pride_flags:upper_mast", { }, on_rightclick = function ( pos, node, player ) - local node_idx = minetest.hash_node_position( pos ) + cycle_flag( pos, player ) + end, - if minetest.check_player_privs( player:get_player_name( ), "server" ) then - local aflag = active_flags[ node_idx ] - local flag - if aflag then - flag = aflag:get_luaentity( ) - end - if flag then - flag:reset_texture( ) - else - spawn_flag( pos ) - end - end + on_punch = function ( pos, node, player ) + cycle_flag( pos, player, -1 ) end, on_construct = function ( pos )