Files
src
build.gradle
manifest.kod
Code
7 872
src/main/java/com/vantage/reborn/VantageRebornPlugin.java
package com.vantage.reborn;
import io.papermc.paper.event.player.AsyncChatEvent;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
public final class VantageRebornPlugin extends JavaPlugin implements Listener {
private static final LegacyComponentSerializer LEGACY = LegacyComponentSerializer.legacyAmpersand();
private PunishmentStore store;
@Override
public void onEnable() {
saveDefaultConfig();
try {
store = new PunishmentStore(this);
store.expireDue(System.currentTimeMillis());
} catch (SQLException exception) {
getLogger().log(Level.SEVERE, "Could not open the punishment database", exception);
getServer().getPluginManager().disablePlugin(this);
return;
}
ModerationCommand moderationCommand = new ModerationCommand(this, store);
for (String name : List.of("tempmute", "unmute", "tempban", "vantageban", "warn", "unwarn", "banlist", "history", "unban")) {
PluginCommand command = getCommand(name);
if (command == null) {
getLogger().severe("Command is missing from plugin.yml: " + name);
continue;
}
command.setExecutor(moderationCommand);
command.setTabCompleter(moderationCommand);
}
getServer().getPluginManager().registerEvents(this, this);
getServer().getScheduler().runTaskTimer(this, () -> {
try {
store.expireDue(System.currentTimeMillis());
} catch (SQLException exception) {
getLogger().log(Level.SEVERE, "Could not expire punishments", exception);
}
}, 20L, 20L);
}
@EventHandler
public void onAsyncChat(AsyncChatEvent event) {
try {
long now = System.currentTimeMillis();
store.expireDue(now);
PunishmentStore.Punishment mute = store.activePunishment(event.getPlayer().getUniqueId(), "MUTE", now);
if (mute == null) return;
event.setCancelled(true);
String notice = getConfig().getString("messages.login-muted-chat", "&cYou are muted.")
.replace("{reason}", mute.reason())
.replace("{remaining}", remaining(mute.expiresAt()));
event.getPlayer().sendMessage(LEGACY.deserialize(notice));
} catch (SQLException exception) {
getLogger().log(Level.SEVERE, "Could not check a player's mute status", exception);
}
}
@EventHandler
public void onPlayerLogin(PlayerLoginEvent event) {
try {
long now = System.currentTimeMillis();
store.expireDue(now);
UUID playerId = event.getPlayer().getUniqueId();
PunishmentStore.Punishment ban = store.activePunishment(playerId, "PERMBAN", now);
if (ban == null) ban = store.activePunishment(playerId, "TEMPBAN", now);
if (ban != null) event.disallow(PlayerLoginEvent.Result.KICK_BANNED, banScreen(ban));
} catch (SQLException exception) {
getLogger().log(Level.SEVERE, "Could not check a player's ban status", exception);
event.disallow(PlayerLoginEvent.Result.KICK_OTHER,
LEGACY.deserialize("&cUnable to verify your punishment status. Please try again shortly."));
}
}
Component banScreen(PunishmentStore.Punishment ban) {
String duration = ban.expiresAt() == null ? "PERMANENT" : remaining(ban.expiresAt());
String issuer = safe(ban.moderatorName());
List<String> lines = getConfig().getStringList("ban-screen.lines");
if (lines.isEmpty()) {
lines = List.of("&8&m━━━━━━━━━━━━━━━━━━━━", "", " &c&lVANTAGE REBORN", "",
" &4&lYOU ARE BANNED", "", " &fBanned by: &c&l{issuer}",
" &fReason: &e{reason}", " &fDuration: &b{duration}", "",
"&7Please contact our staff if you believe this punishment was issued incorrectly.",
"", "&8&m━━━━━━━━━━━━━━━━━━━━");
}
List<String> renderedLines = lines.stream().map(line -> line.replace("{issuer}", issuer)
.replace("{reason}", safe(ban.reason())).replace("{duration}", duration)).toList();
String rendered = centerBanScreen(renderedLines).stream().reduce((a, b) -> a + "\n" + b).orElse("");
return LEGACY.deserialize(rendered);
}
private List<String> centerBanScreen(List<String> lines) {
return lines.stream().map(String::stripLeading).toList();
}
void kickIfOnline(UUID playerId, Component reason) {
Player player = Bukkit.getPlayer(playerId);
if (player != null) player.kick(reason);
}
UUID resolvePlayerId(String name) throws SQLException {
UUID known = store.findPlayerId(name);
if (known != null) return known;
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(name);
return offlinePlayer.getUniqueId();
}
String resolvePlayerName(UUID playerId, String suppliedName) {
Player online = Bukkit.getPlayer(playerId);
if (online != null) return online.getName();
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(playerId);
String knownName = offlinePlayer.getName();
return knownName == null || knownName.isBlank() ? suppliedName : knownName;
}
Component message(String text) {
return LEGACY.deserialize(text);
}
String configuredMessage(String key, String fallback) {
return getConfig().getString("messages." + key, fallback);
}
String remaining(Long expiresAt) {
if (expiresAt == null) return "PERMANENT";
long seconds = Math.max(0, (expiresAt - System.currentTimeMillis() + 999) / 1000);
long days = seconds / 86400;
long hours = (seconds % 86400) / 3600;
long minutes = (seconds % 3600) / 60;
long remainingSeconds = seconds % 60;
StringBuilder result = new StringBuilder();
if (days > 0) result.append(days).append("d ");
if (hours > 0) result.append(hours).append("h ");
if (minutes > 0) result.append(minutes).append("m ");
if (remainingSeconds > 0 || result.isEmpty()) result.append(remainingSeconds).append('s');
return result.toString().trim();
}
String safe(String value) {
return value == null || value.isBlank() ? "Staff" : value;
}
PunishmentStore store() {
return store;
}
@Override
public void onDisable() {
if (store == null) return;
try {
store.close();
} catch (SQLException exception) {
getLogger().log(Level.SEVERE, "Could not close the punishment database", exception);
}
}
}