Skip to content

Commit

Permalink
📍 Added local chat
Browse files Browse the repository at this point in the history
  • Loading branch information
PainOchoco committed Jul 14, 2022
1 parent 7d43903 commit 6b910d9
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
7 changes: 7 additions & 0 deletions src/main/java/net/laboulangerie/townychat/TownyChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class TownyChat extends JavaPlugin {
private ComponentRenderer componentRenderer;
private TownyChatRenderer townyChatRenderer;
private Channel globalChannel;
private Channel localChannel;

private ArrayList<Listener> listeners;

Expand All @@ -55,6 +56,7 @@ public void onEnable() {
this.saveDefaultConfig();

this.globalChannel = new Channel(ChannelTypes.GLOBAL, null);
this.localChannel = new Channel(ChannelTypes.LOCAL, null);

this.townyAPI = TownyAPI.getInstance();
this.townyUniverse = TownyUniverse.getInstance();
Expand All @@ -65,6 +67,7 @@ public void onEnable() {
this.townyChatRenderer = new TownyChatRenderer();

this.channelManager.addChannel(null, this.globalChannel);
this.channelManager.addChannel(null, this.localChannel);

this.getCommand("chat").setExecutor(new ChatCommands());
this.getCommand("spy").setExecutor(new SpyCommand());
Expand Down Expand Up @@ -131,6 +134,10 @@ public Channel getGlobalChannel() {
return this.globalChannel;
}

public Channel getLocalChannel() {
return this.localChannel;
}

private void registerListeners() {
listeners.forEach(l -> this.getServer().getPluginManager().registerEvents(l, this));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public enum ChannelTypes {
TOWN,
NATION,
GLOBAL
GLOBAL,
LOCAL
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package net.laboulangerie.townychat.listeners;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

import com.palmergames.bukkit.towny.TownyAPI;
Expand All @@ -13,11 +17,14 @@
import com.palmergames.bukkit.towny.object.Town;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;

import io.papermc.paper.chat.ChatRenderer;
import io.papermc.paper.event.player.AsyncChatEvent;
Expand Down Expand Up @@ -97,20 +104,38 @@ public void onPlayerChat(AsyncChatEvent event) {
residents.addAll(nation.getResidents());
break;

case LOCAL:
int radius = TownyChat.PLUGIN.getConfig().getInt("channels.local.radius");
try {
residents.addAll(getNearbyResidents(player, radius).get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
break;

case GLOBAL:
residents.addAll(TownyUniverse.getInstance().getResidents());
default:
break;
}

Set<Player> recipients = residents.stream().map(Resident::getPlayer).filter(Objects::nonNull)
Set<Player> recipients = residents.stream().map(Resident::getPlayer)
// Filter null players
.filter(Objects::nonNull)
.collect(Collectors.toSet());

// Removes players that disabled this channel
recipients.removeIf(
p -> !(chatPlayerManager.getChatPlayer(p).getActiveChannels()
.contains(currentChannel)));

System.out.println(currentChannel.getType().name());
if (currentChannel.getType() != ChannelTypes.LOCAL) {
recipients.removeIf(
p -> chatPlayerManager.getChatPlayer(p).getCurrentChannel().getType() == ChannelTypes.LOCAL);
}

event.viewers().addAll(recipients);
event.viewers().add(Bukkit.getConsoleSender());

Expand Down Expand Up @@ -138,4 +163,33 @@ public void onLeave(PlayerQuitEvent event) {
chatPlayerManager.unloadChatPlayer(player);

}

private CompletableFuture<List<Resident>> getNearbyResidents(Player player, int radius) {
List<Resident> nearbyResidents = new ArrayList<Resident>();
Location playerLocation = player.getLocation();

CompletableFuture<List<Resident>> completableFuture = new CompletableFuture<List<Resident>>();

// Bukkit API can't be accessed asynchronously
new BukkitRunnable() {
@Override
public void run() {
for (Entity entity : player.getWorld().getEntities()) {
if (entity instanceof Player && entity.getLocation().distance(playerLocation) <= radius) {

Player nearbyPlayer = (Player) entity;
ChatPlayer nearbyChatPlayer = chatPlayerManager.getChatPlayer(nearbyPlayer);

if (nearbyChatPlayer.getCurrentChannel().getType() == ChannelTypes.LOCAL) {
nearbyResidents.add(townyAPI.getResident(nearbyPlayer));
}
}
}

completableFuture.complete(nearbyResidents);
}
}.runTaskLater(TownyChat.PLUGIN, 0);

return completableFuture;
}
}
10 changes: 7 additions & 3 deletions src/main/java/net/laboulangerie/townychat/player/ChatPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ChatPlayer {
private Map<ChannelTypes, Channel> channels;
private Set<Channel> activeChannels;

private Boolean isSpying;
private boolean isSpying;

public ChatPlayer(OfflinePlayer player) {
this.uniqueId = player.getUniqueId();
Expand Down Expand Up @@ -67,11 +67,11 @@ public void addChannel(ChannelTypes channelType, Channel channel) {
this.channels.put(channelType, channel);
}

public Boolean isSpying() {
public boolean isSpying() {
return this.isSpying;
}

public void setSpying(Boolean isSpying) {
public void setSpying(boolean isSpying) {
this.isSpying = isSpying;
}

Expand Down Expand Up @@ -121,5 +121,9 @@ private void loadPlayerChannels() {
this.activeChannels.add(globalChannel);
this.channels.put(ChannelTypes.GLOBAL, globalChannel);
this.currentChannel = globalChannel;

Channel localChannel = TownyChat.PLUGIN.getLocalChannel();
this.activeChannels.add(localChannel);
this.channels.put(ChannelTypes.LOCAL, localChannel);
}
}
4 changes: 2 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ channels:
local:
name: "Local Chat"
aliases: ["l"]
format: "<local_prefix> <town><nation><rank> <username> > <orange><message>"
format: "<local_prefix> <town><nation><rank> <username> > <green><message>"
spy_format: "<spy_prefix> <local_prefix> <town><nation> <username> > <dark_purple><message>"
radius: 20

Expand All @@ -44,7 +44,7 @@ channels:
tags:
town_prefix: "<aqua><bold>TC</bold></aqua>"
nation_prefix: "<gold><bold>NC</bold></gold>"
local_prefix: "<orange><bold>LC</bold></gold>"
local_prefix: "<green><bold>LC</bold></green>"
spy_prefix: "<dark_purple><bold>SPY</bold></dark_purple>"

rank: "<papi:townyadvanced_towny_colour><papi:townyadvanced_player_status>"
Expand Down

0 comments on commit 6b910d9

Please sign in to comment.