277 lines
9.9 KiB
Java
277 lines
9.9 KiB
Java
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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
/**
|
|
* The main permissionsigns class
|
|
*/
|
|
public final class PermissionSigns extends JavaPlugin {
|
|
|
|
private static final Queue<SignCreationRequest> 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 <p>An instance of this plugin</p>
|
|
*/
|
|
public static PermissionSigns getInstance() {
|
|
return instance;
|
|
}
|
|
|
|
/**
|
|
* Gets the version of this plugin
|
|
*
|
|
* @return <p>This plugin's version</p>
|
|
*/
|
|
public static String getPluginVersion() {
|
|
return pluginVersion;
|
|
}
|
|
|
|
/**
|
|
* Adds a new sign creation request
|
|
*
|
|
* @param player <p>The player that initiated the sign creation</p>
|
|
* @param sign <p>The sign the player is about to create</p>
|
|
*/
|
|
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 <p>The UUID to get a sign creation request for</p>
|
|
* @return <p>A sign creation request, or null if the UUID is not found</p>
|
|
*/
|
|
public static SignCreationRequest getSignCreationRequest(UUID uuid) {
|
|
Stream<SignCreationRequest> matchingRequests = signCreationRequests.stream().filter(
|
|
(item) -> item.getPlayer().getUniqueId().equals(uuid));
|
|
List<SignCreationRequest> requestList = matchingRequests.toList();
|
|
if (!requestList.isEmpty()) {
|
|
return requestList.get(0);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Cancels the sign creation request triggered by the user
|
|
*
|
|
* @param uuid <p>The UUID of the player to cancel the request for</p>
|
|
*/
|
|
public static void cancelSignCreationRequest(UUID uuid) {
|
|
Stream<SignCreationRequest> matchingRequests = signCreationRequests.stream().filter(
|
|
(item) -> item.getPlayer().getUniqueId().equals(uuid));
|
|
List<SignCreationRequest> 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 <p>Whether permissions should be set for the current world</p>
|
|
*/
|
|
public static boolean usePerWorldPermissions() {
|
|
return perWorldPermissions;
|
|
}
|
|
|
|
/**
|
|
* Gets whether permission signs on falling blocks should be protected
|
|
*
|
|
* @return <p>Whether permission signs on falling blocks should be protected</p>
|
|
*/
|
|
public static boolean extensiveSignProtectionEnabled() {
|
|
return enableExtensiveSignProtection;
|
|
}
|
|
|
|
/**
|
|
* Gets whether permission sign explosion and piston protection is enabled
|
|
*
|
|
* @return <p>Whether permission sign explosion and piston protection is enabled</p>
|
|
*/
|
|
public static boolean indirectProtectionEnabled() {
|
|
return enableIndirectSignProtection;
|
|
}
|
|
|
|
/**
|
|
* Gets whether to remove permissions signs if their physical signs are missing
|
|
*
|
|
* @return <p>Whether tor remove permission signs if their signs are missing</p>
|
|
*/
|
|
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 <p>The currently selected language</p>
|
|
*/
|
|
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<Permission> permissionProvider = servicesManager.getRegistration(Permission.class);
|
|
RegisteredServiceProvider<Economy> 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();
|
|
}
|
|
}
|
|
|
|
}
|