Merge branch 'yamanq-master'
This commit is contained in:
commit
27db0f4429
236
protection.lua
236
protection.lua
@ -1,4 +1,3 @@
|
||||
|
||||
-- get the id of the village pos lies in (or nil if outside of villages)
|
||||
mg_villages.get_town_id_at_pos = function( pos )
|
||||
for id, v in pairs( mg_villages.all_villages ) do
|
||||
@ -42,10 +41,26 @@ minetest.is_protected = function(pos, name)
|
||||
if( village_id ) then
|
||||
local is_houseowner = false;
|
||||
for nr, p in ipairs( mg_villages.all_villages[ village_id ].to_add_data.bpos ) do
|
||||
|
||||
trustedusers = p.can_edit
|
||||
trustedUser = false
|
||||
if trustedusers ~= nil then
|
||||
for _,trusted in ipairs(trustedusers) do
|
||||
if trusted == name then
|
||||
trustedUser = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- we have located the right plot; the player can build here if he owns this particular plot
|
||||
if( p.x <= pos.x and (p.x + p.bsizex) >= pos.x
|
||||
and p.z <= pos.z and (p.z + p.bsizez) >= pos.z) then
|
||||
if( p.owner and p.owner == name ) then
|
||||
|
||||
-- If player has been trusted by owner, can build
|
||||
if (trustedUser) then
|
||||
return false;
|
||||
-- If player is owner, can build
|
||||
elseif( p.owner and p.owner == name ) then
|
||||
return false;
|
||||
-- the allmende can be used by all
|
||||
elseif( mg_villages.BUILDINGS[p.btype] and mg_villages.BUILDINGS[p.btype].typ=="allmende" ) then
|
||||
@ -55,7 +70,7 @@ minetest.is_protected = function(pos, name)
|
||||
return true;
|
||||
end
|
||||
-- if the player just owns another plot in the village, check if it's one where villagers may live
|
||||
elseif( p.owner and p.owner == name ) then
|
||||
elseif( p.owner and p.owner == name or trustedUser) then
|
||||
local btype = mg_villages.all_villages[ village_id ].to_add_data.bpos[ nr ].btype;
|
||||
if( btype ~= 'road'
|
||||
and mg_villages.BUILDINGS[btype]
|
||||
@ -73,7 +88,7 @@ minetest.is_protected = function(pos, name)
|
||||
return true;
|
||||
end
|
||||
return old_is_protected(pos, name);
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
minetest.register_on_protection_violation( function(pos, name)
|
||||
@ -100,96 +115,172 @@ mg_villages.plotmarker_formspec = function( pos, formname, fields, player )
|
||||
-- if( not( mg_villages.ENABLE_PROTECTION )) then
|
||||
-- return;
|
||||
-- end
|
||||
|
||||
local meta = minetest.get_meta( pos );
|
||||
if( not( meta )) then
|
||||
return;
|
||||
end
|
||||
local village_id = meta:get_string('village_id');
|
||||
local plot_nr = meta:get_int( 'plot_nr');
|
||||
|
||||
local pname = player:get_player_name();
|
||||
|
||||
if( not( village_id ) or not( mg_villages.all_villages ) or not( mg_villages.all_villages[ village_id ] )
|
||||
or not( plot_nr ) or not( mg_villages.all_villages[ village_id ].to_add_data.bpos[ plot_nr ] )) then
|
||||
|
||||
if( not( village_id )
|
||||
or not( mg_villages.all_villages )
|
||||
or not( mg_villages.all_villages[ village_id ] )
|
||||
or not( plot_nr )
|
||||
or not( mg_villages.all_villages[ village_id ].to_add_data.bpos[ plot_nr ] )) then
|
||||
minetest.chat_send_player( pname, 'Error. This plot marker is not configured correctly.'..minetest.serialize({village_id,plot_nr }));
|
||||
return;
|
||||
end
|
||||
|
||||
local owner = mg_villages.all_villages[ village_id ].to_add_data.bpos[ plot_nr ].owner;
|
||||
local btype = mg_villages.all_villages[ village_id ].to_add_data.bpos[ plot_nr ].btype;
|
||||
|
||||
local price = "default:gold_ingot 2";
|
||||
if( btype ~= 'road'
|
||||
and mg_villages.BUILDINGS[btype]
|
||||
and mg_villages.BUILDINGS[btype].price ) then
|
||||
price = mg_villages.BUILDINGS[btype].price;
|
||||
elseif( btype ~= 'road'
|
||||
and mg_villages.BUILDINGS[btype]
|
||||
and mg_villages.BUILDINGS[btype].typ
|
||||
and mg_villages.prices[ mg_villages.BUILDINGS[btype].typ ] )then
|
||||
price = mg_villages.prices[ mg_villages.BUILDINGS[btype].typ ];
|
||||
end
|
||||
--determine prcie depending on building type
|
||||
local price_stack= ItemStack( price );
|
||||
|
||||
local plot_descr = 'Plot No. '..tostring( plot_nr ).. ' with '..tostring( mg_villages.BUILDINGS[btype].scm);
|
||||
--minetest.chat_send_player( player:get_player_name(),'DATA FOR '..tostring(plot_nr)..': '..minetest.serialize( mg_villages.all_villages[ village_id ].to_add_data.bpos[ plot_nr ] ));
|
||||
local formspec = "size[8,3]"..
|
||||
"label[1.0,0.5;Plot No.: "..tostring( plot_nr ).."]"..
|
||||
"label[2.5,0.5;Building:]"..
|
||||
"label[3.5,0.5;"..tostring( mg_villages.BUILDINGS[btype].scm ).."]"..
|
||||
"field[20,20;0.1,0.1;pos2str;Pos;"..minetest.pos_to_string( pos ).."]";
|
||||
if( owner == pname and fields['abandom'] ) then
|
||||
formspec = formspec.."label[0,2;You have abandomed this plot.]";
|
||||
mg_villages.all_villages[ village_id ].to_add_data.bpos[ plot_nr ].owner = nil;
|
||||
meta:set_string('infotext', plot_descr );
|
||||
mg_villages.save_data();
|
||||
local original_formspec = "size[8,3]"..
|
||||
"label[1.0,0.5;Plot No.: "..tostring( plot_nr ).."]"..
|
||||
"label[2.5,0.5;Building:]"..
|
||||
"label[3.5,0.5;"..tostring( mg_villages.BUILDINGS[btype].scm ).."]"..
|
||||
"field[20,20;0.1,0.1;pos2str;Pos;"..minetest.pos_to_string( pos ).."]";
|
||||
local formspec = "";
|
||||
local ifinhabit = "";
|
||||
|
||||
elseif( (not(owner) or owner=='') and fields['buy'] ) then
|
||||
-- Get Price
|
||||
local price = "default:gold_ingot 2";
|
||||
|
||||
-- check if the price can be paid
|
||||
local inv = player:get_inventory();
|
||||
if( inv and inv:contains_item( 'main', price_stack )) then
|
||||
formspec = formspec.."label[0,0;Congratulations! You have bought this plot.]";
|
||||
mg_villages.all_villages[ village_id ].to_add_data.bpos[ plot_nr ].owner = pname;
|
||||
meta:set_string('infotext', plot_descr..' (owned by '..tostring( pname )..')');
|
||||
-- save the data so that it survives server restart
|
||||
if (btype ~= 'road' and mg_villages.BUILDINGS[btype]) then
|
||||
local plot_descr = 'Plot No. '..tostring( plot_nr ).. ' with '..tostring( mg_villages.BUILDINGS[btype].scm)
|
||||
|
||||
if (mg_villages.BUILDINGS[btype].price) then
|
||||
price = mg_villages.BUILDINGS[btype].price;
|
||||
elseif (mg_villages.BUILDINGS[btype].typ and mg_villages.prices[ mg_villages.BUILDINGS[btype].typ ]) then
|
||||
price = mg_villages.prices[ mg_villages.BUILDINGS[btype].typ ];
|
||||
end
|
||||
-- Get if is inhabitant house
|
||||
if (mg_villages.BUILDINGS[btype].inh and mg_villages.BUILDINGS[btype].inh > 0 ) then
|
||||
ifinhabit = "label[1,1.5;Owners of this plot count as village inhabitants.]";
|
||||
end
|
||||
end
|
||||
-- Determine price depending on building type
|
||||
local price_stack= ItemStack( price );
|
||||
|
||||
|
||||
-- If nobody owns the plot
|
||||
if (not(owner) or owner=='') then
|
||||
|
||||
formspec = original_formspec ..
|
||||
"label[1,1;You can buy this plot for]"..
|
||||
"label[3.8,1;"..tostring( price_stack:get_count() ).." x ]"..
|
||||
"item_image[4.3,0.8;1,1;"..( price_stack:get_name() ).."]"..
|
||||
ifinhabit..
|
||||
"button[2,2.5;1.5,0.5;buy;Buy plot]"..
|
||||
"button_exit[4,2.5;1.5,0.5;abort;Exit]";
|
||||
|
||||
-- On Press buy button
|
||||
if (fields['buy']) then
|
||||
local inv = player:get_inventory();
|
||||
|
||||
if not mg_villages.all_villages[village_id].ownerlist then
|
||||
mg_villages.all_villages[village_id].ownerlist = {}
|
||||
end
|
||||
|
||||
-- Check if player already has a house in the village
|
||||
if mg_villages.all_villages[village_id].ownerlist[pname] then
|
||||
formspec = formspec.."label[1,1.9;Sorry. You already have a plot in this village.]";
|
||||
|
||||
-- Check if the price can be paid
|
||||
elseif( inv and inv:contains_item( 'main', price_stack )) then
|
||||
formspec = original_formspec..
|
||||
"label[1,1;Congratulations! You have bought this plot.]"..
|
||||
"button_exit[5.75,2.5;1.5,0.5;abort;Exit]";
|
||||
mg_villages.all_villages[ village_id ].to_add_data.bpos[ plot_nr ].owner = pname;
|
||||
if mg_villages.all_villages[village_id].ownerlist then
|
||||
mg_villages.all_villages[village_id].ownerlist[pname] = true;
|
||||
else
|
||||
mg_villages.all_villages[village_id].ownerlist[pname] = true;
|
||||
end
|
||||
meta:set_string('infotext', 'Plot No. '..tostring( plot_nr ).. ' with '..tostring( mg_villages.BUILDINGS[btype].scm)..' (owned by '..tostring( pname )..')');
|
||||
-- save the data so that it survives server restart
|
||||
mg_villages.save_data();
|
||||
-- substract the price from the players inventory
|
||||
inv:remove_item( 'main', price_stack );
|
||||
else
|
||||
formspec = formspec.."label[1,1.9;Sorry. You are not able to pay the price.]";
|
||||
end
|
||||
end
|
||||
|
||||
-- If player is the owner of the plot
|
||||
elseif (owner==pname) then
|
||||
|
||||
-- Check if inhabitant house
|
||||
if(btype ~= 'road'
|
||||
and mg_villages.BUILDINGS[btype]
|
||||
and mg_villages.BUILDINGS[btype].inh
|
||||
and mg_villages.BUILDINGS[btype].inh > 0 ) then
|
||||
|
||||
ifinhabit = "label[1,1.5;You are allowed to modify the common village area.]";
|
||||
end
|
||||
|
||||
formspec = original_formspec.."size[8,3]"..
|
||||
"label[1,1;This is your plot. You have bought it.]"..
|
||||
"button[0.75,2.5;3,0.5;add_remove;Add/Remove Players]"..
|
||||
ifinhabit..
|
||||
"button_exit[3.75,2.5;2.0,0.5;abandon;Abandon plot]"..
|
||||
"button_exit[5.75,2.5;1.5,0.5;abort;Exit]";
|
||||
|
||||
-- If Player wants to abandon plot
|
||||
if(fields['abandon'] ) then
|
||||
formspec = original_formspec..
|
||||
"label[1,1;You have abandoned this plot.]"..
|
||||
"button_exit[5.75,2.5;1.5,0.5;abort;Exit]";
|
||||
mg_villages.all_villages[village_id].ownerlist[pname] = nil;
|
||||
mg_villages.all_villages[ village_id ].to_add_data.bpos[ plot_nr ].can_edit = {}
|
||||
mg_villages.all_villages[ village_id ].to_add_data.bpos[ plot_nr ].owner = nil;
|
||||
-- Return price to player
|
||||
local inv = player:get_inventory();
|
||||
inv:add_item( 'main', price_stack );
|
||||
meta:set_string('infotext', 'Plot No. '..tostring( plot_nr ).. ' with '..tostring( mg_villages.BUILDINGS[btype].scm) );
|
||||
mg_villages.save_data();
|
||||
-- substract the price from the players inventory
|
||||
inv:remove_item( 'main', price_stack );
|
||||
else
|
||||
formspec = formspec.."label[0,0;Sorry. You are not able to pay the price.]";
|
||||
end
|
||||
end
|
||||
-- update the owner information
|
||||
owner = mg_villages.all_villages[ village_id ].to_add_data.bpos[ plot_nr ].owner;
|
||||
|
||||
-- If Player wants to add/remove trusted players
|
||||
if (fields['add_remove']) then
|
||||
local previousTrustees = mg_villages.all_villages[ village_id ].to_add_data.bpos[ plot_nr ].can_edit
|
||||
local output = "";
|
||||
if previousTrustees == nil then
|
||||
previousTrustees = {}
|
||||
else
|
||||
for _, player in ipairs(previousTrustees) do
|
||||
output = output..player.."\n"
|
||||
end
|
||||
end
|
||||
formspec = "size[8,3]"..
|
||||
"field[20,20;0.1,0.1;pos2str;Pos;"..minetest.pos_to_string( pos ).."]"..
|
||||
"textarea[0.3,0.2;8,2.5;ownerplayers;Trusted Players;"..output.."]"..
|
||||
"button[3.25,2.5;1.5,0.5;savetrustees;Save]";
|
||||
|
||||
if( owner == pname ) then
|
||||
formspec = formspec.."label[1,1;This is your plot. You have bought it.]"..
|
||||
"button_exit[2,2.5;2.0,0.5;abandom;Abandom plot]"..
|
||||
"button_exit[4,2.5;1.5,0.5;abort;Exit]";
|
||||
elseif( not( owner ) or owner=="" ) then
|
||||
formspec = formspec.."label[1,1;You can buy this plot for]"..
|
||||
"label[3.8,1;"..tostring( price_stack:get_count() ).." x ]"..
|
||||
"item_image[4.3,0.8;1,1;"..( price_stack:get_name() ).."]"..
|
||||
"button_exit[2,2.5;1.5,0.5;buy;Buy plot]"..
|
||||
"button_exit[4,2.5;1.5,0.5;abort;Exit]";
|
||||
mg_villages.save_data()
|
||||
end
|
||||
|
||||
-- Save trusted players
|
||||
if (fields["savetrustees"] == "Save") then
|
||||
|
||||
if not mg_villages.all_villages[ village_id ].to_add_data.bpos[ plot_nr ].can_edit then
|
||||
mg_villages.all_villages[ village_id ].to_add_data.bpos[ plot_nr ].can_edit = {}
|
||||
end
|
||||
|
||||
local x = 1;
|
||||
for _, player in ipairs(fields.ownerplayers:split("\n")) do
|
||||
mg_villages.all_villages[ village_id ].to_add_data.bpos[ plot_nr ].can_edit[x] = player
|
||||
x = x + 1
|
||||
end
|
||||
|
||||
mg_villages.save_data();
|
||||
end
|
||||
|
||||
-- If A different Player owns plot
|
||||
else
|
||||
formspec = formspec.."label[1,1;"..tostring( owner ).." owns this plot.]"..
|
||||
"button_exit[3,2.5;1.5,0.5;abort;Exit]";
|
||||
end
|
||||
|
||||
if( btype ~= 'road'
|
||||
and mg_villages.BUILDINGS[btype]
|
||||
and mg_villages.BUILDINGS[btype].inh
|
||||
and mg_villages.BUILDINGS[btype].inh > 0 ) then
|
||||
if( owner==pname ) then
|
||||
formspec = formspec.."label[1,1.5;You are allowed to modify the common village area.]";
|
||||
else
|
||||
formspec = formspec.."label[1,1.5;Owners of this plot count as village inhabitants.]";
|
||||
end
|
||||
formspec = original_formspec.."label[1,1;"..tostring( owner ).." owns this plot.]"..
|
||||
"button_exit[3,2.5;1.5,0.5;abort;Exit]";
|
||||
end
|
||||
|
||||
minetest.show_formspec( pname, "mg_villages:plotmarker", formspec );
|
||||
@ -198,6 +289,7 @@ end
|
||||
|
||||
|
||||
mg_villages.form_input_handler = function( player, formname, fields)
|
||||
mg_villages.print(mg_villages.DEBUG_LEVEL_NORMAL,minetest.serialize(fields));
|
||||
if( not( mg_villages.ENABLE_PROTECTION )) then
|
||||
return false;
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user