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.knarcraft.permissionsigns.utility.UpdateChecker; 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; /* PermissionSigns - A permission shop plugin for Spigot Copyright (C) 2022 Kristian Knarvik (EpicKnarvik97) The following license notice applies to all source and resource files in the PermissionSigns project: This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ /** * The main permissionsigns class */ 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; private static boolean enableExtensiveSignProtection; private static boolean enableIndirectSignProtection; private static boolean removePermissionSignIfMissing; /** * 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; } /** * Gets whether permission signs on falling blocks should be protected * * @return

Whether permission signs on falling blocks should be protected

*/ public static boolean extensiveSignProtectionEnabled() { return enableExtensiveSignProtection; } /** * Gets whether permission sign explosion and piston protection is enabled * * @return

Whether permission sign explosion and piston protection is enabled

*/ public static boolean indirectProtectionEnabled() { return enableIndirectSignProtection; } /** * Gets whether to remove permissions signs if their physical signs are missing * * @return

Whether tor remove permission signs if their signs are missing

*/ public static boolean removePermissionSignIfMissing() { return removePermissionSignIfMissing; } @Override public void reloadConfig() { super.reloadConfig(); Translator.loadLanguages(loadConfig()); } @Override public void onEnable() { //Check if vault is loaded setupVault(); //Get plugin info PluginDescriptionFile pluginDescriptionFile = this.getDescription(); pluginVersion = pluginDescriptionFile.getVersion(); //Load config and write the default config if necessary FileConfiguration config = this.getConfig(); this.saveDefaultConfig(); config.options().copyDefaults(true); Translator.loadLanguages(loadConfig()); registerListeners(); registerCommands(); runThreads(); SignManager.loadSigns(); PermissionManager.loadTemporaryPermissions(); UpdateChecker.checkForUpdate(this, "https://api.spigotmc.org/legacy/update.php?resource=99426", () -> pluginVersion, null); } /** * Loads the config file * * @return

The currently selected language

*/ private String loadConfig() { FileConfiguration config = this.getConfig(); String language = config.getString("language", "en"); perWorldPermissions = config.getBoolean("perWorldPermissions", false); enableExtensiveSignProtection = config.getBoolean("enableExtensiveSignProtection", false); enableIndirectSignProtection = config.getBoolean("enableIndirectSignProtection", true); removePermissionSignIfMissing = config.getBoolean("removePermissionSignIfMissing", true); saveConfig(); return language; } /** * 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 permissionSignsCommand = this.getCommand("permissionsigns"); if (permissionSignsCommand != null) { permissionSignsCommand.setExecutor(new PermissionSignsCommand()); permissionSignsCommand.setTabCompleter(new PermissionSignsTabCompleter()); } } @Override public void onDisable() { try { SignManager.saveSigns(); } catch (IOException e) { e.printStackTrace(); } } }