From a73236d7eb6ff79476d0187ff46e83872fc798be Mon Sep 17 00:00:00 2001 From: orwell96 Date: Wed, 7 Oct 2020 15:19:55 +0200 Subject: Don't allow wagons with non-empty inventories to be destroyed (H#164) --- advtrains/locale/advtrains.de.tr | 1 + advtrains/wagons.lua | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/advtrains/locale/advtrains.de.tr b/advtrains/locale/advtrains.de.tr index ca67a7e..520b200 100644 --- a/advtrains/locale/advtrains.de.tr +++ b/advtrains/locale/advtrains.de.tr @@ -63,3 +63,4 @@ Detailed Steam Engine=detaillierte Dampflokomotive Passenger Wagon=Passagierwaggon Box wagon=Güterwaggon Subway Passenger Wagon=U-Bahn-Waggon +The wagon's inventory is not empty!=Das Inventar dieses Waggons ist nicht leer! diff --git a/advtrains/wagons.lua b/advtrains/wagons.lua index 6f72d92..90615a5 100644 --- a/advtrains/wagons.lua +++ b/advtrains/wagons.lua @@ -187,6 +187,21 @@ function wagon:on_punch(puncher, time_from_last_punch, tool_capabilities, direct self:set_livery(puncher, itemstack, data) return end + -- check whether wagon has an inventory. Is is empty? + if self.has_inventory then + local inv=minetest.get_inventory({type="detached", name="advtrains_wgn_"..self.id}) + if not inv then -- inventory is not initialized when wagon was never loaded - should never happen + atwarn("Destroying wagon with inventory, but inventory is not found? Shouldn't happen!") + return + end + for listname, _ in pairs(inv:get_lists()) do + if not inv:is_empty(listname) then + minetest.chat_send_player(puncher:get_player_name(), attrans("The wagon's inventory is not empty!")); + return + end + end + end + if #(self:train().trainparts)>1 then minetest.chat_send_player(puncher:get_player_name(), attrans("Wagon needs to be decoupled from other wagons in order to destroy it.")); return -- cgit v1.2.3