@ -26,59 +26,69 @@ public class HushAsyncChatListener implements Listener {
}
@EventHandler ( priority = EventPriority . HIGH , ignoreCancelled = true )
public boolean onChat ( AsyncPlayerChatEvent event ) {
public void onChat ( AsyncPlayerChatEvent event ) {
String chatMessage = event . getMessage ( ) ;
Player sender = event . getPlayer ( ) ;
for ( String permission : plugin . getWatchLists ( ) . keySet ( ) ) {
if ( sender . hasPermission ( "hush." + permission ) ) {
for ( WatchCategory category : plugin . getWatchLists ( ) . get ( permission ) ) {
checkMessage ( category , permission , sender , chatMessage ) ;
boolean is_match = checkMessage ( category , permission , sender , chatMessage ) ;
// If the message matched, and the category requires the event to be canceled
if ( is_match & & category . getCancelMsg ( ) ) {
// cancel event
event . setCancelled ( true ) ;
}
}
}
}
return true ;
}
private void runCommand ( String command ) {
Bukkit . getScheduler ( ) . runTask ( plugin , ( ) - > Bukkit . dispatchCommand ( Bukkit . getConsoleSender ( ) , command ) ) ;
}
private void checkMessage ( WatchCategory category , String watchList , Player player , String chatMessage ) {
private boolean checkMessage ( WatchCategory category , String watchList , Player player , String chatMessage ) {
Matcher match = category . checkPatterns ( chatMessage ) ;
if ( match ! = null ) {
String playerName = player . getName ( ) ;
String webhookURL = plugin . getConfig ( ) . getString ( "webhook" ) ;
for ( String command : category . getCommands ( ) ) {
runCommand ( command . replace ( "{player}" , playerName ) ) ;
}
if ( match = = null ) {
// message was not a match
return false ;
}
plugin . log ( String . format ( "%s matched filter in %s.%s" , playerName , watchList , category . getCategoryName ( ) ) ) ;
if ( webhookURL ! = null & & ! plugin . isInCoolDown ( player ) ) {
plugin . addCoolDown ( player ) ;
chatMessage = match . replaceAll ( "**$0**" ) ;
String message = Javacord . escapeFormat ( playerName ) + " said: " + chatMessage ;
Embed embed = new Embed ( )
. color ( 0xC70039 )
. description ( message )
. addField ( new Field ( "Watchlist" , String . format ( "`%s`" , watchList ) ) )
. addField ( new Field ( "Category" , String . format ( "`%s`" , category . getCategoryName ( ) ) ) )
. author ( new Author ( playerName , "" , String . format ( "https://minotar.net/helm/%s/100.png" , playerName ) , "" ) )
. timestamp ( OffsetDateTime . now ( ) ) ;
Webhook webhook = new Webhook ( "@here" , embed ) ;
this . plugin . getServer ( ) . getScheduler ( ) . runTaskAsynchronously ( this . plugin , ( ) - > {
try {
Javacord . sendWebhook ( webhookURL , webhook ) ;
} catch ( Exception e ) {
this . plugin . log ( "Webhook failed to send." ) ;
}
} ) ;
}
String playerName = player . getName ( ) ;
String webhookURL = plugin . getConfig ( ) . getString ( "webhook" ) ;
for ( String command : category . getCommands ( ) ) {
runCommand ( command . replace ( "{player}" , playerName ) ) ;
}
plugin . log ( String . format ( "%s matched filter in %s.%s" , playerName , watchList , category . getCategoryName ( ) ) ) ;
if ( webhookURL ! = null & & ! plugin . isInCoolDown ( player ) ) {
plugin . addCoolDown ( player ) ;
chatMessage = match . replaceAll ( "**$0**" ) ;
String message = Javacord . escapeFormat ( playerName ) + " said: " + chatMessage ;
Embed embed = new Embed ( )
. color ( 0xC70039 )
. description ( message )
. addField ( new Field ( "Watchlist" , String . format ( "`%s`" , watchList ) ) )
. addField ( new Field ( "Category" , String . format ( "`%s`" , category . getCategoryName ( ) ) ) )
. author ( new Author ( playerName , "" , String . format ( "https://minotar.net/helm/%s/100.png" , playerName ) , "" ) )
. timestamp ( OffsetDateTime . now ( ) ) ;
Webhook webhook = new Webhook ( "@here" , embed ) ;
this . plugin . getServer ( ) . getScheduler ( ) . runTaskAsynchronously ( this . plugin , ( ) - > {
try {
Javacord . sendWebhook ( webhookURL , webhook ) ;
} catch ( Exception e ) {
this . plugin . log ( "Webhook failed to send." ) ;
}
} ) ;
}
return true ;
}
}