From 7f2be86849771b438d6d1c26ad58f864bf249161 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 4 Jul 2020 10:21:21 +0200 Subject: [PATCH] Implementing first multilingual framework --- .../src/de/steamwar/message/Message_12.java | 13 ++ .../src/de/steamwar/message/Message_8.java | 13 ++ .../src/de/steamwar/message/Message.java | 137 ++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 SpigotCore_12/src/de/steamwar/message/Message_12.java create mode 100644 SpigotCore_8/src/de/steamwar/message/Message_8.java create mode 100644 SpigotCore_Main/src/de/steamwar/message/Message.java diff --git a/SpigotCore_12/src/de/steamwar/message/Message_12.java b/SpigotCore_12/src/de/steamwar/message/Message_12.java new file mode 100644 index 0000000..509eb30 --- /dev/null +++ b/SpigotCore_12/src/de/steamwar/message/Message_12.java @@ -0,0 +1,13 @@ +package de.steamwar.message; + +import org.bukkit.entity.Player; + +import java.util.Locale; + +class Message_12 { + private Message_12(){} + + static Locale getLocale(Player player){ + return Locale.forLanguageTag(player.getLocale()); + } +} diff --git a/SpigotCore_8/src/de/steamwar/message/Message_8.java b/SpigotCore_8/src/de/steamwar/message/Message_8.java new file mode 100644 index 0000000..7ec09e4 --- /dev/null +++ b/SpigotCore_8/src/de/steamwar/message/Message_8.java @@ -0,0 +1,13 @@ +package de.steamwar.message; + +import org.bukkit.entity.Player; + +import java.util.Locale; + +class Message_8 { + private Message_8(){} + + static Locale getLocale(Player player){ + return Locale.forLanguageTag(player.spigot().getLocale()); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/message/Message.java b/SpigotCore_Main/src/de/steamwar/message/Message.java new file mode 100644 index 0000000..ebc569e --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/message/Message.java @@ -0,0 +1,137 @@ +package de.steamwar.message; + +import de.steamwar.core.Core; +import net.md_5.bungee.api.ChatMessageType; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.HoverEvent; +import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.text.MessageFormat; +import java.util.Locale; +import java.util.ResourceBundle; + +public class Message { + + private final String resourceBundleName; + + public Message(String resourceBundleName){ + this.resourceBundleName = resourceBundleName; + } + + /* Parsing input to a message */ + + public TextComponent parseToComponent(String message, boolean prefixed, CommandSender sender, Object... params){ + return new TextComponent(TextComponent.fromLegacyText(parse(message, prefixed, sender, params))); + } + + public String parsePrefixed(String message, CommandSender sender, Object... params){ + return parse(message, true, sender, params); + } + + public String parse(String message, CommandSender sender, Object... params){ + return parse(message, false, sender, params); + } + + private String parse(String message, boolean prefixed, CommandSender sender, Object... params){ + Locale locale; + if(sender instanceof Player) + locale = getLocale((Player) sender); + else + locale = Locale.getDefault(); + + ResourceBundle resourceBundle = ResourceBundle.getBundle(resourceBundleName, locale); + String pattern = ""; + if(prefixed) + pattern = resourceBundle.getObject("PREFIX") + " "; + pattern += (String)resourceBundle.getObject(message); + + return new MessageFormat(pattern, locale).format(params); + } + + private Locale getLocale(Player player){ + switch(Core.getVersion()){ + case 8: + case 9: + case 10: + return Message_8.getLocale(player); + case 12: + case 14: + case 15: + default: + return Message_12.getLocale(player); + } + } + + /* Send a message to one player */ + + public void send(String message, CommandSender sender, Object... params){ + send(message, true, sender, ChatMessageType.SYSTEM, null, null, params); + } + + public void sendPrefixless(String message, CommandSender sender, Object... params){ + send(message, false, sender, ChatMessageType.SYSTEM, null, null, params); + } + + public void send(String message, CommandSender sender, ChatMessageType type, Object... params){ + send(message, true, sender, type, null, null, params); + } + + public void sendPrefixless(String message, CommandSender sender, ChatMessageType type, Object... params){ + send(message, false, sender, type, null, null, params); + } + + public void send(String message, CommandSender sender, String onHover, ClickEvent onClick, Object... params){ + send(message, true, sender, ChatMessageType.SYSTEM, onHover, onClick, params); + } + + public void sendPrefixless(String message, CommandSender sender, String onHover, ClickEvent onClick, Object... params){ + send(message, false, sender, ChatMessageType.SYSTEM, onHover, onClick, params); + } + + public void send(String message, boolean prefixed, CommandSender sender, ChatMessageType type, String onHover, ClickEvent onClick, Object... params){ + TextComponent msg = parseToComponent(message, prefixed, sender, params); + if(onHover != null) + msg.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(onHover))); + if(onClick != null) + msg.setClickEvent(onClick); + if(sender instanceof Player) + ((Player)sender).spigot().sendMessage(type, msg); + else + sender.spigot().sendMessage(msg); + } + + /* Send message to all players */ + + public void broadcastPrefixless(String message, String onHover, ClickEvent onClick, Object... params){ + for(Player player : Bukkit.getOnlinePlayers()) + sendPrefixless(message, player, parse(onHover, false, player), onClick, params); + } + + public void broadcastPrefixless(String message, Object... params){ + for(Player player : Bukkit.getOnlinePlayers()) + sendPrefixless(message, player, ChatMessageType.SYSTEM, params); + } + + public void broadcastActionbar(String message, Object... params){ + for(Player player : Bukkit.getOnlinePlayers()) + send(message, player, ChatMessageType.ACTION_BAR, params); + } + + public void broadcast(String message, String onHover, ClickEvent onClick, Object... params){ + for(Player player : Bukkit.getOnlinePlayers()) + send(message, player, parse(onHover, false, player), onClick, params); + } + + public void broadcast(String message, Object... params){ + for(Player player : Bukkit.getOnlinePlayers()) + send(message, player, ChatMessageType.SYSTEM, params); + } + + public void chat(String message, Object... params){ + for(Player player : Bukkit.getOnlinePlayers()) + sendPrefixless(message, player, ChatMessageType.CHAT, params); + } +} -- 2.39.2