forked from cuberite/WorldEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.lua
More file actions
145 lines (120 loc) · 4.24 KB
/
Config.lua
File metadata and controls
145 lines (120 loc) · 4.24 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
-- config.lua
-- Contains functions for initializing and writing a configuration.
g_Config = {}
-- Create an environment for the config loader where the admin can use item names directly without quotes.
local g_LoaderEnv = {}
for Key, Value in pairs(_G) do
if (Key:match("E_.*")) then
g_LoaderEnv[ItemTypeToString(Value)] = Value
end
end
local g_ConfigDefault =
[[
WandItem = woodenaxe,
Limits =
{
ButcherRadius = -1,
MaxBrushRadius = 5,
DisallowedBlocks = {6, 7, 14, 15, 16, 26, 27, 28, 29, 39, 31, 32, 33, 34, 36, 37, 38, 39, 40, 46, 50, 51, 56, 59, 69, 73, 74, 75, 76, 77, 81, 83},
},
Defaults =
{
ButcherRadius = 20,
},
NavigationWand =
{
Item = compass,
MaxDistance = 120,
TeleportNoHit = true,
},
Scripting =
{
-- If true, an error will be logged when a craftscript fails.
Debug = false,
-- The amount of seconds that a script may be active. Any longer and the script will be aborted.
-- If negative, the time a script can run is unlimited.
MaxExecutionTime = 5,
},
Schematics =
{
OverrideExistingFiles = true,
},
Updates =
{
CheckForUpdates = true,
NumAttempts = 3,
ShowMessageWhenUpToDate = true,
DownloadNewerVersion = true,
},
Storage =
{
-- If set to true, the selection of a player will be remembered once he leaves.
RememberPlayerSelection = true,
-- If Edits needs to change a format in the database, the database will be backed up first before changing.
-- This doesn't mean when adding or removing data the database will be backed up. Only when the used database is outdated.
BackupDatabaseWhenUpdating = true,
}
]]
local function WriteDefaultConfiguration(a_Path)
LOGWARNING("Wrote the default Edits configuration to \"" .. a_Path .. "\"")
local File = io.open(a_Path, "w")
File:write(g_ConfigDefault)
File:close()
end
-- Returns the default configuration table. This can be directly used in g_Config.
local function GetDefaultConfigurationTable()
-- Loads the default config...
local Loader = loadstring("return {" .. g_ConfigDefault .. "}")
-- Apply the environment to the configloader.
setfenv(Loader, g_LoaderEnv)
return Loader()
end
-- Sets g_Config to the default configuration.
local function LoadDefaultConfiguration()
LOGWARNING("The default Edits configuration will be used.")
g_Config = GetDefaultConfigurationTable()
end
-- Finds from an error message where the error occurred.
local function FindErrorPosition(a_ErrorMessage)
local ErrorPosition = a_ErrorMessage:match(":(.-):") or 0
return ErrorPosition
end
-- Loads the configuration from the given path. If it doesn't exist, or if there is an error in the config file it will load the defaults.
function InitializeConfiguration(a_Path)
local ConfigContent = cFile:ReadWholeFile(a_Path)
-- The configuration file doesn't exist or is empty. Write and load the default value...
if (ConfigContent == "") then
WriteDefaultConfiguration(a_Path)
LoadDefaultConfiguration()
return
end
-- Load the content of the config file. Place brackets around it to make the whole thing a table.
-- Also, return the table when executed.
local ConfigLoader, Error = loadstring("return {" .. ConfigContent .. "}")
if (not ConfigLoader) then
local ErrorPosition = FindErrorPosition(Error)
LOGWARNING("Found an error in the Edits configuration near (line) " .. ErrorPosition)
LoadDefaultConfiguration()
return
end
-- Apply the environment to the ConfigLoader.
setfenv(ConfigLoader, g_LoaderEnv)
-- Execute the loader. It returns true + the configuration if it executed properly. Else it returns false with the error message.
local Succes, Result = pcall(ConfigLoader)
if (not Succes) then
local ErrorPosition = FindErrorPosition(Result)
LOGWARNING("Found an error in the Edits configuration near (line) " .. ErrorPosition)
LoadDefaultConfiguration()
return
end
-- Merge the configuration with the default configuration.
-- When the admin missed something in the configuration it will be set to the default value.
local DefaultConfig = GetDefaultConfigurationTable()
table.merge(Result, DefaultConfig)
-- Make a dictionary out of the array of disallowed blocks.
if (Result.Limits and (type(Result.Limits.DisallowedBlocks) == "table")) then
Result.Limits.DisallowedBlocks = table.todictionary(Result.Limits.DisallowedBlocks)
end
-- Set the g_Config table.
g_Config = Result
end