Files
src
build.gradle
settings.gradle
Code
6 1,201
src/main/java/com/kodari/punishmentpolicy/PunishmentPolicyPlugin.java
package com.kodari.punishmentpolicy;
import org.bukkit.ChatColor;
import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.plugin.java.JavaPlugin;
import io.papermc.paper.event.player.AsyncChatEvent;
import com.cryptomorin.xseries.XMaterial;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public final class PunishmentPolicyPlugin extends JavaPlugin implements CommandExecutor, TabCompleter, Listener {
private static final int PAGE_SIZE = 8;
private final Map<String, PolicyEntry> policies = new LinkedHashMap<>();
private final Map<String, Long> activeMutes = new ConcurrentHashMap<>();
private final Map<String, Map<String, Integer>> punishmentHistory = new LinkedHashMap<>();
@Override
public void onEnable() {
saveDefaultConfig();
loadPolicies();
loadActiveMutes();
loadPunishmentHistory();
getCommand("punishments").setExecutor(this);
getCommand("punishments").setTabCompleter(this);
getCommand("offend").setExecutor(this);
getCommand("offend").setTabCompleter(this);
getCommand("unoffend").setExecutor(this);
getCommand("untempban").setExecutor(this);
getCommand("untempmute").setExecutor(this);
getCommand("unmuteall").setExecutor(this);
getServer().getPluginManager().registerEvents(this, this);
initializeVoicechatHook();
getServer().getScheduler().runTaskTimer(this, this::expireMutes, 20L, 20L);
}
private void initializeVoicechatHook() {
if (!getServer().getPluginManager().isPluginEnabled("voicechat")) {
return;
}
try {
Class<?> hookClass = Class.forName("com.kodari.punishmentpolicy.SimpleVoiceChatHook", true,
getClass().getClassLoader());
hookClass.getConstructor(PunishmentPolicyPlugin.class).newInstance(this);
} catch (ReflectiveOperationException | LinkageError exception) {
getLogger().warning("Could not initialize the Simple Voice Chat mute integration: " + exception.getMessage());
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("offend")) {
return handleTempBan(sender, label, args);
}
if (command.getName().equalsIgnoreCase("unoffend")) {
return handleUnoffend(sender, label, args);
}
if (command.getName().equalsIgnoreCase("untempban")) {
return handleUntempBan(sender, label, args);
}
if (command.getName().equalsIgnoreCase("untempmute")) {
return handleUntempMute(sender, label, args);
}
if (command.getName().equalsIgnoreCase("unmuteall")) {
return handleUnmuteAll(sender, label, args);
}
if (args.length == 0 || args[0].equalsIgnoreCase("list")) {
int page = 1;
if (args.length > 2) {
sendUsage(sender, label);
return true;
}
if (args.length == 2) {
try {
page = Integer.parseInt(args[1]);
} catch (NumberFormatException exception) {
sendUsage(sender, label);
return true;
}
}
sendIndex(sender, page);
return true;
}
String query = normalize(String.join(" ", args));
PolicyEntry policy = policies.values().stream()
.filter(entry -> normalize(entry.name).equals(query))
.findFirst()
.orElse(null);
if (policy == null) {
sender.sendMessage(color("&cUnknown category. Use &f/" + label + " list&c to browse categories."));
return true;
}
sendPolicy(sender, policy);
return true;
}
private boolean handleTempBan(CommandSender sender, String label, String[] args) {
if (!sender.hasPermission("punishmentpolicy.temppunish")) {
sender.sendMessage(color("&cYou don't have permission to use this command."));
return true;
}
if (args.length == 0) {
if (!(sender instanceof Player player)) {
sender.sendMessage(color("&cRun this command in-game to select an online player."));
return true;
}
openOnlinePlayerMenu(player, 0);
return true;
}
if (args.length == 1 && args[0].matches("[A-Za-z0-9_]{1,16}")) {
if (!(sender instanceof Player player)) {
sender.sendMessage(color("&cRun this command in-game to open the punishment menu."));
return true;
}
openCategoryMenu(player, args[0], 0);
return true;
}
if (args.length < 2 || !args[0].matches("[A-Za-z0-9_]{1,16}")) {
sender.sendMessage(color("&eUsage: /" + label + " <player> <category> [reason]"));
return true;
}
PolicyMatch match = findPolicy(args, 1);
if (match == null) {
sender.sendMessage(color("&cA valid punishment category is required before the reason. Use &f/offend "
+ args[0] + " <category> [reason]&c. Browse categories with &f/punishments list&c."));
return true;
}
int tier = nextTier(args[0], match.policy);
int reasonStart = match.nextArgument;
if (tier > match.policy.sanctions.size()) {
sender.sendMessage(color("&cThat category only has " + match.policy.sanctions.size() + " tier(s)."));
return true;
}
String reason = reasonStart < args.length
? String.join(" ", Arrays.copyOfRange(args, reasonStart, args.length))
: defaultReason(match.policy, tier);
applySanction(sender, args[0], match.policy, tier, reason);
return true;
}
private boolean applySanction(CommandSender sender, String playerName, PolicyEntry policy, int tier, String reason) {
String sanction = policy.sanctions.get(tier - 1);
Matcher sanctionMatcher = Pattern.compile("(?i)^([1-9]\\d*[smhdw])\\s+(ban|mute)$").matcher(sanction);
boolean permanent = sanction.equalsIgnoreCase("Perm ban") || sanction.equalsIgnoreCase("Perm mute");
boolean warning = sanction.equalsIgnoreCase("Warning");
if (!permanent && !warning && !sanctionMatcher.matches()) {
sender.sendMessage(color("&c" + policy.name + (policy.tiered ? " " + roman(tier) : "") + " is &f" + sanction
+ "&c, not an applicable ban or mute punishment."));
return false;
}
String sanctionType = warning ? "warning" : permanent ? sanction.substring(5).toLowerCase(Locale.ROOT)
: sanctionMatcher.group(2).toLowerCase(Locale.ROOT);
Long durationMillis = permanent || warning ? null : parseDurationMillis(sanctionMatcher.group(1));
if (!permanent && !warning && durationMillis == null) {
sender.sendMessage(color("&cThe configured duration for this punishment is invalid or too long."));
return false;
}
Date expires = null;
if (!permanent && !warning) {
try {
expires = new Date(Math.addExact(System.currentTimeMillis(), durationMillis));
} catch (ArithmeticException exception) {
sender.sendMessage(color("&cThat configured duration is too long."));
return false;
}
}
Player onlineTarget = Bukkit.getOnlinePlayers().stream()
.filter(player -> player.getName().equalsIgnoreCase(playerName))