diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index e8d22c4..684b9dd 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.fpghoti FPChat - 1.1.4 + 1.1.6 src/main/java diff --git a/pom.xml b/pom.xml index e68c05d..8a34b46 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.fpghoti FPChat - 1.1.4 + 1.1.6 spigot-repo @@ -77,7 +77,7 @@ com.zaxxer HikariCP - 3.4.5 + 4.0.3 compile diff --git a/src/main/java/com/fpghoti/fpchatx/chat/ShoutChannel.java b/src/main/java/com/fpghoti/fpchatx/chat/ShoutChannel.java index dfef591..1ceee0f 100644 --- a/src/main/java/com/fpghoti/fpchatx/chat/ShoutChannel.java +++ b/src/main/java/com/fpghoti/fpchatx/chat/ShoutChannel.java @@ -1,12 +1,18 @@ package com.fpghoti.fpchatx.chat; import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import java.util.logging.Level; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.entity.Player; import com.fpghoti.fpchatx.FPChat; +import com.fpghoti.fpchatx.event.ShoutChannelChatEvent; import com.fpghoti.fpchatx.player.FPlayer; public class ShoutChannel extends ChatChannel{ @@ -26,8 +32,8 @@ public class ShoutChannel extends ChatChannel{ * * Many functions here will do nothing. Shout extends ChatChannel * for the sake of treating shouts as a form of chat message. - */ - + */ + public ShoutChannel(FPChat plugin) { super(plugin); this.name = "Shout"; @@ -53,7 +59,7 @@ public class ShoutChannel extends ChatChannel{ // No real reason to use this. It will hush the player if called. // Leaving the shout channel is impossible. Standard channel should // be used if the ability to leave is required. - + p.hush(); } @@ -121,7 +127,7 @@ public class ShoutChannel extends ChatChannel{ public int getRadius() { return 0; } - + @Override public ArrayList getPlayers(){ return FPlayer.getPlayers(); @@ -132,7 +138,7 @@ public class ShoutChannel extends ChatChannel{ // No real reason to use this. It will hush the player if called. // Leaving the shout channel is impossible. Standard channel should // be used if the ability to leave is required. - + p.hush(); } @@ -141,18 +147,28 @@ public class ShoutChannel extends ChatChannel{ // No real reason to use this. It will hush the player if called. // Leaving the shout channel is impossible. Standard channel should // be used if the ability to leave is required. - + p.unhush(); } @Override public void sendMessage(String msg, FPlayer from) { - plugin.log(Level.INFO, "Shout: " + msg); - for(FPlayer p : FPlayer.getPlayers()) { - if(p.isShoutVisible() && !p.isIgnoring(from)) { - p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8[&4&lS&r&8]&r") + msg); + CompletableFuture.runAsync(() -> { + plugin.log(Level.INFO, "Shout: " + msg); + Set recipients = new HashSet(); + for(FPlayer p : FPlayer.getPlayers()) { + if(p.isShoutVisible() && !p.isIgnoring(from)) { + recipients.add(p.getPlayer()); + } } - } + ShoutChannelChatEvent event = new ShoutChannelChatEvent(true, from.getPlayer(), msg, recipients, this); + Bukkit.getPluginManager().callEvent(event); + if(!event.isCancelled()) { + for(Player recipient : recipients) { + recipient.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8[&4&lS&r&8]&r") + msg); + } + } + }); } } diff --git a/src/main/java/com/fpghoti/fpchatx/chat/StandardChannel.java b/src/main/java/com/fpghoti/fpchatx/chat/StandardChannel.java index b70cc4f..08114ca 100644 --- a/src/main/java/com/fpghoti/fpchatx/chat/StandardChannel.java +++ b/src/main/java/com/fpghoti/fpchatx/chat/StandardChannel.java @@ -2,15 +2,20 @@ package com.fpghoti.fpchatx.chat; import java.io.IOException; import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import java.util.logging.Level; import org.apache.commons.lang3.StringUtils; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import com.fpghoti.fpchatx.FPChat; import com.fpghoti.fpchatx.config.ChannelFile; +import com.fpghoti.fpchatx.event.StandardChannelChatEvent; import com.fpghoti.fpchatx.player.FPlayer; import com.fpghoti.fpchatx.util.Util; @@ -322,22 +327,32 @@ public class StandardChannel extends ChatChannel{ @Override public void sendMessage(String msg, FPlayer from) { - Player pf = Util.getEP(from.getName()); - plugin.log(Level.INFO, name + ": " + msg); - for(FPlayer p : FPlayer.getPlayers()) { - if(p.getChannels().contains(name) && !p.isIgnoring(from)) { - if(hasRadius) { - Player pp = Util.getEP(p.getName()); - if(pp.getWorld() == pf.getWorld()) { - if(pp.getLocation().distance(pf.getLocation()) < chatRadius){ - p.sendMessage(msg); + CompletableFuture.runAsync(() -> { + Player pf = Util.getEP(from.getName()); + plugin.log(Level.INFO, name + ": " + msg); + Set recipients = new HashSet(); + for(FPlayer p : FPlayer.getPlayers()) { + if(p.getChannels().contains(name) && !p.isIgnoring(from)) { + if(hasRadius) { + Player pp = Util.getEP(p.getName()); + if(pp.getWorld() == pf.getWorld()) { + if(pp.getLocation().distance(pf.getLocation()) < chatRadius){ + recipients.add(p.getPlayer()); + } } + }else { + recipients.add(p.getPlayer()); } - }else { - p.sendMessage(msg); } } - } + StandardChannelChatEvent event = new StandardChannelChatEvent(true, from.getPlayer(), msg, recipients, this); + Bukkit.getPluginManager().callEvent(event); + if(!event.isCancelled()) { + for(Player recipient : recipients) { + recipient.sendMessage(msg); + } + } + }); } public void update(ChannelFile file) { diff --git a/src/main/java/com/fpghoti/fpchatx/chat/TempChannel.java b/src/main/java/com/fpghoti/fpchatx/chat/TempChannel.java index 7280462..8447785 100644 --- a/src/main/java/com/fpghoti/fpchatx/chat/TempChannel.java +++ b/src/main/java/com/fpghoti/fpchatx/chat/TempChannel.java @@ -1,19 +1,24 @@ package com.fpghoti.fpchatx.chat; import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import java.util.logging.Level; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; import com.fpghoti.fpchatx.FPChat; +import com.fpghoti.fpchatx.event.TempChannelChatEvent; import com.fpghoti.fpchatx.player.FPlayer; public class TempChannel extends ChatChannel{ - + private UUID owner; - + public TempChannel(FPChat plugin, FPlayer owner, String name) { super(plugin); this.name = name; @@ -22,12 +27,12 @@ public class TempChannel extends ChatChannel{ this.distinguishedChatFormat = plugin.getMainConfig().getTempChannelFormat(); this.banned = new ArrayList(); } - + public FPlayer getOwner() { OfflinePlayer p = Bukkit.getOfflinePlayer(owner); return FPlayer.getPlayer(p, !p.isOnline()); } - + public boolean isOwner(FPlayer p) { return p.getUniqueId() == this.owner; } @@ -35,7 +40,7 @@ public class TempChannel extends ChatChannel{ public void setOwner(FPlayer p) { this.owner = p.getUniqueId(); } - + @Override public boolean isTemp() { return true; @@ -96,7 +101,7 @@ public class TempChannel extends ChatChannel{ public void enableWhitelist() { this.isWhitelisted = true; } - + @Override public void disableWhitelist() { this.isWhitelisted = false; @@ -111,7 +116,7 @@ public class TempChannel extends ChatChannel{ public void whitelistRemove(UUID uuid) { this.whitelist.remove(uuid); } - + @Override public void addBanned(FPlayer p) { this.banned.add(p.getUniqueId()); @@ -132,7 +137,7 @@ public class TempChannel extends ChatChannel{ public void setDistinguishedChatFormat(String chatFormat) { this.distinguishedChatFormat = chatFormat; } - + @Override public ArrayList getPlayers(){ ArrayList players = new ArrayList(); @@ -143,15 +148,25 @@ public class TempChannel extends ChatChannel{ } return players; } - + @Override public void sendMessage(String msg, FPlayer from) { - plugin.log(Level.INFO, "[TC] " + name + ": " + msg); - for(FPlayer p : FPlayer.getPlayers()) { - if(p.getTempChannels().contains(name) && !p.isIgnoring(from)) { - p.sendMessage(msg); + CompletableFuture.runAsync(() -> { + plugin.log(Level.INFO, "[TC] " + name + ": " + msg); + Set recipients = new HashSet(); + for(FPlayer p : FPlayer.getPlayers()) { + if(p.getTempChannels().contains(name) && !p.isIgnoring(from)) { + recipients.add(p.getPlayer()); + } } - } + TempChannelChatEvent event = new TempChannelChatEvent(true, from.getPlayer(), msg, recipients, this); + Bukkit.getPluginManager().callEvent(event); + if(!event.isCancelled()) { + for(Player recipient : recipients) { + recipient.sendMessage(msg); + } + } + }); } } diff --git a/src/main/java/com/fpghoti/fpchatx/event/FPChatEvent.java b/src/main/java/com/fpghoti/fpchatx/event/FPChatEvent.java new file mode 100644 index 0000000..b637b2e --- /dev/null +++ b/src/main/java/com/fpghoti/fpchatx/event/FPChatEvent.java @@ -0,0 +1,23 @@ +package com.fpghoti.fpchatx.event; + +import java.util.Set; + +import org.bukkit.entity.Player; +import org.bukkit.event.player.AsyncPlayerChatEvent; + +import com.fpghoti.fpchatx.chat.ChatChannel; + +public abstract class FPChatEvent extends AsyncPlayerChatEvent { + + private ChatChannel channel; + + public FPChatEvent(boolean async, Player who, String message, Set players, ChatChannel channel) { + super(async, who, message, players); + this.channel = channel; + } + + + public ChatChannel getChannel() { + return channel; + }; +} diff --git a/src/main/java/com/fpghoti/fpchatx/event/ShoutChannelChatEvent.java b/src/main/java/com/fpghoti/fpchatx/event/ShoutChannelChatEvent.java new file mode 100644 index 0000000..88b6761 --- /dev/null +++ b/src/main/java/com/fpghoti/fpchatx/event/ShoutChannelChatEvent.java @@ -0,0 +1,22 @@ +package com.fpghoti.fpchatx.event; + +import java.util.Set; + +import org.bukkit.entity.Player; + +import com.fpghoti.fpchatx.chat.ShoutChannel; + +public class ShoutChannelChatEvent extends FPChatEvent { + + private ShoutChannel channel; + + public ShoutChannelChatEvent(boolean async, Player who, String message, Set players, ShoutChannel channel) { + super(async, who, message, players, channel); + this.channel = channel; + } + + public ShoutChannel getChannel() { + return channel; + } + +} diff --git a/src/main/java/com/fpghoti/fpchatx/event/StandardChannelChatEvent.java b/src/main/java/com/fpghoti/fpchatx/event/StandardChannelChatEvent.java new file mode 100644 index 0000000..0006141 --- /dev/null +++ b/src/main/java/com/fpghoti/fpchatx/event/StandardChannelChatEvent.java @@ -0,0 +1,22 @@ +package com.fpghoti.fpchatx.event; + +import java.util.Set; + +import org.bukkit.entity.Player; + +import com.fpghoti.fpchatx.chat.StandardChannel; + +public class StandardChannelChatEvent extends FPChatEvent { + + private StandardChannel channel; + + public StandardChannelChatEvent(boolean async, Player who, String message, Set players, StandardChannel channel) { + super(async, who, message, players, channel); + this.channel = channel; + } + + public StandardChannel getChannel() { + return this.channel; + } + +} diff --git a/src/main/java/com/fpghoti/fpchatx/event/TempChannelChatEvent.java b/src/main/java/com/fpghoti/fpchatx/event/TempChannelChatEvent.java new file mode 100644 index 0000000..032ee8e --- /dev/null +++ b/src/main/java/com/fpghoti/fpchatx/event/TempChannelChatEvent.java @@ -0,0 +1,22 @@ +package com.fpghoti.fpchatx.event; + +import java.util.Set; + +import org.bukkit.entity.Player; + +import com.fpghoti.fpchatx.chat.TempChannel; + +public class TempChannelChatEvent extends FPChatEvent { + + private TempChannel channel; + + public TempChannelChatEvent(boolean async, Player who, String message, Set players, TempChannel channel) { + super(async, who, message, players, channel); + this.channel = channel; + } + + public TempChannel getChannel() { + return channel; + } + +} diff --git a/src/main/java/com/fpghoti/fpchatx/listener/PlayerListener.java b/src/main/java/com/fpghoti/fpchatx/listener/PlayerListener.java index 9e92068..948c1e5 100644 --- a/src/main/java/com/fpghoti/fpchatx/listener/PlayerListener.java +++ b/src/main/java/com/fpghoti/fpchatx/listener/PlayerListener.java @@ -13,12 +13,33 @@ import org.bukkit.event.player.PlayerQuitEvent; import com.fpghoti.fpchatx.FPChat; import com.fpghoti.fpchatx.chat.ChatChannel; +import com.fpghoti.fpchatx.event.FPChatEvent; import com.fpghoti.fpchatx.player.FPlayer; public class PlayerListener implements Listener { private boolean enabled = true; + // @EventHandler (priority = EventPriority.HIGH) + // public void onPlayerChat(AsyncPlayerChatEvent event) { + // if(enabled) { + // if (event.isCancelled()) { + // return; + // } + // Player sender = event.getPlayer(); + // FPlayer p = FPlayer.getPlayer(sender); + // if(p.toTalk() && p.getTalkChannel() != null) { + // p.setTalk(false); + // ChatChannel c = p.getTalkChannel(); + // p.setTalkChannel(null); + // p.chat(c, event.getMessage()); + // }else { + // p.chat(event.getMessage()); + // } + // event.setCancelled(true); + // } + // } + @EventHandler (priority = EventPriority.HIGH) public void onPlayerChat(AsyncPlayerChatEvent event) { if(enabled) { @@ -27,15 +48,17 @@ public class PlayerListener implements Listener { } Player sender = event.getPlayer(); FPlayer p = FPlayer.getPlayer(sender); - if(p.toTalk() && p.getTalkChannel() != null) { - p.setTalk(false); - ChatChannel c = p.getTalkChannel(); - p.setTalkChannel(null); - p.chat(c, event.getMessage()); - }else { - p.chat(event.getMessage()); + if(!(event instanceof FPChatEvent)) { + if(p.toTalk() && p.getTalkChannel() != null) { + p.setTalk(false); + ChatChannel c = p.getTalkChannel(); + p.setTalkChannel(null); + p.chat(c, event.getMessage()); + }else { + p.chat(event.getMessage()); + } + event.setCancelled(true); } - event.setCancelled(true); } } diff --git a/src/main/java/com/fpghoti/fpchatx/mysql/MySQLConnection.java b/src/main/java/com/fpghoti/fpchatx/mysql/MySQLConnection.java index 58dce2b..ed448d1 100644 --- a/src/main/java/com/fpghoti/fpchatx/mysql/MySQLConnection.java +++ b/src/main/java/com/fpghoti/fpchatx/mysql/MySQLConnection.java @@ -29,7 +29,7 @@ public class MySQLConnection{ port = config.getPort(); hikari = new HikariDataSource(); hikari.setMaximumPoolSize(10); - hikari.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource"); + hikari.setDataSourceClassName("com.mysql.cj.jdbc.MysqlDataSource"); hikari.addDataSourceProperty("serverName", host); hikari.addDataSourceProperty("user", user); hikari.addDataSourceProperty("password", password); @@ -55,7 +55,9 @@ public class MySQLConnection{ e.printStackTrace(); }finally { try { - connection.close(); + if(connection != null) { + connection.close(); + } } catch (SQLException e) { e.printStackTrace(); } diff --git a/src/main/java/com/fpghoti/fpchatx/player/FPlayer.java b/src/main/java/com/fpghoti/fpchatx/player/FPlayer.java index 372bd1e..9a67a1c 100644 --- a/src/main/java/com/fpghoti/fpchatx/player/FPlayer.java +++ b/src/main/java/com/fpghoti/fpchatx/player/FPlayer.java @@ -441,6 +441,21 @@ public class FPlayer { channel.sendMessage(formatChat(channel,msg), this); } +// public void chat(String msg) { +// if(toShout) { +// toShout = false; +// ChatChannel.getShout().sendMessage(formatChat(ChatChannel.getShout(),msg), this); +// if(FPChat.getPlugin().getMainConfig().shoutCooldownEnabled()) { +// shoutCooldown = FPChat.getPlugin().getMainConfig().getShoutSeconds(); +// } +// }else if(tempspeak) { +// getPrimaryTempChannel().sendMessage(formatChat(getPrimaryTempChannel(),msg), this); +// }else { +// getPrimaryChannel().sendMessage(formatChat(msg), this); +// } +// } + + public void chat(String msg) { if(toShout) { toShout = false; @@ -454,7 +469,6 @@ public class FPlayer { getPrimaryChannel().sendMessage(formatChat(msg), this); } } - public boolean shout(String message) { if(!hushed && FPChat.getPlugin().getMainConfig().shoutCooldownEnabled() && shoutCooldown > 0) { int i = shoutCooldown;