Files
src
manifest.kod
pom.xml
README.md
Code
22 1,982
src/main/java/com/moontags/smp/MoonTagsSMP.java
package com.moontags.smp;
import com.moontags.smp.command.StartCommand;
import com.moontags.smp.command.TagCommand;
import com.moontags.smp.data.Database;
import com.moontags.smp.gui.MenuManager;
import com.moontags.smp.kit.KitManager;
import com.moontags.smp.listener.ChatListener;
import com.moontags.smp.listener.KillListener;
import com.moontags.smp.listener.PlayerListener;
import com.moontags.smp.model.PlayerProfile;
import com.moontags.smp.model.TagDefinition;
import com.moontags.smp.tag.TagDisplay;
import com.moontags.smp.tag.TagRegistry;
import com.moontags.smp.util.Text;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
public final class MoonTagsSMP extends JavaPlugin {
private final Map<UUID, PlayerProfile> profiles = new ConcurrentHashMap<>();
private final Set<UUID> loadedProfiles = ConcurrentHashMap.newKeySet();
private final Map<String, Long> killCredits = new HashMap<>();
private final Map<UUID, Long> lastDeathRewards = new HashMap<>();
private final Map<UUID, Long> abilityCooldowns = new HashMap<>();
private Database database;
private TagRegistry tags;
private TagDisplay display;
private KitManager kits;
private MenuManager menus;
private YamlConfiguration messages;
@Override
public void onEnable() {
saveDefaultConfig();
saveResourceIfMissing("tags.yml");
saveResourceIfMissing("kits.yml");
saveResourceIfMissing("messages.yml");
try {
database = new Database(this);
} catch (SQLException exception) {
getLogger().severe("Could not open the SQLite database: " + exception.getMessage());
getServer().getPluginManager().disablePlugin(this);
return;
}
loadMessages();
tags = new TagRegistry(this);
display = new TagDisplay(this);
kits = new KitManager(this);
menus = new MenuManager(this);
Bukkit.getPluginManager().registerEvents(new PlayerListener(this), this);
Bukkit.getPluginManager().registerEvents(new KillListener(this), this);
Bukkit.getPluginManager().registerEvents(new ChatListener(this), this);
Bukkit.getPluginManager().registerEvents(menus, this);
TagCommand tagCommand = new TagCommand(this);
getCommand("tag").setExecutor(tagCommand);
getCommand("tag").setTabCompleter(tagCommand);
getCommand("starttagsmp").setExecutor(new StartCommand(this));
for (Player player : Bukkit.getOnlinePlayers()) {
ensureProfile(player, true, null);
}
}
@Override
public void onDisable() {
if (database == null) {
return;
}
for (Player player : Bukkit.getOnlinePlayers()) {
display.clear(player);
}
for (PlayerProfile profile : profiles.values()) {
database.saveProfile(profile);
}
try {
database.close();
} catch (SQLException exception) {
getLogger().severe("Could not close SQLite cleanly: " + exception.getMessage());
}
}
private void saveResourceIfMissing(String name) {
if (!new File(getDataFolder(), name).exists()) {
saveResource(name, false);
}
}
private void loadMessages() {
messages = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "messages.yml"));
}
public TagRegistry tags() {
return tags;
}
public TagDisplay display() {
return display;
}
public KitManager kits() {
return kits;
}
public MenuManager menus() {
return menus;
}
public PlayerProfile profile(UUID uuid) {
return profiles.get(uuid);
}
public boolean isStarted() {
return database != null && database.isStarted();
}
public int cachedProfileCount() {
return profiles.size();
}
public void ensureProfile(Player player, boolean assignMissing, Consumer<PlayerProfile> callback) {
UUID uuid = player.getUniqueId();
if (loadedProfiles.contains(uuid)) {
PlayerProfile current = profiles.get(uuid);
if (current == null && assignMissing && isStarted()) {
current = assignRandom(player);
}
if (callback != null) {
callback.accept(current);
}
return;
}
database.loadProfile(uuid).whenComplete((loaded, error) -> Bukkit.getScheduler().runTask(this, () -> {
if (!player.isOnline()) {
return;
}
if (error != null) {
getLogger().severe("Could not load profile for " + player.getName() + ": " + error.getMessage());
return;
}
loadedProfiles.add(uuid);
PlayerProfile current = profiles.get(uuid);
if (current == null) {
current = loaded.orElse(null);
if (current != null && current.assigned()) {
profiles.put(uuid, current);
display.apply(player, current);
} else {
current = null;
if (assignMissing && isStarted()) {
current = assignRandom(player);
}
}
}
if (callback != null) {
callback.accept(current);
}
}));
}
public void unload(Player player) {
loadedProfiles.remove(player.getUniqueId());
profiles.remove(player.getUniqueId());
display.clear(player);
}
public void startSmp(CommandSender sender) {
if (isStarted()) {
send(sender, "already-started");
return;
}
database.setStarted(true).exceptionally(error -> {
getLogger().severe("Could not save SMP started state: " + error.getMessage());
return null;
});
send(sender, "started");
for (Player online : Bukkit.getOnlinePlayers()) {
ensureProfile(online, true, null);
}
}
public void giveTag(Player target, String tagId, CommandSender actor) {
TagDefinition tag = tags.get(tagId);