Files
AnuraCore/src/de/anura/core/Permissions.java
2014-10-19 18:10:52 +02:00

217 lines
8.6 KiB
Java

package de.anura.core;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.permissions.PermissionAttachment;
public final class Permissions implements Listener {
private final HashMap<Integer, String> groupNames = new HashMap<>();
private final HashMap<Integer, ArrayList<String>> groupPerms = new HashMap<>();
private final HashMap<UUID, ArrayList<Integer>> playerGroups = new HashMap<>();
private final HashMap<Integer, Integer> groupParents = new HashMap<>();
private final HashMap<Integer, String> groupPrefix = new HashMap<>();
private final HashMap<Integer, String> groupSuffix = new HashMap<>();
private final HashMap<Integer, String> groupListColor = new HashMap<>();
private final HashMap<Player, PermissionAttachment> permAttachments = new HashMap<>();
private int groupAutoIncrement = 0;
private final AnuraCore plugin;
public Permissions(AnuraCore plugin)
{
plugin.getServer().getPluginManager().registerEvents(this, plugin);
this.reload();
this.plugin = plugin;
this.createPermAttachs();
this.updateAllPlayers();
}
//Event handlers
@EventHandler
public void playerJoin(PlayerJoinEvent event)
{
Player P = event.getPlayer();
this.permAttachments.put(P, P.addAttachment(this.plugin));
this.updatePlayer(P);
}
@EventHandler
public void playerKick(PlayerKickEvent event)
{
Player P = event.getPlayer();
if(this.permAttachments.get(P) == null) return;
P.removeAttachment(this.permAttachments.get(P));
this.permAttachments.remove(P);
}
@EventHandler
public void playerQuit(PlayerQuitEvent event)
{
Player P = event.getPlayer();
if(this.permAttachments.get(P) == null) return;
P.removeAttachment(this.permAttachments.get(P));
this.permAttachments.remove(P);
}
//The permission stuff
private void createPermAttachs()
{
for(Player P : Bukkit.getOnlinePlayers())
{
this.permAttachments.put(P, P.addAttachment(this.plugin));
}
}
public void updateAllPlayers()
{
for(Player P : Bukkit.getOnlinePlayers())
{
this.updatePlayer(P);
}
}
public void updatePlayer(Player P)
{
PermissionAttachment pa = this.permAttachments.get(P);
for(String p : pa.getPermissions().keySet())
{
pa.unsetPermission(p);
}
if(!this.playerGroups.containsKey(P.getUniqueId()))
{
this.playerGroups.put(P.getUniqueId(), new ArrayList<Integer>());
}
ArrayList<Integer> groups = this.playerGroups.get(P.getUniqueId());
ArrayList<Integer> assignedGroups = new ArrayList<>();
String prefixes = "";
String suffixes = "";
String listColor = "";
for(Integer g : groups)
{
if(assignedGroups.contains(g)) continue;
assignedGroups.add(g);
for(String perm : this.groupPerms.get(g))
{
pa.setPermission(perm, true);
}
ArrayList<String> perms = this.getParentPerms(g, assignedGroups);
for(String perm : perms)
{
pa.setPermission(perm, true);
}
if(this.groupPrefix.containsKey(g)) prefixes += this.groupPrefix.get(g);
if(this.groupSuffix.containsKey(g)) suffixes += this.groupSuffix.get(g);
if(this.groupListColor.containsKey(g)) listColor = this.groupListColor.get(g);
}
String name = prefixes + P.getName() + suffixes;
P.setDisplayName(name);
P.setPlayerListName(listColor + P.getName());
}
private ArrayList<String> getParentPerms(Integer group, ArrayList<Integer> assigned)
{
ArrayList<String> toReturn = new ArrayList<>();
if(this.groupParents.get(group) == -1)
{
return toReturn;
}
if(assigned.contains(this.groupParents.get(group)))
{
return toReturn;
}
for(String perm : this.groupPerms.get(this.groupParents.get(group)))
{
toReturn.add(perm);
}
for(String perm : this.getParentPerms(this.groupParents.get(group), assigned))
{
toReturn.add(perm);
}
return toReturn;
}
public String reload()
{
ResultSet groupsRs = AnuraCore.sql.querySelect("SELECT prefix, suffix, parent, name, id, listColor FROM permGroups");
ResultSet permsRs = AnuraCore.sql.querySelect("SELECT `group`, name FROM permPerms");
ResultSet userRs = AnuraCore.sql.querySelect("SELECT playerUUID, `group` FROM permPlayerGroups");
this.groupNames.clear();
this.groupPerms.clear();
this.playerGroups.clear();
this.groupParents.clear();
this.groupPrefix.clear();
this.groupSuffix.clear();
HashMap<Integer, Integer> groupIds = new HashMap<>();
this.groupAutoIncrement = 0;
try {
groupsRs.last();
if(groupsRs.getRow() != 0)
{
groupsRs.beforeFirst();
while(groupsRs.next())
{
int id = this.groupAutoIncrement;
this.groupNames.put(id, groupsRs.getString("name"));
this.groupPerms.put(id, new ArrayList<String>());
this.groupParents.put(id, groupsRs.getInt("parent"));
if(groupsRs.getString("prefix") != null) this.groupPrefix.put(id, groupsRs.getString("prefix"));
if(groupsRs.getString("suffix") != null) this.groupSuffix.put(id, groupsRs.getString("suffix"));
if(groupsRs.getString("listColor") != null) this.groupListColor.put(id, groupsRs.getString("listColor"));
groupIds.put(groupsRs.getInt("id"), id);
this.groupAutoIncrement++;
}
}
permsRs.last();
if(permsRs.getRow() != 0)
{
permsRs.beforeFirst();
while(permsRs.next())
{
int groupId = groupIds.get(permsRs.getInt("group"));
if(this.groupNames.containsKey(groupId))
{
this.groupPerms.get(groupId).add(permsRs.getString("name"));
}
}
}
userRs.last();
if(userRs.getRow() != 0)
{
userRs.beforeFirst();
while(userRs.next())
{
int groupId = groupIds.get(userRs.getInt("group"));
if(this.groupNames.containsKey(groupId))
{
ArrayList<Integer> groupList = this.playerGroups.get(UUID.fromString(userRs.getString("playerUUID")));
if(groupList == null)
{
groupList = new ArrayList<>();
}
groupList.add(groupId);
this.playerGroups.put(UUID.fromString(userRs.getString("playerUUID")), groupList);
}
}
}
} catch (SQLException ex) {
System.err.println("Was not able to load permissions. Sorry. ("+ex.getLocalizedMessage()+")");
}
return ChatColor.GREEN + "Permissions reloaded!";
}
}