Make local events translated
All checks were successful
/ deploy (push) Successful in 2m36s

This commit is contained in:
Minecon724 2024-12-05 20:33:06 +01:00
parent 49c6139d99
commit 799833b685
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
2 changed files with 34 additions and 17 deletions

View file

@ -14,32 +14,32 @@ import net.md_5.bungee.api.chat.hover.content.Text;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class ChatFormatUtils { public class ChatFormatUtils {
public static BaseComponent[] formatPlayer(Player player) { public static BaseComponent formatPlayer(Player player) {
ChatColor nameColor = ChatColor.of("#" + Integer.toHexString(player.getName().hashCode()).substring(0, 6)); ChatColor nameColor = ChatColor.of("#" + Integer.toHexString(player.getName().hashCode()).substring(0, 6));
if (player.getCustomName() != null) { if (player.getCustomName() != null) {
return new ComponentBuilder() return new ComponentBuilder()
.append("~" + player.getCustomName()).color(nameColor) .append("~" + player.getCustomName()).color(nameColor)
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(player.getName()))) .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(player.getName())))
.create(); .build();
} else { } else {
return new ComponentBuilder() return new ComponentBuilder()
.append(player.getName()).color(nameColor) .append(player.getName()).color(nameColor)
.create(); .build();
} }
} }
public static BaseComponent[] chatRoomPrefixShort(ChatRoom chatRoom) { public static BaseComponent chatRoomPrefixShort(ChatRoom chatRoom) {
ChatColor prefixColor = ChatColor.of(chatRoom.color.getColor().darker()); ChatColor prefixColor = ChatColor.of(chatRoom.color.getColor().darker());
return new ComponentBuilder(chatRoom.id.charAt(0) + " ").color(prefixColor) return new ComponentBuilder(chatRoom.id.charAt(0) + " ").color(prefixColor)
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(chatRoom.getInfoComponent()))) .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(chatRoom.getInfoComponent())))
.create(); .build();
} }
public static BaseComponent[] formatChatRoom(ChatRoom chatRoom) { public static BaseComponent formatChatRoom(ChatRoom chatRoom) {
return new ComponentBuilder(chatRoom.id).color(chatRoom.color) return new ComponentBuilder(chatRoom.id).color(chatRoom.color)
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(chatRoom.getInfoComponent()))) .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(chatRoom.getInfoComponent())))
.create(); .build();
} }
} }

View file

@ -10,6 +10,11 @@ import eu.m724.tweaks.TweaksConfig;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.TranslatableComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import net.minecraft.network.chat.Component;
import org.bukkit.craftbukkit.v1_21_R1.CraftRegistry;
import org.bukkit.craftbukkit.v1_21_R1.entity.CraftPlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -39,11 +44,9 @@ public class ChatListener implements Listener {
if (localEvents) { if (localEvents) {
chatRoom.broadcast( chatRoom.broadcast(
new ComponentBuilder() new ComponentBuilder(ChatFormatUtils.chatRoomPrefixShort(chatRoom))
.append(ChatFormatUtils.chatRoomPrefixShort(chatRoom)) .append(new TranslatableComponent("multiplayer.player.joined", ChatFormatUtils.formatPlayer(player))).color(ChatColor.GREEN)
.append(ChatFormatUtils.formatPlayer(player)) .create()
.append(" has joined the server").color(ChatColor.GREEN)
.create()
); );
// remove Minecraft join message // remove Minecraft join message
@ -58,10 +61,8 @@ public class ChatListener implements Listener {
if (localEvents) { if (localEvents) {
chatRoom.broadcast( chatRoom.broadcast(
new ComponentBuilder() new ComponentBuilder(ChatFormatUtils.chatRoomPrefixShort(chatRoom))
.append(ChatFormatUtils.chatRoomPrefixShort(chatRoom)) .append(new TranslatableComponent("multiplayer.player.left", ChatFormatUtils.formatPlayer(player))).color(ChatColor.RED)
.append(ChatFormatUtils.formatPlayer(player))
.append(" has left the server").color(ChatColor.RED)
.create() .create()
); );
@ -76,13 +77,29 @@ public class ChatListener implements Listener {
Player player = event.getEntity(); Player player = event.getEntity();
ChatRoom chatRoom = chatManager.getPlayerChatRoom(player); ChatRoom chatRoom = chatManager.getPlayerChatRoom(player);
// would be easier on Paper but this is not Paper
BaseComponent deathMessage = ComponentSerializer.deserialize(Component.Serializer.toJson(((CraftPlayer)player).getHandle().getCombatTracker().getDeathMessage(), CraftRegistry.getMinecraftRegistry()));
// TODO make players ChatFormatUtils
chatRoom.broadcast( chatRoom.broadcast(
new ComponentBuilder() new ComponentBuilder()
.append(ChatFormatUtils.chatRoomPrefixShort(chatRoom)) .append(ChatFormatUtils.chatRoomPrefixShort(chatRoom))
.append(event.getDeathMessage()) .append(deathMessage)
.create() .create()
); );
// broadcast to killer if available
if (player.getLastDamageCause().getDamageSource().getCausingEntity() instanceof Player killer) {
ChatRoom chatRoom2 = chatManager.getPlayerChatRoom(killer);
chatRoom2.broadcast(
new ComponentBuilder()
.append(ChatFormatUtils.chatRoomPrefixShort(chatRoom2))
.append(deathMessage)
.create()
);
}
// remove Minecraft death message // remove Minecraft death message
event.setDeathMessage(null); event.setDeathMessage(null);
} }