Files
gradle
src
bootstrap.ps1
build.gradle
gradle.properties
gradlew
gradlew.bat
manifest.kod
README.md
settings.gradle
Code
35 755
src/main/java/com/example/advancedpunish/AdvancedPunishPlugin.java
package com.example.advancedpunish;
import com.example.advancedpunish.database.PunishmentDatabase;
import com.example.advancedpunish.discord.DiscordNotifier;
import com.example.advancedpunish.gui.*;
import com.example.advancedpunish.jail.JailManager;
import com.example.advancedpunish.listener.PunishmentListener;
import com.example.advancedpunish.model.*;
import com.example.advancedpunish.service.PunishmentService;
import com.cryptomorin.xseries.XSound;
import net.kyori.adventure.text.*;
import net.kyori.adventure.text.event.*;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.*;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.*;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.time.Duration;
import java.util.*;
public final class AdvancedPunishPlugin extends JavaPlugin {private PunishmentDatabase database;private PunishmentService service;private GUIManager gui;private JailManager jail;private FileConfiguration messages,guiConfig;private DiscordNotifier discord;@Override public void onEnable(){saveDefaultConfig();for(String f:new String[]{"messages.yml","INVENTORY GUI.yml","punishments.yml","database.yml","discord.yml"})saveResource(f,false);reloadFiles();database=new PunishmentDatabase(getDataFolder(),YamlConfiguration.loadConfiguration(new File(getDataFolder(),"database.yml")));service=new PunishmentService(this,database);gui=new GUIManager();jail=new JailManager(this);getServer().getPluginManager().registerEvents(new GUIListener(gui),this);getServer().getPluginManager().registerEvents(new PunishmentListener(this),this);AdvancedCommand c=new AdvancedCommand(this);for(String n:List.of("punish","ban","tempban","mute","tempmute","warn","kick","unban","unmute","ipban","tempipban","ipmute","jail","unjail","freeze","unfreeze","blacklist","unblacklist","softban","history","punishments","punishinfo","punishrevoke","note","notes","delnote","alts","stafflog","setjail","deljail","punishreload")){PluginCommand pc=getCommand(n);if(pc!=null){pc.setExecutor(c);pc.setTabCompleter(c);}}if(Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI"))new com.example.advancedpunish.hook.AdvancedPlaceholder(this).register();database.initialize().whenComplete((v,e)->{if(e!=null)getLogger().severe("Database initialization failed: "+e.getMessage());else getLogger().info("Database ready.");});long period=Math.max(1,getConfig().getLong("punishments.expiration-check-seconds",15))*20L;Bukkit.getScheduler().runTaskTimerAsynchronously(this,()->service.expire(),period,period);}@Override public void onDisable(){if(gui!=null)gui.clear();if(database!=null)database.close();}private void reloadFiles(){messages=YamlConfiguration.loadConfiguration(new File(getDataFolder(),"messages.yml"));guiConfig=YamlConfiguration.loadConfiguration(new File(getDataFolder(),"INVENTORY GUI.yml"));discord=new DiscordNotifier(YamlConfiguration.loadConfiguration(new File(getDataFolder(),"discord.yml")));}public void reloadSafe(){reloadConfig();reloadFiles();}public PunishmentDatabase database(){return database;}public PunishmentService service(){return service;}public GUIManager gui(){return gui;}public JailManager jail(){return jail;}public FileConfiguration guiConfig(){return guiConfig;}public Component message(String key,Map<String,String> values){String text=messages.getString(key,key);for(var e:values.entrySet())text=text.replace("{"+e.getKey()+"}",e.getValue());text=text.replace("{prefix}",messages.getString("prefix",""));return MiniMessage.miniMessage().deserialize(text);}public Component banMessage(Punishment p){return message("ban-screen",Map.of("player",p.getTargetName(),"reason",p.getReason(),"id",p.getId(),"expiration",p.getExpiresAt()==null?"Permanent":p.getExpiresAt().toString(),"appeal",getConfig().getString("appeal-url","Unavailable")));}public void sendMessage(Player p,String key){p.sendMessage(message(key,Map.of()));}public void broadcast(Punishment p){if(!getConfig().getBoolean("broadcasts.enabled",true)||!getConfig().getBoolean("broadcasts."+p.getType().name().toLowerCase(),true))return;String text=messages.getString("broadcasts."+p.getType().name().toLowerCase(),messages.getString("broadcasts.default"));Map<String,String> v=Map.of("player",p.getTargetName(),"punishment",p.getType().name(),"reason",p.getReason(),"duration",p.getExpiresAt()==null?"Permanent":Duration.between(p.getCreatedAt(),p.getExpiresAt()).toString(),"staff",p.getStaffName(),"id",p.getId());for(var e:v.entrySet())text=text.replace("{"+e.getKey()+"}",e.getValue());Component c=MiniMessage.miniMessage().deserialize(text);if(getConfig().getBoolean("broadcasts.show-punishment-id",true))c=c.hoverEvent(HoverEvent.showText(Component.text("Punishment "+p.getId()))).clickEvent(ClickEvent.runCommand("/punishinfo "+p.getId()));Bukkit.broadcast(c);if(getConfig().getBoolean("broadcasts.sound.enabled",true))XSound.matchXSound(getConfig().getString("broadcasts.sound.type","BLOCK_ANVIL_LAND")).ifPresent(s->Bukkit.getOnlinePlayers().forEach(x->s.play(x,(float)getConfig().getDouble("broadcasts.sound.volume",.7),(float)getConfig().getDouble("broadcasts.sound.pitch",1))));discord.send(p);}public void executeGuiPunishment(Player staff,UUID target,String name,PunishmentType type){if(!staff.hasPermission("punish."+type.name().toLowerCase())){sendMessage(staff,"no-permission");return;}createPunishment(staff,target,name,type,type.isTemporary()?Duration.ofHours(1):null,"GUI action");staff.closeInventory();}public void createPunishment(Player staff,UUID target,String name,PunishmentType type,Duration duration,String reason){if(!getConfig().getBoolean("allow-self-punish",false)&&staff.getUniqueId().equals(target)){sendMessage(staff,"no-permission");return;}service.create(target,name,staff.getUniqueId(),staff.getName(),type,reason,duration,null).thenAccept(p->Bukkit.getScheduler().runTask(this,()->{broadcast(p);staff.sendMessage(message("punished",Map.of("player",name,"type",type.name())));if(type==PunishmentType.KICK||type==PunishmentType.SOFTBAN){Player online=Bukkit.getPlayer(target);if(online!=null){if(type==PunishmentType.SOFTBAN)online.getInventory().clear();online.kick(message("punished",Map.of("player",name,"type",type.name())));}}if(type==PunishmentType.JAIL){Player online=Bukkit.getPlayer(target);if(online!=null)jail.sendToJail(online);}})).exceptionally(e->{Bukkit.getScheduler().runTask(this,()->sendMessage(staff,"database-error"));return null;});}public void openPunish(Player p,UUID target,String name){gui.open(new PunishmentGUI(this,target,name),p);}public void sendHistory(Player p,UUID target,String name,int page){service.history(target,page).thenAccept(list->Bukkit.getScheduler().runTask(this,()->{p.sendMessage(Component.text("━━━━━━━━ Punishment history: "+name+" ("+page+") ━━━━━━━━",ChatColor.DARK_RED));if(list.isEmpty())p.sendMessage(Component.text("No punishments found.",ChatColor.GRAY));for(Punishment x:list)p.sendMessage(Component.text("#"+x.getId()+" "+x.getType()+" | "+x.getReason()+" | "+(x.isRevoked()?"REVOKED":x.isActive()?"ACTIVE":"EXPIRED"),ChatColor.DARK_RED));}));}public void sendNotes(Player p,UUID target,String name){database.notes(target).thenAccept(list->Bukkit.getScheduler().runTask(this,()->{p.sendMessage(Component.text("Notes for "+name,ChatColor.DARK_RED));list.forEach(s->p.sendMessage(Component.text(s,ChatColor.GRAY)));}));}}