Files
src
build.gradle
manifest.kod
README.md
settings.gradle
TESTING.md
Code
34 3,379
src/main/java/com/moonsmp/moonabilities/MoonAbilitiesPlugin.java
package com.moonsmp.moonabilities;
import com.moonsmp.moonabilities.command.AbilityCommand;
import com.moonsmp.moonabilities.command.MoonAdminCommand;
import com.moonsmp.moonabilities.command.PearlTPCommand;
import com.moonsmp.moonabilities.gui.MenuService;
import com.moonsmp.moonabilities.listener.GameplayListener;
import com.moonsmp.moonabilities.service.AbilityPowerEngine;
import com.moonsmp.moonabilities.service.AbilityService;
import com.moonsmp.moonabilities.service.PearlTeleportService;
import com.moonsmp.moonabilities.service.UpgradeService;
import com.moonsmp.moonabilities.service.VaultService;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
public final class MoonAbilitiesPlugin extends JavaPlugin {
private static final List<String> CONFIG_FILES = List.of("abilities.yml", "messages.yml", "cooldowns.yml", "gui.yml", "vault.yml", "moon_scythe.yml", "upgrade.yml", "reroll.yml", "pearl_tp.yml");
private final Map<String, FileConfiguration> customConfigs = new HashMap<>();
private final Map<UUID, PlayerProfile> profiles = new ConcurrentHashMap<>();
private final java.util.Set<UUID> pendingDeliveries = ConcurrentHashMap.newKeySet();
private PlayerDataStore dataStore;
private CustomItemService items;
private ProtectionService protection;
private CombatService combat;
private AbilityPowerEngine powerEngine;
private UpgradeService upgradeService;
private AbilityService abilityService;
private PearlTeleportService pearlTeleportService;
private MenuService menuService;
private VaultService vaultService;
@Override
public void onEnable() {
saveDefaultConfig();
loadCustomConfigs();
dataStore = new PlayerDataStore(this);
try {
dataStore.initialize();
} catch (Exception exception) {
getLogger().log(Level.SEVERE, "Could not initialize MoonAbilities database.", exception);
getServer().getPluginManager().disablePlugin(this);
return;
}
items = new CustomItemService(this);
protection = new ProtectionService(this);
combat = new CombatService(this);
powerEngine = new AbilityPowerEngine(this);
upgradeService = new UpgradeService(this);
abilityService = new AbilityService(this, powerEngine);
pearlTeleportService = new PearlTeleportService(this);
menuService = new MenuService(this);
vaultService = new VaultService(this);
getServer().getPluginManager().registerEvents(new GameplayListener(this), this);
getServer().getPluginManager().registerEvents(menuService, this);
getServer().getPluginManager().registerEvents(vaultService, this);
registerCommands();
registerRecipes();
for (Player player : Bukkit.getOnlinePlayers()) loadPlayer(player);
getLogger().info("MoonAbilities 1.0.0 enabled with " + Ability.values().length + " abilities.");
}
@Override
public void onDisable() {
if (dataStore != null) dataStore.shutdown();
profiles.clear();
}
private void registerCommands() {
AbilityCommand ability = new AbilityCommand(this);
Objects.requireNonNull(getCommand("ability")).setExecutor(ability);
Objects.requireNonNull(getCommand("ability1")).setExecutor(ability);
Objects.requireNonNull(getCommand("ability2")).setExecutor(ability);
Objects.requireNonNull(getCommand("ultraability")).setExecutor(ability);
Objects.requireNonNull(getCommand("abilities")).setExecutor(ability);
Objects.requireNonNull(getCommand("pearltp")).setExecutor(new PearlTPCommand(this));
Objects.requireNonNull(getCommand("moonadmin")).setExecutor(new MoonAdminCommand(this));
}
private void loadCustomConfigs() {
customConfigs.clear();
for (String name : CONFIG_FILES) {
File file = new File(getDataFolder(), name);
if (!file.exists()) saveResource(name, false);
customConfigs.put(name, YamlConfiguration.loadConfiguration(file));
}
}
public void reloadPluginConfigs() {
reloadConfig();
loadCustomConfigs();
registerRecipes();
}
private void registerRecipes() {
NamespacedKey pearlKey = new NamespacedKey(this, "pearl_tp_recipe");
NamespacedKey scytheKey = new NamespacedKey(this, "moon_scythe_recipe");
Bukkit.removeRecipe(pearlKey);
Bukkit.removeRecipe(scytheKey);
if (pearlConfig().getBoolean("recipe.enabled", true)) {
Material obsidian = items.resolveMaterial("OBSIDIAN");
Material pearl = items.resolveMaterial("ENDER_PEARL");
if (obsidian != null && pearl != null) {
ShapedRecipe recipe = new ShapedRecipe(pearlKey, items.pearlTp());
recipe.shape("OOO", "OPO", "OOO");
recipe.setIngredient('O', obsidian);
recipe.setIngredient('P', pearl);
Bukkit.addRecipe(recipe);
} else getLogger().warning("Could not register Pearl TP recipe: unresolved XSeries materials.");
}
ConfigurationSection section = scytheConfig().getConfigurationSection("recipe");
if (section == null || !section.getBoolean("enabled", true)) return;
List<String> shape = section.getStringList("shape");
ConfigurationSection ingredients = section.getConfigurationSection("ingredients");
if (shape.isEmpty() || shape.size() > 3 || ingredients == null || shape.stream().anyMatch(row -> row.length() > 3)) {
getLogger().warning("Moon Scythe recipe configuration is invalid; recipe skipped.");
return;
}
for (String row : shape) {
for (char symbol : row.toCharArray()) {
if (symbol != ' ' && !ingredients.contains(Character.toString(symbol))) {
getLogger().warning("Moon Scythe recipe is missing ingredient mapping for " + symbol);
return;
}
}
}
ShapedRecipe recipe = new ShapedRecipe(scytheKey, items.scythe());
recipe.shape(shape.toArray(new String[0]));
for (String symbol : ingredients.getKeys(false)) {
if (symbol.length() != 1) continue;
Material material = items.resolveMaterial(ingredients.getString(symbol));
if (material == null) { getLogger().warning("Unknown Moon Scythe ingredient: " + ingredients.getString(symbol)); return; }
recipe.setIngredient(symbol.charAt(0), material);
}
Bukkit.addRecipe(recipe);
}
public void loadPlayer(Player player) {
UUID uuid = player.getUniqueId();
dataStore.loadOrCreate(uuid, Ability.random()).whenComplete((profile, error) -> {
if (error != null) { getLogger().log(Level.SEVERE, "Could not load MoonAbilities data for " + uuid, error); return; }
Bukkit.getScheduler().runTask(this, () -> {
Player online = Bukkit.getPlayer(uuid);
if (online == null) return;
profiles.put(uuid, profile);
if (profile.isFirstJoin()) {
Messages.send(this, online, "first-ability", Map.of("ability", abilityService.display(profile.getFirstAbility())));
abilityService.reveal(online, profile.getFirstAbility());
}
deliverPendingGrants(online);
});
});
}
public void unloadPlayer(UUID uuid) {
profiles.remove(uuid);
if (pearlTeleportService != null) pearlTeleportService.cancelFor(uuid);
if (combat != null) combat.clear(uuid);
if (menuService != null) menuService.cancelAnimation(uuid);
}
public void deliverPendingGrants(Player player) {
UUID uuid = player.getUniqueId();
if (!pendingDeliveries.add(uuid)) return;
dataStore.pendingGrants(uuid).whenComplete((grants, error) -> Bukkit.getScheduler().runTask(this, () -> {
Player online = Bukkit.getPlayer(uuid);
if (error != null) {
pendingDeliveries.remove(uuid);
getLogger().log(Level.SEVERE, "Could not load pending item grants for " + uuid, error);
return;
}
if (online == null) { pendingDeliveries.remove(uuid); return; }
List<CompletableFuture<Void>> completed = new ArrayList<>();
for (PlayerDataStore.PendingGrant grant : grants) {
if (items.hasGrantId(online, grant.id())) { completed.add(dataStore.completeGrant(grant.id())); continue; }
ItemStack item = items.itemForGrant(grant);
if (item == null) { getLogger().warning("Could not construct pending grant " + grant.id()); continue; }
if (online.getInventory().addItem(item).isEmpty()) completed.add(dataStore.completeGrant(grant.id()));
else Messages.send(this, online, "grant-inventory-full");
}
CompletableFuture.allOf(completed.toArray(new CompletableFuture[0])).whenComplete((ignored, completionError) -> pendingDeliveries.remove(uuid));
}));