-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
大更新! 新增:Language2 工具, wiki已添加该工具使用帮助
- Loading branch information
Showing
11 changed files
with
890 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/src/me/skymc/taboolib/commands/language/Language2Command.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package me.skymc.taboolib.commands.language; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.Material; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandExecutor; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.Player; | ||
|
||
import me.skymc.taboolib.Main; | ||
import me.skymc.taboolib.message.MsgUtils; | ||
import me.skymc.taboolib.string.language2.Language2Value; | ||
|
||
/** | ||
* @author sky | ||
* @since 2018年2月13日 下午5:11:01 | ||
*/ | ||
public class Language2Command implements CommandExecutor { | ||
|
||
@Override | ||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { | ||
if (args.length == 0) { | ||
sender.sendMessage("§f"); | ||
sender.sendMessage("§b§l----- §3§lTabooLib Commands §b§l-----"); | ||
sender.sendMessage("§f"); | ||
sender.sendMessage("§7 /language2 reload §f- §8重载语言库"); | ||
sender.sendMessage("§7 /language2 send [玩家] [语言] <变量> §f- §8发送语言提示"); | ||
sender.sendMessage("§f"); | ||
} | ||
else if (args[0].equalsIgnoreCase("reload")) { | ||
MsgUtils.send(sender, "§7重载中.."); | ||
long time = System.currentTimeMillis(); | ||
Main.getExampleLangauge2().reload(); | ||
MsgUtils.send(sender, "§7重载完成! 耗时: &f" + (System.currentTimeMillis() - time) + "ms"); | ||
} | ||
else if (args[0].equalsIgnoreCase("send")) { | ||
if (args.length < 3) { | ||
MsgUtils.send(sender, "§4参数错误"); | ||
} | ||
else { | ||
// 获取玩家 | ||
Player player = Bukkit.getPlayerExact(args[1]); | ||
if (player == null) { | ||
MsgUtils.send(sender, "§4玩家不在线"); | ||
} | ||
else { | ||
// 时间 | ||
long time = System.currentTimeMillis(); | ||
|
||
// 获取语言文件 | ||
Language2Value value = Main.getExampleLangauge2().get(args[2]); | ||
|
||
// 如果有变量参数 | ||
if (args.length > 3) { | ||
int i = 0; | ||
for (String variable : args[3].split("\\|")) { | ||
value.addPlaceholder("$" + i, variable); | ||
i++; | ||
} | ||
} | ||
|
||
// 发送信息 | ||
value.send(player); | ||
|
||
// 如果发送者是玩家 | ||
if (sender instanceof Player && ((Player) sender).getItemInHand().getType().equals(Material.COMMAND)) { | ||
MsgUtils.send(sender, "§7信息已发送, 本次计算耗时: &f" + (System.currentTimeMillis() - time) + "ms"); | ||
} | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
src/main/src/me/skymc/taboolib/string/language2/Language2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package me.skymc.taboolib.string.language2; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.configuration.file.FileConfiguration; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.plugin.Plugin; | ||
|
||
import lombok.Getter; | ||
import me.clip.placeholderapi.PlaceholderAPI; | ||
import me.skymc.taboolib.fileutils.ConfigUtils; | ||
|
||
/** | ||
* @author sky | ||
* @since 2018年2月13日 下午2:37:07 | ||
*/ | ||
public class Language2 { | ||
|
||
@Getter | ||
private FileConfiguration configuration; | ||
|
||
@Getter | ||
private File languageFile; | ||
|
||
@Getter | ||
private File languageFolder; | ||
|
||
@Getter | ||
private Plugin plugin; | ||
|
||
@Getter | ||
private String languageName; | ||
|
||
/** | ||
* 构造方法 | ||
* | ||
* @param plugin 插件 | ||
*/ | ||
public Language2(Plugin plugin) { | ||
this("zh_CN", plugin); | ||
} | ||
|
||
/** | ||
* 构造方法 | ||
* | ||
* @param languageName 语言文件 | ||
* @param plugin 插件 | ||
*/ | ||
public Language2(String languageName, Plugin plugin) { | ||
this.languageName = languageName; | ||
this.plugin = plugin; | ||
// 重载语言文件 | ||
reload(languageName); | ||
} | ||
|
||
/** | ||
* 获取语言文件 | ||
* | ||
* @param key 键 | ||
* @return {@link Language2Value} | ||
*/ | ||
public Language2Value get(String key) { | ||
return new Language2Value(this, key); | ||
} | ||
|
||
/** | ||
* 重载语言文件 | ||
*/ | ||
public void reload() { | ||
reload(this.languageName); | ||
} | ||
|
||
/** | ||
* 重载语言文件 | ||
* | ||
* @param languageName 新语言文件名称 | ||
*/ | ||
public void reload(String languageName) { | ||
// 初始化文件夹 | ||
createFolder(plugin); | ||
// 格式化配置名 | ||
languageName = formatName(languageName); | ||
// 获取文件 | ||
languageFile = new File(languageFolder, languageName); | ||
// 文件不存在 | ||
if (!languageFile.exists()) { | ||
// 如果语言文件不存在 | ||
if (plugin.getResource("Language2/" + languageName) == null) { | ||
try { | ||
throw new FileNotFoundException("语言文件 " + languageName + " 不存在"); | ||
} | ||
catch (Exception e) { | ||
// TODO: handle exception | ||
} | ||
} | ||
else { | ||
// 释放资源 | ||
plugin.saveResource("Language2/" + languageName, true); | ||
} | ||
} | ||
// 载入配置 | ||
configuration = ConfigUtils.load(plugin, languageFile); | ||
} | ||
|
||
/** | ||
* PlaceholderAPI 变量识别 | ||
* | ||
* @param player 玩家 | ||
* @param string 文本 | ||
* @return String | ||
*/ | ||
public String setPlaceholderAPI(Player player, String string) { | ||
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null && player != null) { | ||
return PlaceholderAPI.setPlaceholders(player, string); | ||
} | ||
return string; | ||
} | ||
|
||
/** | ||
* 语言文件名称格式化 | ||
* | ||
* @param name 语言文件名称 | ||
* @return String | ||
*/ | ||
private String formatName(String name) { | ||
return name.contains(".yml") ? name : name + ".yml"; | ||
} | ||
|
||
/** | ||
* 语言文件夹初始化 | ||
* | ||
* @param plugin | ||
*/ | ||
private void createFolder(Plugin plugin) { | ||
languageFolder = new File(plugin.getDataFolder(), "Language2"); | ||
if (!languageFolder.exists()) { | ||
languageFolder.mkdir(); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/src/me/skymc/taboolib/string/language2/Language2Type.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package me.skymc.taboolib.string.language2; | ||
|
||
/** | ||
* @author sky | ||
* @since 2018年2月13日 下午3:14:00 | ||
*/ | ||
public enum Language2Type { | ||
|
||
/** | ||
* 一般文本 | ||
*/ | ||
TEXT, | ||
|
||
/** | ||
* JSON 文本 | ||
*/ | ||
JSON, | ||
|
||
/** | ||
* 大标题 | ||
*/ | ||
TITLE, | ||
|
||
/** | ||
* 小标题 | ||
*/ | ||
ACTION, | ||
|
||
} |
Oops, something went wrong.