wip: add listeners

This commit is contained in:
ColdDev 2022-10-24 01:05:28 +01:00
parent a62fca9405
commit e4af236671
5 changed files with 226 additions and 10 deletions

View File

@ -1,11 +1,41 @@
package uranium; package uranium;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.messaging.PluginMessageListener;
public class Main extends JavaPlugin { import uranium.listeners.PlayerListener;
import uranium.user.*;
public class Main extends JavaPlugin implements PluginMessageListener {
public void onEnable() { public void onEnable() {
setupConfig(); setupConfig();
setupPluginChannels();
}
public void onDisable() {
getServer().getMessenger().unregisterOutgoingPluginChannel(this);
getServer().getMessenger().unregisterIncomingPluginChannel(this);
}
private void setupListeners() {
getServer().getPluginManager().registerEvents(new PlayerListener(this), this);
}
private void setupCommands() {
}
private void setupTasks() {
}
private void setupPluginChannels() {
getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
} }
private void setupConfig() { private void setupConfig() {
@ -15,4 +45,28 @@ public class Main extends JavaPlugin {
getLogger().info("Done!"); getLogger().info("Done!");
} }
public void reloadConfig() {
super.reloadConfig();
}
@SuppressWarnings("UnstableApiUsage")
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
if (!channel.equals("BungeeCord")) return;
User user = UserManager.getUser(player);
if (user == null) return;
ByteArrayDataInput in = ByteStreams.newDataInput(message);
String subChannel = in.readUTF();
if (subChannel.equals("IP")) {
String ip = in.readUTF();
user.setIP(ip);
} else if (subChannel.equals("EAG|GetDomain")) {
boolean domainRecognized = in.readBoolean();
String domain = in.readUTF();
user.setDomain(domainRecognized ? domain : "null");
}
}
} }

View File

@ -0,0 +1,64 @@
package uranium.listeners;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import uranium.Main;
import uranium.user.User;
import uranium.user.UserManager;
import uranium.util.StringUtil;
public class ChatListener implements Listener {
private final Main plugin;
public ChatListener(Main plugin) {
this.plugin = plugin;
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onPlayerChat(AsyncPlayerChatEvent event) {
User user = UserManager.getUser(event.getPlayer());
String message = event.getMessage();
if (user.hasPermission("uranium.bypass")) return;
if (plugin.getConfig().getBoolean("chat-filter.double-posting")
&& user.getLastMessage() != null
&& user.getLastMessage().equalsIgnoreCase(message)) {
user.sendMessage("&cPlease do not double post.");
event.setCancelled(true);
return;
}
if (plugin.getConfig().getBoolean("message-delay")
&& user.getLastMessageMS() != null
&& (System.currentTimeMillis() - user.getLastMessageMS()) < plugin.getConfig().getInt("chat-filter.message-delay")) {
user.sendMessage("&cYou're sending messages too fast! Slow down.");
event.setCancelled(true);
return;
}
user.setLastMessage(message);
user.setLastMessageMS(System.currentTimeMillis());
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
public void onPlayerChat2(AsyncPlayerChatEvent event) {
User user = UserManager.getUser(event.getPlayer());
String message = event.getMessage();
if (!plugin.getConfig().getBoolean("chat-filter.enabled")) return;
if (!StringUtil.validateMessage(message)) {
event.setCancelled(true);
user.sendMessage("&cYour message contains blacklisted words.");
return;
}
if (StringUtil.checkCaps(message)) {
event.setMessage(StringUtil.capitalize(message.toLowerCase()));
user.sendMessage("&cDon't use so many caps!");
}
}
}

View File

@ -0,0 +1,50 @@
package uranium.listeners;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import uranium.Main;
import uranium.user.User;
import uranium.user.UserManager;
public class PlayerListener implements Listener {
private final Main plugin;
public PlayerListener(Main plugin) {
this.plugin = plugin;
}
@EventHandler
public void onPlayerLogin(PlayerLoginEvent event) {
if (!plugin.getConfig().getBoolean("chat-filter.enabled")) return;
String username = event.getPlayer().getName();
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
User user = UserManager.addUser(event.getPlayer());
UserManager.getUsers().forEach((u) -> {
if (u.isVanished())
if (!user.hasPermission("uranium.vanish"))
user.getPlayer().hidePlayer(u.getPlayer());
});
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
UserManager.removeUser(event.getPlayer());
}
@EventHandler
public void onPlayerKick(PlayerKickEvent event) {
UserManager.removeUser(event.getPlayer());
}
}

View File

@ -19,7 +19,6 @@ public class User {
private String ip; private String ip;
private String domain; private String domain;
private boolean vanished; private boolean vanished;
private boolean staffChat;
private boolean commandSpy; private boolean commandSpy;
public User(Player player) { public User(Player player) {
@ -85,14 +84,6 @@ public class User {
else UserManager.getUsers().forEach((user) -> user.getPlayer().showPlayer(player)); else UserManager.getUsers().forEach((user) -> user.getPlayer().showPlayer(player));
} }
public boolean isStaffChat() {
return staffChat;
}
public void setStaffChat(boolean staffChat) {
this.staffChat = staffChat;
}
public boolean isCommandSpy() { public boolean isCommandSpy() {
return commandSpy; return commandSpy;
} }

View File

@ -1,11 +1,68 @@
package uranium.util; package uranium.util;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;
import uranium.Main;
import java.util.List;
public class StringUtil { public class StringUtil {
private static final Main plugin;
static {
plugin = JavaPlugin.getPlugin(Main.class);
}
private static List<String> getBannedWords() {
return plugin.getConfig().getStringList("chat-filter.banned-words");
}
public static String cc(String message) { public static String cc(String message) {
return ChatColor.translateAlternateColorCodes('&', message); return ChatColor.translateAlternateColorCodes('&', message);
} }
private static String removeCrap(String s) {
return s
.replace(" ", "")
.replace("_", "")
.replace("*", "")
.replace(".", "")
.replace("/", ""); // TODO: Replace with regex
}
public static boolean validateUsername(String username) {
username = removeCrap(username);
for (String word : getBannedWords())
if (username.toLowerCase().contains(word.toLowerCase())) return false;
return true;
}
public static boolean validateMessage(String message) {
message = removeCrap(message);
for (String word : getBannedWords())
if (message.toLowerCase().contains(word.toLowerCase())) return false;
return true;
}
public static String capitalize(String message) {
return Character.toUpperCase(message.charAt(0)) + message.substring(1);
}
public static boolean checkCaps(String message) {
message = removeCrap(message);
if (message.length() <= 5) return false;
double threshold = 50;
double upperCase = 0;
char[] chars = message.toCharArray();
for(char c : chars)
if (Character.isUpperCase(c))
upperCase++;
double upperCasePercent = (upperCase * 100) / message.length();
return upperCasePercent >= threshold;
}
} }