-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfunctions.lua
164 lines (136 loc) · 4.75 KB
/
functions.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
InTown = {}
function CheckPlayerInTown(Player, chunkX, chunkZ)
local sql = "SELECT towns.town_name, nations.nation_name FROM plots LEFT JOIN towns ON towns.town_id = plots.town_id LEFT JOIN nations ON nations.nation_id = towns.town_id WHERE plots.chunkX = ? AND plots.chunkZ = ? AND plots.world = ?";
local parameters = {chunkX, chunkZ, Player:GetWorld():GetName()};
local result = ExecuteStatement(sql, parameters)[1];
local town = InTown[Player:GetUUID()];
local PlayerClientHandle = Player:GetClientHandle();
if (result) and (result[1]) and (not (town) or not (town == result[1])) then
PlayerClientHandle:SendSetTitle(cCompositeChat():AddTextPart("Welcome to "):AddTextPart(result[1], "@a"));
if (result[2]) then
PlayerClientHandle:SendSetSubTitle(cCompositeChat():AddTextPart("Part of "):AddTextPart(result[2], "@6"));
end
PlayerClientHandle:SendTitleTimes(5, 25, 5);
InTown[Player:GetUUID()] = result[1];
elseif not (result) and (town) then
PlayerClientHandle:SendSetTitle(cCompositeChat():AddTextPart(town, "@a"):AddTextPart(" says farewell"));
PlayerClientHandle:SendTitleTimes(5, 25, 5);
InTown[Player:GetUUID()] = nil;
end
end
function GetPlayerNation(UUID)
local sql = "SELECT towns.nation_id FROM town_residents INNER JOIN towns ON town_residents.town_id = towns.town_id WHERE town_residents.player_uuid = ?";
local parameter = {UUID};
local nation = ExecuteStatement(sql, parameter)[1];
if (nation) then
return nation[1];
else
return nil;
end
end
function GetPlayerTown(UUID)
local sql = "SELECT town_id FROM town_residents WHERE player_uuid = ?";
local parameter = {UUID};
local town = ExecuteStatement(sql, parameter)[1];
if (town) then
return town[1];
else
return nil;
end
end
function GetPlayerTownRank(UUID)
local sql = "SELECT town_rank FROM town_residents WHERE player_uuid = ?";
local parameter = {UUID};
local townRankList = ExecuteStatement(sql, parameter);
if not (townRankList) then
return nil;
else
local rank = nil;
for key, value in pairs(townRankList) do
if (rank == nil) or (TownRanks[value[1]] > TownRanks[rank]) then
rank = value[1];
end
end
return rank;
end
end
function GetTownName(townId)
local sql = "SELECT town_name FROM towns WHERE town_id = ?";
local parameter = {townId};
local townName = ExecuteStatement(sql, parameter)[1];
if (townName) then
return townName[1];
else
return nil;
end
end
function GetNationName(nationId)
local sql = "SELECT nation_name FROM nations WHERE nation_id = ?";
local parameter = {nationId};
local nationName = ExecuteStatement(sql, parameter)[1];
if (nationName) then
return nationName[1];
else
return nil;
end
end
function GetTownId(townName)
local sql = "SELECT town_id FROM towns WHERE town_name = ?";
local parameter = {townName};
local townId = ExecuteStatement(sql, parameter)[1];
if (townId) then
return townId[1];
else
return nil;
end
end
function GetNationId(nationName)
local sql = "SELECT nation_id FROM nations WHERE nation_name = ?";
local parameter = {nationName};
local nationId = ExecuteStatement(sql, parameter)[1];
if (nationId) then
return nationId[1];
else
return nil;
end
end
function DeleteTown(townId)
--Since we make use of foreign keys, plots will be deleted accordingly and town_id will be set to null in residents automatically
local sql = "DELETE FROM towns WHERE town_id = ?";
local parameter = {townId};
ExecuteStatement(sql, parameter);
return true;
end
function GetTimestampFromString(timestring) --Returns the Lua timestamp from a string which is formatted as "YYYY-mm-dd HH:MM:SS"
local pattern = "(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)";
local year, month, day, hour, minute, second = timestring:match(pattern);
local convertedTimestamp = os.time({year = year, month = month, day = day, hour = hour, min = minute, sec = second});
return convertedTimestamp;
end
function DisplayVersion(Split, Player)
if not (Player == nil) then
Player:SendMessageInfo("Townvalds version: " .. PLUGIN:GetVersion());
else
LOG("Townvalds version: " .. PLUGIN:GetVersion());
end
return true;
end
function CheckPermission(permissions, permissionToCheck)
if (bit32.band(permissions, permissionToCheck) == 0) then
return false; --Not allowed
else
return true; --Allowed
end
end
function Set(list)
local set = {};
for _, l in ipairs(list) do
set[l] = true;
end
return set;
end
function InsideArea(pos, high, low)
if pos <= high and pos >= low then
return true
end
end