Hi. This is a way to explain a simple (chat) filter with few mor utilities.
I will explain this:
Lets start with the welcome message:
Now the filter:
Now onEnable/Disable methods:
Broadcasts, this you should check it here
Player (name it different, not p.)
Custom CE help (not default, helpful on servers with custom help, like cube)
//another line
Hope you like, if you do not understand it: Java OOP :p
I will explain this:
Code:
package ChatEssentials;
import java.util.List;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.v1_9_R1.entity.CraftPlayer;
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.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import net.minecraft.server.v1_9_R1.IChatBaseComponent.ChatSerializer;
import net.minecraft.server.v1_9_R1.PacketPlayOutTitle;
import net.minecraft.server.v1_9_R1.PacketPlayOutTitle.EnumTitleAction;
import net.minecraft.server.v1_9_R1.PlayerConnection;
public class Main extends JavaPlugin implements Listener {
public final Logger logger = Logger.getLogger("Minecraft");
public static String chatcolor(String s){
return ChatColor.translateAlternateColorCodes('&', s);
}
@EventHandler(priority=EventPriority.HIGHEST)
public void handleJoin(PlayerJoinEvent event)
{
Player p = event.getPlayer();
event.setJoinMessage(chatcolor(getConfig().getString("WelcomeMessage")));
}
List<String> blockedMessages = getConfig().getStringList("Blacklist");
@EventHandler
public void playerChatEvent(AsyncPlayerChatEvent event) {
for (String blockedMessage : blockedMessages) {
if(event.getMessage().contains(blockedMessage)) {
event.setCancelled(true);
event.getPlayer().sendMessage("" + ChatColor.RED + ChatColor.BOLD + getConfig().getString("BlockedMessageERROR") );
return;
}
}
}
public void onEnable()
{
getServer().getPluginManager().registerEvents(this, this);
getConfig().options().copyDefaults(true);
saveConfig();
}
public void onDisable()
{
}
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
Player player = (Player)sender;
if (cmd.getName().equalsIgnoreCase("bc"))
if (player.hasPermission("CE.Broadcast")) {
int laengeArray = args.length;
int indexArray = 0;
String text = "";
for (int i = 0; i < args.length; i++) {
text = text + args[i] + " ";
}
Bukkit.broadcastMessage(ChatColor.DARK_RED + "[Alert] " + ChatColor.RED + text);
return true;
}
if (cmd.getName().equalsIgnoreCase("ChatEssentials")) {
player.sendMessage(ChatColor.DARK_BLUE + "----------------------------------------");
player.sendMessage(ChatColor.BLUE + "ChatEssentials v2 by Hangar555");
player.sendMessage(ChatColor.GREEN + "Commands:");
player.sendMessage(ChatColor.DARK_GREEN + "/ChatEssentials" + ChatColor.GOLD + ":" + ChatColor.GREEN + " Displays this message");
player.sendMessage(ChatColor.DARK_GREEN + "/Broadcast" + ChatColor.GOLD + ":" + ChatColor.GREEN + " Broadcast a desired message");
player.sendMessage(ChatColor.GREEN + "Info:");
player.sendMessage(ChatColor.DARK_GREEN + "This plugin is able to edit player messages, to prevent curse words");
player.sendMessage(ChatColor.DARK_GREEN + "It can also intercept heavy messages");
player.sendMessage(ChatColor.DARK_GREEN + "You are not allowed to edit and/or copy the plugin without the developer permission");
player.sendMessage(ChatColor.DARK_BLUE + "----------------------------------------");
return true;
}
return false;
}
}
Lets start with the welcome message:
Code:
@EventHandler(priority=EventPriority.HIGHEST) //this sets the event priority, in this case the highest
public void handleJoin(PlayerJoinEvent event) //Player join event: When the player joins the server
{
Player p = event.getPlayer(); //sets the player p = event.getPlayer. Simple
event.setJoinMessage(chatcolor(getConfig().getString("WelcomeMessage"))); //sets the join message from the config. Can also be null or be write here
Code:
List<String> blockedMessages = getConfig().getStringList("Blacklist"); //get the string list called "blacklist" from the config
@EventHandler
public void playerChatEvent(AsyncPlayerChatEvent event) { //this is the "Chat" event
for (String blockedMessage : blockedMessages) { //gets the String
if(event.getMessage().contains(blockedMessage)) { //check if the sentece contains a blocked word
event.setCancelled(true); //cacels the message
event.getPlayer().sendMessage("" + ChatColor.RED + ChatColor.BOLD + getConfig().getString("BlockedMessageERROR") ); //gets the error from the config.
return;
}
}
}
Now onEnable/Disable methods:
Code:
public void onEnable()
{
getServer().getPluginManager().registerEvents(this, this); //register the events
getConfig().options().copyDefaults(true); //get the config and copy it from the jar
saveConfig(); //save the config
}
public void onDisable()
//nothing
{
}
Broadcasts, this you should check it here
Player (name it different, not p.)
Code:
Player player = (Player)sender; //the player is the sender, WOW
Code:
//you alredy have the onCommand method
if (cmd.getName().equalsIgnoreCase("ChatEssentials")) { //command "executor"
if(player.hasPermission("CE.help")) //if the player has the required permissions
player.sendMessage(ChatColor.DARK_BLUE + "----------------------------------------"); //just a line
player.sendMessage(ChatColor.BLUE + "ChatEssentials v2 by Hangar555"); //I'm famous!
player.sendMessage(ChatColor.GREEN + "Commands:");
player.sendMessage(ChatColor.DARK_GREEN + "/ChatEssentials" + ChatColor.GOLD + ":" + ChatColor.GREEN + " Displays this message"); //useless message
player.sendMessage(ChatColor.DARK_GREEN + "/Broadcast" + ChatColor.GOLD + ":" + ChatColor.GREEN + " Broadcast a desired message"); //It have a use, I suppose
player.sendMessage(ChatColor.GREEN + "Info:"); //Wow, a word
player.sendMessage(ChatColor.DARK_GREEN + "This plugin is able to edit player messages, to prevent curse words"); //Same as in spigot web. Awesome
player.sendMessage(ChatColor.DARK_GREEN + "You are not allowed to edit and/or copy the plugin without the developer permission"); //Pls
player.sendMessage(ChatColor.DARK_BLUE + "----------------------------------------");
Hope you like, if you do not understand it: Java OOP :p