大概就把语言系统做完了
This commit is contained in:
parent
93e84b1b80
commit
f40039f9ce
@ -6,6 +6,7 @@
|
||||
<w>sendable</w>
|
||||
<w>unserialize</w>
|
||||
<w>unserializer</w>
|
||||
<w>yaml</w>
|
||||
</words>
|
||||
</dictionary>
|
||||
</component>
|
@ -32,5 +32,10 @@
|
||||
<option name="IGNORE_POINT_TO_ITSELF" value="false" />
|
||||
<option name="myAdditionalJavadocTags" value="author,author" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="SpellCheckingInspection" enabled="true" level="233" enabled_by_default="true">
|
||||
<option name="processCode" value="true" />
|
||||
<option name="processLiterals" value="true" />
|
||||
<option name="processComments" value="true" />
|
||||
</inspection_tool>
|
||||
</profile>
|
||||
</component>
|
23
.idea/inspectionProfiles/profiles_settings.xml
Normal file
23
.idea/inspectionProfiles/profiles_settings.xml
Normal file
@ -0,0 +1,23 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<info color="525229">
|
||||
<option name="EFFECT_COLOR" value="659c6b" />
|
||||
<option name="EFFECT_TYPE" value="2" />
|
||||
<option name="myName" value="233" />
|
||||
<option name="myVal" value="50" />
|
||||
<option name="myExternalName" value="233" />
|
||||
<option name="myDefaultAttributes" />
|
||||
</info>
|
||||
<list size="9">
|
||||
<item index="0" class="java.lang.String" itemvalue="UNUSED ENTRY" />
|
||||
<item index="1" class="java.lang.String" itemvalue="INFORMATION" />
|
||||
<item index="2" class="java.lang.String" itemvalue="TYPO" />
|
||||
<item index="3" class="java.lang.String" itemvalue="SERVER PROBLEM" />
|
||||
<item index="4" class="java.lang.String" itemvalue="WEAK WARNING" />
|
||||
<item index="5" class="java.lang.String" itemvalue="INFO" />
|
||||
<item index="6" class="java.lang.String" itemvalue="WARNING" />
|
||||
<item index="7" class="java.lang.String" itemvalue="ERROR" />
|
||||
<item index="8" class="java.lang.String" itemvalue="233" />
|
||||
</list>
|
||||
</settings>
|
||||
</component>
|
@ -1,6 +1,7 @@
|
||||
package com.ilummc.tlib;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
@ -17,6 +18,7 @@ import com.ilummc.tlib.resources.TLocaleLoader;
|
||||
|
||||
import lombok.Getter;
|
||||
import me.skymc.taboolib.Main;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
@Dependency(type = Dependency.Type.LIBRARY, maven = "org.ow2.asm:asm:6.1.1")
|
||||
@Dependency(type = Dependency.Type.LIBRARY, maven = "com.zaxxer:HikariCP:3.1.0")
|
||||
@ -38,11 +40,15 @@ public class TLib {
|
||||
@Getter
|
||||
private File libsFolder;
|
||||
|
||||
@Getter
|
||||
private YamlConfiguration internalLang;
|
||||
|
||||
private TLib() {
|
||||
libsFolder = new File(Main.getInst().getDataFolder(), "/libs");
|
||||
if (!libsFolder.exists()) {
|
||||
libsFolder.mkdirs();
|
||||
}
|
||||
internalLang = YamlConfiguration.loadConfiguration(new InputStreamReader(TLib.class.getResourceAsStream("/internalLang.yml")));
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
|
@ -1,12 +1,10 @@
|
||||
package com.ilummc.tlib.inject;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Triple;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.StandardWatchEventKinds;
|
||||
import java.nio.file.WatchEvent;
|
||||
import java.nio.file.WatchKey;
|
||||
import java.nio.file.WatchService;
|
||||
import java.nio.file.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
@ -15,17 +13,14 @@ import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Triple;
|
||||
|
||||
public class TConfigWatcher {
|
||||
|
||||
private ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
|
||||
private final ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
|
||||
|
||||
private Map<WatchService, Triple<File, Object, Consumer<Object>>> map = new HashMap<>();
|
||||
private final Map<WatchService, Triple<File, Object, Consumer<Object>>> map = new HashMap<>();
|
||||
|
||||
public TConfigWatcher() {
|
||||
service.scheduleAtFixedRate(() -> {
|
||||
map.forEach((service, triple) -> {
|
||||
service.scheduleAtFixedRate(() -> map.forEach((service, triple) -> {
|
||||
WatchKey key;
|
||||
while ((key = service.poll()) != null) {
|
||||
for (WatchEvent<?> watchEvent : key.pollEvents()) {
|
||||
@ -34,20 +29,30 @@ public class TConfigWatcher {
|
||||
}
|
||||
key.reset();
|
||||
}
|
||||
});
|
||||
}, 1000, 100, TimeUnit.MILLISECONDS);
|
||||
}), 1000, 100, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public void addOnListen(File file, Object obj, Consumer<Object> consumer) {
|
||||
try {
|
||||
WatchService service = FileSystems.getDefault().newWatchService();
|
||||
file.getParentFile().toPath().register(service, StandardWatchEventKinds.ENTRY_MODIFY);
|
||||
map.put(service, Triple.of(file, obj, consumer));
|
||||
map.putIfAbsent(service, Triple.of(file, obj, consumer));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> void addListener(File file, T obj, Consumer<T> consumer) {
|
||||
addOnListen(file, obj, (Consumer<Object>) consumer);
|
||||
}
|
||||
|
||||
public void removeListener(File file) {
|
||||
synchronized (map) {
|
||||
map.entrySet().removeIf(entry -> entry.getValue().getLeft().equals(file));
|
||||
}
|
||||
}
|
||||
|
||||
public void unregisterAll() {
|
||||
service.shutdown();
|
||||
map.forEach((service, pair) -> {
|
||||
|
@ -1,15 +1,13 @@
|
||||
package com.ilummc.tlib.resources;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.util.Ref;
|
||||
import me.skymc.taboolib.Main;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.util.Ref;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public final class TLocale {
|
||||
|
||||
|
@ -1,41 +1,41 @@
|
||||
package com.ilummc.tlib.resources;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.resources.type.TLocaleText;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ThreadSafe
|
||||
@SuppressWarnings("rawtypes")
|
||||
class TLocaleInstance {
|
||||
|
||||
TLocaleInstance() {
|
||||
}
|
||||
private final Plugin plugin;
|
||||
|
||||
void sendTo(String path, CommandSender sender) {
|
||||
try {
|
||||
map.getOrDefault(path, ImmutableList.of(TLocaleSendable.getEmpty(path))).forEach(sendable -> sendable.sendTo(sender));
|
||||
} catch (Exception e) {
|
||||
TLib.getTLib().getLogger().error("语言文件发送失败: " + path);
|
||||
TLib.getTLib().getLogger().error("原因: " + e.getMessage());
|
||||
}
|
||||
TLocaleInstance(Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
void sendTo(String path, CommandSender sender, String... args) {
|
||||
try {
|
||||
map.getOrDefault(path, ImmutableList.of(TLocaleSendable.getEmpty(path))).forEach(sendable -> sendable.sendTo(sender, args));
|
||||
map.getOrDefault(path, ImmutableList.of(TLocaleSendable.getEmpty(path))).forEach(sendable -> {
|
||||
if (Bukkit.isPrimaryThread()) {
|
||||
sendable.sendTo(sender, args);
|
||||
} else {
|
||||
Bukkit.getScheduler().runTask(plugin, () -> sendable.sendTo(sender, args));
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
TLib.getTLib().getLogger().error("语言文件发送失败: " + path);
|
||||
TLib.getTLib().getLogger().error("原因: " + e.getMessage());
|
||||
@ -51,14 +51,11 @@ class TLocaleInstance {
|
||||
Object object = configuration.get(s);
|
||||
if (object instanceof ConfigurationSection) {
|
||||
loadRecursively(s, (ConfigurationSection) object);
|
||||
}
|
||||
else if (object instanceof TLocaleSendable) {
|
||||
} else if (object instanceof TLocaleSendable) {
|
||||
map.put(s, Collections.singletonList((TLocaleSendable) object));
|
||||
}
|
||||
else if (object instanceof List && !((List) object).isEmpty()) {
|
||||
} else if (object instanceof List && !((List) object).isEmpty()) {
|
||||
map.put(s, ((List<?>) object).stream().map(TO_SENDABLE).collect(Collectors.toList()));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
map.put(s, Collections.singletonList(TLocaleText.of(String.valueOf(object))));
|
||||
}
|
||||
});
|
||||
@ -74,21 +71,18 @@ class TLocaleInstance {
|
||||
}
|
||||
};
|
||||
|
||||
private final Map<String, List<TLocaleSendable>> map = new ConcurrentHashMap<>();
|
||||
private final Map<String, List<TLocaleSendable>> map = new HashMap<>();
|
||||
|
||||
private void loadRecursively(String path, ConfigurationSection section) {
|
||||
section.getKeys(false).forEach(s -> {
|
||||
Object object = section.get(path + "." + s);
|
||||
if (object instanceof ConfigurationSection) {
|
||||
loadRecursively(path + "." + s, (ConfigurationSection) object);
|
||||
}
|
||||
else if (object instanceof TLocaleSendable) {
|
||||
} else if (object instanceof TLocaleSendable) {
|
||||
map.put(path + "." + s, Collections.singletonList((TLocaleSendable) object));
|
||||
}
|
||||
else if (object instanceof List && !((List) object).isEmpty()) {
|
||||
} else if (object instanceof List && !((List) object).isEmpty()) {
|
||||
map.put(path + "." + s, ((List<?>) object).stream().map(TO_SENDABLE).collect(Collectors.toList()));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
map.put(path + "." + s, Collections.singletonList(TLocaleText.of(String.valueOf(object))));
|
||||
}
|
||||
});
|
||||
|
@ -1,34 +1,38 @@
|
||||
package com.ilummc.tlib.resources;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.resources.type.TLocaleText;
|
||||
import com.ilummc.tlib.resources.type.TLocaleTitle;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.fileutils.ConfigUtils;
|
||||
import me.skymc.taboolib.fileutils.FileUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import com.ilummc.tlib.TLib;
|
||||
import com.ilummc.tlib.inject.TConfigInjector;
|
||||
import com.ilummc.tlib.resources.type.TLocaleText;
|
||||
import com.ilummc.tlib.resources.type.TLocaleTitle;
|
||||
|
||||
import me.skymc.taboolib.Main;
|
||||
import me.skymc.taboolib.fileutils.ConfigUtils;
|
||||
import me.skymc.taboolib.fileutils.FileUtils;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class TLocaleLoader {
|
||||
|
||||
private static final Map<String, TLocaleInstance> map = new HashMap<>();
|
||||
private static final Map<String, TLocaleInstance> map = new ConcurrentHashMap<>();
|
||||
|
||||
static void sendTo(Plugin plugin, String path, CommandSender sender, String... args) {
|
||||
if (Bukkit.isPrimaryThread())
|
||||
Optional.ofNullable(map.get(plugin.getName())).ifPresent(localeInstance -> localeInstance.sendTo(path, sender, args));
|
||||
else synchronized (TLocaleLoader.class) {
|
||||
Optional.ofNullable(map.get(plugin.getName())).ifPresent(localeInstance -> localeInstance.sendTo(path, sender, args));
|
||||
}
|
||||
}
|
||||
|
||||
static String asString(Plugin plugin, String path) {
|
||||
@ -57,7 +61,7 @@ public class TLocaleLoader {
|
||||
}
|
||||
}
|
||||
if (inputStream == null) {
|
||||
TLib.getTLib().getLogger().error("语言文件加载失败");
|
||||
TLib.getTLib().getLogger().error(TLib.getTLib().getInternalLang().getString("LANG-LOAD-FAIL"));
|
||||
return;
|
||||
}
|
||||
if (!file.exists()) {
|
||||
@ -65,35 +69,34 @@ public class TLocaleLoader {
|
||||
file.createNewFile();
|
||||
saveResource(inputStream, file);
|
||||
}
|
||||
TLib.getTLib().getLogger().info("尝试加载 " + lang + ".yml 作为语言文件");
|
||||
TLib.getTLib().getLogger().info(Strings.replaceWithOrder(TLib.getTLib().getInternalLang().getString("TRY-LOADING-LANG"), plugin.getName(), lang));
|
||||
synchronized (TLocaleLoader.class) {
|
||||
YamlConfiguration configuration = ConfigUtils.loadYaml(plugin, file);
|
||||
TLocaleInstance localeInstance = new TLocaleInstance();
|
||||
TLocaleInstance localeInstance = new TLocaleInstance(plugin);
|
||||
localeInstance.load(configuration);
|
||||
map.put(plugin.getName(), localeInstance);
|
||||
TConfigInjector.fixUnicode(configuration);
|
||||
TLib.getTLib().getLogger().info("成功加载 " + lang + " 语言文件");
|
||||
}
|
||||
File finalFile = file;
|
||||
String finalLang = lang;
|
||||
TLib.getTLib().getConfigWatcher().addListener(file, null, obj -> {
|
||||
TLib.getTLib().getLogger().info(Strings.replaceWithOrder(TLib.getTLib().getInternalLang().getString("RELOADING-LANG"), plugin.getName()));
|
||||
YamlConfiguration configuration = ConfigUtils.loadYaml(plugin, finalFile);
|
||||
TLocaleInstance localeInstance = new TLocaleInstance(plugin);
|
||||
localeInstance.load(configuration);
|
||||
map.put(plugin.getName(), localeInstance);
|
||||
TLib.getTLib().getLogger().info(Strings.replaceWithOrder(TLib.getTLib().getInternalLang().getString("SUCCESS-LOADING-LANG"), plugin.getName(), finalLang));
|
||||
});
|
||||
TLib.getTLib().getLogger().info(Strings.replaceWithOrder(TLib.getTLib().getInternalLang().getString("SUCCESS-LOADING-LANG"), plugin.getName(), lang));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
TLib.getTLib().getLogger().error("载入语言文件发生异常:" + e.toString());
|
||||
TLib.getTLib().getLogger().error(Strings.replaceWithOrder(TLib.getTLib().getInternalLang().getString("ERROR-LOADING-LANG"), plugin.getName(), e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
private static void saveResource(InputStream inputStream, File file) {
|
||||
FileOutputStream fileOutputStream = null;
|
||||
try {
|
||||
private static void saveResource(InputStream inputStream, File file) throws IOException {
|
||||
byte[] data = FileUtils.read(inputStream);
|
||||
fileOutputStream = new FileOutputStream(file);
|
||||
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
|
||||
fileOutputStream.write(data);
|
||||
} catch (Exception e) {
|
||||
TLib.getTLib().getLogger().error("资源文件写入失败: " + file);
|
||||
TLib.getTLib().getLogger().error("原因: " + e.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
if (fileOutputStream != null) {
|
||||
fileOutputStream.close();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,9 +4,8 @@ import org.bukkit.command.CommandSender;
|
||||
|
||||
public interface TLocaleSendable {
|
||||
|
||||
public static TLocaleSendable getEmpty(String path) {
|
||||
static TLocaleSendable getEmpty(String path) {
|
||||
return new TLocaleSendable() {
|
||||
|
||||
@Override
|
||||
public void sendTo(CommandSender sender, String... args) {
|
||||
sender.sendMessage("§4<" + path + "§4>");
|
||||
|
@ -8,6 +8,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
|
5
src/main/resources/internalLang.yml
Normal file
5
src/main/resources/internalLang.yml
Normal file
@ -0,0 +1,5 @@
|
||||
LANG-LOAD-FAIL: '语言文件加载失败'
|
||||
TRY-LOADING-LANG: '插件 {0} 尝试加载 {1} .yml 作为语言文件'
|
||||
SUCCESS-LOADING-LANG: '成功加载 {0} 插件的 {1} 语言文件'
|
||||
ERROR-LOADING-LANG: '加载 {0} 插件的语言文件时发生异常:{1}'
|
||||
RELOADING-LANG: '正在重新载入 {0} 插件的语言文件'
|
Loading…
Reference in New Issue
Block a user