LimeworkLanguages/code/!lang.sk

231 lines
8.9 KiB
Plaintext
Raw Normal View History

#structure of table languages
#EN = VARCHAR(333)
#RU = VARCHAR(333)
#... and so on (for each language)
2020-11-14 13:02:39 +00:00
function YAMLtoSQL():
loop {languageslist::*}:
load yaml "plugins/Skript/scripts/languages/Language%loop-value in lowercase%.yml" as "lang%loop-value in lowercase%"
execute "TRUNCATE TABLE languages" in {-sql}
2020-11-14 13:02:39 +00:00
send last sql error to console if last sql error is set
wait a tick
loop yaml nodes from "langen":
loop {languageslist::*}:
set {_list::%loop-value-2%} to yaml value loop-value-1 from "lang%loop-value-2 in lowercase%"
execute "INSERT INTO languages VALUES (%{_list::en}%, %{_list::lt}%, %{_list::ru}%, %{_list::es}%, %{_list::da}%, %{_list::pt}%, %{_list::CN_Simplified}%)" in {-sql}
2021-02-06 12:17:37 +00:00
if last sql error is set:
send last sql error to console and ops if last sql error is set
send "Error happened on %{_list::en}%" to console and ops
2020-11-14 13:02:39 +00:00
#load languages from mysql
function loadSQLLanguages():
execute "SELECT * FROM languages" in {-sql} and store the result in {_l::*}
2020-11-14 13:02:39 +00:00
loop indexes of {_l::*}:
set {_list::%loop-value%} to loop-value
2020-11-14 13:02:39 +00:00
#done to make it the first index
set {_list::1} to "en"
delete {_list::en}
loop {_list::*}:
loop {_l::%loop-value%::*}:
if loop-value-1 is "en":
set {_index::%loop-index-2%} to loop-value-2
set {-langmsg::%loop-value-1%::%{_index::%loop-index-2%}%} to loop-value-2
on skript load:
if {langmsg::*} is set:
delete {langmsg::*}
on script load:
wait a tick
loadSQLLanguages()
function SQLtoYAML():
execute "SELECT * FROM languages" in {-sql} and store the result in {_l::*}
2020-11-14 13:02:39 +00:00
send last sql error to console if last sql error is set
loop indexes of {_l::*}:
unload yaml "lang%loop-value%"
load yaml "plugins/Skript/scripts/languages/Language%loop-value%.yml" as "lang%loop-value%"
set {_list::%loop-value%} to loop-value
2020-11-14 13:02:39 +00:00
#done to make it the first index
2021-02-06 12:17:37 +00:00
#TODO make it sorted on unedited vs edited, so unedited shows first
2020-11-14 13:02:39 +00:00
set {_list::1} to "en"
delete {_list::en}
loop {_list::*}:
loop {_l::%loop-value%::*}:
if loop-value-1 is "en":
set {_index::%loop-index-2%} to loop-value-2
set yaml value "%{_index::%loop-index-2%}%" from "lang%loop-value-1%" to "%loop-value-2%"
save yaml "lang%loop-value%"
on script load:
send "LOADING GLOBAL LANGUAGE FILES" to console
set {_languages::*} to "EN", "LT", "RU", "ES", "DA", "PT", "CN_Simplified"
loop {_languages::*}:
set {languageslist::%loop-value%} to loop-value
expression:
patterns:
translate %string% [for %player/string%] [with variables %-strings%]
translate %string% [with variables %-strings%] [for %player/string%]
get:
if expressions 3 is not set:
return translate(expression 1, expression 2)
else:
return translate(expression 1, expression 2, expressions 3)
function insertlater(m: text):
execute "INSERT INTO languages VALUES (%{_m}%, %{_m}%, %{_m}%, %{_m}%, %{_m}%, %{_m}%, %{_m}%)" in {-sql}
2020-11-14 13:02:39 +00:00
function translate(m: text, p: object, variables: strings = "null") :: text:
#support both UUIDS and player names for getting language of the player
2020-11-14 13:02:39 +00:00
set {_uuid} to {uuid::%{_p}%} if {name::%{_p}%} is not set else {_p}
2021-02-06 12:17:37 +00:00
set {_lang} to (epic variable "%{_uuid}%::language") ? "EN"
2020-11-14 13:02:39 +00:00
if {-langmsg::%{_lang}%::%{_m}%} is not set:
2020-11-14 13:02:39 +00:00
set {-langmsg::%{_lang}%::%{_m}%} to {_m}
insertlater({_m})
2020-11-14 13:02:39 +00:00
#useful for knowing when was last time this message was triggered, so we can later remove messages that are never used (possibly were removed from code)
set {-langmsgusage::%{_m}%} to now
#replace $1$, $2$, etc. stuff into proper values (function provides arguments for it)
if {_variables::1} is not "null":
set {_msg} to {-langmsg::%{_lang}%::%{_m}%}
loop {_variables::*}:
replace "$%loop-index%$" with loop-value in {_msg}
return {_msg}
2020-11-14 13:02:39 +00:00
#Translation Mode 1, return the message (MOST USEFUL!)
return {-langmsg::%{_lang}%::%{_m}%}
#structure of table languages_usage
#message = VARCHAR(333)
#lastused = BIGINT(18)
function saveLastMessageUsageDate():
loop {-langmsgusage::*}:
set {_msg} to {-langmsg::en::%loop-index%}
unix timestamp of loop-value is higher than unix timestamp of {-lastlanguagesave}
execute "INSERT INTO languages_usage (message, lastused) VALUES (%{_msg}%, %rounded unix timestamp of loop-value%) ON DUPLICATE KEY UPDATE lastused=VALUES(lastused)" in {-sql}
if last sql error is set:
send last sql error to console and ops
stop
set {-lastlanguagesave} to now
every 3 hours:
send "Starting a every 3 hours task for saving last used dates of language messages to SQL." to console
saveLastMessageUsageDate()
2020-11-14 13:02:39 +00:00
function getSlotFromLanguage(lang: text) :: number:
if {_lang} is "EN":
return 0
else if {_lang} is "LT":
return 1
else if {_lang} is "RU":
return 2
else if {_lang} is "DA":
return 3
else if {_lang} is "PT":
return 4
else if {_lang} is "CN_Simplified":
return 5
else:
return 0
2020-11-14 13:02:39 +00:00
command /languages [<text>]:
description: Set your language
usage: /languages
trigger:
wait a tick
set {_inventory} to chest inventory with 2 rows named "&6- &a&lSelect language &6-"
2020-11-14 13:02:39 +00:00
#diamond block is displayed if the banner variable isn't set
set slot 0 of {_inventory} to ({banners::english} ? diamond block) named "&aEnglish" with lore "&e&lCLICK TO SELECT"
set slot 1 of {_inventory} to ({banners::lithuania} ? diamond block) named "&aLietuvių" with lore "&e&lCLICK TO SELECT"
set slot 2 of {_inventory} to ({banners::russia} ? diamond block) named "&aРусский" with lore "&e&lCLICK TO SELECT"
set slot 3 of {_inventory} to ({banners::denmark} ? diamond block) named "&aDansk" with lore "&e&lCLICK TO SELECT"
set slot 4 of {_inventory} to ({banners::portugal} ? diamond block) named "&aPortuguês" with lore "&e&lCLICK TO SELECT"
set slot 5 of {_inventory} to ({banners::china} ? diamond block) named "&aChinese (Simplified)" with lore "&e&lCLICK TO SELECT"
2020-11-14 13:02:39 +00:00
set {_slot} to getSlotFromLanguage(epic variable "%uuid of player%::language")
set {_item} to slot {_slot} of {_inventory}
2020-11-14 13:02:39 +00:00
set lore of {_item} to "&a&lSELECTED"
set slot {_slot} of {_inventory} to {_item}
2020-11-14 13:02:39 +00:00
set {_sp} to "Automatically detects your language based on your location."
2020-11-14 13:02:39 +00:00
if {forcedenglish::%uuid of player%} is not set:
set slot 8 of {_inventory} to glowing redstone dust named "&eAutomatic Language Detection &2[&a&lON&2]" with lore "&7%{_sp}%", "&7This is enabled by default.", "&a", "&a&lCLICK TO TOGGLE"
else:
set slot 8 of {_inventory} to redstone dust named "&eAutomatic Language Detection &2[&c&lOFF&2]" with lore "&7%{_sp}%", "&7This is enabled by default.", "&a", "&a&lCLICK TO TOGGLE"
set slot 9 of {_inventory} to arrow named "&a%translate("Go back", player)%"
open {_inventory} to player
2020-11-14 13:02:39 +00:00
on inventory click:
name of event-inventory = "&6- &a&lSelect language &6-"
cancel event
2020-11-14 13:02:39 +00:00
clicked inventory is not player's inventory
#Go back to Player Menu
if event-slot is arrow:
make player execute command "/playermenu"
stop
2020-11-14 13:02:39 +00:00
set {_l} to epic variable "%uuid of player%::language"
if event-slot's index is 0:
set {_lang} to "EN"
else if event-slot's index is 1:
set {_lang} to "LT"
else if event-slot's index is 2:
set {_lang} to "RU"
else if event-slot's index is 3:
set {_lang} to "DA"
else if event-slot's index is 4:
set {_lang} to "PT"
else if event-slot's index is 5:
set {_lang} to "CN_Simplified"
else if event-slot's index is 8:
{forcedenglish::%uuid of player%} is not set:
set {forcedenglish::%uuid of player%} to true
send "&cAutomatic language detection has been disabled."
else:
delete {forcedenglish::%uuid of player%}
send "&aAutomatic language detection has been enabled back."
stop
2020-11-14 13:02:39 +00:00
{_l} is not {_lang}:
set epic variable "%uuid of player%::language" to {_lang}
send "&eYou have selected &a%{_lang}% &elanguage!"
close inventory of player
# epic variable "%uuid of player%::country" is set by LimeworkProxy on join
2020-11-14 13:02:39 +00:00
on join:
{server} is not "gl"
epic variable "%uuid of player%::language" is not set:
set {_lang} to epic variable "%{_uuid}%::language"
if {languageslist::%{_lang}%} is not set:
set {_default} to "EN"
set {_p} to player
set {_uuid} to uuid of player
if {forcedenglish::%uuid of player%} is true:
epic variable "%uuid of player%::language" is not "EN"
set epic variable "%uuid of player%::language" to "EN"
else:
set {_country} to epic variable "%uuid of player%::country"
if {_country} contains "Lithuania":
set {_lang} to "LT"
else if {_country} contains "Denmark":
set {_lang} to "DA"
else if {_country} contains "Russia":
set {_lang} to "RU"
else if {_country} contains "Portugal" or "Brazil":
set {_lang} to "PT"
2020-11-14 13:02:39 +00:00
{_lang} is not epic variable "%uuid of player%::language":
set epic variable "%uuid of player%::language" to {_lang}
else if {_default} is set:
set epic variable "%uuid of player%::language" to {_default}