This is a clientside swear filter. It's fairly efficient as far as I can see. It's used on Era right now and it doesn't seem to cause any issues.
There are three levels (it's easy to add more):
- Off
- Moderate (recommended)
- Strict (for babies only)
Players can choose any one. Only chat is filtered, so you will want to use the default wordfilter for PMs, nicks, and toalls.
PHP Code:
//#CLIENTSIDE
enum LEVEL {
OFF,
MODERATE,
STRICT
}
const DEFAULT_LEVEL = LEVEL.MODERATE;
function onCreated() {
this.setDefaultFilterLevel();
this.loadFilterRules();
}
// filter rules
function loadFilterRules() {
this.resetRules();
if (getFilterLevel() <= LEVEL.OFF) {
return;
}
// rules shouald be in all lowercase and contain no spaces
// if words contain eachother use the one which is most inclusive
// first, e.g. if you want to have "pooping" and "oop",
// put "pooping" first since otherwise it is filtered to
// p***ing which looks odd
// moderate rules (also apply to strict)
temp.rulesToAdd = {
"drat",
"darn"
};
this.addRules(rulesToAdd);
if (getFilterLevel() <= LEVEL.MODERATE) {
return;
}
// strict rules
temp.rulesToAdd = {
"crap",
"poo"
};
this.addRules(rulesToAdd);
}
// filtering
function onRemotePlayerChats(pl) {
this.filterChat(pl);
}
function onPlayerChats() {
this.filterChat(player);
}
function onPlayerEnters() {
for (temp.pl : players) {
if (pl == player) {
continue;
}
this.filterChat(pl);
}
}
function filterChat(pl) {
if (pl.chat.trim().length() <= 0) {
return;
}
temp.testChat = "";
temp.newChat = pl.chat;
temp.chatPositions = null;
for (temp.i = 0; i < pl.chat.length(); i ++) {
temp.char = pl.chat.charat(i);
if (char == " ") {
continue;
}
chatPositions.add(i);
testChat @= char.lower();
}
for (temp.rule : this.getRules()) {
temp.positions = testChat.positions(rule);
temp.lastFix = (- 1);
for (temp.position : positions) {
if (lastFix != (- 1) && position < (lastFix + rule.length())) {
continue; // was already fixed partially
}
temp.replacement = getReplacement(rule.length());
testChat = testChat.substring(0, position) @ replacement @ testChat.substring(position + rule.length());
newChat = newChat.substring(0, chatPositions[position]) @ replacement @ newChat.substring(chatPositions[position + rule.length() - 1] + 1);
}
}
if (newChat != pl.chat) {
pl.chat = newChat;
}
}
function getReplacement(characters) {
// you could easily change this to like @#$%! if you wanted
temp.replacement = "******************************";
return replacement.substring(0, characters);
}
public function setFilterLevel(newFilterLevel) {
player.client.option.filter.level = newFilterLevel;
this.loadFilterRules();
}
public function getFilterLevel() {
return player.client.option.filter.level;
}
function setDefaultFilterLevel() {
if (! player.client.option.filter.init) {
player.client.option.filter.init = true;
this.setFilterLevel(DEFAULT_LEVEL);
}
}
function resetRules() {
this.rules = null;
}
function addRules(rulesToAdd) {
for (temp.ruleToAdd : rulesToAdd) {
this.rules.add(ruleToAdd);
}
}
function getRules() {
return this.rules;
}
Use the public functions getFilterLevel() and setFilterLevel(int newFilterLevel) for getting/setting the level with chat commands or a GUI.
If you want Era's word list I'm happy to give you them, just send me a PM. I don't think Darlene would be happy if I posted it.