TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/permissions/PermissionManager.java

127 lines
3.9 KiB
Java
Raw Normal View History

2020-04-14 06:40:22 +00:00
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.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();
@Override
2020-07-01 01:51:06 +00:00
public void onStart()
2020-04-14 06:40:22 +00:00
{
loadPermissionNodes();
}
@Override
2020-07-01 01:51:06 +00:00
public void onStop()
2020-04-14 06:40:22 +00:00
{
}
public void loadPermissionNodes()
{
FLog.info("Loading permission nodes...");
permissions.clear();
List<String> operatorPermissions;
List<String> masterBuilderPermissions;
2020-08-15 22:42:46 +00:00
List<String> adminPermissions;
List<String> senioradminPermissions;
2020-04-14 06:40:22 +00:00
operatorPermissions = PermissionEntry.OPERATORS.getEntry();
permissions.put(Rank.OP, operatorPermissions);
masterBuilderPermissions = PermissionEntry.MASTER_BUILDERS.getEntry();
masterBuilderPermissions.addAll(operatorPermissions);
permissions.put(Title.MASTER_BUILDER, masterBuilderPermissions);
2020-08-15 22:42:46 +00:00
adminPermissions = PermissionEntry.ADMINS.getEntry();
adminPermissions.addAll(masterBuilderPermissions);
permissions.put(Rank.NEW_ADMIN, adminPermissions);
senioradminPermissions = PermissionEntry.SENIOR_ADMINS.getEntry();
senioradminPermissions.addAll(adminPermissions);
permissions.put(Rank.SENIOR_ADMIN, senioradminPermissions);
2020-04-14 06:40:22 +00:00
int count = PermissionEntry.OPERATORS.getEntry().size() + PermissionEntry.MASTER_BUILDERS.getEntry().size() + PermissionEntry.ADMINS.getEntry().size() + PermissionEntry.SENIOR_ADMINS.getEntry().size();
FLog.info("Loaded " + count + " permission nodes");
2020-04-14 06:40:22 +00:00
}
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);
}
}
2020-08-15 22:42:46 +00:00
if (plugin.pl.getData(player).isMasterBuilder() && !plugin.sl.isStaff(player))
2020-04-14 06:40:22 +00:00
{
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());
}
}