Skip to content

Commit d8b3d29

Browse files
committed
NyaaCore init
1 parent 038929a commit d8b3d29

File tree

75 files changed

+182
-5819
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+182
-5819
lines changed

.travis.yml

-44
This file was deleted.

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2016 NyaaCat Community
3+
Copyright (c) 2017 NyaaCat Community
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
## nyaautils
1+
## NyaaCore
2+
Common library used for all NyaaCat plugin.
3+
Provides infrastructures to simplify plugin development.
24

3-
Gaming utilities/helpers for NyaaCat Minecraft Server
5+
Current version: Minecraft 1.11.2
46

5-
Detailed function manual please refer to [Wiki](https://github.com/NyaaCat/nyaautils/wiki).
7+
## Component List
68

7-
[![Build Status](https://travis-ci.org/NyaaCat/nyaautils.svg?branch=master)](https://travis-ci.org/NyaaCat/nyaautils)
9+
- Annotation based command dispatcher
10+
- Annotation based configuration serializer
11+
- Annotation based database serializer
12+
- JSON message builder
13+
- Server side I18n support
814

915
## Use as dependency in Gradle
1016

1117
```
1218
repositories {
1319
maven {
14-
name 'nyaa'
15-
url 'https://raw.githubusercontent.com/NyaaCat/nyaautils/maven-repo'
20+
name 'NyaaCore'
21+
url 'https://raw.githubusercontent.com/NyaaCat/NyaaCore/maven-repo'
1622
}
1723
}
1824
1925
dependencies {
20-
compile('cat.nyaa:nyaautils:2.0-SNAPSHOT') {
26+
compile('cat.nyaa:nyaacore:2.0-SNAPSHOT') {
2127
transitive = false
2228
}
2329
}

build.gradle

+1-16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ plugins {
66

77
sourceCompatibility = 1.8
88
targetCompatibility = 1.8
9+
archivesBaseName = 'NyaaCore-mc1.11.2'
910

1011
repositories {
1112
jcenter()
@@ -50,19 +51,3 @@ dependencies {
5051
compileJava {
5152
options.compilerArgs += ["-Xplugin:NyaaUtilsLangAnnotationProcessor"]
5253
}
53-
54-
publishing {
55-
publications {
56-
mavenJava(MavenPublication) {
57-
groupId "cat.nyaa"
58-
artifactId "nyaautils"
59-
version '2.0-SNAPSHOT'
60-
from components.java
61-
}
62-
}
63-
repositories {
64-
maven {
65-
url "$buildDir/repo"
66-
}
67-
}
68-
}

manually_deploy_repo.sh

-13
This file was deleted.

src/main/java/cat/nyaa/utils/BasicItemMatcher.java renamed to src/main/java/cat/nyaa/nyaacore/BasicItemMatcher.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package cat.nyaa.utils;
1+
package cat.nyaa.nyaacore;
22

3+
import cat.nyaa.nyaacore.configuration.ISerializable;
34
import org.bukkit.ChatColor;
45
import org.bukkit.enchantments.Enchantment;
56
import org.bukkit.inventory.ItemStack;

src/main/java/cat/nyaa/utils/CommandReceiver.java renamed to src/main/java/cat/nyaa/nyaacore/CommandReceiver.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cat.nyaa.utils;
1+
package cat.nyaa.nyaacore;
22

33
import org.bukkit.Bukkit;
44
import org.bukkit.Material;
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
package cat.nyaa.utils;
1+
package cat.nyaa.nyaacore;
22

3-
import cat.nyaa.nyaautils.NyaaUtils;
43
import org.bukkit.ChatColor;
54
import org.bukkit.configuration.ConfigurationSection;
65
import org.bukkit.configuration.file.YamlConfiguration;
@@ -16,53 +15,70 @@
1615
import java.util.Map;
1716

1817
public abstract class Internationalization {
19-
private final String DEFAULT_LANGUAGE = "en_US";
20-
private final Map<String, String> map = new HashMap<>();
21-
// internal language keys will only be loaded from NyaaUtils
22-
// but share across all dependent plugins
23-
// TODO do not depend on NyaaUtils
24-
// TODO HEH load before NU ?!
18+
/**
19+
* Use English as default & fallback language
20+
*/
21+
private static final String DEFAULT_LANGUAGE = "en_US";
22+
/**
23+
* Internal language map is loaded and should only be loaded by {@link NyaaCoreLoader#onLoad()}
24+
* Once it's loaded, it cannot be reloaded or modified.
25+
* The internal map will be shared across all plugins using {@link Internationalization}
26+
*/
2527
private final static Map<String, String> internalMap = new HashMap<>();
28+
/**
29+
* Per-plugin language map used by {@link Internationalization}
30+
* This map has a higher priority than {@link this#internalMap}
31+
* So it's possible to overwrite some internal language keys here.
32+
*/
33+
private final Map<String, String> map = new HashMap<>();
2634

35+
/**
36+
* Dependent plugins should provide the "main" class instance
37+
* So that language files could be loaded automatically
38+
*
39+
* @return the plugin class instance
40+
*/
2741
protected abstract JavaPlugin getPlugin();
2842

43+
/**
44+
* @return the language to be loaded into {@link this#map}
45+
*/
2946
protected abstract String getLanguage();
3047

48+
49+
/**
50+
* Reset then load per-plugin language map
51+
* Based on {@link this#getPlugin()} and {@link this#getLanguage()}
52+
*/
3153
public void load() {
54+
map.clear();
3255
String language = getLanguage();
3356
JavaPlugin plugin = getPlugin();
3457
if (language == null) language = DEFAULT_LANGUAGE;
35-
// internal map
36-
if (plugin instanceof NyaaUtils) {
37-
loadInternalMap((NyaaUtils) plugin, language);
38-
}
3958
// language map
4059
loadLanguageMap(plugin, language);
4160
// save (probably) modified language file back to disk
42-
saveLanguageMap(plugin, language);
61+
saveLanguageMap();
4362

4463
plugin.getLogger().info(get("internal.info.using_language", language));
4564
}
4665

47-
private void loadInternalMap(NyaaUtils plugin, String language) {
48-
internalMap.clear();
49-
boolean forceJar = System.getProperty("nyaautils.i18n.loadFromDisk", "true").equals("false");
50-
if (!forceJar) { // load from disk
51-
File localLangFile = new File(plugin.getDataFolder(), language + ".yml");
52-
if (localLangFile.exists()) {
53-
ConfigurationSection section = YamlConfiguration.loadConfiguration(localLangFile);
54-
loadLanguageSection(internalMap, section.getConfigurationSection("internal"), "internal.", false);
66+
/**
67+
* Load per-plugin language map
68+
* Based on {@link this#getPlugin()} and {@link this#getLanguage()}
69+
*/
70+
public void saveLanguageMap() {
71+
JavaPlugin plugin = getPlugin();
72+
String language = getLanguage();
73+
File localLangFile = new File(plugin.getDataFolder(), language + ".yml");
74+
try {
75+
YamlConfiguration yaml = new YamlConfiguration();
76+
for (String key : map.keySet()) {
77+
yaml.set(key, map.get(key));
5578
}
56-
}
57-
InputStream stream = plugin.getResource("lang/" + language + ".yml");
58-
if (stream != null) {
59-
ConfigurationSection section = YamlConfiguration.loadConfiguration(new InputStreamReader(stream));
60-
loadLanguageSection(internalMap, section.getConfigurationSection("internal"), "internal.", false);
61-
}
62-
stream = plugin.getResource("lang/" + DEFAULT_LANGUAGE + ".yml");
63-
if (stream != null) {
64-
ConfigurationSection section = YamlConfiguration.loadConfiguration(new InputStreamReader(stream));
65-
loadLanguageSection(internalMap, section.getConfigurationSection("internal"), "internal.", false);
79+
yaml.save(localLangFile);
80+
} catch (IOException ex) {
81+
plugin.getLogger().warning("Cannot save language file: " + language + ".yml");
6682
}
6783
}
6884

@@ -87,24 +103,6 @@ private void loadLanguageMap(JavaPlugin plugin, String language) {
87103
loadLanguageSection(map, YamlConfiguration.loadConfiguration(new InputStreamReader(stream)), "", true);
88104
}
89105

90-
private void saveLanguageMap(JavaPlugin plugin, String language) {
91-
File localLangFile = new File(plugin.getDataFolder(), language + ".yml");
92-
try {
93-
YamlConfiguration yaml = new YamlConfiguration();
94-
for (String key : map.keySet()) {
95-
yaml.set(key, map.get(key));
96-
}
97-
if (plugin instanceof NyaaUtils) { // save internal section if is NyaaUtils
98-
for (String key : internalMap.keySet()) {
99-
yaml.set(key, internalMap.get(key));
100-
}
101-
}
102-
yaml.save(localLangFile);
103-
} catch (IOException ex) {
104-
plugin.getLogger().warning("Cannot save language file: " + language + ".yml");
105-
}
106-
}
107-
108106
/**
109107
* add all language items from section into language map recursively
110108
* existing items won't be overwritten
@@ -113,7 +111,7 @@ private void saveLanguageMap(JavaPlugin plugin, String language) {
113111
* @param prefix used in recursion to determine the proper prefix
114112
* @param ignoreInternal ignore keys prefixed with `internal'
115113
*/
116-
private void loadLanguageSection(Map<String, String> map, ConfigurationSection section, String prefix, boolean ignoreInternal) {
114+
private static void loadLanguageSection(Map<String, String> map, ConfigurationSection section, String prefix, boolean ignoreInternal, JavaPlugin plugin) {
117115
if (map == null || section == null || prefix == null) return;
118116
for (String key : section.getKeys(false)) {
119117
String path = prefix + key;
@@ -122,13 +120,49 @@ private void loadLanguageSection(Map<String, String> map, ConfigurationSection s
122120
map.put(path, ChatColor.translateAlternateColorCodes('&', section.getString(key)));
123121
}
124122
} else if (section.isConfigurationSection(key)) {
125-
loadLanguageSection(map, section.getConfigurationSection(key), path + ".", ignoreInternal);
123+
loadLanguageSection(map, section.getConfigurationSection(key), path + ".", ignoreInternal, plugin);
126124
} else {
127-
getPlugin().getLogger().warning("Skipped language section: " + path);
125+
plugin.getLogger().warning("Skipped language section: " + path);
128126
}
129127
}
130128
}
131129

130+
private void loadLanguageSection(Map<String, String> map, ConfigurationSection section, String prefix, boolean ignoreInternal) {
131+
loadLanguageSection(map, section, prefix, ignoreInternal, getPlugin());
132+
}
133+
134+
135+
/**
136+
* Load the internal map
137+
* should only be called from {@link NyaaCoreLoader#onLoad()}
138+
*
139+
* @param plugin the NyaaCore plugin
140+
*/
141+
static void loadInternalMap(NyaaCoreLoader plugin) {
142+
loadInternalMap(plugin, DEFAULT_LANGUAGE);
143+
}
144+
145+
private static void loadInternalMap(NyaaCoreLoader plugin, String language) {
146+
internalMap.clear();
147+
boolean forceJar = System.getProperty("nyaautils.i18n.loadFromDisk", "true").equals("false");
148+
if (!forceJar) { // load from disk
149+
File localLangFile = new File(plugin.getDataFolder(), language + ".yml");
150+
if (localLangFile.exists()) {
151+
ConfigurationSection section = YamlConfiguration.loadConfiguration(localLangFile);
152+
loadLanguageSection(internalMap, section.getConfigurationSection("internal"), "internal.", false, plugin);
153+
}
154+
}
155+
InputStream stream = plugin.getResource("lang/" + language + ".yml");
156+
if (stream != null) {
157+
ConfigurationSection section = YamlConfiguration.loadConfiguration(new InputStreamReader(stream));
158+
loadLanguageSection(internalMap, section.getConfigurationSection("internal"), "internal.", false, plugin);
159+
}
160+
stream = plugin.getResource("lang/" + DEFAULT_LANGUAGE + ".yml");
161+
if (stream != null) {
162+
ConfigurationSection section = YamlConfiguration.loadConfiguration(new InputStreamReader(stream));
163+
loadLanguageSection(internalMap, section.getConfigurationSection("internal"), "internal.", false, plugin);
164+
}
165+
}
132166

133167
public String get(@LangKey String key, Object... para) {
134168
String val = map.get(key);
@@ -141,9 +175,9 @@ public String get(@LangKey String key, Object... para) {
141175
}
142176
return keyBuilder.toString();
143177
} else {
144-
try{
178+
try {
145179
return String.format(val, para);
146-
} catch (IllegalFormatConversionException e){
180+
} catch (IllegalFormatConversionException e) {
147181
e.printStackTrace();
148182
getPlugin().getLogger().warning("Corrupted language key: " + key);
149183
getPlugin().getLogger().warning("val: " + val);
@@ -161,9 +195,4 @@ public String get(@LangKey String key, Object... para) {
161195
public boolean hasKey(String key) {
162196
return map.containsKey(key) || internalMap.containsKey(key);
163197
}
164-
165-
public void reset() {
166-
if (getPlugin() instanceof NyaaUtils) internalMap.clear();
167-
map.clear();
168-
}
169198
}

0 commit comments

Comments
 (0)