Skip to content

Commit 739f524

Browse files
authored
Merge pull request #13 from VertexDezign/fix-issue-with-add-spec
Fix issue with add spec
2 parents 50984da + 69290f0 commit 739f524

File tree

6 files changed

+192
-254
lines changed

6 files changed

+192
-254
lines changed

ParkVehicle.lua

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,25 @@
66
-- v2.0.0.0 - 2018-12-03 - FS19
77
-- v2.1.0.0 - 2019-04-01 - Support all enterable vehicles, create modSettings folder
88
-- v3.0.0.0 - 2021-11-19 - FS22
9+
-- v3.1.0.0 - 2022-04-23 - Add possibiltiy to unpark all, fix issue with registration in loader
910
-- @Descripion: Allows temporary disabling of the tab function
1011
-- @web: https://grisu118.ch or https://vertexdezign.net
1112
-- Copyright (C) Grisu118, All Rights Reserved.
1213

14+
---@class ParkVehicle
1315
ParkVehicle = {}
1416
ParkVehicle.inputName = "parkVehicle"
15-
ParkVehicle.modDir = g_currentModDirectory
17+
ParkVehicle.modDir = g_parkVehicleSystem.modDir
1618

1719
function ParkVehicle.prerequisitesPresent(specializations)
1820
return SpecializationUtil.hasSpecialization(Enterable, specializations)
1921
end
2022

23+
function ParkVehicle.registerFunctions(vehicleType)
24+
SpecializationUtil.registerFunction(vehicleType, "getParkVehicleState", ParkVehicle.getParkVehicleState)
25+
SpecializationUtil.registerFunction(vehicleType, "setParkVehicleState", ParkVehicle.setParkVehicleState)
26+
end
27+
2128
function ParkVehicle.registerEventListeners(vehicleType)
2229
SpecializationUtil.registerEventListener(vehicleType, "onLoad", ParkVehicle)
2330
SpecializationUtil.registerEventListener(vehicleType, "onUpdate", ParkVehicle)
@@ -27,6 +34,7 @@ function ParkVehicle.registerEventListeners(vehicleType)
2734
SpecializationUtil.registerEventListener(vehicleType, "onWriteUpdateStream", ParkVehicle)
2835
SpecializationUtil.registerEventListener(vehicleType, "onReadUpdateStream", ParkVehicle)
2936
SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", ParkVehicle)
37+
SpecializationUtil.registerEventListener(vehicleType, "onDelete", ParkVehicle)
3038
end
3139

3240
function ParkVehicle:onLoad(savegame)
@@ -55,6 +63,7 @@ function ParkVehicle:onLoad(savegame)
5563
end
5664

5765
spec.inputPressed = false
66+
spec.registrationKey = nil
5867
spec.actionEvents = {}
5968
spec.icon = createImageOverlay(ParkVehicle.modDir .. "icon.png")
6069
spec.overlay = createImageOverlay(ParkVehicle.modDir .. "overlay.png")
@@ -66,7 +75,12 @@ function ParkVehicle:onLoad(savegame)
6675
if savegame ~= nil then
6776
local i = 0
6877
while true do
69-
local key = string.format("%s.ParkVehicle.player(%d)", savegame.key, i)
78+
79+
local legacykey = string.format("%s.ParkVehicle.player(%d)", savegame.key, i) -- TODO Remove with next release
80+
local key = string.format("%s.%s.ParkVehicle.player(%d)", savegame.key, g_parkVehicleSystem.modName, i)
81+
if not hasXMLProperty(savegame.xmlFile.handle, key) then
82+
key = legacykey
83+
end
7084
if not hasXMLProperty(savegame.xmlFile.handle, key) then
7185
break
7286
end
@@ -85,21 +99,34 @@ function ParkVehicle:onLoad(savegame)
8599
end
86100

87101
self.spec_enterable:setIsTabbable(not spec.state[spec.uniqueUserId])
102+
spec.registrationKey = g_parkVehicleSystem:registerInstance(self)
88103
end
89104

90105
function ParkVehicle:onUpdate(dt, isActiveForInput, isSelected)
91106
if self.isClient then
92107
local spec = self.spec_parkvehicle
93108
if spec.inputPressed then
94-
local newValue = not spec.state[spec.uniqueUserId]
95-
self.spec_enterable:setIsTabbable(not newValue)
96-
spec.state[spec.uniqueUserId] = newValue
97-
self:raiseDirtyFlags(spec.dirtyFlag)
109+
local newValue = not self:getParkVehicleState()
110+
self:setParkVehicleState(newValue)
98111
spec.inputPressed = false
99112
end
100113
end
101114
end
102115

116+
---@param newValue boolean
117+
function ParkVehicle:setParkVehicleState(newValue)
118+
local spec = self.spec_parkvehicle
119+
self.spec_enterable:setIsTabbable(not newValue)
120+
spec.state[spec.uniqueUserId] = newValue
121+
self:raiseDirtyFlags(spec.dirtyFlag)
122+
end
123+
124+
---@return boolean
125+
function ParkVehicle:getParkVehicleState()
126+
local spec = self.spec_parkvehicle
127+
return spec.state[spec.uniqueUserId]
128+
end
129+
103130
function ParkVehicle:onDraw()
104131
if self.isClient and self:getIsActive() then
105132
local spec = self.spec_parkvehicle
@@ -116,6 +143,12 @@ function ParkVehicle:onDraw()
116143
end
117144
end
118145

146+
function ParkVehicle:onDelete()
147+
local spec = self.spec_parkvehicle
148+
print("onDelete")
149+
g_parkVehicleSystem:unregisterInstance(spec.registrationKey)
150+
end
151+
119152
--Called on server side on join
120153
-- @param integer streamId streamId
121154
-- @param integer connection connection
@@ -234,5 +267,6 @@ function ParkVehicle.randomString(length)
234267
math.randomseed(getDate("%d%m%y%H%M%S"))
235268
return randomString(length - 1) .. charset[math.random(1, #charset)]
236269
end
270+
237271
return randomString(length)
238272
end

ParkVehicleSystem.lua

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
--- Global instance of park vehicle.
2+
---@class ParkVehicleSystem
3+
---@field modName string
4+
---@field modDir string
5+
---@field inputManager InputBinding
6+
---@field debug boolean
7+
---@field instances table<integer, ParkVehicle>
8+
---@field counter integer Increased if an instance is registered, used as key within the table
9+
ParkVehicleSystem = {}
10+
11+
local ParkVehicleSystem_mt = Class(ParkVehicleSystem)
12+
13+
---
14+
---@param modName string
15+
---@param modDir string
16+
---@param inputManager InputBinding
17+
---@param debug boolean
18+
---@return ParkVehicleSystem
19+
function ParkVehicleSystem:new(modName, modDir, inputManager, debug)
20+
local self = {}
21+
22+
setmetatable(self, ParkVehicleSystem_mt)
23+
24+
self.modName = modName
25+
self.modDir = modDir
26+
self.debug = debug
27+
self.inputManager = inputManager
28+
29+
self.instances = {}
30+
self.counter = 0
31+
32+
return self
33+
end
34+
35+
---@param typeManager TypeManager
36+
---@param specManager SpecializationManager
37+
function ParkVehicleSystem:installSpecialization(typeManager, specManager)
38+
-- register spec
39+
specManager:addSpecialization("parkVehicle", "ParkVehicle", Utils.getFilename("ParkVehicle.lua", self.modDir), nil)
40+
41+
-- add spec to vehicle types
42+
local totalCount = 0
43+
local modified = 0
44+
for typeName, typeEntry in pairs(typeManager:getTypes()) do
45+
totalCount = totalCount + 1
46+
if SpecializationUtil.hasSpecialization(Enterable, typeEntry.specializations) and
47+
not SpecializationUtil.hasSpecialization(Rideable, typeEntry.specializations) and
48+
not SpecializationUtil.hasSpecialization(ParkVehicle, typeEntry.specializations) then
49+
typeManager:addSpecialization(typeName, self.modName .. ".parkVehicle")
50+
modified = modified + 1
51+
if (self.debug) then
52+
print("Adding park vehicle to " .. typeName)
53+
end
54+
else
55+
if (self.debug) then
56+
print("Not adding park vehicle to " .. typeName)
57+
end
58+
end
59+
end
60+
61+
print(string.format("Inserted Park Vehicle into %i of %i vehicle types", modified, totalCount))
62+
end
63+
64+
function ParkVehicleSystem:registerActionEvents()
65+
local _, eventId = self.inputManager:registerActionEvent(InputAction.PARKVEHICLE_UNPARK_ALL, self, self.unparkAll, false, true, false, true)
66+
self.inputManager:setActionEventTextPriority(eventId, GS_PRIO_VERY_LOW)
67+
68+
end
69+
70+
function ParkVehicleSystem:unregisterActionEvents()
71+
self.inputManager:removeActionEventsByTarget(self)
72+
end
73+
74+
---@param instance ParkVehicle
75+
---@return integer the key to unregister this instance
76+
function ParkVehicleSystem:registerInstance(instance)
77+
local key = self.counter
78+
self.instances[key] = instance
79+
80+
81+
self.counter = self.counter + 1
82+
83+
return key
84+
end
85+
86+
---@param key integer
87+
function ParkVehicleSystem:unregisterInstance(key)
88+
self.instances[key] = nil
89+
end
90+
91+
function ParkVehicleSystem:unparkAll()
92+
for _, value in pairs(self.instances) do
93+
value:setParkVehicleState(false)
94+
end
95+
end

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![ModHub Download](https://img.shields.io/badge/%5BFS17%5D%20ModHub-1.0.1.0-green.svg?style=flat-square)](https://farming-simulator.com/mod.php?lang=de&country=ch&mod_id=91210&title=fs2017)
44
[![ModHub Download](https://img.shields.io/badge/%5BFS19%5D%20ModHub-1.1.0.0-green.svg?style=flat-square)](https://farming-simulator.com/mod.php?mod_id=125747&title=fs2019)
5-
[![ModHub Download](https://img.shields.io/badge/%5BFS22%5D%20ModHub-1.0.0.0-blue.svg?style=flat-square)](https://farming-simulator.com/mod.php?mod_id=224816)
5+
[![ModHub Download](https://img.shields.io/badge/%5BFS22%5D%20ModHub-1.1.0.0-blue.svg?style=flat-square)](https://farming-simulator.com/mod.php?mod_id=224816)
66

77
A small script, which allows temporary disabling of tabbing function for a vehicle
88

0 commit comments

Comments
 (0)