Files
src
Code
6 871
src/main/java/com/kodari/friendssmp/FriendsSMPPlugin.java
package com.kodari.friendssmp;
import com.kodari.friendssmp.bridge.DiscordBridge;
import com.kodari.friendssmp.shop.ShopService;
import com.kodari.friendssmp.tab.TabListManager;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
public final class FriendsSMPPlugin extends JavaPlugin {
private Economy economy;
private ShopService shopService;
private TabListManager tabListManager;
private DiscordBridge discordBridge;
@Override
public void onEnable() {
saveDefaultConfig();
setupEconomy();
shopService = new ShopService(this, economy);
tabListManager = new TabListManager(this, economy);
discordBridge = new DiscordBridge(this);
discordBridge.configure();
Bukkit.getPluginManager().registerEvents(shopService, this);
Bukkit.getPluginManager().registerEvents(tabListManager, this);
Bukkit.getPluginManager().registerEvents(discordBridge, this);
PluginCommand command = getCommand("friendsmp");
if (command == null) {
throw new IllegalStateException("The friendsmp command is missing from plugin.yml");
}
command.setExecutor(this::handleCommand);
PluginCommand shopCommand = getCommand("shop");
if (shopCommand == null) {
throw new IllegalStateException("The shop command is missing from plugin.yml");
}
shopCommand.setExecutor((sender, shop, label, args) -> {
if (!(sender instanceof Player)) {
sender.sendMessage(color("&cOnly players can open the shop."));
return true;
}
if (!sender.hasPermission("friendssmp.shop")) {
sender.sendMessage(color("&cYou do not have permission to use the shop."));
return true;
}
shopService.openShop((Player) sender, 0);
return true;
});
tabListManager.start();
}
@Override
public void onDisable() {
if (tabListManager != null) {
tabListManager.stop();
}
if (discordBridge != null) {
discordBridge.shutdown();
}
}
public Economy getEconomy() {
return economy;
}
private void setupEconomy() {
if (Bukkit.getPluginManager().getPlugin("Vault") == null) {
getLogger().warning("Vault is not installed; shop purchases and economy ranks are unavailable.");
return;
}
RegisteredServiceProvider<Economy> registration = getServer().getServicesManager().getRegistration(Economy.class);
if (registration == null || registration.getProvider() == null) {
getLogger().warning("Vault has no registered economy provider; shop purchases and economy ranks are unavailable.");
return;
}
economy = registration.getProvider();
getLogger().info("Using Vault economy provider: " + economy.getName());
}
private boolean handleCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 0) {
sender.sendMessage(color("&bFriendsSMP commands: &f/friendsmp shop &7| &f/friendsmp reload"));
return true;
}
if (args[0].equalsIgnoreCase("shop")) {
if (!(sender instanceof Player)) {
sender.sendMessage(color("&cOnly players can open the shop."));
return true;
}
if (!sender.hasPermission("friendssmp.shop")) {
sender.sendMessage(color("&cYou do not have permission to use the shop."));
return true;
}
shopService.openShop((Player) sender, 0);
return true;
}
if (args[0].equalsIgnoreCase("reload")) {
if (!sender.hasPermission("friendssmp.reload")) {
sender.sendMessage(color("&cYou do not have permission to reload this plugin."));
return true;
}
reloadConfig();
tabListManager.restart();
discordBridge.configure();
sender.sendMessage(color("&aFriendsSMP configuration reloaded."));
return true;
}
sender.sendMessage(color("&cUsage: /friendsmp <shop|reload>"));
return true;
}
private String color(String message) {
return ChatColor.translateAlternateColorCodes('&', message);
}
}