Core/src/main/java/eu/univento/core/listeners/Chat.java

141 lines
6.3 KiB
Java

package eu.univento.core.listeners;
import eu.univento.commons.player.rank.Rank;
import eu.univento.commons.player.warn.WarnReason;
import eu.univento.core.Core;
import eu.univento.core.api.blocks.MiniBlock;
import eu.univento.core.api.customitems.swords.BestSwordEver;
import eu.univento.core.api.languages.Messages;
import eu.univento.core.api.player.CustomPlayer;
import eu.univento.core.api.server.ServerSettings;
import eu.univento.core.api.shop.ShopItem;
import eu.univento.core.api.shop.ShopMenu;
import eu.univento.core.api.shop.entity.ShopVillager;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* manages chat
*
* @author joethei
* @version 1.0
*/
public class Chat implements Listener {
private List<CustomPlayer> players = new LinkedList<>();
private Map<CustomPlayer, Long> spam = new LinkedHashMap<>();
private Map<CustomPlayer, Integer> spamPlayers = new LinkedHashMap<>();
@EventHandler
public void onChat(AsyncPlayerChatEvent e) {
CustomPlayer p = CustomPlayer.getPlayer(e.getPlayer().getName());
Messages msgs = p.getMessages();
e.setCancelled(true);
p.getDatabasePlayer().isMuted().whenComplete((aBoolean, throwable) -> {
if (aBoolean) {
p.sendMessage(msgs.PREFIX() + msgs.Core_SERVER_MUTED());
} else {
//TODO: remove this, used for debugging
if (e.getMessage().equalsIgnoreCase("bestswordever")) {
new BestSwordEver(p).addItem();
}
if (e.getMessage().equalsIgnoreCase("shopvillager")) {
ShopMenu menu = new ShopMenu(Core.getInstance(), "Shop Villager", ShopItem.TestItem);
new ShopVillager(Core.getInstance(), "Shop Villager", p.getLocation(), Villager.Profession.LIBRARIAN, menu);
}
if (e.getMessage().equalsIgnoreCase("miniblock")) {
new MiniBlock(p.getLocation(), new ItemStack(Material.GRASS), 1);
new MiniBlock(p.getEyeLocation(), new ItemStack(Material.DIAMOND_SWORD), 2);
new MiniBlock(p.getWorld().getSpawnLocation(), new ItemStack(Material.WOOL), 3);
}
if (ServerSettings.isMute()) {
if (!p.getDatabasePlayer().isAllowed(Rank.Sound)) {
e.setCancelled(true);
p.sendMessage(msgs.PREFIX() + msgs.Core_SERVER_MUTED());
}
}
if (players.contains(p)) {
e.setCancelled(true);
p.sendMessage("§cDu musst dich bewegen bevor du chatten kannst");
}
if (System.currentTimeMillis() - spam.get(p) < 5 * 20L) {
e.setCancelled(true);
p.sendMessage("Nicht spammen");
spamPlayers.put(p, spamPlayers.get(p) + 1);
}
if (spamPlayers.get(p) > 100) {
p.warn(WarnReason.SPAM, null, "https://players.univento.eu/" + p.getUniqueId().toString() + "/chatlog");
}
char[] parts = e.getMessage().toCharArray();
int sum = 0;
for (char part : parts) {
sum += part;
}
double ratio = sum / parts.length;
int percent = (int) (100.0D * ratio);
if (percent < 50) {
e.setCancelled(true);
p.sendMessage("§cKeine Caps verwenden");
}
String message = e.getMessage();
if (e.getFormat().contains("%")) message = message.replace("%", "%%");
if (p.getDatabasePlayer().isAllowed(Rank.VIP)) {
if (p.getDatabasePlayer().getSettings().isNicked() && !ServerSettings.isLobby()) {
e.setFormat(p.getDatabasePlayer().getRank().getPrefix() + p.getDatabasePlayer().getRank().getColor() + p.getDisplayName() + p.getDatabasePlayer().getRank().getSuffix() + ChatColor.translateAlternateColorCodes('&', message));
for(CustomPlayer player : Core.getOnlinePlayers()) player.sendMessage(e.getMessage());
} else {
e.setFormat(p.getDatabasePlayer().getRank().getPrefix() + p.getDisplayName() + p.getDatabasePlayer().getRank().getSuffix() + ChatColor.translateAlternateColorCodes('&', message));
for(CustomPlayer player : Core.getOnlinePlayers()) player.sendMessage(e.getMessage());
}
} else {
e.setFormat(p.getDatabasePlayer().getRank().getPrefix() + p.getDisplayName() + p.getDatabasePlayer().getRank().getSuffix() + message);
for(CustomPlayer player : Core.getOnlinePlayers()) player.sendMessage(e.getMessage());
Core.getOnlinePlayers().stream().filter(player -> e.getMessage().toLowerCase().contains(player.getDisplayName().toLowerCase()) || e.getMessage().toLowerCase().contains(player.getName().toLowerCase())).forEach(player -> player.playSound(p.getEyeLocation(), Sound.BLOCK_NOTE_PLING, 100.0F, 1.0F));
}
}
});
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
CustomPlayer p = CustomPlayer.getPlayer(e.getPlayer());
players.add(p);
spamPlayers.put(p, 0);
spam.put(p, System.currentTimeMillis());
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent e) {
CustomPlayer p = CustomPlayer.getPlayer(e.getPlayer());
if (players.contains(p)) players.remove(p);
if (spam.containsKey(p)) spam.remove(p);
}
@EventHandler
public void onPlayerMove(PlayerMoveEvent e) {
CustomPlayer p = CustomPlayer.getPlayer(e.getPlayer());
if (players.contains(p)) players.remove(p);
}
}