Accounts for world specifiers where necessary Makes the reload command work? Stores the world as part of temporary permissions Fixes sign break protection Allows sign interaction with both right- and left-click Fixes temporary permissions sometimes colliding Cleans the main class a bit Adds a "star" permission Adds missing check for the use permission
211 lines
7.4 KiB
Java
211 lines
7.4 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.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<SignCreationRequest> 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 <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;
|
|
}
|
|
|
|
@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<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 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();
|
|
}
|
|
}
|
|
|
|
}
|