diff --git a/modDesc.xml b/modDesc.xml index 66c54ba..e1954c2 100644 --- a/modDesc.xml +++ b/modDesc.xml @@ -1,7 +1,7 @@ Farmsim Tim (timmeey86) - 1.0.0.0 + 1.0.0.1 <en>Unload Bales Early</en> <de>Ballen Frühzeitig Abladen</de> @@ -9,6 +9,7 @@ <pl>Wcześniejszy rozładunek bel</pl> <es>Descarga Anticipada de Pacas</es> <ea>Descarga Anticipada de Pacas</ea> + <it>Scarico Anticipato Delle Balle</it> @@ -23,7 +24,13 @@ The mod works for: - Two-Chamber Cotton Harvesters and modded two-chamber balers Hotkeys: O: Unloads unfinished bales or overloads the first chamber to the second one. Dependent on the type of baler, you need to turn the baler on or off for this to work. + GitHub link: https://github.com/Timmeey86/FS25_UnloadBalesEarly + +Changelog: +1.0.0.1: +- Fixed unloading and overloading for multiplayer clients +- Added italian translation provided by FirenzeIT ]]> @@ -50,6 +62,7 @@ GitHub-Link: https://github.com/Timmeey86/FS25_UnloadBalesEarly + @@ -62,6 +75,7 @@ GitHub-Link: https://github.com/Timmeey86/FS25_UnloadBalesEarly Przeładunek do drugiej komory Descarga en Segunda Camara Descarga en Segunda Camara + Scarico nella Seconda Camera Unload Bales Early @@ -70,6 +84,7 @@ GitHub-Link: https://github.com/Timmeey86/FS25_UnloadBalesEarly Wcześniejszy rozładunek bel Descarga anticipada de pacas Descarga anticipada de pacas + Scarico Anticipato delle Balle diff --git a/scripts/EarlyUnloadHandler.lua b/scripts/EarlyUnloadHandler.lua index 4e7bb1c..90f6c9c 100644 --- a/scripts/EarlyUnloadHandler.lua +++ b/scripts/EarlyUnloadHandler.lua @@ -70,6 +70,16 @@ function EarlyUnloadHandler:onHandleUnloadingBaleEvent(baler, superFunc) superFunc(baler) end +---Causes the baler to automatically start overloading its first chamber into its second one +---@param baler Baler @The baler +function EarlyUnloadHandler.startOverloading(baler) + traceMethod("startOverloading") + --Two-chamber vehicles: Reduce the overloading percentage so the baler starts unloading + local spec = baler.spec_baler + spec.buffer.overloadingStartFillLevelPct = g_currentMission.unloadBalesEarlySettings:getUnloadThresholdInPercent() / 100 + spec.overloadingThresholdIsOverridden = true +end + ---Intercepts the action call in order to start overloading if necessary. ---@param baler table @The baler instace ---@param superFunc function @The base game implementation @@ -79,16 +89,20 @@ end ---@param param4 any @Unknown param (not needed, but forwarded to superFunc) function EarlyUnloadHandler.onActionEventUnloading(baler, superFunc, param1, param2, param3, param4) traceMethod("onActionEventUnloading") - local spec = baler.spec_baler if EarlyUnloadHandler.getCanOverloadBuffer(baler) then traceMethod("onActionEventUnloading/can overload") - --Two-chamber vehicles: Reduce the overloading percentage so the baler starts unloading - spec.buffer.overloadingStartFillLevelPct = g_currentMission.unloadBalesEarlySettings:getUnloadThresholdInPercent() / 100 - spec.overloadingThresholdIsOverridden = true - -- Ignore the event in this case, don't forward it + if g_server == nil then + -- Ask the server to trigger an overload + g_client:getServerConnection():sendEvent(OverloadChamberEarlyEvent.new(baler)) + else + -- Single player and multiplayer host: Overload directly + EarlyUnloadHandler.startOverloading(baler) + end + -- Do not call super func since we wanted the overload rather than the unload elseif g_server == nil and baler:getCanUnloadUnfinishedBale() then -- Ask the server to trigger an early unload. g_client:getServerConnection():sendEvent(UnloadBaleEarlyEvent.new(baler)) + -- Do not call super func. The server will make sure the necessary functions get called on the clients else traceMethod("onActionEventUnloading/can not overload") -- Forward the event through base game mechanism in all other cases diff --git a/scripts/OverloadChamberEarlyEvent.lua b/scripts/OverloadChamberEarlyEvent.lua new file mode 100644 index 0000000..170b5ed --- /dev/null +++ b/scripts/OverloadChamberEarlyEvent.lua @@ -0,0 +1,60 @@ +---This event is sent from the client to the server when an early unload is requested by a client +---@class OverloadChamberEarlyEvent +---@field baler Baler @The baler to be overloaded +OverloadChamberEarlyEvent = {} +local OverloadChamberEarlyEvent_mt = Class(OverloadChamberEarlyEvent, Event) + +InitEventClass(OverloadChamberEarlyEvent, "OverloadChamberEarlyEvent") + +---Creates a new empty event +---@return table @The new instance +function OverloadChamberEarlyEvent.emptyNew() + return Event.new(OverloadChamberEarlyEvent_mt) +end + +---Creates a new event +---@param baler Baler @The baler to be overloaded +---@return table @The new instance +function OverloadChamberEarlyEvent.new(baler) + local self = OverloadChamberEarlyEvent.emptyNew() + self.baler = baler + return self +end + +local debugMp = false +local function dbgPrintMp(text) + if debugMp then + print(MOD_NAME .. ": " .. text) + end +end + +---This will be executed on the server if a client sends an OverloadChamberEarlyEvent Event +---@param streamId any @The ID of the stream to read from. +---@param connection any @The connection which sent the event. +function OverloadChamberEarlyEvent:readStream(streamId, connection) + dbgPrintMp("Receiving OverloadChamberEarlyEvent") + if not connection:getIsServer() then + dbgPrintMp("Running OverloadChamberEarlyEvent") + -- We are the server: Act as if the event was triggered on the server, this should trigger all necessary client actions on the way + self.baler = NetworkUtil.readNodeObject(streamId) + EarlyUnloadHandler.startOverloading(self.baler) + dbgPrintMp("Done running OverloadChamberEarlyEvent") + else + Logging.error("%s: OverloadChamberEarlyEvent is a client-to-server-only event.", MOD_NAME) + end + dbgPrintMp("Done receiving OverloadChamberEarlyEvent") +end + +---Asks the server to trigger an early overload for the baler stored in the event. +---@param streamId any @The stream ID. +---@param connection any @The connection to use. +function OverloadChamberEarlyEvent:writeStream(streamId, connection) + dbgPrintMp("Sending OverloadChamberEarlyEvent") + if connection:getIsServer() then + -- Connected to a server: Tell the server which baler to unload + NetworkUtil.writeNodeObject(streamId, self.baler) + else + Logging.error("%s: OverloadChamberEarlyEvent is a client-to-server-only event.", MOD_NAME) + end + dbgPrintMp("Done sending OverloadChamberEarlyEvent") +end \ No newline at end of file diff --git a/scripts/UnloadBaleEarlyEvent.lua b/scripts/UnloadBaleEarlyEvent.lua index 6bddada..4e7415b 100644 --- a/scripts/UnloadBaleEarlyEvent.lua +++ b/scripts/UnloadBaleEarlyEvent.lua @@ -27,7 +27,8 @@ local function dbgPrintMp(text) print(MOD_NAME .. ": " .. text) end end ----Reads settings which were sent by another network participant and then applies them locally + +---This will be executed on the server if a client sends an UnloadBaleEarly Event ---@param streamId any @The ID of the stream to read from. ---@param connection any @The connection which sent the event. function UnloadBaleEarlyEvent:readStream(streamId, connection) @@ -44,7 +45,7 @@ function UnloadBaleEarlyEvent:readStream(streamId, connection) dbgPrintMp("Done receiving UnloadBaleEarlyEvent") end ----Sends event data, in this case exclusively from the client to the server +---Asks the server to trigger an early unload for the baler stored in the event. ---@param streamId any @The stream ID. ---@param connection any @The connection to use. function UnloadBaleEarlyEvent:writeStream(streamId, connection)