Skip to content

Commit

Permalink
Implemented language system and fixed packet interceptor (global and …
Browse files Browse the repository at this point in the history
…team chat). Untested, unstable and only in 1.16.x
  • Loading branch information
PauMAVA committed Jun 9, 2021
1 parent 4c438d0 commit 0b239cc
Show file tree
Hide file tree
Showing 19 changed files with 418 additions and 93 deletions.
2 changes: 1 addition & 1 deletion v1_16_1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<parent>
<groupId>me.PauMAVA</groupId>
<artifactId>TheTowersRemastered</artifactId>
<version>1.2.1</version>
<version>1.3</version>
</parent>

<name>TheTowersRemastered [1.16.1]</name>
Expand Down
29 changes: 26 additions & 3 deletions v1_16_1/src/main/java/me/PauMAVA/TTR/TTRCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
import me.PauMAVA.TTR.commands.EnableDisableCommand;
import me.PauMAVA.TTR.commands.StartMatchCommand;
import me.PauMAVA.TTR.config.TTRConfigManager;
import me.PauMAVA.TTR.lang.LanguageManager;
import me.PauMAVA.TTR.lang.PluginString;
import me.PauMAVA.TTR.match.AutoStarter;
import me.PauMAVA.TTR.match.MatchStatus;
import me.PauMAVA.TTR.match.TTRMatch;
import me.PauMAVA.TTR.teams.TTRTeamHandler;
import me.PauMAVA.TTR.ui.TTRCustomTab;
import me.PauMAVA.TTR.ui.TTRScoreboard;
import me.PauMAVA.TTR.util.EventListener;
import me.PauMAVA.TTR.util.PacketInterceptor;
import me.PauMAVA.TTR.world.TTRWorldHandler;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class TTRCore extends JavaPlugin {
Expand All @@ -41,6 +45,8 @@ public class TTRCore extends JavaPlugin {
private TTRCustomTab customTab;
private TTRScoreboard scoreboard;
private AutoStarter autoStarter;
private LanguageManager languageManager;
private PacketInterceptor packetInterceptor;

private boolean isCounting = false;

Expand All @@ -50,19 +56,24 @@ public void onEnable() {
if (this.getConfig().getBoolean("enable_on_start")) {
enabled = true;
} else {
getLogger().warning("TTR is disabled on server start. Use /ttrenable to enable it on start. Make sure to be using the desired map.");
getLogger().warning("" + PluginString.DISABLED_ON_STARTUP_NOTICE);
}
this.configManager = new TTRConfigManager(this.getConfig());
this.languageManager = new LanguageManager(this);
this.packetInterceptor = new PacketInterceptor(this);
for (Player player: this.getServer().getOnlinePlayers()) {
this.packetInterceptor.addPlayer(player);
}
if (enabled) {
this.customTab = new TTRCustomTab();
this.customTab = new TTRCustomTab(this);
this.scoreboard = new TTRScoreboard();
this.match = new TTRMatch(MatchStatus.PREGAME);
this.customTab.runTaskTimer(this, 0L, 20L);
this.teamHandler = new TTRTeamHandler();
this.teamHandler.setUpDefaultTeams();
this.worldHandler = new TTRWorldHandler(this, this.getServer().getWorlds().get(0));
this.worldHandler.setUpWorld();
this.getServer().getPluginManager().registerEvents(new EventListener(), this);
this.getServer().getPluginManager().registerEvents(new EventListener(this), this);
} else {
this.match = new TTRMatch(MatchStatus.DISABLED);
}
Expand All @@ -79,6 +90,9 @@ public void onDisable() {
try {
this.customTab.cancel();
this.scoreboard.removeScoreboard();
for (Player player: this.getServer().getOnlinePlayers()) {
this.packetInterceptor.removePlayer(player);
}
} catch (NullPointerException ignored) {
}
}
Expand Down Expand Up @@ -122,4 +136,13 @@ public void setCounting(boolean counting) {
public AutoStarter getAutoStarter() {
return autoStarter;
}

public LanguageManager getLanguageManager() {
return languageManager;
}

public PacketInterceptor getPacketInterceptor() {
return packetInterceptor;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package me.PauMAVA.TTR.commands;

import me.PauMAVA.TTR.TTRCore;
import me.PauMAVA.TTR.lang.PluginString;
import me.PauMAVA.TTR.util.TTRPrefix;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
Expand All @@ -37,10 +38,10 @@ public EnableDisableCommand(TTRCore plugin) {
public boolean onCommand(CommandSender theSender, Command cmd, String label, String[] args) {
if (label.equalsIgnoreCase("ttrenable")) {
plugin.getConfigManager().setEnableOnStart(true);
theSender.sendMessage(TTRPrefix.TTR_GAME + "" + ChatColor.GREEN + "Plugin enabled on server start. /reload or restart server to apply changes! Players should rejoin...");
theSender.sendMessage(TTRPrefix.TTR_GAME + "" + ChatColor.GREEN + PluginString.TTR_ENABLE_OUTPUT);
} else if (label.equalsIgnoreCase("ttrdisable")) {
plugin.getConfigManager().setEnableOnStart(false);
theSender.sendMessage(TTRPrefix.TTR_GAME + "" + ChatColor.RED + "Plugin disabled on server start. /reload or restart server to apply changes!");
theSender.sendMessage(TTRPrefix.TTR_GAME + "" + ChatColor.RED + PluginString.TTR_DISABLE_OUTPUT);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package me.PauMAVA.TTR.commands;

import me.PauMAVA.TTR.TTRCore;
import me.PauMAVA.TTR.lang.PluginString;
import me.PauMAVA.TTR.util.TTRPrefix;
import me.PauMAVA.TTR.util.XPBarTimer;
import org.bukkit.ChatColor;
Expand All @@ -37,7 +38,7 @@ public boolean onCommand(CommandSender theSender, Command command, String label,
try {
timer = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
theSender.sendMessage(TTRPrefix.TTR_GAME + "" + ChatColor.GRAY + "You must input an integer!");
theSender.sendMessage(TTRPrefix.TTR_GAME + "" + ChatColor.GRAY + PluginString.ERROR_EXPECTED_INTEGER);
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public void setEnableOnStart(boolean value) {
saveConfig();
}

public String getLocale() {
return this.configuration.getString("locale");
}

private void saveConfig() {
TTRCore.getInstance().saveConfig();
}
Expand All @@ -136,6 +140,7 @@ public void resetFile() {

private void setUpFile() {
this.configuration.addDefault("enable_on_start", false);
this.configuration.addDefault("locale", "en");
this.autoStartSection = this.configuration.createSection("autostart");
autoStartSection.addDefault("enabled", true);
autoStartSection.addDefault("count", 4);
Expand Down
89 changes: 89 additions & 0 deletions v1_16_1/src/main/java/me/PauMAVA/TTR/lang/LanguageManager.java
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 "";
}

}
27 changes: 27 additions & 0 deletions v1_16_1/src/main/java/me/PauMAVA/TTR/lang/Locale.java
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 v1_16_1/src/main/java/me/PauMAVA/TTR/lang/LocaleRegistry.java
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 v1_16_1/src/main/java/me/PauMAVA/TTR/lang/PluginString.java
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);
}
}
5 changes: 3 additions & 2 deletions v1_16_1/src/main/java/me/PauMAVA/TTR/match/CageChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package me.PauMAVA.TTR.match;

import me.PauMAVA.TTR.TTRCore;
import me.PauMAVA.TTR.lang.PluginString;
import me.PauMAVA.TTR.teams.TTRTeam;
import me.PauMAVA.TTR.util.TTRPrefix;
import org.bukkit.*;
Expand Down Expand Up @@ -47,7 +48,7 @@ public void run() {
if (cage.isInCage(p) && TTRCore.getInstance().getTeamHandler().getPlayerTeam(p) != null) {
if (cage.getOwner().equals(TTRCore.getInstance().getTeamHandler().getPlayerTeam(p))) {
p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_BASS, 10, 1);
p.sendMessage(TTRPrefix.TTR_GAME + "" + ChatColor.RED + "You can't do that!");
p.sendMessage(TTRPrefix.TTR_GAME + "" + ChatColor.RED + PluginString.ALLY_CAGE_ENTER_OUTPUT);
p.teleport(TTRCore.getInstance().getConfigManager().getTeamSpawn(TTRCore.getInstance().getTeamHandler().getPlayerTeam(p).getIdentifier()));
} else {
cage.getLocation().getWorld().strikeLightningEffect(cage.getLocation());
Expand All @@ -69,7 +70,7 @@ private void playerOnCage(Player player) {
player.teleport(TTRCore.getInstance().getConfigManager().getTeamSpawn(playersTeam.getIdentifier()));
playersTeam.addPoints(1);
TTRCore.getInstance().getScoreboard().refreshScoreboard();
Bukkit.broadcastMessage(TTRPrefix.TTR_GAME + "" + ChatColor.GRAY + player.getName() + " has scored a point!");
Bukkit.broadcastMessage(TTRPrefix.TTR_GAME + "" + ChatColor.GRAY + player.getName() + PluginString.SCORE_OUTPUT);
for (Player p : Bukkit.getServer().getOnlinePlayers()) {
p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 10, 1);
}
Expand Down
3 changes: 2 additions & 1 deletion v1_16_1/src/main/java/me/PauMAVA/TTR/match/TTRMatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package me.PauMAVA.TTR.match;

import me.PauMAVA.TTR.TTRCore;
import me.PauMAVA.TTR.lang.PluginString;
import me.PauMAVA.TTR.teams.TTRTeam;
import me.PauMAVA.TTR.util.ReflectionUtils;
import org.bukkit.*;
Expand Down Expand Up @@ -89,7 +90,7 @@ public void endMatch(TTRTeam team) {
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
player.setGameMode(GameMode.SPECTATOR);
ChatColor teamColor = TTRCore.getInstance().getConfigManager().getTeamColor(team.getIdentifier());
player.sendTitle(teamColor + "" + ChatColor.BOLD + team.getIdentifier(), ChatColor.AQUA + "WINS!", 10, 100, 20);
player.sendTitle(teamColor + "" + ChatColor.BOLD + team.getIdentifier(), ChatColor.AQUA + "" + PluginString.WIN_OUTPUT, 10, 100, 20);
player.playSound(player.getLocation(), Sound.UI_TOAST_CHALLENGE_COMPLETE, 10, 1);
}
TTRCore.getInstance().getWorldHandler().enableDayLightCycle();
Expand Down
Loading

0 comments on commit 0b239cc

Please sign in to comment.