package net.knarcraft.permissionsigns; import net.knarcraft.permissionsigns.command.PermissionSignsCommand; import net.knarcraft.permissionsigns.command.PermissionSignsTabCompleter; import net.knarcraft.permissionsigns.container.PermissionSign; import net.knarcraft.permissionsigns.container.SignCreationRequest; import net.knarcraft.permissionsigns.formatting.Translator; import net.knarcraft.permissionsigns.listener.BlockListener; import net.knarcraft.permissionsigns.listener.PlayerListener; import net.knarcraft.permissionsigns.listener.SignListener; import net.knarcraft.permissionsigns.manager.EconomyManager; import net.knarcraft.permissionsigns.manager.PermissionManager; import net.knarcraft.permissionsigns.manager.SignManager; import net.knarcraft.permissionsigns.thread.PermissionTimeoutThread; import net.knarcraft.permissionsigns.thread.SignCreationRequestTimeoutThread; import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.permission.Permission; import org.bukkit.Bukkit; import org.bukkit.command.PluginCommand; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.ServicesManager; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitScheduler; import java.io.IOException; import java.util.List; import java.util.PriorityQueue; import java.util.Queue; import java.util.UUID; import java.util.stream.Stream; public final class PermissionSigns extends JavaPlugin { private static final Queue signCreationRequests = new PriorityQueue<>(); private static String pluginVersion; private static PermissionSigns instance; private static boolean perWorldPermissions; /** * Instantiates the permission signs class */ @SuppressWarnings("unused") public PermissionSigns() { super(); instance = this; } /** * Gets an instance of this plugin * * @return

An instance of this plugin

*/ public static PermissionSigns getInstance() { return instance; } /** * Gets the version of this plugin * * @return

This plugin's version

*/ public static String getPluginVersion() { return pluginVersion; } /** * Adds a new sign creation request * * @param player

The player that initiated the sign creation

* @param sign

The sign the player is about to create

*/ public static void addSignCreationRequest(Player player, PermissionSign sign) { signCreationRequests.add(new SignCreationRequest(sign, player, System.currentTimeMillis())); } /** * Gets the sign creation request for the player with the given UUID * * @param uuid

The UUID to get a sign creation request for

* @return

A sign creation request, or null if the UUID is not found

*/ public static SignCreationRequest getSignCreationRequest(UUID uuid) { Stream matchingRequests = signCreationRequests.stream().filter( (item) -> item.getPlayer().getUniqueId().equals(uuid)); List requestList = matchingRequests.toList(); if (!requestList.isEmpty()) { return requestList.get(0); } else { return null; } } /** * Cancels the sign creation request triggered by the user * * @param uuid

The UUID of the player to cancel the request for

*/ public static void cancelSignCreationRequest(UUID uuid) { Stream matchingRequests = signCreationRequests.stream().filter( (item) -> item.getPlayer().getUniqueId().equals(uuid)); List requestList = matchingRequests.toList(); if (requestList.size() > 0) { signCreationRequests.remove(requestList.get(0)); } } /** * Checks whether permissions should always be set for the world the sign belongs to * * @return

Whether permissions should be set for the current world

*/ public static boolean usePerWorldPermissions() { return perWorldPermissions; } @Override public void reloadConfig() { super.reloadConfig(); FileConfiguration config = this.getConfig(); String language = config.getString("language"); perWorldPermissions = config.getBoolean("perWorldPermissions"); Translator.loadLanguages(language); } @Override public void onEnable() { PluginDescriptionFile pluginDescriptionFile = this.getDescription(); pluginVersion = pluginDescriptionFile.getVersion(); //TODO: Allow for custom language files. Perhaps just look for strings.yml in the folder FileConfiguration config = this.getConfig(); config.options().copyDefaults(true); this.saveDefaultConfig(); String language = config.getString("language"); perWorldPermissions = config.getBoolean("perWorldPermissions"); //Check if vault is loaded setupVault(); registerListeners(); Translator.loadLanguages(language); registerCommands(); runThreads(); SignManager.loadSigns(); PermissionManager.loadTemporaryPermissions(); } /** * Starts all separate threads for executing tasks */ private void runThreads() { BukkitScheduler scheduler = Bukkit.getScheduler(); scheduler.runTaskTimer(this, new SignCreationRequestTimeoutThread(signCreationRequests), 0L, 100L); scheduler.runTaskTimer(this, new PermissionTimeoutThread(), 0L, 25L); } /** * Registers all necessary listeners */ private void registerListeners() { PluginManager pluginManager = getServer().getPluginManager(); pluginManager.registerEvents(new SignListener(), this); pluginManager.registerEvents(new PlayerListener(), this); pluginManager.registerEvents(new BlockListener(), this); } /** * Sets up Vault by getting plugins from their providers */ private void setupVault() { ServicesManager servicesManager = this.getServer().getServicesManager(); RegisteredServiceProvider permissionProvider = servicesManager.getRegistration(Permission.class); RegisteredServiceProvider economyProvider = servicesManager.getRegistration(Economy.class); if (permissionProvider != null && economyProvider != null) { EconomyManager.initialize(economyProvider.getProvider()); PermissionManager.initialize(permissionProvider.getProvider()); } else { throw new IllegalStateException("[PermissionSigns] Error: Vault could not be loaded"); } } /** * Registers a command for this plugin */ private void registerCommands() { PluginCommand stargateCommand = this.getCommand("permissionsigns"); if (stargateCommand != null) { stargateCommand.setExecutor(new PermissionSignsCommand()); stargateCommand.setTabCompleter(new PermissionSignsTabCompleter()); } } @Override public void onDisable() { try { SignManager.saveSigns(); } catch (IOException e) { e.printStackTrace(); } } }