This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/cat/nyaa/nyaautils/commandwarpper/EsschatListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cat.nyaa.nyaautils.commandwarpper; | ||
|
||
import cat.nyaa.nyaautils.NyaaUtils; | ||
import cat.nyaa.nyaautils.mention.MentionListener; | ||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import net.ess3.api.IEssentials; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.EventPriority; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.PlayerCommandPreprocessEvent; | ||
|
||
import java.util.Collections; | ||
import java.util.UUID; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Stream; | ||
|
||
public class EsschatListener implements Listener { | ||
private final IEssentials ess; | ||
private final NyaaUtils plugin; | ||
private final Cache<UUID, UUID> r; | ||
|
||
public EsschatListener(NyaaUtils pl) { | ||
this.plugin = pl; | ||
this.ess = (IEssentials) plugin.getServer().getPluginManager().getPlugin("Essentials"); | ||
if (plugin.cfg.mention_enable) { | ||
plugin.getServer().getPluginManager().registerEvents(this, plugin); | ||
} | ||
r = CacheBuilder.newBuilder().expireAfterWrite(ess.getSettings().getLastMessageReplyRecipientTimeout(), TimeUnit.SECONDS).build(); | ||
} | ||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
public void onCommandPreProcess(PlayerCommandPreprocessEvent e) throws ExecutionException { | ||
if (plugin.cfg.mention_enable) { | ||
String cmd = e.getMessage().toLowerCase().trim(); | ||
if(Stream.of("/msg ", "/tell ", "/m ", "/t ", "/whisper ").parallel().anyMatch(cmd::startsWith)){ | ||
String[] split = cmd.split(" ", 3); | ||
if(split.length != 3) return; | ||
String recipient = split[1]; | ||
String raw = split[2]; | ||
Player p = Bukkit.getPlayer(recipient); | ||
if(p == null)return; | ||
MentionListener.notify(e.getPlayer(), raw, Collections.singleton(p), plugin); | ||
r.put(e.getPlayer().getUniqueId(), p.getUniqueId()); | ||
r.get(p.getUniqueId(), e.getPlayer()::getUniqueId); | ||
} | ||
|
||
if(Stream.of("/r ", "/reply ").parallel().anyMatch(cmd::startsWith) && ess.getSettings().isLastMessageReplyRecipient() && r.getIfPresent(e.getPlayer().getUniqueId()) != null){ | ||
r.put(e.getPlayer().getUniqueId(), r.getIfPresent(e.getPlayer().getUniqueId())); | ||
String[] split = cmd.split(" ", 2); | ||
if(split.length != 2) return; | ||
Player p = Bukkit.getPlayer(r.getIfPresent(e.getPlayer().getUniqueId())); | ||
if(p == null)return; | ||
MentionListener.notify(e.getPlayer(), null, Collections.singleton(p), plugin); | ||
} | ||
} | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
src/main/java/cat/nyaa/nyaautils/mention/MentionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package cat.nyaa.nyaautils.mention; | ||
|
||
import cat.nyaa.nyaautils.NyaaUtils; | ||
import net.md_5.bungee.api.ChatMessageType; | ||
import net.md_5.bungee.api.chat.TextComponent; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.ChatColor; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.EventPriority; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.AsyncPlayerChatEvent; | ||
import org.bukkit.scheduler.BukkitRunnable; | ||
|
||
import java.util.Iterator; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
// see cat.nyaa.nyaautils.commandwarpper.EsschatListener for /msg | ||
public class MentionListener implements Listener { | ||
final private NyaaUtils plugin; | ||
|
||
public MentionListener(NyaaUtils pl) { | ||
plugin = pl; | ||
plugin.getServer().getPluginManager().registerEvents(this, pl); | ||
} | ||
|
||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
public void AsyncChatEvent(AsyncPlayerChatEvent e) { | ||
if (!plugin.cfg.mention_enable) return; | ||
Runnable r = () -> { // In case if we got an asynchronous event | ||
if (e.getMessage().contains("@")) { | ||
Player sender = e.getPlayer(); | ||
String raw = e.getMessage(); | ||
String rep = raw.replace("@ ", "@"); | ||
Set<Player> playersNotified = Bukkit.getOnlinePlayers().parallelStream() | ||
.filter(p -> rep.contains("@" + p.getName())) | ||
.filter(p -> e.getRecipients().contains(p)) | ||
.collect(Collectors.toSet()); | ||
notify(sender, raw, playersNotified, plugin); | ||
} | ||
}; | ||
if (e.isAsynchronous()) { | ||
Bukkit.getScheduler().runTask(plugin, r); | ||
} else { | ||
r.run(); | ||
} | ||
} | ||
|
||
public static void notify(Player sender, String m, Set<Player> playersNotified, NyaaUtils plugin) { | ||
playersNotified.forEach(p -> { | ||
if(m != null) { | ||
String raw = ChatColor.translateAlternateColorCodes('&', m); | ||
String msg = sender.getDisplayName() + ": " + raw; | ||
switch (plugin.cfg.mention_notification) { | ||
case TITLE: | ||
if (plugin.cfg.mention_blink) { | ||
new BukkitRunnable() { | ||
int c = 3; | ||
|
||
@Override | ||
public void run() { | ||
p.sendTitle(msg, "", 3, 5, 2); | ||
if (--c == 0) this.cancel(); | ||
} | ||
}.runTaskTimer(plugin, 0, 10); | ||
} else { | ||
p.sendTitle(msg, "", 10, 20, 10); | ||
} | ||
break; | ||
case SUBTITLE: | ||
if (plugin.cfg.mention_blink) { | ||
new BukkitRunnable() { | ||
int c = 3; | ||
|
||
@Override | ||
public void run() { | ||
p.sendTitle("", msg, 3, 5, 2); | ||
if (--c == 0) this.cancel(); | ||
} | ||
}.runTaskTimer(plugin, 0, 10); | ||
} else { | ||
p.sendTitle("", msg, 10, 40, 10); | ||
} | ||
break; | ||
case ACTION_BAR: | ||
if (plugin.cfg.mention_blink) { | ||
new BukkitRunnable() { | ||
int c = 3; | ||
|
||
@Override | ||
public void run() { | ||
|
||
p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(sender.getDisplayName() + ": " + ChatColor.COLOR_CHAR + c + ChatColor.COLOR_CHAR + "l" + ChatColor.stripColor(raw))); | ||
if (--c == 0) this.cancel(); | ||
} | ||
}.runTaskTimer(plugin, 0, 20); | ||
} else { | ||
p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(sender.getDisplayName() + ": " + ChatColor.COLOR_CHAR + "l" + raw)); | ||
} | ||
break; | ||
case NONE: | ||
break; | ||
} | ||
} | ||
new BukkitRunnable() { | ||
final Iterator<String> sound = plugin.cfg.mention_sound.iterator(); | ||
final Iterator<Double> pitch = plugin.cfg.mention_pitch.iterator(); | ||
|
||
@Override | ||
public void run() { | ||
p.playSound(p.getEyeLocation(), sound.next(), 1, pitch.next().floatValue()); | ||
if (!sound.hasNext() || !pitch.hasNext()) this.cancel(); | ||
} | ||
}.runTaskTimer(plugin, 0, 5); | ||
}); | ||
} | ||
|
||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/cat/nyaa/nyaautils/mention/MentionNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package cat.nyaa.nyaautils.mention; | ||
|
||
public enum MentionNotification { | ||
TITLE, | ||
SUBTITLE, | ||
ACTION_BAR, | ||
NONE | ||
} |