Skip to content

Commit

Permalink
Merge pull request #2 from YouHaveTrouble/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
YouHaveTrouble authored Sep 29, 2020
2 parents a71aee4 + effad46 commit 8d4b63a
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>eu.endermite</groupId>
<artifactId>censura</artifactId>
<version>1.1</version>
<version>1.2</version>
<packaging>jar</packaging>

<name>Censura</name>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/eu/endermite/censura/Censura.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public void onEnable() {
getServer().getPluginManager().registerEvents(new BookEditListener(), this);
getServer().getPluginManager().registerEvents(new ItemRenameListener(), this);
getServer().getPluginManager().registerEvents(new CommandListener(), this);
getServer().getPluginManager().registerEvents(new EntityRenameListener(), this);
getServer().getPluginManager().registerEvents(new PlayerJoinListener(), this);

try {
getCommand("censura").setExecutor(new CensuraCommand());
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/eu/endermite/censura/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,35 @@ public static boolean filter(String message, Player player) {
return false;
}

public static boolean filterNoActions(String message) {

if (detectPhrases(message, "severe") || detectPhrases(normalizedString(message), "severe")) {
return true;
}

if (detectPhrases(noRepeatChars(message), "severe") || detectPhrases(noRepeatChars(normalizedString(message)), "severe")) {
return true;
}

if (detectPhrases(message, "normal") || detectPhrases(normalizedString(message), "normal")) {
return true;
}

if (detectPhrases(noRepeatChars(message), "normal") || detectPhrases(noRepeatChars(normalizedString(message)), "normal")) {
return true;
}

if (detectPhrases(message, "lite") || detectPhrases(normalizedString(message), "lite")) {
return true;
}

if (detectPhrases(noRepeatChars(message), "lite") || detectPhrases(noRepeatChars(normalizedString(message)), "lite")) {
return true;
}
return false;

}

private static String noRepeatChars(String string) {
char[] chars;
chars = string.toCharArray();
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/eu/endermite/censura/command/CensuraCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ public class CensuraCommand implements TabExecutor {

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length > 0) {
if (args[0].equalsIgnoreCase("reload")) {
if (sender.hasPermission("censura.reload")) {
Censura.getPlugin().asyncReloadConfigCache(sender);
} else {
sender.sendMessage(Censura.getCachedConfig().getNoPermission());
}
if (args.length == 0) {
sendCredits(sender);
return true;
}

if (args[0].equalsIgnoreCase("reload")) {
if (sender.hasPermission("censura.reload")) {
Censura.getPlugin().asyncReloadConfigCache(sender);
} else {
sender.sendMessage(Censura.getCachedConfig().getNoSuchCommand());
sender.sendMessage(Censura.getCachedConfig().getNoPermission());
}
} else {
sendCredits(sender);
sender.sendMessage(Censura.getCachedConfig().getNoSuchCommand());
}
return true;
}
Expand All @@ -48,7 +49,7 @@ public List<String> onTabComplete(CommandSender sender, Command command, String

public void sendCredits(CommandSender sender) {
PluginDescriptionFile desc = Censura.getPlugin().getDescription();
sender.sendMessage("Censura "+ desc.getVersion()+ " by YouHaveTrouble");
sender.sendMessage("Censura " + desc.getVersion() + " by YouHaveTrouble");
assert desc.getDescription() != null;
sender.sendMessage(desc.getDescription());
}
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/eu/endermite/censura/config/CachedConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class CachedConfig {

List<String> commandsToFilter = new ArrayList<>();

String noPermission, noSuchCommand, configReloaded;
boolean opBypass;
String noPermission, noSuchCommand, configReloaded, kickBadName;
boolean opBypass, kickOnJoin;

public CachedConfig(FileConfiguration config) {

Expand All @@ -38,7 +38,6 @@ public CachedConfig(FileConfiguration config) {

litePunishments = lite.getStringList("action");


ConfigurationSection liteMatch = lite.getConfigurationSection("match");

for (String liteMatchString : liteMatch.getKeys(false)) {
Expand Down Expand Up @@ -79,13 +78,14 @@ public CachedConfig(FileConfiguration config) {
}

commandsToFilter.addAll(config.getStringList("filtered-commands"));
opBypass = config.getBoolean("op-bypass");
opBypass = config.getBoolean("op-bypass", true);
kickOnJoin = config.getBoolean("kick-on-bad-name", true);

ConfigurationSection messages = config.getConfigurationSection("messages");
noPermission = messages.getString("no-permission");
noSuchCommand = messages.getString("no-such-command");
configReloaded = messages.getString("config-reloaded");

noPermission = messages.getString("no-permission", "Censura - &cYou don't have permission to do this.");
noSuchCommand = messages.getString("no-such-command", "Censura - &cThere is no such command.");
configReloaded = messages.getString("config-reloaded", "Censura - &aConfiguration reloaded.");
kickBadName = messages.getString("kick-bad-name", "Censura\n&cYour name contains bad words!");

}

Expand Down Expand Up @@ -141,8 +141,16 @@ public String getConfigReloaded() {
return ChatColor.translateAlternateColorCodes('&', configReloaded);
}

public String getKickBadName() {
return ChatColor.translateAlternateColorCodes('&', kickBadName);
}

public boolean getOpBypass() {
return opBypass;
}

public boolean getKickOnJoin() {
return kickOnJoin;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package eu.endermite.censura.listener;

import eu.endermite.censura.Filter;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;

public class EntityRenameListener implements Listener {

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onEntityRename(org.bukkit.event.player.PlayerInteractEntityEvent event) {

ItemStack handItem = event.getPlayer().getInventory().getItemInMainHand();

if (handItem.getType() != Material.NAME_TAG)
return;

if (Filter.filter(handItem.getItemMeta().getDisplayName(), event.getPlayer()))
event.setCancelled(true);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package eu.endermite.censura.listener;

import eu.endermite.censura.Censura;
import eu.endermite.censura.Filter;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;

public class PlayerJoinListener implements Listener {

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerJoin(org.bukkit.event.player.AsyncPlayerPreLoginEvent event) {

if (!Censura.getCachedConfig().getKickOnJoin())
return;

String name = event.getPlayerProfile().getName();
if (Filter.filterNoActions(name)) {
event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, Censura.getCachedConfig().getKickBadName());
event.setKickMessage(Censura.getCachedConfig().getKickBadName());
}
}

}
6 changes: 5 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
# Should server operators bypass the filters?
op-bypass: true

# Should players be denied to join if their name contains bad words?
kick-on-bad-name: true

# Commands of which arguments should be checked.
# It's recommended to list all private message commands and their aliases
# and every command that allows changing item name and etc.
Expand Down Expand Up @@ -68,4 +71,5 @@ filter:
messages:
no-permission: "Censura - &cYou don't have permission to do this."
no-such-command: "Censura - &cThere is no such command."
config-reloaded: "Censura - &aConfiguration reloaded."
config-reloaded: "Censura - &aConfiguration reloaded."
kick-bad-name: "Censura\n&cYour name contains bad words!"

0 comments on commit 8d4b63a

Please sign in to comment.