大概就把语言系统做完了
This commit is contained in:
		
							
								
								
									
										1
									
								
								.idea/dictionaries/csh20.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1
									
								
								.idea/dictionaries/csh20.xml
									
									
									
										generated
									
									
									
								
							@@ -6,6 +6,7 @@
 | 
			
		||||
      <w>sendable</w>
 | 
			
		||||
      <w>unserialize</w>
 | 
			
		||||
      <w>unserializer</w>
 | 
			
		||||
      <w>yaml</w>
 | 
			
		||||
    </words>
 | 
			
		||||
  </dictionary>
 | 
			
		||||
</component>
 | 
			
		||||
							
								
								
									
										5
									
								
								.idea/inspectionProfiles/Project_Default.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										5
									
								
								.idea/inspectionProfiles/Project_Default.xml
									
									
									
										generated
									
									
									
								
							@@ -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
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								.idea/inspectionProfiles/profiles_settings.xml
									
									
									
										generated
									
									
									
										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,39 +13,46 @@ 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) -> {
 | 
			
		||||
                WatchKey key;
 | 
			
		||||
                while ((key = service.poll()) != null) {
 | 
			
		||||
                    for (WatchEvent<?> watchEvent : key.pollEvents()) {
 | 
			
		||||
                        if (triple.getLeft().getName().equals(Objects.toString(watchEvent.context())))
 | 
			
		||||
                            triple.getRight().accept(triple.getMiddle());
 | 
			
		||||
                    }
 | 
			
		||||
                    key.reset();
 | 
			
		||||
        service.scheduleAtFixedRate(() -> map.forEach((service, triple) -> {
 | 
			
		||||
            WatchKey key;
 | 
			
		||||
            while ((key = service.poll()) != null) {
 | 
			
		||||
                for (WatchEvent<?> watchEvent : key.pollEvents()) {
 | 
			
		||||
                    if (triple.getLeft().getName().equals(Objects.toString(watchEvent.context())))
 | 
			
		||||
                        triple.getRight().accept(triple.getMiddle());
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
        }, 1000, 100, TimeUnit.MILLISECONDS);
 | 
			
		||||
                key.reset();
 | 
			
		||||
            }
 | 
			
		||||
        }), 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 {
 | 
			
		||||
 | 
			
		||||
@@ -18,22 +16,22 @@ public final class TLocale {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static JavaPlugin getCallerPlugin(Class<?> callerClass) {
 | 
			
		||||
    	try {
 | 
			
		||||
    		Field pluginField = callerClass.getClassLoader().getClass().getDeclaredField("plugin");
 | 
			
		||||
        try {
 | 
			
		||||
            Field pluginField = callerClass.getClassLoader().getClass().getDeclaredField("plugin");
 | 
			
		||||
            pluginField.setAccessible(true);
 | 
			
		||||
            return (JavaPlugin) pluginField.get(callerClass.getClassLoader());
 | 
			
		||||
    	} catch (Exception ignored) {
 | 
			
		||||
    		TLib.getTLib().getLogger().error("无效的语言文件发送形式: &4" + callerClass.getName());
 | 
			
		||||
    	}
 | 
			
		||||
    	return (JavaPlugin) Main.getInst();
 | 
			
		||||
        } catch (Exception ignored) {
 | 
			
		||||
            TLib.getTLib().getLogger().error("无效的语言文件发送形式: &4" + callerClass.getName());
 | 
			
		||||
        }
 | 
			
		||||
        return (JavaPlugin) Main.getInst();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static void sendTo(String path, CommandSender sender, String[] args, Class<?> callerClass) {
 | 
			
		||||
    	TLocaleLoader.sendTo(getCallerPlugin(callerClass), path, sender, args);
 | 
			
		||||
        TLocaleLoader.sendTo(getCallerPlugin(callerClass), path, sender, args);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static String asString(String path, String[] args, Class<?> callerClass) {
 | 
			
		||||
    	return TLocaleLoader.asString(getCallerPlugin(callerClass), path);
 | 
			
		||||
        return TLocaleLoader.asString(getCallerPlugin(callerClass), path);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void sendToConsole(String path, String... args) {
 | 
			
		||||
@@ -49,16 +47,16 @@ public final class TLocale {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static String asString(String path, String... args) {
 | 
			
		||||
    	try {
 | 
			
		||||
    		return asString(path, args, Ref.getCallerClass(3).get());
 | 
			
		||||
    	} catch (Exception e) {
 | 
			
		||||
    		TLib.getTLib().getLogger().error("语言文件获取失败: " + path);
 | 
			
		||||
    		TLib.getTLib().getLogger().error("原因: " + e.getMessage());
 | 
			
		||||
    		return "§4<" + path + "§4>"; 
 | 
			
		||||
    	}
 | 
			
		||||
        try {
 | 
			
		||||
            return asString(path, args, Ref.getCallerClass(3).get());
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            TLib.getTLib().getLogger().error("语言文件获取失败: " + path);
 | 
			
		||||
            TLib.getTLib().getLogger().error("原因: " + e.getMessage());
 | 
			
		||||
            return "§4<" + path + "§4>";
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void reload() {
 | 
			
		||||
    	Ref.getCallerClass(3).ifPresent(clazz -> TLocaleLoader.load(getCallerPlugin(clazz), false));
 | 
			
		||||
        Ref.getCallerClass(3).ifPresent(clazz -> TLocaleLoader.load(getCallerPlugin(clazz), false));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,49 +1,49 @@
 | 
			
		||||
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());
 | 
			
		||||
            TLib.getTLib().getLogger().error("语言文件发送失败: " + path);
 | 
			
		||||
            TLib.getTLib().getLogger().error("原因: " + e.getMessage());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    String asString(String path) {
 | 
			
		||||
    	return map.getOrDefault(path, ImmutableList.of(TLocaleSendable.getEmpty(path))).get(0).asString();
 | 
			
		||||
        return map.getOrDefault(path, ImmutableList.of(TLocaleSendable.getEmpty(path))).get(0).asString();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void load(YamlConfiguration configuration) {
 | 
			
		||||
@@ -51,45 +51,39 @@ 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 {
 | 
			
		||||
            	map.put(s, Collections.singletonList(TLocaleText.of(String.valueOf(object))));
 | 
			
		||||
            } else {
 | 
			
		||||
                map.put(s, Collections.singletonList(TLocaleText.of(String.valueOf(object))));
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static final Function<Object, TLocaleSendable> TO_SENDABLE = o -> {
 | 
			
		||||
        if (o instanceof TLocaleSendable) {
 | 
			
		||||
        	return ((TLocaleSendable) o);
 | 
			
		||||
            return ((TLocaleSendable) o);
 | 
			
		||||
        } else if (o instanceof String) {
 | 
			
		||||
        	return TLocaleText.of(((String) o));
 | 
			
		||||
            return TLocaleText.of(((String) o));
 | 
			
		||||
        } else {
 | 
			
		||||
        	return TLocaleText.of(String.valueOf(o));
 | 
			
		||||
            return TLocaleText.of(String.valueOf(o));
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    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) {
 | 
			
		||||
    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 {
 | 
			
		||||
            	map.put(path + "." + s, Collections.singletonList(TLocaleText.of(String.valueOf(object))));
 | 
			
		||||
            } else {
 | 
			
		||||
                map.put(path + "." + s, Collections.singletonList(TLocaleText.of(String.valueOf(object))));
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,38 +1,42 @@
 | 
			
		||||
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) {
 | 
			
		||||
        Optional.ofNullable(map.get(plugin.getName())).ifPresent(localeInstance -> localeInstance.sendTo(path, sender, 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) {
 | 
			
		||||
    	return map.get(plugin.getName()).asString(path);
 | 
			
		||||
        return map.get(plugin.getName()).asString(path);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void init() {
 | 
			
		||||
@@ -57,43 +61,42 @@ public class TLocaleLoader {
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                if (inputStream == null) {
 | 
			
		||||
                	TLib.getTLib().getLogger().error("语言文件加载失败");
 | 
			
		||||
                	return;
 | 
			
		||||
                    TLib.getTLib().getLogger().error(TLib.getTLib().getInternalLang().getString("LANG-LOAD-FAIL"));
 | 
			
		||||
                    return;
 | 
			
		||||
                }
 | 
			
		||||
                if (!file.exists()) {
 | 
			
		||||
                    file.getParentFile().mkdirs();
 | 
			
		||||
                    file.createNewFile();
 | 
			
		||||
                    saveResource(inputStream, file);
 | 
			
		||||
                }
 | 
			
		||||
                TLib.getTLib().getLogger().info("尝试加载 " + lang + ".yml 作为语言文件");
 | 
			
		||||
                YamlConfiguration configuration = ConfigUtils.loadYaml(plugin, file);
 | 
			
		||||
                TLocaleInstance localeInstance = new TLocaleInstance();
 | 
			
		||||
                localeInstance.load(configuration);
 | 
			
		||||
                map.put(plugin.getName(), localeInstance);
 | 
			
		||||
                TConfigInjector.fixUnicode(configuration);
 | 
			
		||||
                TLib.getTLib().getLogger().info("成功加载 " + lang + " 语言文件");
 | 
			
		||||
                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(plugin);
 | 
			
		||||
                    localeInstance.load(configuration);
 | 
			
		||||
                    map.put(plugin.getName(), localeInstance);
 | 
			
		||||
                }
 | 
			
		||||
                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 {
 | 
			
		||||
    		byte[] data = FileUtils.read(inputStream);
 | 
			
		||||
    		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) {
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
    private static void saveResource(InputStream inputStream, File file) throws IOException {
 | 
			
		||||
        byte[] data = FileUtils.read(inputStream);
 | 
			
		||||
        try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
 | 
			
		||||
            fileOutputStream.write(data);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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} 插件的语言文件'
 | 
			
		||||
		Reference in New Issue
	
	Block a user