Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
lang
Browse files Browse the repository at this point in the history
  • Loading branch information
2A5F committed Oct 12, 2019
1 parent afcf332 commit c77b5f0
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 219 deletions.
370 changes: 196 additions & 174 deletions .idea/workspace.xml

Large diffs are not rendered by default.

20 changes: 8 additions & 12 deletions src/io/meowtype/homeorback/HomeOrBack.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class HomeOrBack extends JavaPlugin {

// region Configs

def lang: String = getConfig.getString("lang", "en")
def lang: String = getConfig.getString("lang", "en").toLowerCase

def auto_respawn: Boolean = getConfig.getBoolean("auto_respawn", true)
def auto_back: Boolean = getConfig.getBoolean("auto_back", false)
Expand All @@ -60,15 +60,20 @@ class HomeOrBack extends JavaPlugin {

// endregion

private val key_showP = new Object
private val key_show_death_loc = new Object

override def onCommand(sender: CommandSender, command: Command, label: String, args: Array[String]): Boolean = {
if(!sender.isInstanceOf[Player]) return true
val player = sender.asInstanceOf[Player]
if(label == "hob") {
player sendMessage util.Arrays.deepToString(args.asInstanceOf[Array[AnyRef]])
Tpr.debug_show_randomPointWithinTheRing(player, player.getLocation, 100)
if(args(0) == "back") {
if(!player.hasPermission("hob.back")) {
player sendMessage (Lang getFor player).no_permission_command
} else {
player backTo (self.deathLocationMap get player)
}
}
//todo
true
} else if(label == "back") {
Expand All @@ -86,7 +91,6 @@ class HomeOrBack extends JavaPlugin {
val player = event.getEntity
val loc = player.getLocation
deathLocationMap.put(player, loc)
player sendMessage "你死了 " + player.hashCode

if(show_death_loc) {
runTask(player, 0, 1, key_show_death_loc) { ()=>
Expand All @@ -103,10 +107,7 @@ class HomeOrBack extends JavaPlugin {

@EventHandler def onPlayerRespawn(event: PlayerRespawnEvent) {
val player = event.getPlayer
player sendMessage "重生了 " + player.hashCode
if(deathLocationMap.containsKey(player)) {
player sendMessage "死亡地点: " + deathLocationMap.get(player)

if(auto_back) {
player backTo (self.deathLocationMap get player)
} else {
Expand All @@ -117,11 +118,6 @@ class HomeOrBack extends JavaPlugin {
}
}

@EventHandler def onInventoryClose(event: InventoryCloseEvent) {
val player = event.getPlayer
player sendMessage "你关闭了gui " + event.getInventory.getTitle
}

@EventHandler def onInventoryClick(e: InventoryClickEvent) {
if(e.getInventory.getHolder.isInstanceOf[ReSpawnGui]) {
e.getInventory.getHolder.asInstanceOf[ReSpawnGui] onInventoryClick e
Expand Down
76 changes: 52 additions & 24 deletions src/io/meowtype/homeorback/Lang.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,87 @@ package io.meowtype.homeorback

import java.io._
import java.util
import java.util.stream._
import java.util.zip._
import java.util.function._

import org.bukkit.configuration.file._
import org.bukkit.entity.Player

class Lang(val name: String, val choose_respawn: String,
class Lang(val name: String,
val mapping: util.List[String],
val when_default_override: util.List[String],
val choose_respawn: String,
val respawn_on_spawn: String,
val auto_choose_when_close: String,
val respawn_near_death: String,
val respawn_at_death: String,
val back_near_any_time: String,
val back_at_any_time: String,
val tpr_failed: String,
val no_death_loc: String)
val no_death_loc: String,
val no_permission_command: String) {
var i = 0
loop(()=> i < mapping.size, ()=> i += 1) { _ =>
mapping.set(i, mapping.get(i).toLowerCase)
}
i = 0
loop(()=> i < when_default_override.size, ()=> i += 1) { _ =>
when_default_override.set(i, when_default_override.get(i).toLowerCase)
}
}
object Lang {
val lang_map = new util.HashMap[String, Lang]()
val lang_map = new util.HashMap[String, Lang]

def getFor(player: Player): Lang = {
val local = player.getLocale.toLowerCase
if((lang_map containsKey local) && !(local == "zh_cn" && self.lang == "lzh_Hans")) lang_map get local
else if(lang_map containsKey self.lang) lang_map get self.lang
else lang_map get "en"
val default = self.lang
val default_lang = if(lang_map containsKey default) lang_map get default else lang_map get "en"
val player_lang = lang_map get local
if(player_lang == null || default_lang.when_default_override.contains(local) || default_lang.mapping.contains(local)) default_lang
else player_lang
}

def loadLang(): Unit = {
val src = classOf[HomeOrBack].getProtectionDomain.getCodeSource
val jar = src.getLocation
val zip = new ZipInputStream(jar.openStream)

val langs = zip filter { e =>
val langs: Stream[Lang] = (zip filter { e =>
val name = e.getName
name.startsWith("lang/") && name.endsWith(".yml")
} stream() map[Lang] { e =>
val reader = new InputStreamReader(self.getResource(e.getName), "UTF8")
val lang: FileConfiguration = YamlConfiguration loadConfiguration reader
new Lang(
e.getName substring (5, e.getName.length - 4) toLowerCase,
lang getString "choose_respawn",
lang getString "respawn_on_spawn",
lang getString "auto_choose_when_close",
lang getString "respawn_near_death",
lang getString "respawn_at_death",
lang getString "back_near_any_time",
lang getString "back_at_any_time",
lang getString "tpr_failed",
lang getString "no_death_loc",
)
}
} stream()).map[Lang](new Function[ZipEntry, Lang] {
override def apply(e: ZipEntry): Lang = {
val reader = new InputStreamReader(self.getResource(e.getName), "UTF8")
val lang: FileConfiguration = YamlConfiguration loadConfiguration reader
new Lang(
e.getName substring (5, e.getName.length - 4) toLowerCase,
{
val mapping = lang getStringList "mapping"
if(mapping != null) mapping else new util.ArrayList[String]
},
{
val when_default_override = lang getStringList "when_default_override"
if(when_default_override != null) when_default_override else new util.ArrayList[String]
},
lang getString "choose_respawn",
lang getString "respawn_on_spawn",
lang getString "auto_choose_when_close",
lang getString "respawn_near_death",
lang getString "respawn_at_death",
lang getString "back_near_any_time",
lang getString "back_at_any_time",
lang getString "tpr_failed",
lang getString "no_death_loc",
lang getString "no_permission_command",
)
}
})

lang_map.clear()
langs forEach { l: Lang =>
lang_map.put(l.name, l)
if(l.name == "cmn_hans") lang_map.put("zh_cn", l)
l.mapping forEach { m => lang_map.put(m, l)}
}
}
}
7 changes: 4 additions & 3 deletions src/io/meowtype/homeorback/ReSpawnGui.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.meowtype.homeorback

import java.util
import java.util.regex._
import java.util.stream.Collectors

import org.bukkit._
import org.bukkit.entity._
Expand Down Expand Up @@ -31,9 +33,8 @@ class ReSpawnGui(lang: Lang) extends InventoryHolder {
meta setDisplayName name

if(lore != null) {
val metalore = new util.ArrayList[String]
metalore add lore
meta setLore metalore
val lores = lore.split("\\\\n")
meta setLore (util.Arrays stream lores collect Collectors.toList[String])
}

item setItemMeta meta
Expand Down
1 change: 1 addition & 0 deletions src/io/meowtype/homeorback/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import java.util.zip.{ZipEntry, ZipInputStream}

import org.bukkit.{Bukkit, Location, Particle}
import org.bukkit.entity.Player
import org.bukkit.inventory.meta.ItemMeta
import org.bukkit.scheduler.{BukkitRunnable, BukkitTask}
import org.sqlite.SQLiteConfig

Expand Down
4 changes: 3 additions & 1 deletion src/lang/cmn_Hans.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mapping: [zh_cn]
choose_respawn: 选择复活方式
respawn_on_spawn: §6在复活点重生
auto_choose_when_close: §f关闭界面自动选择此项
Expand All @@ -6,4 +7,5 @@ respawn_at_death: §6在死亡地点重生
back_near_any_time: §f你随时都能使用 §6/back §f来回到死亡地点附近
back_at_any_time: §f你随时都能使用 §6/back §f来回到死亡地点
tpr_failed: §c随机传送失败,附近没有任何落脚点
no_death_loc: §c返回失败,你还没有死呢!
no_death_loc: §c返回失败,你还没有死呢!
no_permission_command: §c执行失败,你没有权限
10 changes: 6 additions & 4 deletions src/lang/en.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
choose_respawn: Choose a resurrection method
mapping: [en_us]
choose_respawn: Choose how to respawn
respawn_on_spawn: §6Respawn at the spawn location
auto_choose_when_close: §fWhen close auto choose this
respawn_near_death: §6Respawn near the death location
respawn_at_death: §6Respawn at the death location
back_near_any_time: §fYou can always use §f/back §fto get back to near the death location
back_at_any_time: §fYou can always use §f/back §fto get back to the death location
back_near_any_time: §fYou can always use §f/back \n§fto get back to near the death location
back_at_any_time: §fYou can always use §f/back \n§fto get back to the death location
tpr_failed: §cRandom teleport failed, there is no space nearby to stand
no_death_loc: §cCan't back, you have not died yet!
no_death_loc: §cCan't back, you have not died yet!
no_permission_command: §cExecution command failed, you do not have permission
4 changes: 3 additions & 1 deletion src/lang/lzh_Hans.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
when_default_override: [zh_cn]
choose_respawn: 择生也
respawn_on_spawn: §6生于生点
auto_choose_when_close: §f闭自择此项
Expand All @@ -6,4 +7,5 @@ respawn_at_death: §6死地复生
back_near_any_time: §f尔时皆可以 §6/back §f归死地近
back_at_any_time: §f尔时皆可以 §6/back §f归死地
tpr_failed: §c随传失败,近无可立之间
no_death_loc: §c不可归,汝尚未死!
no_death_loc: §c不可归,汝尚未死!
no_permission_command: §c不可执令,汝无权度

0 comments on commit c77b5f0

Please sign in to comment.