-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOrderFilter.lua
69 lines (55 loc) · 1.87 KB
/
OrderFilter.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
--[[
Overwrite the existing order filter to allows multiple order filters using the default API.
It is important to require this file from or after Activate function in addon_game_mode.lua
is called, otherwise GameRules:GetGameModeEntity will return nil.
Example usage:
--Filter 1
GameRules:GetGameModeEntity():SetExecuteOrderFilter( function( self, order )
print('Filter 1')
return true
end, ORDER_FILTER_PRIORITY_LOW )
--Filter 2
GameRules:GetGameModeEntity():SetExecuteOrderFilter( function( self, order )
print('Filter 2')
return true
end )
--Filter 3
GameRules:GetGameModeEntity():SetExecuteOrderFilter( DynamicWrap( MyGameMode, 'OrderFilter' ), self )
--Output For each order:
Filter 2
Filter 3
Filter 1
]]
if orderFilterOverwritten == nil then
orderFilterOverwritten = true
ORDER_FILTER_PRIORITY_LOWEST = 0
ORDER_FILTER_PRIORITY_LOW = 1
ORDER_FILTER_PRIORITY_NORMAL = 2
ORDER_FILTER_PRIORITY_HIGH = 3
ORDER_FILTER_PRIORITY_HIGHEST = 4
--Save a list of different functions
orderFilters = {}
--Set the actual order filter to the function that just iterates over all filters
GameRules:GetGameModeEntity():SetExecuteOrderFilter( function( self, order )
for _,filter in ipairs( orderFilters ) do
if filter.func( filter.context, order ) ~= true then
return false
end
end
return true
end, {} )
--Overwrite the original function
GameRules:GetGameModeEntity().SetExecuteOrderFilter = function( self, filterFunc, context, priority )
if type( context ) == 'number' then
priority = context
context = self
end
--Set default priority if it's not set
if priority == nil then
priority = ORDER_FILTER_PRIORITY_NORMAL
end
table.insert( orderFilters, {func = filterFunc, priority = priority, context = context } )
--Sort table based on priorities
table.sort( orderFilters, function( a, b ) return a.priority - b.priority end )
end
end