mirror of
https://github.com/AtlasMediaGroup/TotalFreedomMod.git
synced 2025-06-29 03:36:42 +00:00
permission system
This commit is contained in:
@ -0,0 +1,184 @@
|
||||
package me.totalfreedom.totalfreedommod.permissions;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
||||
import me.totalfreedom.totalfreedommod.util.FLog;
|
||||
import net.pravian.aero.component.PluginComponent;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
public class PermissionConfig extends PluginComponent<TotalFreedomMod>
|
||||
{
|
||||
|
||||
public static final String PERMISSIONS_FILENAME = "permissions.yml";
|
||||
//
|
||||
private final EnumMap<PermissionEntry, Object> entries;
|
||||
private final PermissionDefaults defaults;
|
||||
public YamlConfiguration configuration;
|
||||
|
||||
public PermissionConfig(TotalFreedomMod plugin)
|
||||
{
|
||||
super(plugin);
|
||||
|
||||
entries = new EnumMap<>(PermissionEntry.class);
|
||||
|
||||
PermissionDefaults tempDefaults = null;
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
try (InputStream defaultConfig = getDefaultConfig())
|
||||
{
|
||||
tempDefaults = new PermissionDefaults(defaultConfig);
|
||||
for (PermissionEntry entry : PermissionEntry.values())
|
||||
{
|
||||
entries.put(entry, tempDefaults.get(entry.getConfigName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
FLog.severe(ex);
|
||||
}
|
||||
|
||||
copyDefaultConfig(getConfigFile());
|
||||
|
||||
load();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
FLog.severe(ex);
|
||||
}
|
||||
|
||||
defaults = tempDefaults;
|
||||
}
|
||||
|
||||
public void load()
|
||||
{
|
||||
try
|
||||
{
|
||||
YamlConfiguration config = new YamlConfiguration();
|
||||
|
||||
config.load(getConfigFile());
|
||||
|
||||
configuration = config;
|
||||
|
||||
for (PermissionEntry entry : PermissionEntry.values())
|
||||
{
|
||||
String path = entry.getConfigName();
|
||||
if (config.contains(path))
|
||||
{
|
||||
Object value = config.get(path);
|
||||
if (value != null)
|
||||
{
|
||||
entries.put(entry, value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
FLog.warning("Missing permission entry " + entry.getConfigName() + ". Using default value.");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException | InvalidConfigurationException ex)
|
||||
{
|
||||
FLog.severe(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private File getConfigFile()
|
||||
{
|
||||
return new File(plugin.getDataFolder(), PERMISSIONS_FILENAME);
|
||||
}
|
||||
|
||||
public List getList(PermissionEntry entry)
|
||||
{
|
||||
try
|
||||
{
|
||||
return get(entry, List.class);
|
||||
}
|
||||
catch (IllegalArgumentException ex)
|
||||
{
|
||||
FLog.severe(ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public <T> T get(PermissionEntry entry, Class<T> type) throws IllegalArgumentException
|
||||
{
|
||||
Object value = entries.get(entry);
|
||||
try
|
||||
{
|
||||
return type.cast(value);
|
||||
}
|
||||
catch (ClassCastException ex)
|
||||
{
|
||||
throw new IllegalArgumentException(entry.name() + " is not of type " + type.getSimpleName());
|
||||
}
|
||||
}
|
||||
|
||||
public <T> void set(PermissionEntry entry, T value)
|
||||
{
|
||||
entries.put(entry, value);
|
||||
}
|
||||
|
||||
private void copyDefaultConfig(File targetFile)
|
||||
{
|
||||
if (targetFile.exists())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
FLog.info("Installing default permission file template: " + targetFile.getPath());
|
||||
|
||||
try
|
||||
{
|
||||
try (InputStream defaultConfig = getDefaultConfig())
|
||||
{
|
||||
FileUtils.copyInputStreamToFile(defaultConfig, targetFile);
|
||||
}
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
FLog.severe(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private InputStream getDefaultConfig()
|
||||
{
|
||||
return plugin.getResource(PERMISSIONS_FILENAME);
|
||||
}
|
||||
|
||||
public static class PermissionDefaults
|
||||
{
|
||||
|
||||
private YamlConfiguration defaults = null;
|
||||
|
||||
private PermissionDefaults(InputStream defaultConfig)
|
||||
{
|
||||
try
|
||||
{
|
||||
defaults = new YamlConfiguration();
|
||||
final InputStreamReader isr = new InputStreamReader(defaultConfig);
|
||||
defaults.load(isr);
|
||||
isr.close();
|
||||
}
|
||||
catch (IOException | InvalidConfigurationException ex)
|
||||
{
|
||||
FLog.severe(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public Object get(String path)
|
||||
{
|
||||
return defaults.get(path);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package me.totalfreedom.totalfreedommod.permissions;
|
||||
|
||||
import java.util.List;
|
||||
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
||||
|
||||
|
||||
public enum PermissionEntry
|
||||
{
|
||||
REMOVE("remove"),
|
||||
OPERATORS("operators"),
|
||||
MASTER_BUILDERS("master_builders"),
|
||||
SUPER_ADMINS("super_admins"),
|
||||
TELNET_ADMINS("telnet_admins"),
|
||||
SENIOR_ADMINS("senior_admins");
|
||||
|
||||
|
||||
private final String configName;
|
||||
|
||||
PermissionEntry(String configName)
|
||||
{
|
||||
this.configName = configName;
|
||||
}
|
||||
|
||||
public String getConfigName()
|
||||
{
|
||||
return configName;
|
||||
}
|
||||
|
||||
public List<?> getList()
|
||||
{
|
||||
return getConfig().getList(this);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<String> getEntry()
|
||||
{
|
||||
return (List<String>)getList();
|
||||
}
|
||||
|
||||
private PermissionConfig getConfig()
|
||||
{
|
||||
return TotalFreedomMod.plugin().permissions;
|
||||
}
|
||||
}
|
@ -0,0 +1,138 @@
|
||||
package me.totalfreedom.totalfreedommod.permissions;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import me.totalfreedom.totalfreedommod.FreedomService;
|
||||
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
||||
import me.totalfreedom.totalfreedommod.rank.Displayable;
|
||||
import me.totalfreedom.totalfreedommod.rank.Rank;
|
||||
import me.totalfreedom.totalfreedommod.rank.Title;
|
||||
import me.totalfreedom.totalfreedommod.util.FLog;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.permissions.PermissionAttachment;
|
||||
import org.bukkit.permissions.PermissionAttachmentInfo;
|
||||
|
||||
public class PermissionManager extends FreedomService
|
||||
{
|
||||
|
||||
public Map<Displayable, List<String>> permissions = Maps.newHashMap();
|
||||
|
||||
public Map<Player, PermissionAttachment> attachments = Maps.newHashMap();
|
||||
|
||||
public PermissionManager(TotalFreedomMod plugin)
|
||||
{
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart()
|
||||
{
|
||||
loadPermissionNodes();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop()
|
||||
{
|
||||
}
|
||||
|
||||
public void loadPermissionNodes()
|
||||
{
|
||||
|
||||
FLog.info("Loading permission nodes...");
|
||||
|
||||
permissions.clear();
|
||||
|
||||
List<String> operatorPermissions;
|
||||
List<String> masterBuilderPermissions;
|
||||
List<String> superAdminPermissions;
|
||||
List<String> telnetAdminPermissions;
|
||||
List<String> seniorAdminPermissions;
|
||||
|
||||
operatorPermissions = PermissionEntry.OPERATORS.getEntry();
|
||||
permissions.put(Rank.OP, operatorPermissions);
|
||||
|
||||
masterBuilderPermissions = PermissionEntry.MASTER_BUILDERS.getEntry();
|
||||
masterBuilderPermissions.addAll(operatorPermissions);
|
||||
permissions.put(Title.MASTER_BUILDER, masterBuilderPermissions);
|
||||
|
||||
|
||||
superAdminPermissions = PermissionEntry.SUPER_ADMINS.getEntry();
|
||||
superAdminPermissions.addAll(masterBuilderPermissions);
|
||||
permissions.put(Rank.SUPER_ADMIN, superAdminPermissions);
|
||||
|
||||
telnetAdminPermissions = PermissionEntry.TELNET_ADMINS.getEntry();
|
||||
telnetAdminPermissions.addAll(superAdminPermissions);
|
||||
permissions.put(Rank.TELNET_ADMIN, superAdminPermissions);
|
||||
|
||||
seniorAdminPermissions = PermissionEntry.SENIOR_ADMINS.getEntry();
|
||||
seniorAdminPermissions.addAll(telnetAdminPermissions);
|
||||
permissions.put(Rank.SENIOR_ADMIN, seniorAdminPermissions);
|
||||
|
||||
FLog.info("Loaded " + permissions.values().size() + " permission nodes");
|
||||
}
|
||||
|
||||
public void setPermissions(Player player)
|
||||
{
|
||||
PermissionAttachment attachment = attachments.get(player);
|
||||
|
||||
if (attachment != null)
|
||||
{
|
||||
player.removeAttachment(attachment);
|
||||
}
|
||||
|
||||
attachment = player.addAttachment(plugin);
|
||||
|
||||
for (PermissionAttachmentInfo attachmentInfo : player.getEffectivePermissions())
|
||||
{
|
||||
for (String rootNode : PermissionEntry.REMOVE.getEntry())
|
||||
{
|
||||
String permission = attachmentInfo.getPermission();
|
||||
if (permission.startsWith(rootNode))
|
||||
{
|
||||
attachment.setPermission(attachmentInfo.getPermission(), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
List<String> nodes = permissions.get(plugin.rm.getRank(player));
|
||||
if (nodes != null)
|
||||
{
|
||||
for (String node : nodes)
|
||||
{
|
||||
attachment.setPermission(node, true);
|
||||
}
|
||||
}
|
||||
|
||||
if (plugin.mbl.isMasterBuilder(player) && !plugin.al.isAdmin(player))
|
||||
{
|
||||
if (nodes != null)
|
||||
{
|
||||
for (String node : permissions.get(Title.MASTER_BUILDER))
|
||||
{
|
||||
attachment.setPermission(node, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
attachments.put(player, attachment);
|
||||
|
||||
player.recalculatePermissions();
|
||||
}
|
||||
|
||||
public void updatePlayers()
|
||||
{
|
||||
for (Player player : server.getOnlinePlayers())
|
||||
{
|
||||
setPermissions(player);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.NORMAL)
|
||||
public void onPlayerJoin(PlayerJoinEvent event)
|
||||
{
|
||||
setPermissions(event.getPlayer());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user