diff --git a/README.md b/README.md index 9a4c101..3a66efe 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,9 @@ Chat rooms players can freely create and join. Alerts like death and join messag `/chat` - switch chat room \ `/chatmanage` - create, delete, modify etc. (`tweaks724.chatmanage`) +### Proximity chat +Self-explanatory + ### Compass Holding a compass shows a bar with 4 directions and stuff like beds, lodestones, death pos (TODO) etc. diff --git a/src/main/java/eu/m724/tweaks/TweaksConfig.java b/src/main/java/eu/m724/tweaks/TweaksConfig.java index 3b68894..53e8972 100644 --- a/src/main/java/eu/m724/tweaks/TweaksConfig.java +++ b/src/main/java/eu/m724/tweaks/TweaksConfig.java @@ -30,6 +30,7 @@ public record TweaksConfig( boolean chatEnabled, boolean chatLocalEvents, String chatDefaultName, + int chatRadius, boolean compassEnabled, int compassWidth, @@ -91,6 +92,7 @@ public record TweaksConfig( boolean chatEnabled = config.getBoolean("chat.enabled"); boolean chatLocalEvents = config.getBoolean("chat.localEvents"); String chatDefaultName = config.getString("chat.defaultName"); + int chatRadius = config.getInt("chat.radius"); boolean compassEnabled = config.getBoolean("compass.enabled"); int compassWidth = config.getInt("compass.width"); @@ -117,7 +119,7 @@ public record TweaksConfig( brandEnabled, brandText, brandShowPing, brandShowMspt, doorEnabled, doorDoubleOpen, doorKnocking, motdEnabled, motdSet, - chatEnabled, chatLocalEvents, chatDefaultName, + chatEnabled, chatLocalEvents, chatDefaultName, chatRadius, compassEnabled, compassWidth, compassPrecision, pomodoroEnabled, pomodoroForce, updaterEnabled, diff --git a/src/main/java/eu/m724/tweaks/chat/ChatCommands.java b/src/main/java/eu/m724/tweaks/chat/ChatCommands.java index a99719b..09e19b7 100644 --- a/src/main/java/eu/m724/tweaks/chat/ChatCommands.java +++ b/src/main/java/eu/m724/tweaks/chat/ChatCommands.java @@ -9,9 +9,6 @@ package eu.m724.tweaks.chat; import eu.m724.tweaks.Language; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.BaseComponent; -import net.md_5.bungee.api.chat.ComponentBuilder; -import net.md_5.bungee.api.chat.HoverEvent; -import net.md_5.bungee.api.chat.hover.content.Text; import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -71,15 +68,16 @@ public class ChatCommands implements CommandExecutor { } if (authenticated) { + /*component = new ComponentBuilder(Language.getComponent("chatJoined", ChatColor.GOLD)) + .append(" ") + .append(ChatFormatUtils.formatChatRoom(chatRoom)).color(newRoom.color) + .build();*/ + player.sendMessage(""); manager.setPlayerChatRoom(newRoom, player); - component = new ComponentBuilder(Language.getComponent("chatJoined", ChatColor.GOLD)) - .append(newRoom.id).color(newRoom.color) - .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(newRoom.getInfoComponent()))) - .append(Language.getComponent("chatPlayers", ChatColor.GOLD, newRoom.players.size())) - .build(); + } else { + player.spigot().sendMessage(component); } - player.spigot().sendMessage(component); } } else if (command.getName().equals("chatmanage")) { Player player = (Player) sender; diff --git a/src/main/java/eu/m724/tweaks/chat/ChatListener.java b/src/main/java/eu/m724/tweaks/chat/ChatListener.java index 2875a84..f114e58 100644 --- a/src/main/java/eu/m724/tweaks/chat/ChatListener.java +++ b/src/main/java/eu/m724/tweaks/chat/ChatListener.java @@ -10,6 +10,7 @@ import eu.m724.tweaks.TweaksConfig; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.ComponentBuilder; +import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TranslatableComponent; import net.md_5.bungee.chat.ComponentSerializer; import net.minecraft.network.chat.Component; @@ -25,11 +26,27 @@ import org.bukkit.event.player.PlayerQuitEvent; public class ChatListener implements Listener { private final ChatManager chatManager; - private final boolean localEvents; + private final String defaultRoom = TweaksConfig.getConfig().chatDefaultName(); + private final boolean localEvents = TweaksConfig.getConfig().chatLocalEvents(); + private final int radius; public ChatListener(ChatManager chatManager) { this.chatManager = chatManager; - this.localEvents = TweaksConfig.getConfig().chatLocalEvents(); + if (TweaksConfig.getConfig().chatRadius() < 0) { + radius = 0; + } else { + radius = (int) Math.pow(TweaksConfig.getConfig().chatRadius(), 2); + } + } + + private BaseComponent chatPrefix(ChatRoom room) { + if (room.id.equals(defaultRoom)) + return new TextComponent(); + return ChatFormatUtils.chatRoomPrefixShort(room); + } + + private boolean proximityFor(ChatRoom room) { + return radius > 0 && room.id.equals(defaultRoom); } @EventHandler @@ -37,17 +54,20 @@ public class ChatListener implements Listener { Player player = event.getPlayer(); ChatRoom chatRoom = chatManager.getPlayerChatRoom(player); - BaseComponent[] component = new ComponentBuilder("Chat room: ").color(ChatColor.GOLD) - .append(ChatFormatUtils.formatChatRoom(chatRoom)) - .create(); - player.spigot().sendMessage(component); - if (localEvents) { - chatRoom.broadcast( - new ComponentBuilder(ChatFormatUtils.chatRoomPrefixShort(chatRoom)) - .append(new TranslatableComponent("multiplayer.player.joined", ChatFormatUtils.formatPlayer(player))).color(ChatColor.GREEN) - .create() - ); + var cb = new ComponentBuilder() + .append(chatPrefix(chatRoom)) + .append(new TranslatableComponent("multiplayer.player.joined", ChatFormatUtils.formatPlayer(player))); + + if (proximityFor(chatRoom)) { + chatRoom.broadcastNearCond( + player.getLocation(), radius, + cb.color(ChatColor.GREEN).create(), + cb.color(ChatColor.of("#77AA77")).create() + ); + } else { + chatRoom.broadcast(cb.color(ChatColor.GREEN).create()); + } // remove Minecraft join message event.setJoinMessage(null); @@ -60,11 +80,19 @@ public class ChatListener implements Listener { ChatRoom chatRoom = chatManager.removePlayer(player); if (localEvents) { - chatRoom.broadcast( - new ComponentBuilder(ChatFormatUtils.chatRoomPrefixShort(chatRoom)) - .append(new TranslatableComponent("multiplayer.player.left", ChatFormatUtils.formatPlayer(player))).color(ChatColor.RED) - .create() - ); + var cb = new ComponentBuilder() + .append(chatPrefix(chatRoom)) + .append(new TranslatableComponent("multiplayer.player.left", ChatFormatUtils.formatPlayer(player))); + + if (proximityFor(chatRoom)) { + chatRoom.broadcastNearCond( + player.getLocation(), radius, + cb.color(ChatColor.RED).create(), + cb.color(ChatColor.of("#AA7777")).create() + ); + } else { + chatRoom.broadcast(cb.color(ChatColor.RED).create()); + } // remove Minecraft quit message event.setQuitMessage(null); @@ -82,22 +110,28 @@ public class ChatListener implements Listener { // TODO make players ChatFormatUtils - chatRoom.broadcast( - new ComponentBuilder() - .append(ChatFormatUtils.chatRoomPrefixShort(chatRoom)) - .append(deathMessage) - .create() - ); + var component = new ComponentBuilder() + .append(chatPrefix(chatRoom)) + .append(deathMessage) + .color(ChatColor.YELLOW) + .create(); + + if (proximityFor(chatRoom)) { + chatRoom.broadcastNear(player.getLocation(), radius, component); + } else { + chatRoom.broadcast(component); + } // 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() - ); + if (chatRoom != chatRoom2) { + if (proximityFor(chatRoom)) { + chatRoom2.broadcastNear(killer.getLocation(), radius, component); + } else { + chatRoom2.broadcast(component); + } + } } // remove Minecraft death message @@ -111,12 +145,17 @@ public class ChatListener implements Listener { ChatRoom chatRoom = chatManager.getPlayerChatRoom(player); String message = event.getMessage(); - ComponentBuilder builder = new ComponentBuilder(); - builder.append(ChatFormatUtils.chatRoomPrefixShort(chatRoom)); - builder.append(ChatFormatUtils.formatPlayer(player)).append(": "); - builder.append(message).color(chatRoom.color); + var component = new ComponentBuilder() + .append(chatPrefix(chatRoom)) + .append(ChatFormatUtils.formatPlayer(player)).append(": ") + .append(message).color(chatRoom.color) + .create(); - chatRoom.broadcast(builder.create()); + if (proximityFor(chatRoom)) { + chatRoom.broadcastNear(player.getLocation(), radius, component); + } else { + chatRoom.broadcast(component); + } // remove the original message event.setCancelled(true); diff --git a/src/main/java/eu/m724/tweaks/chat/ChatManager.java b/src/main/java/eu/m724/tweaks/chat/ChatManager.java index 29c2e78..ec4461f 100644 --- a/src/main/java/eu/m724/tweaks/chat/ChatManager.java +++ b/src/main/java/eu/m724/tweaks/chat/ChatManager.java @@ -97,7 +97,7 @@ public class ChatManager { oldRoom.broadcast( new ComponentBuilder() - .append(ChatFormatUtils.chatRoomPrefixShort(chatRoom)) + .append(ChatFormatUtils.chatRoomPrefixShort(oldRoom)) .append(ChatFormatUtils.formatPlayer(player)) .append(" has left the chat room").color(ChatColor.RED) .create() diff --git a/src/main/java/eu/m724/tweaks/chat/ChatRoom.java b/src/main/java/eu/m724/tweaks/chat/ChatRoom.java index 433580f..a659694 100644 --- a/src/main/java/eu/m724/tweaks/chat/ChatRoom.java +++ b/src/main/java/eu/m724/tweaks/chat/ChatRoom.java @@ -9,6 +9,7 @@ package eu.m724.tweaks.chat; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.ComponentBuilder; +import org.bukkit.Location; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; @@ -69,4 +70,20 @@ public class ChatRoom { players.forEach(p -> p.spigot().sendMessage(component)); } + public void broadcastNear(Location source, int diameter, BaseComponent[] component) { + players.forEach(p -> { + double distance = p.getLocation().distanceSquared(source); + if (distance < diameter) p.spigot().sendMessage(component); + }); + } + + public void broadcastNearCond(Location source, int diameter, BaseComponent[] near, BaseComponent[] far) { + players.forEach(p -> { + double distance = p.getLocation().distanceSquared(source); + + if (distance < diameter) p.spigot().sendMessage(near); + else p.spigot().sendMessage(far); + }); + } + } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 1a49d93..a5d2d38 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -49,6 +49,9 @@ chat: localEvents: true # Name of the default / global chatroom defaultName: "global" + # Proximity only works in the global chat room + # Value is in blocks, set it to more than 0 to enable proximity chat + radius: 0 # Compass shown in a text form on the actionbar compass: diff --git a/src/main/resources/strings.properties b/src/main/resources/strings.properties index ad644d5..a7dde07 100644 --- a/src/main/resources/strings.properties +++ b/src/main/resources/strings.properties @@ -19,9 +19,6 @@ chatPasswordProtected = This room is password protected chatWrongPassword = Wrong password chatNoSuchRoom = No room named %s chatAlreadyHere = You're already in this room -# Room name is added at end -chatJoined = Joined chat room: -chatPlayers = %d other players are here # Used when a player joins using the wrong key or no key authKickWrongKey = You're connecting to the wrong server address. You must connect to the one you're registered to.