Files
src
build.gradle
Code
11 1,533
src/main/java/com/kodari/rankedsmp/RankedSMPPlugin.java
package com.kodari.rankedsmp;
import com.cryptomorin.xseries.XAttribute;
import com.kodari.rankedsmp.data.ProfileStore;
import com.kodari.rankedsmp.listener.GameplayListener;
import com.kodari.rankedsmp.service.PowerService;
import com.kodari.rankedsmp.service.RankService;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public final class RankedSMPPlugin extends JavaPlugin {
private ProfileStore profileStore;
private RankService rankService;
private PowerService powerService;
@Override
public void onEnable() {
saveDefaultConfig();
profileStore = new ProfileStore(this);
profileStore.load();
rankService = new RankService(this, profileStore);
powerService = new PowerService(this, profileStore);
GameplayListener gameplay = new GameplayListener(this, profileStore, rankService, powerService);
RankedSMPCommand command = new RankedSMPCommand(this, profileStore, rankService, powerService, gameplay);
getServer().getPluginManager().registerEvents(gameplay, this);
getServer().getPluginManager().registerEvents(command, this);
getCommand("rankedsmp").setExecutor(command);
getCommand("rankedsmp").setTabCompleter(command);
for (Player player : Bukkit.getOnlinePlayers()) {
rankService.initialize(player);
}
Bukkit.getScheduler().runTaskTimer(this, profileStore::save, 6000L, 6000L);
Bukkit.getScheduler().runTaskTimer(this, rankService::refreshCrownEffects, 20L, 20L);
startHudAndCrownPulse();
}
@Override
public void onDisable() {
if (profileStore != null) {
profileStore.save();
}
for (Player player : Bukkit.getOnlinePlayers()) {
player.setCustomName(null);
player.setCustomNameVisible(false);
player.setGlowing(false);
player.setPlayerListName(player.getName());
}
}
public ProfileStore getProfileStore() {
return profileStore;
}
public RankService getRankService() {
return rankService;
}
public PowerService getPowerService() {
return powerService;
}
public String color(String text) {
return ChatColor.translateAlternateColorCodes('&', text);
}
public String message(String text) {
return color(getConfig().getString("messages.prefix", "&8[&6RankedSMP&8] &r") + text);
}
public void setMaxHealth(Player player, double health) {
XAttribute.of("max_health").ifPresent(attribute -> {
var instance = player.getAttribute(attribute.get());
if (instance != null) {
double maximum = Math.max(2.0, health);
instance.setBaseValue(maximum);
if (player.getHealth() > maximum) {
player.setHealth(maximum);
}
}
});
}
private void startHudAndCrownPulse() {
Bukkit.getScheduler().runTaskTimer(this, new Runnable() {
private int hudTicks;
private int pulseTicks;
@Override
public void run() {
hudTicks += 20;
pulseTicks += 20;
if (getConfig().getBoolean("hud.enabled", false)
&& hudTicks >= Math.max(20, getConfig().getInt("hud.interval-ticks", 40))) {
hudTicks = 0;
for (Player player : Bukkit.getOnlinePlayers()) {
var profile = profileStore.get(player.getUniqueId());
if (profile == null) continue;
String rank = profile.getRank() > 0 ? "#" + profile.getRank() : "Unranked";
String crown = profile.isCrownHolder() ? " &6♛ CROWN" : "";
player.sendActionBar(LegacyComponentSerializer.legacyAmpersand()
.deserialize("&8[&b" + rank + "&8]" + crown));
}
}
if (!getConfig().getBoolean("crown.pulse.enabled", false)) {
pulseTicks = 0;
return;
}
int interval = Math.max(1, getConfig().getInt("crown.pulse.interval-seconds", 600)) * 20;
if (pulseTicks < interval) return;
pulseTicks = 0;
var holderId = rankService.crownHolderId();
Player holder = holderId == null ? null : Bukkit.getPlayer(holderId);
if (holder == null) return;
double radius = getConfig().getDouble("crown.pulse.radius", 96.0);
double radiusSquared = radius * radius;
for (Player nearby : holder.getWorld().getPlayers()) {
if (nearby.equals(holder) || nearby.getLocation().distanceSquared(holder.getLocation()) > radiusSquared) continue;
nearby.sendMessage(message("&6A Crown Pulse stirs nearby..."));
}
}
}, 20L, 20L);
}
}