2012-04-29 00:11:39 +00:00
|
|
|
package net.crystalyx.bukkit.simplyperms;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2012-05-04 21:23:37 +00:00
|
|
|
import java.util.ArrayList;
|
2012-04-29 00:11:39 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import net.crystalyx.bukkit.simplyperms.io.ConfigFile;
|
|
|
|
import net.crystalyx.bukkit.simplyperms.io.ConfigSQL;
|
|
|
|
import net.crystalyx.bukkit.simplyperms.io.PermsConfig;
|
|
|
|
|
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
|
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
|
import org.bukkit.entity.Player;
|
2012-04-29 00:22:45 +00:00
|
|
|
import org.bukkit.permissions.Permission;
|
2012-04-29 00:11:39 +00:00
|
|
|
import org.bukkit.permissions.PermissionAttachment;
|
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
|
|
|
|
public class SimplyPlugin extends JavaPlugin {
|
|
|
|
|
|
|
|
protected PermsConfig config;
|
2012-04-29 00:55:03 +00:00
|
|
|
private SimplyPlayer playerListener = new SimplyPlayer(this);
|
|
|
|
private SimplyCommands commandExecutor = new SimplyCommands(this);
|
2012-04-29 00:11:39 +00:00
|
|
|
private HashMap<String, PermissionAttachment> permissions = new HashMap<String, PermissionAttachment>();
|
|
|
|
|
|
|
|
private File configFile;
|
2012-04-29 00:55:03 +00:00
|
|
|
private YamlConfiguration YamlConfig;
|
|
|
|
|
|
|
|
// -- Basic stuff
|
2012-04-29 00:11:39 +00:00
|
|
|
@Override
|
|
|
|
public void onEnable() {
|
2012-05-08 12:36:48 +00:00
|
|
|
// Disable PermissionsBukkit to avoid bugs
|
2012-04-29 00:22:45 +00:00
|
|
|
if (getServer().getPluginManager().isPluginEnabled("PermissionsBukkit")) {
|
|
|
|
for (Permission perm : getServer().getPluginManager().getPlugin("PermissionsBukkit").getDescription().getPermissions()) {
|
|
|
|
getServer().getPluginManager().removePermission(perm);
|
|
|
|
}
|
2012-05-08 12:36:48 +00:00
|
|
|
getServer().getPluginManager().disablePlugin(getServer().getPluginManager().getPlugin("PermissionsBukkit"));
|
2012-04-29 00:22:45 +00:00
|
|
|
}
|
|
|
|
|
2012-04-29 00:55:03 +00:00
|
|
|
// Take care of configuration
|
|
|
|
configFile = new File(getDataFolder(), "config.yml");
|
|
|
|
if (!configFile.exists()) {
|
|
|
|
saveDefaultConfig();
|
|
|
|
}
|
|
|
|
reloadConfig();
|
2012-04-29 00:22:45 +00:00
|
|
|
|
2012-04-29 00:55:03 +00:00
|
|
|
// Register stuff
|
2012-04-29 00:11:39 +00:00
|
|
|
getCommand("permissions").setExecutor(commandExecutor);
|
|
|
|
getServer().getPluginManager().registerEvents(playerListener, this);
|
|
|
|
registerEvents();
|
|
|
|
|
2012-04-29 00:55:03 +00:00
|
|
|
// Register everyone online right now
|
|
|
|
for (Player p : getServer().getOnlinePlayers()) {
|
|
|
|
registerPlayer(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
// How are you gentlemen
|
|
|
|
getLogger().info("Enabled successfully, " + getServer().getOnlinePlayers().length + " players registered");
|
|
|
|
}
|
2012-04-29 00:11:39 +00:00
|
|
|
|
2012-04-29 00:55:03 +00:00
|
|
|
@Override
|
|
|
|
public FileConfiguration getConfig() {
|
|
|
|
return YamlConfig;
|
2012-04-29 00:11:39 +00:00
|
|
|
}
|
|
|
|
|
2012-04-29 00:55:03 +00:00
|
|
|
@Override
|
|
|
|
public void reloadConfig() {
|
|
|
|
YamlConfig = new YamlConfiguration();
|
|
|
|
YamlConfig.options().pathSeparator('/');
|
|
|
|
try {
|
|
|
|
YamlConfig.load(configFile);
|
|
|
|
} catch (Exception e) {
|
|
|
|
getLogger().severe("Unable to load configuration!");
|
|
|
|
}
|
2012-04-29 00:11:39 +00:00
|
|
|
|
|
|
|
// Init DB
|
|
|
|
initDatabase();
|
2012-04-29 00:55:03 +00:00
|
|
|
}
|
|
|
|
|
2012-04-29 00:11:39 +00:00
|
|
|
@Override
|
|
|
|
public void onDisable() {
|
2012-04-29 00:55:03 +00:00
|
|
|
// Unregister everyone
|
|
|
|
for (Player p : getServer().getOnlinePlayers()) {
|
|
|
|
unregisterPlayer(p);
|
|
|
|
}
|
2012-04-29 00:11:39 +00:00
|
|
|
|
2012-04-29 00:55:03 +00:00
|
|
|
// Good day to you! I said good day!
|
|
|
|
getLogger().info("Disabled successfully, " + getServer().getOnlinePlayers().length + " players unregistered");
|
2012-04-29 00:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void initDatabase() {
|
|
|
|
if (getConfig().getString("db/type") != null
|
|
|
|
&& !getConfig().getString("db/type").equals("file")) {
|
|
|
|
ConfigSQL configsql = new ConfigSQL(this);
|
|
|
|
if (configsql.checkDatabase()) {
|
|
|
|
config = configsql;
|
|
|
|
} else {
|
|
|
|
debug("Fail to connect to database !");
|
|
|
|
config = new ConfigFile(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
config = new ConfigFile(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public SimplyAPI getAPI() {
|
|
|
|
return new SimplyAPI(this);
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:14:45 +00:00
|
|
|
public List<String> getKeys(YamlConfiguration config, String node) {
|
|
|
|
if (config.isConfigurationSection(node)) {
|
|
|
|
return new ArrayList<String>(config.getConfigurationSection(node).getKeys(false));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return new ArrayList<String>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-29 00:55:03 +00:00
|
|
|
// -- Plugin stuff
|
|
|
|
|
|
|
|
protected void registerPlayer(Player player) {
|
|
|
|
if (permissions.containsKey(player.getName())) {
|
|
|
|
debug("Registering " + player.getName() + ": was already registered");
|
|
|
|
unregisterPlayer(player);
|
|
|
|
}
|
|
|
|
PermissionAttachment attachment = player.addAttachment(this);
|
|
|
|
permissions.put(player.getName(), attachment);
|
|
|
|
calculateAttachment(player);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void unregisterPlayer(Player player) {
|
|
|
|
if (permissions.containsKey(player.getName())) {
|
|
|
|
try {
|
|
|
|
player.removeAttachment(permissions.get(player.getName()));
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ex) {
|
|
|
|
debug("Unregistering " + player.getName() + ": player did not have attachment");
|
|
|
|
}
|
|
|
|
permissions.remove(player.getName());
|
|
|
|
} else {
|
|
|
|
debug("Unregistering " + player.getName() + ": was not registered");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void refreshPermissions() {
|
|
|
|
try {
|
|
|
|
getConfig().save(configFile);
|
|
|
|
reloadConfig();
|
|
|
|
} catch (IOException e) {
|
|
|
|
getLogger().warning("Failed to write changed config.yml: " + e.getMessage());
|
|
|
|
}
|
|
|
|
for (String player : permissions.keySet()) {
|
|
|
|
PermissionAttachment attachment = permissions.get(player);
|
|
|
|
for (String key : attachment.getPermissions().keySet()) {
|
|
|
|
attachment.unsetPermission(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
calculateAttachment(getServer().getPlayer(player));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public ConfigurationSection getNode(String node) {
|
|
|
|
for (String entry : getConfig().getKeys(true)) {
|
|
|
|
if (node.equalsIgnoreCase(entry) && getConfig().isConfigurationSection(entry)) {
|
|
|
|
return getConfig().getConfigurationSection(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-04-29 00:11:39 +00:00
|
|
|
public void debug(String message) {
|
2012-05-08 12:33:19 +00:00
|
|
|
if (config.getDebug()) {
|
2012-04-29 00:11:39 +00:00
|
|
|
getLogger().info("Debug: " + message);
|
|
|
|
}
|
|
|
|
}
|
2012-04-29 00:55:03 +00:00
|
|
|
|
|
|
|
protected void calculateAttachment(Player player) {
|
|
|
|
if (player == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PermissionAttachment attachment = permissions.get(player.getName());
|
|
|
|
if (attachment == null) {
|
|
|
|
debug("Calculating permissions on " + player.getName() + ": attachment was null");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String key : attachment.getPermissions().keySet()) {
|
|
|
|
attachment.unsetPermission(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Map.Entry<String, Boolean> entry : calculatePlayerPermissions(player.getName().toLowerCase(), player.getWorld().getName()).entrySet()) {
|
|
|
|
attachment.setPermission(entry.getKey(), entry.getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
player.recalculatePermissions();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Private stuff
|
|
|
|
|
|
|
|
private Map<String, Boolean> calculatePlayerPermissions(String player, String world) {
|
2012-05-08 12:33:19 +00:00
|
|
|
String default_group = config.getDefaultGroup();
|
2012-04-29 00:55:03 +00:00
|
|
|
if (!config.isPlayerInDB(player)) {
|
|
|
|
return calculateGroupPermissions(default_group, world);
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, Boolean> perms = new HashMap<String, Boolean>();
|
|
|
|
List<String> groups = config.getPlayerGroups(player);
|
|
|
|
if (groups.isEmpty()) groups.add(default_group);
|
2012-05-07 18:03:54 +00:00
|
|
|
perms.putAll(config.getPlayerPermissions(player));
|
|
|
|
// No containskey; world overrides non-world
|
|
|
|
perms.putAll(config.getPlayerPermissions(player, world));
|
2012-04-29 00:55:03 +00:00
|
|
|
|
|
|
|
for (String group : groups) {
|
|
|
|
for (Map.Entry<String, Boolean> entry : calculateGroupPermissions(group, world).entrySet()) {
|
|
|
|
if (!perms.containsKey(entry.getKey())) { // User overrides group
|
|
|
|
perms.put(entry.getKey(), entry.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return perms;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Map<String, Boolean> calculateGroupPermissions(String group, String world) {
|
|
|
|
if (getNode("groups/" + group) == null) {
|
|
|
|
return new HashMap<String, Boolean>();
|
|
|
|
}
|
|
|
|
|
2012-05-07 18:03:54 +00:00
|
|
|
Map<String, Boolean> perms = config.getGroupPermissions(group);
|
|
|
|
// No containskey; world overrides non-world
|
|
|
|
perms.putAll(config.getGroupPermissions(group, world));
|
2012-04-29 00:55:03 +00:00
|
|
|
|
2012-05-07 18:03:54 +00:00
|
|
|
for (String parent : config.getGroupInheritance(group)) {
|
2012-04-29 00:55:03 +00:00
|
|
|
for (Map.Entry<String, Boolean> entry : calculateGroupPermissions(parent, world).entrySet()) {
|
|
|
|
if (!perms.containsKey(entry.getKey())) { // Children override permissions
|
|
|
|
perms.put(entry.getKey(), entry.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return perms;
|
|
|
|
}
|
2012-04-29 00:11:39 +00:00
|
|
|
|
|
|
|
private void registerEvents() {
|
|
|
|
String path = getDescription().getMain().substring(0, getDescription().getMain().lastIndexOf('.'));
|
|
|
|
for (String prevent : SimplyPrevents.preventions) {
|
|
|
|
try {
|
|
|
|
getServer().getPluginManager().registerEvents((SimplyPrevents) Class.forName(path + ".preventions." + prevent).getDeclaredConstructor(SimplyPlugin.class).newInstance(this), this);
|
|
|
|
} catch (Exception e) {
|
|
|
|
debug(e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
2012-04-29 00:55:03 +00:00
|
|
|
}
|
2012-04-29 00:11:39 +00:00
|
|
|
|
|
|
|
}
|