Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
mention #44
Browse files Browse the repository at this point in the history
  • Loading branch information
Librazy committed Mar 17, 2018
1 parent 63c90ce commit 585eb77
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/main/java/cat/nyaa/nyaautils/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import cat.nyaa.nyaautils.enchant.EnchantSrcConfig;
import cat.nyaa.nyaautils.lootprotect.LootProtectMode;
import cat.nyaa.nyaautils.mailbox.MailboxLocations;
import cat.nyaa.nyaautils.mention.MentionNotification;
import cat.nyaa.nyaautils.particle.ParticleConfig;
import cat.nyaa.nyaautils.particle.ParticleLimit;
import cat.nyaa.nyaautils.particle.ParticleType;
Expand Down Expand Up @@ -89,7 +90,7 @@ public class Configuration extends PluginConfigure {
@Serializable(name = "elytra_enhance.fuel_notify_threshold", alias = "elytra_fuel_notify")
public int elytra_fuel_notify = 10;
@Serializable(name = "elytra_enhance.disabled_worlds", alias = "disabled_world")
public List<String> disabled_world = new ArrayList<String>(Arrays.asList("world1", "world2"));
public List<String> disabled_world = new ArrayList<>(Arrays.asList("world1", "world2"));
/* Mailing System */
@Serializable(name = "mail.handFee")
public int mailHandFee = 10;
Expand Down Expand Up @@ -189,6 +190,17 @@ public class Configuration extends PluginConfigure {
@Serializable(name = "signedit.max_length")
public int signedit_max_length = 15;

@Serializable(name = "mention.enable")
public Boolean mention_enable = true;
@Serializable(name = "mention.sound")
public List<String> mention_sound = new ArrayList<>(Collections.singletonList("entity.experience_orb.pickup"));
@Serializable(name = "mention.pitch")
public List<Double> mention_pitch = new ArrayList<>(Collections.singletonList(1d));
@Serializable(name = "mention.notification")
public MentionNotification mention_notification = MentionNotification.ACTION_BAR;
@Serializable(name = "mention.blink")
public Boolean mention_blink = true;

@Serializable
public Material expCapsuleType = Material.EXP_BOTTLE;

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/cat/nyaa/nyaautils/NyaaUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import cat.nyaa.nyaacore.component.ComponentNotAvailableException;
import cat.nyaa.nyaacore.component.ISystemBalance;
import cat.nyaa.nyaacore.component.NyaaComponent;
import cat.nyaa.nyaautils.commandwarpper.EsschatListener;
import cat.nyaa.nyaautils.commandwarpper.Teleport;
import cat.nyaa.nyaautils.dropprotect.DropProtectListener;
import cat.nyaa.nyaautils.elytra.ElytraEnhanceListener;
import cat.nyaa.nyaautils.elytra.FuelManager;
import cat.nyaa.nyaautils.exhibition.ExhibitionListener;
import cat.nyaa.nyaautils.lootprotect.LootProtectListener;
import cat.nyaa.nyaautils.mailbox.MailboxListener;
import cat.nyaa.nyaautils.mention.MentionListener;
import cat.nyaa.nyaautils.particle.ParticleListener;
import cat.nyaa.nyaautils.particle.ParticleTask;
import cat.nyaa.nyaautils.realm.RealmListener;
Expand Down Expand Up @@ -44,6 +46,8 @@ public class NyaaUtils extends JavaPlugin {
public ParticleListener particleListener;
public ParticleTask particleTask;
public SignEditListener signEditListener;
public MentionListener mentionListener;
public EsschatListener esschatListener;

@Override
public void onEnable() {
Expand Down Expand Up @@ -75,6 +79,8 @@ public void onEnable() {
particleTask = new ParticleTask(this);
particleListener = new ParticleListener(this);
signEditListener = new SignEditListener(this);
mentionListener = new MentionListener(this);
esschatListener = new EsschatListener(this);
}

@Override
Expand Down
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 src/main/java/cat/nyaa/nyaautils/mention/MentionListener.java
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);
});
}


}
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
}

0 comments on commit 585eb77

Please sign in to comment.