From 2242720ded3e6d83df8263da58f860faea3c46fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Mon, 7 May 2018 15:04:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/inspectionProfiles/Project_Default.xml | 14 +++- ...aven__org_projectlombok_lombok_1_16_20.xml | 13 ---- .../com/ilummc/tlib/annotations/TConfig.java | 29 ++++++++ .../tlib/annotations/TLocalePlugin.java | 11 +++ .../ilummc/tlib/inject/TConfigInjector.java | 4 +- .../tlib/resources/type/TLocaleSound.java | 69 +++++++++++++++++++ src/main/resources/lang/internal.yml | 9 +++ 7 files changed, 133 insertions(+), 16 deletions(-) delete mode 100644 .idea/libraries/Maven__org_projectlombok_lombok_1_16_20.xml create mode 100644 src/main/java/com/ilummc/tlib/annotations/TConfig.java create mode 100644 src/main/java/com/ilummc/tlib/annotations/TLocalePlugin.java create mode 100644 src/main/java/com/ilummc/tlib/resources/type/TLocaleSound.java create mode 100644 src/main/resources/lang/internal.yml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 49d45ba..d10bf95 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -11,7 +11,7 @@ - + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_projectlombok_lombok_1_16_20.xml b/.idea/libraries/Maven__org_projectlombok_lombok_1_16_20.xml deleted file mode 100644 index bcbf2ac..0000000 --- a/.idea/libraries/Maven__org_projectlombok_lombok_1_16_20.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/main/java/com/ilummc/tlib/annotations/TConfig.java b/src/main/java/com/ilummc/tlib/annotations/TConfig.java new file mode 100644 index 0000000..30a7797 --- /dev/null +++ b/src/main/java/com/ilummc/tlib/annotations/TConfig.java @@ -0,0 +1,29 @@ +package com.ilummc.tlib.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.reflect.Modifier; + +import com.ilummc.tlib.util.Ref; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface TConfig { + + String name() default "config.yml"; + + boolean fromJar() default false; + + boolean saveOnExit() default false; + + boolean readOnly() default true; + + String charset() default "UTF-8"; + + boolean listenChanges() default false; + + int excludeModifiers() default Modifier.STATIC | Modifier.TRANSIENT | Ref.ACC_SYNTHETIC | Ref.ACC_BRIDGE; + +} diff --git a/src/main/java/com/ilummc/tlib/annotations/TLocalePlugin.java b/src/main/java/com/ilummc/tlib/annotations/TLocalePlugin.java new file mode 100644 index 0000000..40f6ce6 --- /dev/null +++ b/src/main/java/com/ilummc/tlib/annotations/TLocalePlugin.java @@ -0,0 +1,11 @@ +package com.ilummc.tlib.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface TLocalePlugin { +} diff --git a/src/main/java/com/ilummc/tlib/inject/TConfigInjector.java b/src/main/java/com/ilummc/tlib/inject/TConfigInjector.java index 09ce66f..9a0950a 100644 --- a/src/main/java/com/ilummc/tlib/inject/TConfigInjector.java +++ b/src/main/java/com/ilummc/tlib/inject/TConfigInjector.java @@ -56,7 +56,7 @@ public class TConfigInjector { if (!file.exists()) if (config.fromJar()) plugin.saveResource(config.name(), true); else saveConfig(plugin, clazz.newInstance()); Object obj = unserialize(plugin, clazz); - if (!config.readOnly()) saveConfig(plugin, obj); + if (config.readOnly()) saveConfig(plugin, obj); return obj; } catch (NullPointerException e) { TLocale.Logger.warn("CONFIG.LOAD-FAIL-NO-ANNOTATION", plugin.toString(), clazz.getSimpleName()); @@ -73,7 +73,7 @@ public class TConfigInjector { File file = new File(plugin.getDataFolder(), config.name()); Map map = ConfigUtils.confToMap(ConfigUtils.loadYaml(plugin, file)); Object obj = ConfigUtils.mapToObj(map, object); - if (!config.readOnly()) saveConfig(plugin, obj); + if (config.readOnly()) saveConfig(plugin, obj); } catch (NullPointerException e) { TLocale.Logger.warn("CONFIG.LOAD-FAIL-NO-ANNOTATION", plugin.toString(), object.getClass().getSimpleName()); } catch (Exception e) { diff --git a/src/main/java/com/ilummc/tlib/resources/type/TLocaleSound.java b/src/main/java/com/ilummc/tlib/resources/type/TLocaleSound.java new file mode 100644 index 0000000..3156053 --- /dev/null +++ b/src/main/java/com/ilummc/tlib/resources/type/TLocaleSound.java @@ -0,0 +1,69 @@ +package com.ilummc.tlib.resources.type; + +import com.google.common.collect.Maps; +import com.ilummc.tlib.resources.TLocaleSendable; +import me.skymc.taboolib.sound.SoundPack; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.serialization.ConfigurationSerializable; +import org.bukkit.configuration.serialization.SerializableAs; +import org.bukkit.entity.Player; + +import javax.annotation.concurrent.Immutable; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * @Author sky + * @Since 2018-05-06 14:35 + */ +@Immutable +@SerializableAs("ACTION") +public class TLocaleSound implements TLocaleSendable, ConfigurationSerializable { + + private final List soundPacks; + + public TLocaleSound(List soundPacks) { + this.soundPacks = soundPacks; + } + + @Override + public void sendTo(CommandSender sender, String... args) { + if (sender instanceof Player) { + soundPacks.forEach(x -> x.play((Player) sender)); + } + } + + @Override + public String asString(String... args) { + return toString(); + } + + @Override + public String toString() { + return "soundPacks=" + "TLocaleSound{" + soundPacks + '}'; + } + + @Override + public Map serialize() { + Map map = Maps.newHashMap(); + if (soundPacks.size() == 1) { + map.put("sounds", soundPacks.get(0).toString()); + } else if (soundPacks.size() > 1) { + map.put("sounds", soundPacks.stream().map(SoundPack::toString).collect(Collectors.toList())); + } + return map; + } + + public static TLocaleSound valueOf(Map map) { + List soundPacks = new ArrayList<>(); + Object sounds = map.containsKey("sounds") ? map.get("sounds") : map.getOrDefault("sound", ""); + if (sounds instanceof List) { + soundPacks = ((List) sounds).stream().map(SoundPack::new).collect(Collectors.toList()); + } else { + soundPacks.add(new SoundPack(sounds.toString())); + } + return new TLocaleSound(soundPacks); + } +} diff --git a/src/main/resources/lang/internal.yml b/src/main/resources/lang/internal.yml new file mode 100644 index 0000000..fb41bc9 --- /dev/null +++ b/src/main/resources/lang/internal.yml @@ -0,0 +1,9 @@ +TRY-LOADING-LANG: '插件 {0} 尝试加载 {1} 作为语言文件' +SUCCESS-LOADING-LANG-NORMAL: '成功加载 {0} 插件的 {1} 语言文件, 共 {2} 项' +SUCCESS-LOADING-LANG-UPDATE: '成功加载 {0} 插件的 {1} 语言文件, 共 {2} 项, 及 {3} 项新条目' +ERROR-LOADING-LANG: '加载 {0} 插件的语言文件时发生异常:{1}' +RELOADING-LANG: '正在重新载入 {0} 插件的语言文件' +FETCH-LOCALE-ERROR: '语言文件获取失败:{0}' +SEND-LOCALE-ERROR: '语言文件发送失败:{0}' +LOCALE-ERROR-REASON: '原因:{0}' +MISSING-ARGUMENT: '语言文本含有没有找到的参数 {0}' \ No newline at end of file