Skip to content
Snippets Groups Projects
Verified Commit a3c95d4d authored by TheJoeCoder's avatar TheJoeCoder
Browse files

Add main features


Signed-off-by: default avatarTheJoeCoder <joe@radialbog9.uk>
parent d1dac0c3
No related branches found
No related tags found
No related merge requests found
Pipeline #40 canceled
package net.dappergoose.simplechatfilter;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.ChatEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;
import net.md_5.bungee.event.EventHandler;
public final class SimpleChatFilter extends Plugin {
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public final class SimpleChatFilter extends Plugin implements Listener {
public void makeConfig() throws IOException {
// Create plugin config folder if it doesn't exist
if (!getDataFolder().exists()) {
getLogger().info("Created config folder: " + getDataFolder().mkdir());
}
File configFile = new File(getDataFolder(), "config.yml");
// Copy default config if it doesn't exist
if (!configFile.exists()) {
FileOutputStream outputStream = new FileOutputStream(configFile); // Throws IOException
InputStream in = getResourceAsStream("config.yml"); // This file must exist in the jar resources folder
in.transferTo(outputStream); // Throws IOException
}
}
private Configuration pluginConfig;
@Override
public void onEnable() {
// Plugin startup logic
try {
makeConfig();
pluginConfig =
ConfigurationProvider.getProvider(YamlConfiguration.class)
.load(new File(getDataFolder(), "config.yml"));
} catch (IOException e) {
e.printStackTrace();
}
getProxy().getPluginManager().registerListener(this, this);
}
@Override
public void onDisable() {
// Plugin shutdown logic
getProxy().getPluginManager().unregisterListener(this);
}
@EventHandler
public void onChatEvent(ChatEvent e) {
if(!(e.getSender() instanceof ProxiedPlayer)) return;
ProxiedPlayer p = (ProxiedPlayer) e.getSender();
if (e.isCommand()) // TODO message commands
return; // ignore commands
String message = e.getMessage();
boolean violated = false;
String matchedCategory = null;
for(String category : pluginConfig.getSection("categories").getKeys()) {
if(p.hasPermission("chatfilter.bypass." + category)) continue;
for(String word : pluginConfig.getStringList("categories." + category + ".blocked")) {
if(message.contains(word)) {
violated = true;
matchedCategory = category;
break;
}
}
if(violated) break;
}
if(violated) {
e.setCancelled(true);
sendStaffMessage(new TextComponent(
ChatColor.translateAlternateColorCodes('&',
pluginConfig.getString("staff-message")
.replace("{player}", p.getDisplayName())
.replace("{category}", matchedCategory)
.replace("{message}", message)
.replace("{server}", p.getServer().getInfo().getName())
)
));
p.sendMessage(new TextComponent(
ChatColor.translateAlternateColorCodes('&',
pluginConfig.getString("categories." + matchedCategory + ".message")
)
));
for(String command : pluginConfig.getStringList("categories." + matchedCategory + ".commands")) {
getProxy().getPluginManager().dispatchCommand(getProxy().getConsole(), command.replace("{player}", p.getName()));
}
}
}
private void sendStaffMessage(BaseComponent component) {
for(ProxiedPlayer p : getProxy().getPlayers()) {
if(p.hasPermission("chatfilter.staff")) {
p.sendMessage(component);
}
}
}
}
categories:
# A list of categories - !! most severe first !!
advertising-pfcloud:
blocked:
- a friendly bot designed to join servers
- If you are searching for a good server hosting provider
- https://shop.pfcloud.io
commands:
- ipban {username} Bot/Advertising -s
message: '&cPlease do not advertise!'
staff-message: '&a[ChatFilter] &c{username} &7 broke rule &c{category} &7on server &c{server}&7.'
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment