Skip to content

Commit

Permalink
fix multiplayer overloading and add IT translation
Browse files Browse the repository at this point in the history
  • Loading branch information
Timmeey86 committed Nov 20, 2024
1 parent 16b3dca commit 9005909
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 8 deletions.
17 changes: 16 additions & 1 deletion modDesc.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<modDesc descVersion="92">
<author>Farmsim Tim (timmeey86)</author>
<version>1.0.0.0</version>
<version>1.0.0.1</version>
<title>
<en>Unload Bales Early</en>
<de>Ballen Frühzeitig Abladen</de>
<ru>Выгрузка Неполных Тюков</ru>
<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>
</title>

<description>
Expand All @@ -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
]]></en>
<de><![CDATA[
Beschreibung: Diese Mod erlaubt das frühzeitige Abladen von Ballen.
Expand All @@ -37,6 +44,11 @@ Die Mod funktioniert für:
Tastenkombinationen: O: Lädt Ballen vorzeitig ab oder lädt den Inhalt von der ersten Kammer in die zweite Kammer über. Abhängig von der Art der Ballenpresse muss diese ein- oder ausgeschaltet sein, damit dies funktioniert.
GitHub-Link: https://github.com/Timmeey86/FS25_UnloadBalesEarly
Changelog:
1.0.0.1:
- Fehler beim Abladen und Überladen im Mehrspielermodus behoben
- Added italian translation provided by FirenzeIT
]]></de>
</description>

Expand All @@ -50,6 +62,7 @@ GitHub-Link: https://github.com/Timmeey86/FS25_UnloadBalesEarly
<sourceFile filename="scripts/UnloadBalesSettingsRepository.lua" />
<sourceFile filename="scripts/UnloadBalesUI.lua" />
<sourceFile filename="scripts/UnloadBaleEarlyEvent.lua" />
<sourceFile filename="scripts/OverloadChamberEarlyEvent.lua" />
<sourceFile filename="scripts/EarlyUnloadHandler.lua" />
<sourceFile filename="scripts/UnloadBalesEarly.lua" />
</extraSourceFiles>
Expand All @@ -62,6 +75,7 @@ GitHub-Link: https://github.com/Timmeey86/FS25_UnloadBalesEarly
<pl>Przeładunek do drugiej komory</pl>
<es>Descarga en Segunda Camara</es>
<ea>Descarga en Segunda Camara</ea>
<it>Scarico nella Seconda Camera</it>
</text>
<text name="ub_group_title">
<en>Unload Bales Early</en>
Expand All @@ -70,6 +84,7 @@ GitHub-Link: https://github.com/Timmeey86/FS25_UnloadBalesEarly
<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>
</text>
</l10n>
</modDesc>
24 changes: 19 additions & 5 deletions scripts/EarlyUnloadHandler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
60 changes: 60 additions & 0 deletions scripts/OverloadChamberEarlyEvent.lua
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions scripts/UnloadBaleEarlyEvent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 9005909

Please sign in to comment.