SteamWar/SpigotCore
Archiviert
13
0

Implementing first multilingual framework

Dieser Commit ist enthalten in:
Lixfel 2020-07-04 10:21:21 +02:00
Ursprung 35cad5e64c
Commit 7f2be86849
3 geänderte Dateien mit 163 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -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());
}
}

Datei anzeigen

@ -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());
}
}

Datei anzeigen

@ -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);
}
}