summaryrefslogtreecommitdiffstats
path: root/MCServer/Plugins/TransAPI/main.lua
blob: 2b6f77da6d47e37a5c1bb2ab1200e5dfb89fb4bc (plain) (blame)
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
-- This plugin copyright Alexander Harkness 2013, licensed under the MIT license.

-- Configuration
g_ServerLang    = "en"
g_ConsoleLang   = "en"

-- Global Variables
g_Plugin        = nil
g_PluginManager = nil
g_PluginDir     = nil
g_UserData      = nil

-- START WITH DA AWESOME!
function Initialize( Plugin )

	-- Set up the globals.
	g_Plugin        = Plugin
	g_PluginManager = cRoot:Get():GetPluginManager()
	g_PluginDir     = Plugin:GetDirectory()

	-- Set up the plugin details.
	Plugin:SetName( "TransAPI" )
	Plugin:SetVersion( 1 )

	-- This is the place for commands!
	g_PluginManager:BindCommand("/language", "transapi.setlang", HandleLanguageCommand, " - Set your preferred language (use ISO 639-1)")

	-- Load the userdata file.
	g_UserData = cIniFile( g_PluginDir .. "/userdata.ini" )
	if g_UserData ~= true then
		LOGERROR( "TransAPI INI file could not be opened!" )
	end

	LOG( "Initialized " .. Plugin:GetName() .. " v." .. Plugin:GetVersion() )

	return true

end

function GetLanguage( Player )

	-- Returns a language to use.
	if g_UserData:ReadFile() == true then
		local userLang = g_UserData:GetValueSet( Player:GetName(), "language", "false" )
		g_UserData:WriteFile()
	end

	if userLang == "false" then
		return g_ServerLang
	else
		return userLang
	end

end

function GetConsoleLanguage()
	-- Return the language to use for console messages.
	return g_ConsoleLang
end

function HandleLanguageCommand ( Split, Player )

	-- If the user is not setting the language, tell them the currently selected one.
	if #Split ~= 2 then

		local userLang = g_UserData:GetValueSet( Player:GetName(), "language", "false" )
		if userLang == "false" then
			return g_ServerLang
		else
			return userLang
		end

	end

	-- Set the language.
	local success = g_UserData:SetValue( Player:GetName(), "language" Split[2] )
	g_UserData:WriteFile()

	if not success then
		Player:SendMessage( "Language could not be set!" )
	else
		Player:SendMessage( "Language set!" )
	end

	return true

end

function OnDisable()
	LOG( "Disabled TransAPI!" )
end