Skip to content

Commit

Permalink
Re-register translations when towny reloads them
Browse files Browse the repository at this point in the history
  • Loading branch information
Warriorrrr committed Oct 13, 2023
1 parent ee09fc7 commit 2a7bc02
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/main/java/io/github/townyadvanced/townymenus/TownyMenus.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.palmergames.bukkit.towny.TownyAPI;
import com.palmergames.bukkit.towny.TownyCommandAddonAPI;
import com.palmergames.bukkit.towny.TownyCommandAddonAPI.CommandType;
import com.palmergames.bukkit.towny.event.TranslationLoadEvent;
import com.palmergames.bukkit.towny.object.TranslationLoader;
import com.palmergames.bukkit.towny.scheduling.TaskScheduler;
import com.palmergames.bukkit.towny.scheduling.impl.BukkitTaskScheduler;
Expand All @@ -20,12 +21,13 @@
import io.github.townyadvanced.townymenus.menu.PlotMenu;
import io.github.townyadvanced.townymenus.menu.ResidentMenu;
import io.github.townyadvanced.townymenus.menu.TownMenu;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.logging.Logger;

public class TownyMenus extends JavaPlugin {
public class TownyMenus extends JavaPlugin implements Listener {

private static final Version requiredTownyVersion = Version.fromString("0.99.0.8");
private static TownyMenus plugin;
Expand All @@ -42,7 +44,7 @@ public void onEnable() {
if (!townyVersionCheck()) {
getLogger().severe("Towny version does not meet required minimum version: " + requiredTownyVersion);
getLogger().severe("Download the latest version here: https://github.com/TownyAdvanced/Towny/releases");
Bukkit.getPluginManager().disablePlugin(this);
getServer().getPluginManager().disablePlugin(this);
return;
} else {
getLogger().info("Towny version " + Towny.getPlugin().getVersion() + " found.");
Expand All @@ -52,9 +54,10 @@ public void onEnable() {
MenuSettings.loadConfig();
*/

Bukkit.getPluginManager().registerEvents(new InventoryListener(this), this);
Bukkit.getPluginManager().registerEvents(new PlayerListener(), this);
Bukkit.getPluginManager().registerEvents(new AwaitingConfirmation(), this);
getServer().getPluginManager().registerEvents(new InventoryListener(this), this);
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
getServer().getPluginManager().registerEvents(new AwaitingConfirmation(), this);
getServer().getPluginManager().registerEvents(this, this);

TownyMenuCommand townyMenuCommand = new TownyMenuCommand(this);

Expand All @@ -67,15 +70,13 @@ public void onEnable() {
TownyCommandAddonAPI.addSubCommand(CommandType.TOWN, "menu", new MenuExtensionCommand(TownMenu::createTownMenu));

logger().info("Loading translations...");
TranslationLoader loader = new TranslationLoader(getDataFolder().toPath().resolve("lang"), this, TownyMenus.class);
loader.load();
TownyAPI.getInstance().addTranslations(this, loader.getTranslations());
TownyAPI.getInstance().addTranslations(this, loadTranslations().getTranslations());
}

@Override
public void onDisable() {
// Close any open menu inventories
for (Player player : Bukkit.getOnlinePlayers())
for (Player player : getServer().getOnlinePlayers())
if (player.getOpenInventory().getTopInventory().getHolder() instanceof MenuInventory)
player.closeInventory();

Expand Down Expand Up @@ -113,4 +114,16 @@ private static boolean isFoliaClassPresent() {
return false;
}
}

private TranslationLoader loadTranslations() {
TranslationLoader loader = new TranslationLoader(getDataFolder().toPath().resolve("lang"), this, TownyMenus.class);
loader.load();

return loader;
}

@EventHandler
public void onTranslationLoad(final TranslationLoadEvent event) {
event.getAddedTranslations().putAll(loadTranslations().getTranslations());
}
}
1 change: 1 addition & 0 deletions src/main/resources/Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@
- Fixed some more localization issues.
- Prevented snapshotting of the clicked inventory holder, however useful that may be.
- Updated AnvilGUI to version 1.9.0-SNAPSHOT for 1.20.2 support.
- Fixed translations not being re-added when Towny reloads them.

0 comments on commit 2a7bc02

Please sign in to comment.