-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented language system and fixed packet interceptor (global and …
…team chat). Untested, unstable and only in 1.16.x
- Loading branch information
Showing
19 changed files
with
418 additions
and
93 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
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
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
89 changes: 89 additions & 0 deletions
89
v1_16_1/src/main/java/me/PauMAVA/TTR/lang/LanguageManager.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,89 @@ | ||
package me.PauMAVA.TTR.lang; | ||
|
||
import org.bukkit.configuration.file.FileConfiguration; | ||
import me.PauMAVA.TTR.TTRCore; | ||
import org.bukkit.configuration.file.YamlConfiguration; | ||
|
||
import java.io.*; | ||
import java.util.HashMap; | ||
|
||
public class LanguageManager { | ||
|
||
private final TTRCore plugin; | ||
private Locale selectedLocale; | ||
private FileConfiguration languageFile; | ||
|
||
public LanguageManager(TTRCore plugin) { | ||
this.plugin = plugin; | ||
setUpLocales(); | ||
extractLanguageFiles(); | ||
String shortName = plugin.getConfigManager().getLocale(); | ||
if (!setLocale(LocaleRegistry.getLocaleByShortName(shortName))) { | ||
plugin.getLogger().warning("Couldn't load lang " + shortName + "!"); | ||
plugin.getLogger().warning("Loading default language lang_en..."); | ||
if (!setLocale(LocaleRegistry.getLocaleByShortName("en"))) { | ||
plugin.getLogger().severe("Failed to load default language! Plugin won't work properly!"); | ||
} else { | ||
plugin.getLogger().info("Successfully loaded lang_en.yml!"); | ||
} | ||
} else { | ||
plugin.getLogger().info("Successfully loaded '" + shortName + "' locale!"); | ||
} | ||
} | ||
|
||
private void setUpLocales() { | ||
LocaleRegistry.registerLocale(new Locale("ENGLISH", "en", "PauMAVA")); | ||
} | ||
|
||
private void extractLanguageFiles() { | ||
HashMap<File, InputStream> streams = new HashMap<>(); | ||
for (Locale locale: LocaleRegistry.getLocales()) { | ||
streams.put( | ||
new File(plugin.getDataFolder().getPath() + "/lang_" + locale.getShortName() + ".yml"), | ||
LanguageManager.class.getResourceAsStream("/lang_" + locale.getShortName() + ".yml") | ||
); | ||
} | ||
for (File destination: streams.keySet()) { | ||
try { | ||
if (!destination.exists()) { | ||
destination.createNewFile(); | ||
InputStream in = streams.get(destination); | ||
byte[] buffer = new byte[in.available()]; | ||
in.read(buffer); | ||
OutputStream out = new FileOutputStream(destination); | ||
out.write(buffer); | ||
in.close(); | ||
out.close(); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
private boolean setLocale(Locale locale) { | ||
File targetFile = new File(plugin.getDataFolder().toString() + "/lang_" + locale.getShortName() + ".yml"); | ||
if (targetFile.exists()) { | ||
this.selectedLocale = locale; | ||
this.languageFile = YamlConfiguration.loadConfiguration(targetFile); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public Locale getSelectedLocale() { | ||
return selectedLocale; | ||
} | ||
|
||
public String getString(PluginString string) { | ||
if (this.languageFile.isSet(string.getPath())) { | ||
String unprocessed = this.languageFile.getString(string.getPath()); | ||
if (unprocessed == null) { | ||
return ""; | ||
} | ||
return unprocessed.replace("&", "§"); | ||
} | ||
return ""; | ||
} | ||
|
||
} |
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,27 @@ | ||
package me.PauMAVA.TTR.lang; | ||
|
||
public class Locale { | ||
|
||
private final String languageName; | ||
private final String shortName; | ||
private final String author; | ||
|
||
public Locale(String languageName, String shortName, String author) { | ||
this.languageName = languageName; | ||
this.shortName = shortName; | ||
this.author = author; | ||
} | ||
|
||
public String getLanguageName() { | ||
return languageName; | ||
} | ||
|
||
public String getShortName() { | ||
return shortName; | ||
} | ||
|
||
public String getAuthor() { | ||
return author; | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
v1_16_1/src/main/java/me/PauMAVA/TTR/lang/LocaleRegistry.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,42 @@ | ||
package me.PauMAVA.TTR.lang; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LocaleRegistry { | ||
|
||
private static List<Locale> locales = new ArrayList<>(); | ||
|
||
public static void registerLocale(Locale locale) { | ||
if (!locales.contains(locale)) { | ||
locales.add(locale); | ||
} | ||
} | ||
|
||
public static void unregisterLocale(Locale locale) { | ||
locales.remove(locale); | ||
} | ||
|
||
public static List<Locale> getLocales() { | ||
return locales; | ||
} | ||
|
||
public static Locale getLocaleByLongName(String longName) { | ||
for (Locale locale: locales) { | ||
if (locale.getLanguageName().equalsIgnoreCase(longName)) { | ||
return locale; | ||
} | ||
} | ||
return new Locale("Unknown", "Unknown", "Unknown"); | ||
} | ||
|
||
public static Locale getLocaleByShortName(String shortName) { | ||
for (Locale locale: locales) { | ||
if (locale.getShortName().equalsIgnoreCase(shortName)) { | ||
return locale; | ||
} | ||
} | ||
return new Locale("Unknown", "Unknown", "Unknown"); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
v1_16_1/src/main/java/me/PauMAVA/TTR/lang/PluginString.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,35 @@ | ||
package me.PauMAVA.TTR.lang; | ||
|
||
import me.PauMAVA.TTR.TTRCore; | ||
|
||
public enum PluginString { | ||
|
||
ALLY_CAGE_ENTER_OUTPUT("gameplay.on_ally_cage_enter"), | ||
DISABLED_ON_STARTUP_NOTICE("other.disabled_on_startup"), | ||
SCORE_OUTPUT("gameplay.on_score"), | ||
WIN_OUTPUT("gameplay.on_win"), | ||
ERROR_EXPECTED_INTEGER("commands.error_expected_integer"), | ||
ON_PLAYER_JOIN_OUTPUT("events.on_player_join"), | ||
ON_PLAYER_LEAVE_OUTPUT("events.on_player_leave"), | ||
ON_PLACE_BLOCK_ERROR("events.on_place_block_error"), | ||
ON_BREAK_BLOCK_ERROR("events.on_break_block_error"), | ||
TOTAL_TIME_LABEL("other.total_time_label"), | ||
TRANSLATION_MADE_BY("other.translation_made_by"), | ||
TTR_ENABLE_OUTPUT("commands.on_plugin_enable"), | ||
TTR_DISABLE_OUTPUT("commands.on_plugin_disable"); | ||
|
||
private final String path; | ||
|
||
PluginString(String path) { | ||
this.path = path; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return TTRCore.getInstance().getLanguageManager().getString(this); | ||
} | ||
} |
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
Oops, something went wrong.