Hi I'm trying to setup a hostname pattern for a minecraft-server plugin. It will look through the arguments of the chat message and try to find any possible hostnames. The code works unless the message only contains one word.
Here's the class that checks the message event:
package com.carlgo11.preventip.player;
import com.carlgo11.preventip.Main;
import java.util.regex.Matcher;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
public class ChatEvent implements Listener {
Main plugin;
public ChatEvent(Main plug)
{
super();
this.plugin = plug;
}
@EventHandler
public void PlayerChat(AsyncPlayerChatEvent e)
{
if (!plugin.getConfig().getBoolean("ignore-chat")) {
Player p = e.getPlayer();
String msg = e.getMessage();
String[] args = msg.split(" ");
Boolean match = false;
if (!p.hasPermission("preventip.ignore")) {
for (int i = 0; i < args.length; i++) {
Matcher hnre = plugin.hostnamePattern.matcher(msg.toString());
Boolean hnrematch = hnre.find();
if (hnrematch) {
match = true;
break;
}
}
if (match) {
e.setCancelled(true);
Action.action(p, plugin);
}
}
}
}
}
And here's the pattern
Pattern hostnamePattern = Pattern.compile("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$");
So when args contains more than 1 word the pattern works but if not the pattern acts as true even though hnre.find() outputs false.
Thanks in advance :)