Skip to content

Commit

Permalink
Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jmshal committed Jan 30, 2016
1 parent e3230e0 commit 6aa8933
Show file tree
Hide file tree
Showing 14 changed files with 455 additions and 95 deletions.
2 changes: 1 addition & 1 deletion MelooonCensor.iml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
Expand Down
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
# MelooonCensor

A simple censor plugin for Bukkit/CraftBukkit.

## License

This repository and code is made available under the [MIT License](./LICENSE.md).
7 changes: 7 additions & 0 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
</relocations>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
Expand Down
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,18 @@
package io.github.jacobmarshall.meloooncensor;

import io.github.jacobmarshall.meloooncensor.filter.ClassicFilter;
import io.github.jacobmarshall.meloooncensor.filter.Filter;
import io.github.jacobmarshall.meloooncensor.filter.StrictFilter;
import io.github.jacobmarshall.meloooncensor.listeners.ChatEventListener;
import org.bukkit.configuration.file.FileConfiguration;
import io.github.jacobmarshall.meloooncensor.config.Configuration;
import io.github.jacobmarshall.meloooncensor.listener.ChatEventListener;
import io.github.jacobmarshall.meloooncensor.command.CensorCommandExecutor;
import org.bukkit.plugin.java.JavaPlugin;
import org.mcstats.Metrics;

import java.io.IOException;
import java.util.List;

public class MelooonCensor extends JavaPlugin {

static final String DEFAULT_TYPE = "classic";
static final char DEFAULT_CHAR = '*';
static final String[] DEFAULT_LIST = new String[] {"fuck", "shit", "piss", "bitch"};
static final String[] DEFAULT_IGNORE = new String[] {"shitsu"};
static final String DEFAULT_MESSAGE = "Please don't use that kind of language on this server.";
private Configuration config;

FileConfiguration config = getConfig();
Filter filter;
String warning;

void setupConfig () {
config.options().header("MelooonCensor Configuration");
config.addDefault("censor.type", DEFAULT_TYPE);
config.addDefault("censor.char", DEFAULT_CHAR);
config.addDefault("censor.list", DEFAULT_LIST);
config.addDefault("censor.ignore", DEFAULT_IGNORE);
config.addDefault("censor.message", DEFAULT_MESSAGE);
config.options().copyDefaults(true);
saveConfig();
reloadConfig();
}

void loadConfig () {
config = getConfig();
filter = getFilter();
warning = config.getString("censor.message");
}

void startMetrics () {
protected void startMetrics () {
try {
Metrics metrics = new Metrics(this);
metrics.start();
Expand All @@ -50,38 +21,23 @@ void startMetrics () {
}
}

void registerEvents () {
protected void registerEvents () {
getServer().getPluginManager().registerEvents(
new ChatEventListener(filter, warning), this
new ChatEventListener(this.config), this
);
}

Filter getFilter () {
Filter filter;

char replace = config.getString("censor.char", "*").charAt(0);
List<String> censor = config.getStringList("censor.list");
List<String> ignore = config.getStringList("censor.ignore");

String name = config.getString("censor.type", "classic");

if (name.equalsIgnoreCase("strict")) {
// StrictFilter extends from ClassicFilter's detection technique, but doesn't allow any messages
// that violate the policy from sending out publicly.
filter = new StrictFilter(replace, censor, ignore);
} else {
// ClassicFilter takes any words that contain/are censored words, which aren't ignored words,
// and censors the whole word.
filter = new ClassicFilter(replace, censor, ignore);
}
getCommand("censor").setExecutor(
new CensorCommandExecutor(this.config)
);
}

return filter;
protected void setupConfig () {
config = new Configuration(this);
}

@Override
public void onEnable () {
setupConfig();
loadConfig();
startMetrics();
registerEvents();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package io.github.jacobmarshall.meloooncensor.command;

import io.github.jacobmarshall.meloooncensor.config.Configuration;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;

public class CensorCommandExecutor implements CommandExecutor {

Configuration config;

public CensorCommandExecutor (Configuration config) {
this.config = config;
}

public boolean onCommand (CommandSender sender, Command command, String label, String[] args) {
String cmd = "";
String word = "";
String type = "";

if (args.length >= 1) {
cmd = args[0].toLowerCase();
}

if (args.length >= 2) {
type = args[1].toLowerCase();
}

if (args.length >= 3) {
word = args[2];
}

try {
switch (cmd) {
case "add":
case "a":
switch (type) {
case "censor":
case "censored":
case "c":
assertPermission(sender, "meloooncensor.add.censor");

if (config.addCensor(word)) {
config.save();
sender.sendMessage(ChatColor.GREEN + "Added to censored words.");
} else {
sender.sendMessage(ChatColor.YELLOW + "Already a censored word.");
}
break;
case "ignore":
case "ignored":
case "i":
assertPermission(sender, "meloooncensor.add.ignore");

if (config.addIgnore(word)) {
config.save();
sender.sendMessage(ChatColor.GREEN + "Added to ignored words.");
} else {
sender.sendMessage(ChatColor.YELLOW + "Already an ignored word.");
}
break;
default:
sender.sendMessage(ChatColor.RED + "Provide a list to add a word to (censor/ignore).");
break;
}
break;
case "remove":
case "r":
case "delete":
case "d":
switch (type) {
case "censor":
case "censored":
case "c":
assertPermission(sender, "meloooncensor.remove.censor");

if (config.removeCensor(word)) {
config.save();
sender.sendMessage(ChatColor.GREEN + "Removed from censored words.");
} else {
sender.sendMessage(ChatColor.YELLOW + "Not a censored word.");
}
break;
case "ignore":
case "ignored":
case "i":
assertPermission(sender, "meloooncensor.remove.ignore");

if (config.removeIgnore(word)) {
config.save();
sender.sendMessage(ChatColor.GREEN + "Removed from ignored words.");
} else {
sender.sendMessage(ChatColor.YELLOW + "Not an ignored word.");
}
break;
default:
sender.sendMessage(ChatColor.RED + "Provide a list to remove a word from (censor/ignore).");
break;
}
break;
case "list":
case "l":
switch (type) {
case "censor":
case "censored":
case "c":
assertPermission(sender, "meloooncensor.list.censor");

sender.sendMessage(toStringArrayPretty(config.getCensor()));
break;
case "ignore":
case "ignored":
case "i":
assertPermission(sender, "meloooncensor.list.ignore");

sender.sendMessage(toStringArrayPretty(config.getIgnore()));
break;
default:
assertPermission(sender, "meloooncensor.list.censor");
assertPermission(sender, "meloooncensor.list.ignore");

List<String> all = new ArrayList<>();
all.addAll(config.getCensor());
all.addAll(config.getIgnore());
sender.sendMessage(toStringArrayPretty(all));
break;
}
break;
default:
return false;
}
} catch (NoPermissionException err) {
sender.sendMessage(ChatColor.RED + "You do not have permission to perform this action.");
}

return true;
}

private String toStringArrayPretty (List<String> strings) {
StringBuilder pretty = new StringBuilder();

for (int index = 0; index < strings.size(); index++) {
if (index != 0) pretty.append(", ");
pretty.append(strings.get(index));
}

if (pretty.length() != 0) {
return pretty.toString();
} else {
return "*Empty*";
}
}

private void assertPermission (CommandSender sender, String permission) throws NoPermissionException {
if (sender instanceof Player) {
Player player = (Player) sender;

if ( ! player.isOp() || ! player.hasPermission(permission)) {
throw new NoPermissionException();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.github.jacobmarshall.meloooncensor.command;

public class NoPermissionException extends Exception {
}
Loading

0 comments on commit 6aa8933

Please sign in to comment.